Bætti við interrupt (pin D2) til að skoða encoder lestur, umb fyrir Part 1 í verkefni

This commit is contained in:
2026-10-14 16:02:44 +00:00
parent 1250860efe
commit b90b84d94c
4 changed files with 31 additions and 8 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ class State;
class MotorController class MotorController
{ {
private: 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 constexpr double PWM_PER_PERCENT = 255.0 / 100.0;
//static const uint8_t LED_PIN = 5; // PB5 = D13 = LED_BUILTIN //static const uint8_t LED_PIN = 5; // PB5 = D13 = LED_BUILTIN
static constexpr uint8_t LED_PIN = Digital_out::STATUS_LED; static constexpr uint8_t LED_PIN = Digital_out::STATUS_LED;
+1 -1
View File
@@ -7,7 +7,7 @@
// Sett á 1 til að prenta CSV (time_ms,ref_rpm,actual_rpm,pwm) í Operational, // 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 // t.d. fyrir step og load response. Einnig hægt með build_flags = -D LOG_CSV=1
#ifndef LOG_CSV #ifndef LOG_CSV
#define LOG_CSV 1 #define LOG_CSV 0
#endif #endif
+19 -5
View File
@@ -3,6 +3,13 @@
#include <errno.h> #include <errno.h>
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/sys/util.h> #include <zephyr/sys/util.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
//#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 = static const struct gpio_dt_spec encoder_c1 =
GPIO_DT_SPEC_GET(DT_ALIAS(encoder_c1), gpios); GPIO_DT_SPEC_GET(DT_ALIAS(encoder_c1), gpios);
@@ -24,11 +31,17 @@ RotaryEncoderDriver::RotaryEncoderDriver()
int RotaryEncoderDriver::begin() int RotaryEncoderDriver::begin()
{ {
if (!gpio_is_ready_dt(&encoder_c1) || 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; 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) { if (ret < 0) {
return ret; return ret;
} }
@@ -48,7 +61,6 @@ int RotaryEncoderDriver::begin()
return ret; return ret;
} }
// C1 is active high, so “to active” means a rising edge.
ret = gpio_pin_interrupt_configure_dt( ret = gpio_pin_interrupt_configure_dt(
&encoder_c1, &encoder_c1,
GPIO_INT_EDGE_TO_ACTIVE); GPIO_INT_EDGE_TO_ACTIVE);
@@ -58,8 +70,8 @@ int RotaryEncoderDriver::begin()
timer.set_callback(timer_expired, this); timer.set_callback(timer_expired, this);
timer.init(SAMPLE_PERIOD_US); timer.init(SAMPLE_PERIOD_US);
reset_speed_measurement(); reset_speed_measurement();
return 0; return 0;
} }
@@ -76,7 +88,9 @@ void RotaryEncoderDriver::gpio_edge_handler(
encoder_c1_callback); encoder_c1_callback);
if ((pins & BIT(encoder_c1.pin)) != 0U) { 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
} }
} }
+9
View File
@@ -11,6 +11,7 @@
motor-flt = &motor_flt; motor-flt = &motor_flt;
motor-slp = &motor_slp; motor-slp = &motor_slp;
fault-test-button = &fault_test_button; fault-test-button = &fault_test_button;
encoder-isr-probe = &encoder_isr_probe;
}; };
buttons { buttons {
@@ -49,6 +50,11 @@
gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; /* A1, PA1 */ gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; /* A1, PA1 */
label = "Motor sleep"; label = "Motor sleep";
}; };
encoder_isr_probe: encoder_isr_probe {
gpios = <&gpioa 12 GPIO_ACTIVE_HIGH>; /* D2, PA12 */
label = "Encoder ISR probe";
};
}; };
motor_pwm_outputs { motor_pwm_outputs {
@@ -59,6 +65,9 @@
label = "Motor BIN1 PWM"; label = "Motor BIN1 PWM";
}; };
}; };
zephyr,user {
encoder-isr-probe-gpios = <&gpioa 12 GPIO_ACTIVE_HIGH>; /* D2, PA12 */
};
}; };
&timers1 { &timers1 {