Skip to content

Commit c28036b

Browse files
committed
timer added
1 parent a11914c commit c28036b

3 files changed

Lines changed: 47 additions & 14 deletions

File tree

Core/Inc/u_wheel_speed.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ void wheel_speed_capture_callback(TIM_HandleTypeDef *htim);
2929
*/
3030
void calculate_wheel_rpm(int frequency, int *rpm);
3131

32+
/**
33+
* @brief Function called by wheel speed pulse timer. If we do not recieve a pulse
34+
* in the span of the timer, we set rpm to 0.
35+
*/
36+
void wheel_pulse_check();
37+
3238
/**
3339
* Sends wheel speed data over CAN
3440
*/

Core/Src/u_threads.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ void can_outgoing_thread(ULONG thread_input) {
100100

101101
/* Sensors Thread */
102102
static nertimer_t data_send_timer;
103+
static nertimer_t wheel_pulse_timer;
103104
static thread_t _sensors_thread = {
104105
.name = "Sensors Thread", /* Name */
105106
.size = 2048, /* Stack Size (in bytes) */
@@ -111,24 +112,31 @@ static thread_t _sensors_thread = {
111112
.function = sensors_thread /* Thread Function */
112113
};
113114
void sensors_thread(ULONG thread_input) {
114-
const uint16_t DATA_SEND_INTERVAL = 25 * _sensors_thread.sleep;
115-
start_timer(&data_send_timer, DATA_SEND_INTERVAL);
115+
const uint16_t DATA_SEND_INTERVAL = 25 * _sensors_thread.sleep;
116+
const uint16_t WHEEL_PULSE_INTERVAL = 15 * _sensors_thread.sleep;
117+
118+
start_timer(&data_send_timer, DATA_SEND_INTERVAL);
119+
start_timer(&wheel_pulse_timer, WHEEL_PULSE_INTERVAL);
116120

117-
while (1) {
118-
CATCH_ERROR(read_imu_and_magnometer(), U_SUCCESS);
121+
while (1) {
122+
CATCH_ERROR(read_imu_and_magnometer(), U_SUCCESS);
119123

120-
if (is_timer_expired(&data_send_timer)) {
121-
send_vl53l7cx_data();
122-
send_imu_and_magnometer_data();
123-
start_timer(&data_send_timer, DATA_SEND_INTERVAL);
124+
if (is_timer_expired(&data_send_timer)) {
125+
send_vl53l7cx_data();
126+
send_imu_and_magnometer_data();
127+
start_timer(&data_send_timer, DATA_SEND_INTERVAL);
124128

125-
if (device_loc == DEVICE_FRONT) {
126-
send_wheel_speed();
127-
}
128-
}
129+
if (device_loc == DEVICE_FRONT) {
130+
send_wheel_speed();
131+
}
132+
}
129133

130-
tx_thread_sleep(_sensors_thread.sleep);
131-
}
134+
if (is_timer_expired(&wheel_pulse_timer)) {
135+
wheel_pulse_check();
136+
}
137+
138+
tx_thread_sleep(_sensors_thread.sleep);
139+
}
132140
}
133141

134142
/* ADCs Thread */

Core/Src/u_wheel_speed.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ static uint32_t left_val1 = 0;
1010
static uint32_t left_val2 = 0;
1111
static uint8_t left_captured = 0;
1212
static float left_rpm = 0;
13+
static bool pulse_detected_left = false;
1314

1415
/* Right Wheel */
1516
static uint32_t right_val1 = 0;
1617
static uint32_t right_val2 = 0;
1718
static uint8_t right_captured = 0;
1819
static float right_rpm = 0;
20+
static bool pulse_detected_right = false;
1921

2022
void wheel_speed_init(TIM_HandleTypeDef *_htim_left, TIM_HandleTypeDef *_htim_right) {
2123
htim_left = _htim_left;
@@ -27,6 +29,8 @@ void wheel_speed_init(TIM_HandleTypeDef *_htim_left, TIM_HandleTypeDef *_htim_ri
2729

2830
void wheel_speed_capture_callback(TIM_HandleTypeDef *htim) {
2931
if (htim->Instance == htim_left->Instance && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
32+
pulse_detected_left = true;
33+
3034
if (left_captured == 0) {
3135
left_val1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
3236
left_captured = 1;
@@ -58,6 +62,8 @@ void wheel_speed_capture_callback(TIM_HandleTypeDef *htim) {
5862
}
5963

6064
if (htim->Instance == htim_right->Instance && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
65+
pulse_detected_right = true;
66+
6167
if (right_captured == 0) {
6268
right_val1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
6369
right_captured = 1;
@@ -92,6 +98,19 @@ void calculate_wheel_rpm(int frequency, int *rpm) {
9298
*rpm = (frequency * 60.0f) / PULSES_PER_ROTATION;
9399
}
94100

101+
void wheel_pulse_check() {
102+
if (!pulse_detected_left) {
103+
left_rpm = 0;
104+
}
105+
106+
if (!pulse_detected_right) {
107+
right_rpm = 0;
108+
}
109+
110+
pulse_detected_left = false;
111+
pulse_detected_right = false;
112+
}
113+
95114
void send_wheel_speed() {
96115
struct __attribute__((__packed__)) {
97116
uint16_t right_rpm;

0 commit comments

Comments
 (0)