From 1250860efe810abd307ad8756a0ef3ac4cd2a43c Mon Sep 17 00:00:00 2001 From: isakg22 Date: Wed, 14 Oct 2026 12:21:45 +0000 Subject: [PATCH 1/2] =?UTF-8?q?Uppf=C3=A6r=C3=B0i=20k=C3=B3=C3=B0ann=20til?= =?UTF-8?q?=20a=C3=B0=20virkja=20Fault=20takka?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/operational_state.h | 2 +- src/motor_driver.cpp | 36 +++++++++++++++++++++++------ zephyr/boards/nucleo_l432kc.overlay | 8 ++++++- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/include/operational_state.h b/include/operational_state.h index f213ce9..e56bde2 100644 --- a/include/operational_state.h +++ b/include/operational_state.h @@ -7,7 +7,7 @@ // Sett á 1 til að prenta CSV (time_ms,ref_rpm,actual_rpm,pwm) í Operational, // t.d. fyrir step og load response. Einnig hægt með build_flags = -D LOG_CSV=1 #ifndef LOG_CSV -#define LOG_CSV 0 +#define LOG_CSV 1 #endif diff --git a/src/motor_driver.cpp b/src/motor_driver.cpp index 5d3fd50..bc990ff 100644 --- a/src/motor_driver.cpp +++ b/src/motor_driver.cpp @@ -45,6 +45,9 @@ static const struct gpio_dt_spec bin2 = static const struct gpio_dt_spec motor_flt = GPIO_DT_SPEC_GET(DT_ALIAS(motor_flt), gpios); +static const struct gpio_dt_spec fault_test_button = + GPIO_DT_SPEC_GET(DT_ALIAS(fault_test_button), gpios); + static const struct gpio_dt_spec motor_slp = GPIO_DT_SPEC_GET(DT_ALIAS(motor_slp), gpios); @@ -87,12 +90,14 @@ void MotorDriver::begin() if (!pwm_is_ready_dt(&bin1_pwm) || !gpio_is_ready_dt(&bin2) || !gpio_is_ready_dt(&motor_flt) || + !gpio_is_ready_dt(&fault_test_button) || !gpio_is_ready_dt(&motor_slp)) { return; } if (gpio_pin_configure_dt(&bin2, GPIO_OUTPUT_INACTIVE) < 0 || gpio_pin_configure_dt(&motor_flt, GPIO_INPUT) < 0 || + gpio_pin_configure_dt(&fault_test_button, GPIO_INPUT) < 0 || gpio_pin_configure_dt(&motor_slp, GPIO_OUTPUT_INACTIVE) < 0) { return; } @@ -206,16 +211,33 @@ void MotorDriver::set(double pwm) } +// bool MotorDriver::fault_active() +// { +// //return !(PIND & (1 << PIND4)); + +// if (!initialized) { +// return true; // Fail safe if the driver did not initialize. +// } + +// // The overlay marks FLT as active low. gpio_pin_get_dt() returns +// // the logical active state, so 1 means a fault is active. +// const int fault = gpio_pin_get_dt(&motor_flt); +// return fault < 0 || fault != 0; +// } + bool MotorDriver::fault_active() { - //return !(PIND & (1 << PIND4)); - if (!initialized) { return true; // Fail safe if the driver did not initialize. } - // The overlay marks FLT as active low. gpio_pin_get_dt() returns - // the logical active state, so 1 means a fault is active. - const int fault = gpio_pin_get_dt(&motor_flt); - return fault < 0 || fault != 0; -} + const int driver_fault = gpio_pin_get_dt(&motor_flt); + const int button_fault = gpio_pin_get_dt(&fault_test_button); + + // Negative values indicate a GPIO read error, so treat them as a fault. + // With GPIO_ACTIVE_LOW, a pressed button reads as logical 1. + return driver_fault < 0 || + button_fault < 0 || + driver_fault != 0 || + button_fault != 0; +} \ No newline at end of file diff --git a/zephyr/boards/nucleo_l432kc.overlay b/zephyr/boards/nucleo_l432kc.overlay index 5d27c9f..bac38e6 100644 --- a/zephyr/boards/nucleo_l432kc.overlay +++ b/zephyr/boards/nucleo_l432kc.overlay @@ -10,6 +10,7 @@ motor-bin2 = &motor_bin2; motor-flt = &motor_flt; motor-slp = &motor_slp; + fault-test-button = &fault_test_button; }; buttons { @@ -29,8 +30,13 @@ gpios = <&gpioa 0 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; /* A0, PA0 */ label = "Motor fault"; }; + + fault_test_button: fault_test_button { + gpios = <&gpiob 0 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; /* D3, PB0 */ + label = "Fault test button"; + }; }; - + motor_gpio_outputs { compatible = "gpio-leds"; From b90b84d94c6ce287c71a0bb50ff9262647b95263 Mon Sep 17 00:00:00 2001 From: isakg22 Date: Wed, 14 Oct 2026 16:02:44 +0000 Subject: [PATCH 2/2] =?UTF-8?q?B=C3=A6tti=20vi=C3=B0=20interrupt=20(pin=20?= =?UTF-8?q?D2)=20til=20a=C3=B0=20sko=C3=B0a=20encoder=20lestur,=20umb=20fy?= =?UTF-8?q?rir=20Part=201=20=C3=AD=20verkefni?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/motor_controller.h | 2 +- include/operational_state.h | 2 +- src/rotary_encoder_driver.cpp | 24 +++++++++++++++++++----- zephyr/boards/nucleo_l432kc.overlay | 11 ++++++++++- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/include/motor_controller.h b/include/motor_controller.h index 4923ec9..4115db4 100644 --- a/include/motor_controller.h +++ b/include/motor_controller.h @@ -14,7 +14,7 @@ class State; class MotorController { private: - static constexpr double DEFAULT_REFERENCE_RPM = 40.0; + static constexpr double DEFAULT_REFERENCE_RPM = -40.0; static constexpr double PWM_PER_PERCENT = 255.0 / 100.0; //static const uint8_t LED_PIN = 5; // PB5 = D13 = LED_BUILTIN static constexpr uint8_t LED_PIN = Digital_out::STATUS_LED; diff --git a/include/operational_state.h b/include/operational_state.h index e56bde2..f213ce9 100644 --- a/include/operational_state.h +++ b/include/operational_state.h @@ -7,7 +7,7 @@ // Sett á 1 til að prenta CSV (time_ms,ref_rpm,actual_rpm,pwm) í Operational, // t.d. fyrir step og load response. Einnig hægt með build_flags = -D LOG_CSV=1 #ifndef LOG_CSV -#define LOG_CSV 1 +#define LOG_CSV 0 #endif diff --git a/src/rotary_encoder_driver.cpp b/src/rotary_encoder_driver.cpp index b7b75d0..002fc5c 100644 --- a/src/rotary_encoder_driver.cpp +++ b/src/rotary_encoder_driver.cpp @@ -3,6 +3,13 @@ #include #include #include +#include +#include + +//#define ISR_PROBE_NODE DT_PATH(zephyr_user) + +static const struct gpio_dt_spec isr_probe = + GPIO_DT_SPEC_GET(DT_ALIAS(encoder_isr_probe), gpios); static const struct gpio_dt_spec encoder_c1 = GPIO_DT_SPEC_GET(DT_ALIAS(encoder_c1), gpios); @@ -24,11 +31,17 @@ RotaryEncoderDriver::RotaryEncoderDriver() int RotaryEncoderDriver::begin() { if (!gpio_is_ready_dt(&encoder_c1) || - !gpio_is_ready_dt(&encoder_c2)) { + !gpio_is_ready_dt(&encoder_c2) || + !gpio_is_ready_dt(&isr_probe)) { return -ENODEV; } - int ret = gpio_pin_configure_dt(&encoder_c1, GPIO_INPUT); + int ret = gpio_pin_configure_dt(&isr_probe, GPIO_OUTPUT_INACTIVE); + if (ret < 0) { + return ret; + } + + ret = gpio_pin_configure_dt(&encoder_c1, GPIO_INPUT); if (ret < 0) { return ret; } @@ -48,7 +61,6 @@ int RotaryEncoderDriver::begin() return ret; } - // C1 is active high, so “to active” means a rising edge. ret = gpio_pin_interrupt_configure_dt( &encoder_c1, GPIO_INT_EDGE_TO_ACTIVE); @@ -58,8 +70,8 @@ int RotaryEncoderDriver::begin() timer.set_callback(timer_expired, this); timer.init(SAMPLE_PERIOD_US); - reset_speed_measurement(); + return 0; } @@ -76,7 +88,9 @@ void RotaryEncoderDriver::gpio_edge_handler( encoder_c1_callback); if ((pins & BIT(encoder_c1.pin)) != 0U) { - encoder->update(); + encoder->update(); // Reads C2 and updates the count + gpio_pin_set_dt(&isr_probe, 1); // Marker rising edge + gpio_pin_set_dt(&isr_probe, 0); // Reset for the next interrupt } } diff --git a/zephyr/boards/nucleo_l432kc.overlay b/zephyr/boards/nucleo_l432kc.overlay index bac38e6..cca7b88 100644 --- a/zephyr/boards/nucleo_l432kc.overlay +++ b/zephyr/boards/nucleo_l432kc.overlay @@ -11,6 +11,7 @@ motor-flt = &motor_flt; motor-slp = &motor_slp; fault-test-button = &fault_test_button; + encoder-isr-probe = &encoder_isr_probe; }; buttons { @@ -36,7 +37,7 @@ label = "Fault test button"; }; }; - + motor_gpio_outputs { compatible = "gpio-leds"; @@ -49,6 +50,11 @@ gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; /* A1, PA1 */ label = "Motor sleep"; }; + + encoder_isr_probe: encoder_isr_probe { + gpios = <&gpioa 12 GPIO_ACTIVE_HIGH>; /* D2, PA12 */ + label = "Encoder ISR probe"; + }; }; motor_pwm_outputs { @@ -59,6 +65,9 @@ label = "Motor BIN1 PWM"; }; }; + zephyr,user { + encoder-isr-probe-gpios = <&gpioa 12 GPIO_ACTIVE_HIGH>; /* D2, PA12 */ + }; }; &timers1 {