From: LeonardoBizzoni Date: Wed, 22 Oct 2025 19:53:44 +0000 (+0200) Subject: upgraded `Encoder` to C X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=d0b53ee571298e2e00858dcdf56c140baf50fb50;p=pioneer-stm32 upgraded `Encoder` to C --- diff --git a/otto_controller/Core/Inc/control/encoder.h b/otto_controller/Core/Inc/control/encoder.h index 933e5b5..296ae27 100644 --- a/otto_controller/Core/Inc/control/encoder.h +++ b/otto_controller/Core/Inc/control/encoder.h @@ -3,54 +3,24 @@ #include "main.h" -typedef struct Encoder { +typedef struct { TIM_HandleTypeDef *timer; uint32_t previous_millis; uint32_t current_millis; int32_t ticks; //if negative the wheel is going backwards - float_t wheel_circumference; int32_t ticks_per_revolution; -}; + float wheel_circumference; +} Encoder; -void Setup() { - HAL_TIM_Encoder_Start(timer_, TIM_CHANNEL_ALL); - this->ResetCount(); - this->previous_millis_ = 0; - this->current_millis_ = HAL_GetTick(); -} +void encoder_init(Encoder *encoders); +void encoder_update(Encoder *encoder); +float encoder_linear_velocity(Encoder *encoder); -void ResetCount() { - //set counter to half its maximum value - __HAL_TIM_SET_COUNTER(timer_, (timer_->Init.Period / 2)); -} +void encoder_count_reset(Encoder *encoder); +int encoder_count_get(Encoder *encoder); -int GetCount() { - int count = ((int) __HAL_TIM_GET_COUNTER(this->timer_) - - ((this->timer_->Init.Period) / 2)); - return count; -} - -void UpdateValues() { - this->previous_millis_ = this->current_millis_; - this->current_millis_ = HAL_GetTick(); - this->ticks_ = this->GetCount(); - this->ResetCount(); -} - -float GetMeters() { - float meters = ((float) this->ticks_ * this->wheel_circumference_) - / ticks_per_revolution_; - return meters; -} - -float GetLinearVelocity() { - this->UpdateValues(); - float meters = this->GetMeters(); - float deltaTime = this->current_millis_ - this->previous_millis_; - if (deltaTime == 0) - return 0; - float linear_velocity = (meters / (deltaTime / 1000)); - return linear_velocity; -} +inline float meters_from_ticks(float encoder_ticks, + float wheel_circumference, + float ticks_per_revolution); #endif diff --git a/otto_controller/Core/Src/main.cpp b/otto_controller/Core/Src/main.cpp index 5fb089c..b646ff6 100644 --- a/otto_controller/Core/Src/main.cpp +++ b/otto_controller/Core/Src/main.cpp @@ -37,6 +37,7 @@ // NOTE(lb): couldn't get it to link in the final executable #include "./control/motor_controller.c" +#include "./control/encoder.c" /* USER CODE END Includes */ @@ -59,8 +60,10 @@ /* USER CODE BEGIN PV */ //Odometry -Encoder encoder_left = {0}; -Encoder encoder_right = {0}; +static Encoder encoders[2] = {{0}, {0}}; +Encoder *encoder_right = &encoders[0]; +Encoder *encoder_left = &encoders[1]; + Odometry odom; //PID @@ -69,7 +72,7 @@ Pid right_pid; Pid cross_pid; //MotorController -static MotorController motors[] = { +static MotorController motors[2] = { { // Right motor .sleep_gpio_port = sleep1_GPIO_Port, @@ -165,19 +168,17 @@ int main(void) { } } - left_encoder.timer = &htim2; - left_encoder.wheel_circumference = config_msg.left_wheel_circumference; - left_encoder.ticks_per_revolution = config_msg.ticks_per_revolution; + encoder_left->timer = &htim2; + encoder_left->wheel_circumference = config_msg.left_wheel_circumference; + encoder_left->ticks_per_revolution = config_msg.ticks_per_revolution; - right_encoder.timer = &htim5; - right_encoder.wheel_circumference = config_msg.right_wheel_circumference; - right_encoder.ticks_per_revolution = config_msg.ticks_per_revolution; + encoder_right->timer = &htim5; + encoder_right->wheel_circumference = config_msg.right_wheel_circumference; + encoder_right->ticks_per_revolution = config_msg.ticks_per_revolution; odom = Odometry(config_msg.baseline); - left_encoder.Setup(); - right_encoder.Setup(); - + encoder_init(encoders); motorcontroller_init(motors); //right and left motors have the same parameters @@ -290,14 +291,16 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { //TIMER 100Hz PID control //accumulate ticks for transmission - left_ticks += left_encoder.GetCount(); - right_ticks += right_encoder.GetCount(); + left_ticks += encoder_count_get(encoder_left); + right_ticks += encoder_count_get(encoder_right); //PID control - float left_velocity = left_encoder.GetLinearVelocity(); + encoder_update(encoder_left); + float left_velocity = encoder_linear_velocity(encoder_left); int left_dutycycle = left_pid.Update(left_velocity); - float right_velocity = right_encoder.GetLinearVelocity(); + encoder_update(encoder_right); + float right_velocity = encoder_linear_velocity(encoder_right); int right_dutycycle = right_pid.Update(right_velocity); float difference = left_velocity - right_velocity; @@ -346,8 +349,8 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) { * Manage new transmission */ - int32_t left_ticks_tx = left_ticks + left_encoder.GetCount(); - int32_t right_ticks_tx = right_ticks + right_encoder.GetCount(); + int32_t left_ticks_tx = left_ticks + encoder_count_get(encoder_left); + int32_t right_ticks_tx = right_ticks + encoder_count_get(encoder_right); status_msg.left_ticks = left_ticks_tx; status_msg.right_ticks = right_ticks_tx;