PWM inputs and debugging

Hi folks,

I’m trying to use a PWM input for something slightly different than what’s in the documentation. I have an encoder that doesn’t have ABI output, but it does have PWM.

It’s 0-100% style, not 1ms-2ms (as mentioned in this thread). So I made the following modifications to the firmware:

ODrive/Firmware$ git diff MotorControl/low_level.cpp
diff --git a/Firmware/MotorControl/low_level.cpp b/Firmware/MotorControl/low_level.cpp
index b40b327..f81da23 100644
--- a/Firmware/MotorControl/low_level.cpp
+++ b/Firmware/MotorControl/low_level.cpp
@@ -669,9 +669,9 @@ void pwm_in_init() {
 
 //TODO: These expressions have integer division by 1MHz, so it will be incorrect for clock speeds of not-integer MHz
 #define TIM_2_5_CLOCK_HZ        TIM_APB1_CLOCK_HZ
-#define PWM_MIN_HIGH_TIME          ((TIM_2_5_CLOCK_HZ / 1000000UL) * 1000UL) // 1ms high is considered full reverse
-#define PWM_MAX_HIGH_TIME          ((TIM_2_5_CLOCK_HZ / 1000000UL) * 2000UL) // 2ms high is considered full forward
-#define PWM_MIN_LEGAL_HIGH_TIME    ((TIM_2_5_CLOCK_HZ / 1000000UL) * 500UL) // ignore high periods shorter than 0.5ms
+#define PWM_MIN_HIGH_TIME          ((TIM_2_5_CLOCK_HZ / 1000000UL) * 1UL) // 1us high is considered full reverse
+#define PWM_MAX_HIGH_TIME          ((TIM_2_5_CLOCK_HZ / 1000000UL) * 1000UL) // 1ms high is considered full forward
+#define PWM_MIN_LEGAL_HIGH_TIME    ((TIM_2_5_CLOCK_HZ / 1000000UL) * 1UL) // ignore high periods shorter than 1us
 #define PWM_MAX_LEGAL_HIGH_TIME    ((TIM_2_5_CLOCK_HZ / 1000000UL) * 2500UL) // ignore high periods longer than 2.5ms
 #define PWM_INVERT_INPUT        false
 
@@ -713,4 +713,4 @@ void pwm_in_cb(int channel, uint32_t timestamp) {
     last_timestamp[gpio_num - 1] = timestamp;
     last_pin_state[gpio_num - 1] = current_pin_state;
     last_sample_valid[gpio_num - 1] = true;

Then I configured the pin as follows:

odrv0.config.gpio4_pwm_mapping.min = 0
odrv0.config.gpio4_pwm_mapping.max = 3000
odrv0.config.gpio4_pwm_mapping.endpoing = odrv0.axis0.encoder._remote_attributes['count_in_cpr']

However… nothing happens. count_in_cpr is never updated (I also tried _remote_attributes['pos_estimate']). And further, I don’t seem to have any way to debug. Besides setting the endpoint, is there any way to get the current measured value from a PWM pin, to see if the measurement is working?