From 41699330484683101a0922f33b757d7f35194ccb Mon Sep 17 00:00:00 2001 From: Federica Di Lauro Date: Thu, 6 Feb 2020 14:51:35 +0100 Subject: [PATCH] remove constants.h --- otto_controller/Core/Inc/constants.h | 11 --- otto_controller/Core/Inc/encoder.h | 13 ++-- otto_controller/Core/Inc/motor_controller.h | 11 +-- otto_controller/Core/Inc/odometry.h | 19 ++++-- otto_controller/Core/Inc/pid.h | 5 -- otto_controller/Core/Src/main.cpp | 76 ++++++++++----------- 6 files changed, 64 insertions(+), 71 deletions(-) delete mode 100644 otto_controller/Core/Inc/constants.h diff --git a/otto_controller/Core/Inc/constants.h b/otto_controller/Core/Inc/constants.h deleted file mode 100644 index de60e91..0000000 --- a/otto_controller/Core/Inc/constants.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef CONSTANTS_H -#define CONSTANTS_H - -#define BASELINE 0.3 //distance between wheels in meters -#define MAX_DUTY_CYCLE 790 - -#define TICKS_PER_REVOLUTION 148000 //x4 resolution -#define RIGHT_WHEEL_CIRCUMFERENCE 0.783 //in meters -#define LEFT_WHEEL_CIRCUMFERENCE 0.789 //in meters - -#endif diff --git a/otto_controller/Core/Inc/encoder.h b/otto_controller/Core/Inc/encoder.h index 479cc1a..50004a6 100644 --- a/otto_controller/Core/Inc/encoder.h +++ b/otto_controller/Core/Inc/encoder.h @@ -2,7 +2,6 @@ #define ENCODER_H #include "main.h" -#include "constants.h" class Encoder { public: @@ -11,15 +10,19 @@ class Encoder { uint32_t current_millis_; int32_t ticks_; //if negative the wheel is going backwards float wheel_circumference_; + int ticks_per_revolution_; Encoder() { timer_ = NULL; wheel_circumference_ = 0; + ticks_per_revolution_ = 0; } - Encoder(TIM_HandleTypeDef *timer, float wheel_circ) { - timer_ = timer; - wheel_circumference_ = wheel_circ; + Encoder(TIM_HandleTypeDef *timer, float wheel_circ, + int ticks_per_revolution) { + timer_ = timer; + wheel_circumference_ = wheel_circ; + ticks_per_revolution_ = ticks_per_revolution; } @@ -50,7 +53,7 @@ class Encoder { float GetMeters() { float meters = ((float) this->ticks_ * this->wheel_circumference_) - / TICKS_PER_REVOLUTION; + / ticks_per_revolution_; return meters; } diff --git a/otto_controller/Core/Inc/motor_controller.h b/otto_controller/Core/Inc/motor_controller.h index c4b5e8d..597ca0a 100644 --- a/otto_controller/Core/Inc/motor_controller.h +++ b/otto_controller/Core/Inc/motor_controller.h @@ -2,7 +2,6 @@ #define MOTOR_CONTROLLER_H #include "main.h" -#include "constants.h" class MotorController { public: @@ -12,6 +11,7 @@ class MotorController { uint16_t dir_pin_; TIM_HandleTypeDef *pwm_timer_; uint32_t pwm_channel_; + int32_t max_dutycycle_; MotorController(GPIO_TypeDef *sleep_gpio_port, uint16_t sleep_pin, GPIO_TypeDef *dir_gpio_port, uint16_t dir_pin, @@ -22,6 +22,7 @@ class MotorController { this->dir_pin_ = dir_pin; this->pwm_timer_ = pwm_timer; this->pwm_channel_ = pwm_channel; + this->max_dutycycle_ = pwm_timer->Instance->ARR; } void setup() { @@ -34,8 +35,8 @@ class MotorController { HAL_GPIO_WritePin(dir_gpio_port_, dir_pin_, GPIO_PIN_SET); //check if duty_cycle exceeds maximum - if (duty_cycle > MAX_DUTY_CYCLE) - __HAL_TIM_SET_COMPARE(pwm_timer_, pwm_channel_, MAX_DUTY_CYCLE); + if (duty_cycle > max_dutycycle_) + __HAL_TIM_SET_COMPARE(pwm_timer_, pwm_channel_, max_dutycycle_); else __HAL_TIM_SET_COMPARE(pwm_timer_, pwm_channel_, duty_cycle); @@ -44,8 +45,8 @@ class MotorController { HAL_GPIO_WritePin(dir_gpio_port_, dir_pin_, GPIO_PIN_RESET); //check if duty_cycle is lower than minimum - if (duty_cycle < -MAX_DUTY_CYCLE) - __HAL_TIM_SET_COMPARE(pwm_timer_, pwm_channel_, MAX_DUTY_CYCLE); + if (duty_cycle < -max_dutycycle_) + __HAL_TIM_SET_COMPARE(pwm_timer_, pwm_channel_, max_dutycycle_); else //invert sign to make duty_cycle positive __HAL_TIM_SET_COMPARE(pwm_timer_, pwm_channel_, -duty_cycle); diff --git a/otto_controller/Core/Inc/odometry.h b/otto_controller/Core/Inc/odometry.h index 56e0e8b..85f24f7 100644 --- a/otto_controller/Core/Inc/odometry.h +++ b/otto_controller/Core/Inc/odometry.h @@ -1,30 +1,35 @@ #ifndef ODOMETRY_H #define ODOMETRY_H -#include "constants.h" - class Odometry { private: float left_velocity_; float right_velocity_; - + float baseline_; public: Odometry() { left_velocity_ = 0; right_velocity_ = 0; + baseline_ = 0; + } + + Odometry(float baseline) { + left_velocity_ = 0; + right_velocity_ = 0; + baseline_ = baseline; } void UpdateValues(float linear_vel, float angular_vel) { - left_velocity_ = linear_vel - (BASELINE * angular_vel)/2; - right_velocity_ = linear_vel + (BASELINE * angular_vel)/2; + left_velocity_ = linear_vel - (baseline_ * angular_vel) / 2; + right_velocity_ = linear_vel + (baseline_ * angular_vel) / 2; } - float GetLeftVelocity(){ + float GetLeftVelocity() { return left_velocity_; } - float GetRightVelocity(){ + float GetRightVelocity() { return right_velocity_; } diff --git a/otto_controller/Core/Inc/pid.h b/otto_controller/Core/Inc/pid.h index 7663f6b..b351abe 100644 --- a/otto_controller/Core/Inc/pid.h +++ b/otto_controller/Core/Inc/pid.h @@ -1,8 +1,6 @@ #ifndef PID_H #define PID_H -#include "constants.h" - class Pid { public: //PID constants @@ -33,9 +31,6 @@ class Pid { this->previous_error_ = 0; this->error_sum_ = 0; - this->min_ = -MAX_DUTY_CYCLE; - this->max_ = MAX_DUTY_CYCLE; - } void config(float kp, float ki, float kd) { diff --git a/otto_controller/Core/Src/main.cpp b/otto_controller/Core/Src/main.cpp index 3dcab1c..e27ecf7 100644 --- a/otto_controller/Core/Src/main.cpp +++ b/otto_controller/Core/Src/main.cpp @@ -52,10 +52,18 @@ /* USER CODE BEGIN PV */ +//Parameters +float baseline = 0.3; +int ticks_per_revolution = 148000; //x4 resolution +float right_wheel_circumference = 0.783; //in meters +float left_wheel_circumference = 0.789; //in meters + //Odometry -Encoder right_encoder = Encoder(&htim5, RIGHT_WHEEL_CIRCUMFERENCE); -Encoder left_encoder = Encoder(&htim2, LEFT_WHEEL_CIRCUMFERENCE); -Odometry odom = Odometry(); +Encoder right_encoder = Encoder(&htim5, right_wheel_circumference, + ticks_per_revolution); +Encoder left_encoder = Encoder(&htim2, left_wheel_circumference, + ticks_per_revolution); +Odometry odom = Odometry(baseline); float left_velocity; float right_velocity; @@ -107,14 +115,12 @@ static void MX_NVIC_Init(void); /* USER CODE END 0 */ /** - * @brief The application entry point. - * @retval int - */ -int main(void) -{ + * @brief The application entry point. + * @retval int + */ +int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ - /* MCU Configuration--------------------------------------------------------*/ @@ -173,56 +179,51 @@ int main(void) } /** - * @brief System Clock Configuration - * @retval None - */ -void SystemClock_Config(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; - RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; - RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) { + RCC_OscInitTypeDef RCC_OscInitStruct = { 0 }; + RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 }; + RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = { 0 }; /** Configure the main internal regulator output voltage - */ + */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3); /** Initializes the CPU, AHB and APB busses clocks - */ + */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK + | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) - { + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART6; PeriphClkInitStruct.Usart6ClockSelection = RCC_USART6CLKSOURCE_PCLK2; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) - { + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { Error_Handler(); } } /** - * @brief NVIC Configuration. - * @retval None - */ -static void MX_NVIC_Init(void) -{ + * @brief NVIC Configuration. + * @retval None + */ +static void MX_NVIC_Init(void) { /* TIM3_IRQn interrupt configuration */ HAL_NVIC_SetPriority(TIM3_IRQn, 2, 1); HAL_NVIC_EnableIRQ(TIM3_IRQn); @@ -301,11 +302,10 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { /* USER CODE END 4 */ /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ -void Error_Handler(void) -{ + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ -- 2.52.0