Add zero speed pulse measurement updates for better rev count

master
Tom Wilson 4 years ago
parent 820f5843aa
commit b1e280004f

@ -3,8 +3,8 @@
#include "roller_speed.h"
#include "io.h"
#include "freertos/FreeRTOS.h"
#include "driver/mcpwm.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "soc/rtc.h"
@ -50,17 +50,19 @@ static bool speed_pulse_isr_handler(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id
int64_t time_now;
time_now = esp_timer_get_time();
new_measurement.timestamp = time_now;
new_measurement.pulses = roller_pulses[r_id];
// Just make sure we have a previous edge to compare to, and that it was no more than ZERO_SPEED_TIMEOUT_US ago
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 pulses 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);
} else {
// If there's no previous edge detected, or it's too far away, we still want to give an update with the new
// pulse count
new_measurement.period = 0;
}
xQueueOverwriteFromISR(roller_measurement_queue[r_id], &new_measurement, &high_task_wakeup);
roller_last_edge_time[r_id] = edata->cap_value;
roller_last_timestamp[r_id] = time_now;
@ -71,6 +73,8 @@ float get_roller_speed(roller_id_t roller_id) {
pulse_measurement_t pulse_measurement;
// Get the pulse period in APB clock ticks.
xQueuePeek(roller_measurement_queue[roller_id], &pulse_measurement, 0);
// A period of zero means that it didn't actually have a previous pulse to compare to
if ((pulse_measurement.period == 0) or
((esp_timer_get_time() - pulse_measurement.timestamp) >= ZERO_SPEED_TIMEOUT_US)) {
return 0.0;

@ -4,7 +4,7 @@
#include "../common/shared_structs.h"
// Number of sensor pulses per roller rev. Note that this will overflow at about 4 billion pulses
#define ROLLER_PULSES_PER_REV 40
#define ROLLER_PULSES_PER_REV 30
// Time without a pulse after which the speed will be considered to be 0.
// Note that with a clock at 80MHz, the pulse period counter overflows every 53 seconds

Loading…
Cancel
Save