diff --git a/Core/Inc/stm32f4xx_it.h b/Core/Inc/stm32f4xx_it.h index 0fb5082..7199d72 100644 --- a/Core/Inc/stm32f4xx_it.h +++ b/Core/Inc/stm32f4xx_it.h @@ -57,6 +57,7 @@ void PendSV_Handler(void); void SysTick_Handler(void); void TIM1_BRK_TIM9_IRQHandler(void); void TIM4_IRQHandler(void); +void TIM8_BRK_TIM12_IRQHandler(void); void TIM5_IRQHandler(void); /* USER CODE BEGIN EFP */ diff --git a/Core/Inc/tim.h b/Core/Inc/tim.h index 9c79f98..30162f3 100644 --- a/Core/Inc/tim.h +++ b/Core/Inc/tim.h @@ -44,6 +44,8 @@ extern TIM_HandleTypeDef htim5; extern TIM_HandleTypeDef htim9; +extern TIM_HandleTypeDef htim12; + /* USER CODE BEGIN Private defines */ /* USER CODE END Private defines */ @@ -54,6 +56,7 @@ void MX_TIM3_Init(void); void MX_TIM4_Init(void); void MX_TIM5_Init(void); void MX_TIM9_Init(void); +void MX_TIM12_Init(void); void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 7159c5a..71ae669 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -69,6 +69,7 @@ void initInterrupt(void) { HAL_TIM_Base_Start_IT(&htim4); // 割り込み処理開始 HAL_TIM_Base_Start_IT(&htim5); // 割り込み処理開始 HAL_TIM_Base_Start_IT(&htim9); // 割り込み処理開始 + HAL_TIM_Base_Start_IT(&htim12); // 割り込み処理開始 initEncoder(&encoder_l, &htim2, MotorParam::PULSE_PER_TIRE_ONEROTATION, true); initEncoder(&encoder_r, &htim3, MotorParam::PULSE_PER_TIRE_ONEROTATION, false); } @@ -120,6 +121,7 @@ int main(void) MX_ADC2_Init(); MX_TIM5_Init(); MX_TIM9_Init(); + MX_TIM12_Init(); /* USER CODE BEGIN 2 */ setbuf(stdout, NULL); // std::outのバッファリングを無効にし、ログを即出力する gyroInit(&gyro); @@ -133,6 +135,7 @@ int main(void) ledBlink.toggle(); robot_controller.mainControl(); HAL_Delay(MotorParam::RATE); + // printf("encoder_l: %lf, encoder_r: %lf\n\r", encoder_l.rotation_speed, encoder_r.rotation_speed); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c index 78fbce7..1761e86 100644 --- a/Core/Src/stm32f4xx_it.c +++ b/Core/Src/stm32f4xx_it.c @@ -26,6 +26,7 @@ #include "gyro.h" extern void pwmControl(); +extern void updateTargetVelocity(); extern void updateModeManager(); extern void updateLog(); /* USER CODE END Includes */ @@ -67,6 +68,7 @@ extern TIM_HandleTypeDef htim1; extern TIM_HandleTypeDef htim4; extern TIM_HandleTypeDef htim5; extern TIM_HandleTypeDef htim9; +extern TIM_HandleTypeDef htim12; /* USER CODE BEGIN EV */ /* USER CODE END EV */ @@ -219,9 +221,9 @@ void TIM1_BRK_TIM9_IRQHandler(void) /* USER CODE END TIM1_BRK_TIM9_IRQn 0 */ HAL_TIM_IRQHandler(&htim1); HAL_TIM_IRQHandler(&htim9); + /* USER CODE BEGIN TIM1_BRK_TIM9_IRQn 1 */ updateModeManager(); updateLog(); - /* USER CODE BEGIN TIM1_BRK_TIM9_IRQn 1 */ /* USER CODE END TIM1_BRK_TIM9_IRQn 1 */ } @@ -246,6 +248,21 @@ void TIM4_IRQHandler(void) /* USER CODE END TIM4_IRQn 1 */ } +/** + * @brief This function handles TIM8 break interrupt and TIM12 global interrupt. + */ +void TIM8_BRK_TIM12_IRQHandler(void) +{ + /* USER CODE BEGIN TIM8_BRK_TIM12_IRQn 0 */ + updateTargetVelocity(); + + /* USER CODE END TIM8_BRK_TIM12_IRQn 0 */ + HAL_TIM_IRQHandler(&htim12); + /* USER CODE BEGIN TIM8_BRK_TIM12_IRQn 1 */ + + /* USER CODE END TIM8_BRK_TIM12_IRQn 1 */ +} + /** * @brief This function handles TIM5 global interrupt. */ diff --git a/Core/Src/tim.c b/Core/Src/tim.c index 0ffebc3..7f2bf41 100644 --- a/Core/Src/tim.c +++ b/Core/Src/tim.c @@ -30,6 +30,7 @@ TIM_HandleTypeDef htim3; TIM_HandleTypeDef htim4; TIM_HandleTypeDef htim5; TIM_HandleTypeDef htim9; +TIM_HandleTypeDef htim12; /* TIM1 init function */ void MX_TIM1_Init(void) @@ -305,6 +306,39 @@ void MX_TIM9_Init(void) /* USER CODE END TIM9_Init 2 */ +} +/* TIM12 init function */ +void MX_TIM12_Init(void) +{ + + /* USER CODE BEGIN TIM12_Init 0 */ + + /* USER CODE END TIM12_Init 0 */ + + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + + /* USER CODE BEGIN TIM12_Init 1 */ + + /* USER CODE END TIM12_Init 1 */ + htim12.Instance = TIM12; + htim12.Init.Prescaler = 84-1; + htim12.Init.CounterMode = TIM_COUNTERMODE_UP; + htim12.Init.Period = 1000-1; + htim12.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim12.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim12) != HAL_OK) + { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim12, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM12_Init 2 */ + + /* USER CODE END TIM12_Init 2 */ + } void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) @@ -370,6 +404,21 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) /* USER CODE END TIM9_MspInit 1 */ } + else if(tim_baseHandle->Instance==TIM12) + { + /* USER CODE BEGIN TIM12_MspInit 0 */ + + /* USER CODE END TIM12_MspInit 0 */ + /* TIM12 clock enable */ + __HAL_RCC_TIM12_CLK_ENABLE(); + + /* TIM12 interrupt Init */ + HAL_NVIC_SetPriority(TIM8_BRK_TIM12_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(TIM8_BRK_TIM12_IRQn); + /* USER CODE BEGIN TIM12_MspInit 1 */ + + /* USER CODE END TIM12_MspInit 1 */ + } } void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* tim_encoderHandle) @@ -527,6 +576,20 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) /* USER CODE END TIM9_MspDeInit 1 */ } + else if(tim_baseHandle->Instance==TIM12) + { + /* USER CODE BEGIN TIM12_MspDeInit 0 */ + + /* USER CODE END TIM12_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM12_CLK_DISABLE(); + + /* TIM12 interrupt Deinit */ + HAL_NVIC_DisableIRQ(TIM8_BRK_TIM12_IRQn); + /* USER CODE BEGIN TIM12_MspDeInit 1 */ + + /* USER CODE END TIM12_MspDeInit 1 */ + } } void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef* tim_encoderHandle)