|
|
|
|
@ -20,6 +20,7 @@ static xQueueHandle roller_measurement_queue[3];
|
|
|
|
|
typedef struct {
|
|
|
|
|
uint32_t period; // period since last pulse in APB clock ticks
|
|
|
|
|
int64_t timestamp; // time measurement occurred in microseconds since boot
|
|
|
|
|
uint32_t pulses; // Total pulses of roller so far
|
|
|
|
|
} pulse_measurement_t;
|
|
|
|
|
|
|
|
|
|
// Gets called by the MCPWM capture module. Tells us which capture unit it was, which edge, and when it happened
|
|
|
|
|
@ -29,7 +30,7 @@ static bool speed_pulse_isr_handler(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id
|
|
|
|
|
// Not strictly necessary to do the wakeup check here, as we shouldn't be blokcing anything. Doesn't hurt though.
|
|
|
|
|
BaseType_t high_task_wakeup = pdFALSE;
|
|
|
|
|
|
|
|
|
|
uint r_id = 0;
|
|
|
|
|
int r_id = 0;
|
|
|
|
|
|
|
|
|
|
if (cap_sig == MCPWM_SELECT_CAP0) {
|
|
|
|
|
r_id = 0;
|
|
|
|
|
@ -44,7 +45,7 @@ static bool speed_pulse_isr_handler(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id
|
|
|
|
|
// Add one to our revs counter, and overflow it if necessary
|
|
|
|
|
roller_pulses[r_id]++;
|
|
|
|
|
if ((roller_pulses[r_id] / ROLLER_PULSES_PER_REV) > REV_OVERFLOW) {
|
|
|
|
|
roller_pulses[0] = 0;
|
|
|
|
|
roller_pulses[r_id] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pulse_measurement_t new_measurement;
|
|
|
|
|
@ -55,6 +56,10 @@ static bool speed_pulse_isr_handler(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id
|
|
|
|
|
if ((roller_last_timestamp[r_id] != 0) and ((time_now - roller_last_timestamp[r_id]) < ZERO_SPEED_TIMEOUT_US)) {
|
|
|
|
|
new_measurement.timestamp = time_now;
|
|
|
|
|
new_measurement.period = edata->cap_value - roller_last_edge_time[r_id];
|
|
|
|
|
// Technically passing the roller passes with the rest of the measurement here means that it won't be updated
|
|
|
|
|
// for the first pulse detected after being stopped, but for the purposes of tracking total revs in a given
|
|
|
|
|
// period of several seconds this shouldn't matter.
|
|
|
|
|
new_measurement.pulses = roller_pulses[r_id];
|
|
|
|
|
xQueueOverwriteFromISR(roller_measurement_queue[r_id], &new_measurement, &high_task_wakeup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -76,6 +81,12 @@ float get_roller_speed(int roller_id) {
|
|
|
|
|
return ((float)rtc_clk_apb_freq_get() / (float)(pulse_measurement.period)) / (float)(ROLLER_PULSES_PER_REV);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float get_roller_revs(int roller_id) {
|
|
|
|
|
pulse_measurement_t pulse_measurement;
|
|
|
|
|
xQueuePeek(roller_measurement_queue[roller_id], &pulse_measurement, 0);
|
|
|
|
|
return (float)pulse_measurement.pulses / (float)ROLLER_PULSES_PER_REV;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void roller_speed_setup(void) {
|
|
|
|
|
// Create our measurement queues
|
|
|
|
|
roller_measurement_queue[0] = xQueueCreate(1, sizeof(pulse_measurement_t));
|
|
|
|
|
|