diff --git a/otto_controller/.cproject b/otto_controller/.cproject index bcf34be..d467a48 100644 --- a/otto_controller/.cproject +++ b/otto_controller/.cproject @@ -44,8 +44,8 @@ - + @@ -58,8 +58,8 @@ - + diff --git a/otto_controller/.mxproject b/otto_controller/.mxproject index b4aeebb..68ed7a1 100644 --- a/otto_controller/.mxproject +++ b/otto_controller/.mxproject @@ -20,8 +20,6 @@ SourceFiles#4=/home/fdila/Projects/otto/otto_controller/Core/Src/stm32f7xx_it.c SourceFiles#5=/home/fdila/Projects/otto/otto_controller/Core/Src/stm32f7xx_hal_msp.c HeaderFiles#6=/home/fdila/Projects/otto/otto_controller/Core/Inc/main.h SourceFiles#6=/home/fdila/Projects/otto/otto_controller/Core/Src/main.c -HeaderFiles=; -SourceFiles=; [PreviousLibFiles] LibFiles=Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_cortex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_tim.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_tim_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_rcc.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_rcc_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_flash.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_flash_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_gpio.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_gpio_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_dma.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_dma_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_pwr.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_pwr_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_def.h;Drivers/STM32F7xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_i2c.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_i2c_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_exti.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_uart.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_uart_ex.h;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_cortex.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_tim.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_tim_ex.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_rcc.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_rcc_ex.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_flash.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_flash_ex.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_gpio.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_dma.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_dma_ex.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_pwr.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_pwr_ex.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_i2c.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_i2c_ex.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_exti.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_uart.c;Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_uart_ex.c;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_cortex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_tim.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_tim_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_rcc.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_rcc_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_flash.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_flash_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_gpio.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_gpio_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_dma.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_dma_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_pwr.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_pwr_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_def.h;Drivers/STM32F7xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_i2c.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_i2c_ex.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_exti.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_uart.h;Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_uart_ex.h;Drivers/CMSIS/Device/ST/STM32F7xx/Include/stm32f767xx.h;Drivers/CMSIS/Device/ST/STM32F7xx/Include/stm32f7xx.h;Drivers/CMSIS/Device/ST/STM32F7xx/Include/system_stm32f7xx.h;Drivers/CMSIS/Device/ST/STM32F7xx/Source/Templates/system_stm32f7xx.c;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/tz_context.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/core_cm4.h; diff --git a/otto_controller/Core/Inc/main.h b/otto_controller/Core/Inc/main.h index fb0a3d8..80943f3 100644 --- a/otto_controller/Core/Inc/main.h +++ b/otto_controller/Core/Inc/main.h @@ -58,6 +58,9 @@ void Error_Handler(void); /* USER CODE END EFP */ /* Private defines -----------------------------------------------------------*/ +#define user_button_Pin GPIO_PIN_13 +#define user_button_GPIO_Port GPIOC +#define user_button_EXTI_IRQn EXTI15_10_IRQn #define current2_Pin GPIO_PIN_0 #define current2_GPIO_Port GPIOC #define encoder_dx1_Pin GPIO_PIN_0 diff --git a/otto_controller/Core/Src/gpio.c b/otto_controller/Core/Src/gpio.c index b3dac9b..7998962 100644 --- a/otto_controller/Core/Src/gpio.c +++ b/otto_controller/Core/Src/gpio.c @@ -56,11 +56,11 @@ void MX_GPIO_Init(void) /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOF, sleep2_Pin|sleep1_Pin, GPIO_PIN_SET); - /*Configure GPIO pin : PC13 */ - GPIO_InitStruct.Pin = GPIO_PIN_13; + /*Configure GPIO pin : PtPin */ + GPIO_InitStruct.Pin = user_button_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + HAL_GPIO_Init(user_button_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = current2_Pin; @@ -76,8 +76,8 @@ void MX_GPIO_Init(void) /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = fault2_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; + GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(fault2_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : PFPin PFPin */ @@ -96,8 +96,8 @@ void MX_GPIO_Init(void) /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = fault1_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; + GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(fault1_GPIO_Port, &GPIO_InitStruct); /* EXTI interrupt init*/ diff --git a/otto_controller/Core/Src/main.cpp b/otto_controller/Core/Src/main.cpp index 06216ed..9d51e08 100644 --- a/otto_controller/Core/Src/main.cpp +++ b/otto_controller/Core/Src/main.cpp @@ -129,14 +129,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--------------------------------------------------------*/ @@ -194,7 +192,7 @@ int main(void) //Enables UART RX interrupt HAL_UART_Receive_DMA(&huart6, (uint8_t*) &proto_buffer_rx, - VelocityCommand_size); + VelocityCommand_size); /* USER CODE END 2 */ @@ -209,56 +207,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); @@ -295,7 +288,8 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { //TIMER 10Hz Transmit if (htim->Instance == TIM6) { - pb_ostream_t stream = pb_ostream_from_buffer(proto_buffer_tx, sizeof(proto_buffer_tx)); + pb_ostream_t stream = pb_ostream_from_buffer(proto_buffer_tx, + sizeof(proto_buffer_tx)); float left_wheel = left_encoder.GetLinearVelocity(); float right_wheel = right_encoder.GetLinearVelocity(); @@ -313,7 +307,8 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { pb_encode(&stream, StatusMessage_fields, &status_msg); - HAL_UART_Transmit_DMA(&huart6,(uint8_t*) &proto_buffer_tx, StatusMessage_size); + HAL_UART_Transmit_DMA(&huart6, (uint8_t*) &proto_buffer_tx, + StatusMessage_size); } } @@ -330,7 +325,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) { float angular_velocity; pb_istream_t stream = pb_istream_from_buffer(proto_buffer_rx, - VelocityCommand_size); + VelocityCommand_size); bool status = pb_decode(&stream, VelocityCommand_fields, &vel_cmd); @@ -349,32 +344,82 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) { float cross_setpoint = left_setpoint - right_setpoint; cross_pid.set(cross_setpoint); - } HAL_UART_Receive_DMA(&huart6, (uint8_t*) &proto_buffer_rx, - VelocityCommand_size); + VelocityCommand_size); } void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { //Blue user button on the NUCLEO board - if (GPIO_Pin == GPIO_PIN_13) { + if (GPIO_Pin == user_button_Pin) { if (mode == 0) { mode = 1; //Enables TIM3 interrupt (used for PID control) HAL_TIM_Base_Start_IT(&htim3); - } + + } else if (GPIO_Pin == fault1_Pin) { + left_motor.brake(); + right_motor.brake(); + //stop TIM3 interrupt (used for PID control) + HAL_TIM_Base_Stop_IT(&htim3); + + //Send status message with error code + status_msg.linear_velocity = 0; + status_msg.angular_velocity = 0; + + float current_tx_millis = HAL_GetTick(); + status_msg.delta_millis = current_tx_millis - previous_tx_millis; + previous_tx_millis = current_tx_millis; + + status_msg.status = 4; + + pb_ostream_t stream = pb_ostream_from_buffer(proto_buffer_tx, + sizeof(proto_buffer_tx)); + pb_encode(&stream, StatusMessage_fields, &status_msg); + + HAL_UART_Transmit_DMA(&huart6, (uint8_t*) &proto_buffer_tx, + StatusMessage_size); + + //loops forever, manual reset is needed + while (1); + + } else if (GPIO_Pin == fault2_Pin) { + left_motor.brake(); + right_motor.brake(); + //stop TIM3 interrupt (used for PID control) + HAL_TIM_Base_Stop_IT(&htim3); + + //Send status message with error code + status_msg.linear_velocity = 0; + status_msg.angular_velocity = 0; + + float current_tx_millis = HAL_GetTick(); + status_msg.delta_millis = current_tx_millis - previous_tx_millis; + previous_tx_millis = current_tx_millis; + + status_msg.status = 5; + + pb_ostream_t stream = pb_ostream_from_buffer(proto_buffer_tx, + sizeof(proto_buffer_tx)); + pb_encode(&stream, StatusMessage_fields, &status_msg); + + HAL_UART_Transmit_DMA(&huart6, (uint8_t*) &proto_buffer_tx, + StatusMessage_size); + + //loops forever, manual reset is needed + while (1); } + } /* 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 */ diff --git a/otto_controller/otto_controller.ioc b/otto_controller/otto_controller.ioc index cf90633..2f5322d 100644 --- a/otto_controller/otto_controller.ioc +++ b/otto_controller/otto_controller.ioc @@ -105,10 +105,12 @@ PA5.GPIOParameters=GPIO_Label PA5.GPIO_Label=encoder_sx1 PA5.Locked=true PA5.Signal=S_TIM2_CH1_ETR -PA6.GPIOParameters=GPIO_Label +PA6.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI PA6.GPIO_Label=fault2 +PA6.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING +PA6.GPIO_PuPd=GPIO_PULLUP PA6.Locked=true -PA6.Signal=GPIO_Input +PA6.Signal=GPXTI6 PB3.GPIOParameters=GPIO_Label PB3.GPIO_Label=encoder_sx2 PB3.Locked=true @@ -117,6 +119,8 @@ PC0.GPIOParameters=GPIO_Label PC0.GPIO_Label=current2 PC0.Locked=true PC0.Signal=GPIO_Analog +PC13.GPIOParameters=GPIO_Label +PC13.GPIO_Label=user_button PC13.Locked=true PC13.Signal=GPXTI13 PC6.Mode=Asynchronous @@ -139,10 +143,12 @@ PD15.GPIOParameters=GPIO_Label PD15.GPIO_Label=pwm1 PD15.Locked=true PD15.Signal=S_TIM4_CH4 -PE9.GPIOParameters=GPIO_Label +PE9.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI PE9.GPIO_Label=fault1 +PE9.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING +PE9.GPIO_PuPd=GPIO_PULLUP PE9.Locked=true -PE9.Signal=GPIO_Input +PE9.Signal=GPXTI9 PF12.GPIOParameters=GPIO_Label PF12.GPIO_Label=dir2 PF12.Locked=true @@ -224,6 +230,10 @@ RCC.VCOOutputFreq_Value=192000000 RCC.VCOSAIOutputFreq_Value=192000000 SH.GPXTI13.0=GPIO_EXTI13 SH.GPXTI13.ConfNb=1 +SH.GPXTI6.0=GPIO_EXTI6 +SH.GPXTI6.ConfNb=1 +SH.GPXTI9.0=GPIO_EXTI9 +SH.GPXTI9.ConfNb=1 SH.S_TIM2_CH1_ETR.0=TIM2_CH1,Encoder_Interface SH.S_TIM2_CH1_ETR.ConfNb=1 SH.S_TIM2_CH2.0=TIM2_CH2,Encoder_Interface