From dbf83bfbe92873524320b388f55b1f2b575b9303 Mon Sep 17 00:00:00 2001 From: Federica Di Lauro Date: Thu, 6 Feb 2020 14:21:50 +0100 Subject: [PATCH] move encoder methods to header file --- otto_controller/Core/Inc/encoder.h | 36 ++++++++++++++++++++++---- otto_controller/Core/Src/encoder.cpp | 38 ---------------------------- 2 files changed, 31 insertions(+), 43 deletions(-) delete mode 100644 otto_controller/Core/Src/encoder.cpp diff --git a/otto_controller/Core/Inc/encoder.h b/otto_controller/Core/Inc/encoder.h index 977507b..479cc1a 100644 --- a/otto_controller/Core/Inc/encoder.h +++ b/otto_controller/Core/Inc/encoder.h @@ -17,9 +17,11 @@ class Encoder { wheel_circumference_ = 0; } - Encoder(TIM_HandleTypeDef *timer, float wheel_circ); + Encoder(TIM_HandleTypeDef *timer, float wheel_circ) { + timer_ = timer; + wheel_circumference_ = wheel_circ; - void Setup(); + } int GetCount() { int count = ((int) __HAL_TIM_GET_COUNTER(this->timer_) @@ -32,11 +34,35 @@ class Encoder { __HAL_TIM_SET_COUNTER(timer_, (timer_->Init.Period / 2)); } - void UpdateValues(); + void Setup() { + HAL_TIM_Encoder_Start(timer_, TIM_CHANNEL_ALL); + this->ResetCount(); + this->previous_millis_ = 0; + this->current_millis_ = HAL_GetTick(); + } + + void UpdateValues() { + this->previous_millis_ = this->current_millis_; + this->current_millis_ = HAL_GetTick(); + this->ticks_ = this->GetCount(); + this->ResetCount(); + } - float GetMeters(); + float GetMeters() { + float meters = ((float) this->ticks_ * this->wheel_circumference_) + / TICKS_PER_REVOLUTION; + return meters; + } - float GetLinearVelocity(); + 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; + } }; #endif diff --git a/otto_controller/Core/Src/encoder.cpp b/otto_controller/Core/Src/encoder.cpp deleted file mode 100644 index f3f7860..0000000 --- a/otto_controller/Core/Src/encoder.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "encoder.h" - -Encoder::Encoder(TIM_HandleTypeDef *timer, float wheel_circ) { - timer_ = timer; - wheel_circumference_ = wheel_circ; - -} - -void Encoder::Setup() { - HAL_TIM_Encoder_Start(timer_, TIM_CHANNEL_ALL); - this->ResetCount(); - this->previous_millis_ = 0; - this->current_millis_ = HAL_GetTick(); -} - -void Encoder::UpdateValues() { - this->previous_millis_ = this->current_millis_; - this->current_millis_ = HAL_GetTick(); - this->ticks_ = this->GetCount(); - this->ResetCount(); -} - -float Encoder::GetMeters() { - float meters = ((float) this->ticks_ * this->wheel_circumference_) - / TICKS_PER_REVOLUTION; - return meters; -} - -float Encoder::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; -} - -- 2.52.0