Compare commits
3 Commits
abbd7981a9
...
main
Author | SHA1 | Date | |
---|---|---|---|
bbf07cc233 | |||
95b0d059ce | |||
aa36b579a4 |
19
.vscode/iar-vsc.json
vendored
Normal file
19
.vscode/iar-vsc.json
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"workspace": {
|
||||
"path": "${workspaceFolder}\\Robot_balancer\\Robot_balancer.eww"
|
||||
},
|
||||
"workspaces": {
|
||||
"${workspaceFolder}\\Robot_balancer\\Robot_balancer.eww": {
|
||||
"configs": {
|
||||
"${workspaceFolder}\\Robot_balancer\\ACAR\\ACAR.ewp": "Debug",
|
||||
"${workspaceFolder}\\Robot_balancer\\PID\\PID.ewp": "Debug"
|
||||
},
|
||||
"selected": {
|
||||
"path": "${workspaceFolder}\\Robot_balancer\\PID\\PID.ewp"
|
||||
}
|
||||
}
|
||||
},
|
||||
"workbench": {
|
||||
"path": "C:\\iar\\ewarm-9.60.3"
|
||||
}
|
||||
}
|
@ -1,105 +1,105 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file system_stm32g4xx.c
|
||||
* @author MCD Application Team
|
||||
* @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File
|
||||
*
|
||||
* This file provides two functions and one global variable to be called from
|
||||
* user application:
|
||||
* - SystemInit(): This function is called at startup just after reset and
|
||||
* before branch to main program. This call is made inside
|
||||
* the "startup_stm32g4xx.s" file.
|
||||
*
|
||||
* - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
|
||||
* by the user application to setup the SysTick
|
||||
* timer or configure other parameters.
|
||||
*
|
||||
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
|
||||
* be called whenever the core clock is changed
|
||||
* during program execution.
|
||||
*
|
||||
* After each device reset the HSI (16 MHz) is used as system clock source.
|
||||
* Then SystemInit() function is called, in "startup_stm32g4xx.s" file, to
|
||||
* configure the system clock before to branch to main program.
|
||||
*
|
||||
* This file configures the system clock as follows:
|
||||
*=============================================================================
|
||||
*-----------------------------------------------------------------------------
|
||||
* System Clock source | HSI
|
||||
*-----------------------------------------------------------------------------
|
||||
* SYSCLK(Hz) | 16000000
|
||||
*-----------------------------------------------------------------------------
|
||||
* HCLK(Hz) | 16000000
|
||||
*-----------------------------------------------------------------------------
|
||||
* AHB Prescaler | 1
|
||||
*-----------------------------------------------------------------------------
|
||||
* APB1 Prescaler | 1
|
||||
*-----------------------------------------------------------------------------
|
||||
* APB2 Prescaler | 1
|
||||
*-----------------------------------------------------------------------------
|
||||
* PLL_M | 1
|
||||
*-----------------------------------------------------------------------------
|
||||
* PLL_N | 16
|
||||
*-----------------------------------------------------------------------------
|
||||
* PLL_P | 7
|
||||
*-----------------------------------------------------------------------------
|
||||
* PLL_Q | 2
|
||||
*-----------------------------------------------------------------------------
|
||||
* PLL_R | 2
|
||||
*-----------------------------------------------------------------------------
|
||||
* Require 48MHz for RNG | Disabled
|
||||
*-----------------------------------------------------------------------------
|
||||
*=============================================================================
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2019 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
******************************************************************************
|
||||
* @file system_stm32g4xx.c
|
||||
* @author MCD Application Team
|
||||
* @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File
|
||||
*
|
||||
* This file provides two functions and one global variable to be called from
|
||||
* user application:
|
||||
* - SystemInit(): This function is called at startup just after reset and
|
||||
* before branch to main program. This call is made inside
|
||||
* the "startup_stm32g4xx.s" file.
|
||||
*
|
||||
* - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
|
||||
* by the user application to setup the SysTick
|
||||
* timer or configure other parameters.
|
||||
*
|
||||
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
|
||||
* be called whenever the core clock is changed
|
||||
* during program execution.
|
||||
*
|
||||
* After each device reset the HSI (16 MHz) is used as system clock source.
|
||||
* Then SystemInit() function is called, in "startup_stm32g4xx.s" file, to
|
||||
* configure the system clock before to branch to main program.
|
||||
*
|
||||
* This file configures the system clock as follows:
|
||||
*=============================================================================
|
||||
*-----------------------------------------------------------------------------
|
||||
* System Clock source | HSI
|
||||
*-----------------------------------------------------------------------------
|
||||
* SYSCLK(Hz) | 16000000
|
||||
*-----------------------------------------------------------------------------
|
||||
* HCLK(Hz) | 16000000
|
||||
*-----------------------------------------------------------------------------
|
||||
* AHB Prescaler | 1
|
||||
*-----------------------------------------------------------------------------
|
||||
* APB1 Prescaler | 1
|
||||
*-----------------------------------------------------------------------------
|
||||
* APB2 Prescaler | 1
|
||||
*-----------------------------------------------------------------------------
|
||||
* PLL_M | 1
|
||||
*-----------------------------------------------------------------------------
|
||||
* PLL_N | 16
|
||||
*-----------------------------------------------------------------------------
|
||||
* PLL_P | 7
|
||||
*-----------------------------------------------------------------------------
|
||||
* PLL_Q | 2
|
||||
*-----------------------------------------------------------------------------
|
||||
* PLL_R | 2
|
||||
*-----------------------------------------------------------------------------
|
||||
* Require 48MHz for RNG | Disabled
|
||||
*-----------------------------------------------------------------------------
|
||||
*=============================================================================
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2019 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/** @addtogroup CMSIS
|
||||
* @{
|
||||
*/
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup stm32g4xx_system
|
||||
* @{
|
||||
*/
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32G4xx_System_Private_Includes
|
||||
* @{
|
||||
*/
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include "stm32g4xx.h"
|
||||
|
||||
#if !defined (HSE_VALUE)
|
||||
#define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */
|
||||
#endif /* HSE_VALUE */
|
||||
#if !defined(HSE_VALUE)
|
||||
#define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */
|
||||
#endif /* HSE_VALUE */
|
||||
|
||||
#if !defined (HSI_VALUE)
|
||||
#define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/
|
||||
#endif /* HSI_VALUE */
|
||||
#if !defined(HSI_VALUE)
|
||||
#define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/
|
||||
#endif /* HSI_VALUE */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32G4xx_System_Private_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32G4xx_System_Private_Defines
|
||||
* @{
|
||||
*/
|
||||
* @{
|
||||
*/
|
||||
|
||||
/************************* Miscellaneous Configuration ************************/
|
||||
/* Note: Following vector table addresses must be defined in line with linker
|
||||
@ -114,117 +114,160 @@
|
||||
in Sram else user remap will be done in Flash. */
|
||||
/* #define VECT_TAB_SRAM */
|
||||
#if defined(VECT_TAB_SRAM)
|
||||
#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field. \
|
||||
This value must be a multiple of 0x200. */
|
||||
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. \
|
||||
This value must be a multiple of 0x200. */
|
||||
#else
|
||||
#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
|
||||
This value must be a multiple of 0x200. */
|
||||
#endif /* VECT_TAB_SRAM */
|
||||
#endif /* USER_VECT_TAB_ADDRESS */
|
||||
/******************************************************************************/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field. \
|
||||
This value must be a multiple of 0x200. */
|
||||
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. \
|
||||
This value must be a multiple of 0x200. */
|
||||
#endif /* VECT_TAB_SRAM */
|
||||
#endif /* USER_VECT_TAB_ADDRESS */
|
||||
/******************************************************************************/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32G4xx_System_Private_Macros
|
||||
* @{
|
||||
*/
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32G4xx_System_Private_Variables
|
||||
* @{
|
||||
*/
|
||||
/* The SystemCoreClock variable is updated in three ways:
|
||||
1) by calling CMSIS function SystemCoreClockUpdate()
|
||||
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
|
||||
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
|
||||
Note: If you use this function to configure the system clock; then there
|
||||
is no need to call the 2 first functions listed above, since SystemCoreClock
|
||||
variable is updated automatically.
|
||||
*/
|
||||
uint32_t SystemCoreClock = HSI_VALUE;
|
||||
* @{
|
||||
*/
|
||||
/* The SystemCoreClock variable is updated in three ways:
|
||||
1) by calling CMSIS function SystemCoreClockUpdate()
|
||||
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
|
||||
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
|
||||
Note: If you use this function to configure the system clock; then there
|
||||
is no need to call the 2 first functions listed above, since SystemCoreClock
|
||||
variable is updated automatically.
|
||||
*/
|
||||
uint32_t SystemCoreClock = HSI_VALUE;
|
||||
|
||||
const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
|
||||
const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
|
||||
const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
|
||||
const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32G4xx_System_Private_FunctionPrototypes
|
||||
* @{
|
||||
*/
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32G4xx_System_Private_Functions
|
||||
* @{
|
||||
*/
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Setup the microcontroller system.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
* @brief Setup the microcontroller system.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
void SystemClockInit()
|
||||
{
|
||||
// 1. Включаем HSE
|
||||
RCC->CR |= RCC_CR_HSEON;
|
||||
while (!(RCC->CR & RCC_CR_HSERDY))
|
||||
;
|
||||
|
||||
// 2. Отключаем PLL
|
||||
RCC->CR &= ~RCC_CR_PLLON;
|
||||
while (RCC->CR & RCC_CR_PLLRDY)
|
||||
;
|
||||
|
||||
// 3. Устанавливаем источник PLL — HSE
|
||||
RCC->PLLCFGR &= ~RCC_PLLCFGR_PLLSRC;
|
||||
RCC->PLLCFGR |= RCC_PLLCFGR_PLLSRC_HSE;
|
||||
|
||||
// 4. Настраиваем PLLM, PLLN, PLLR
|
||||
RCC->PLLCFGR =
|
||||
(1U << RCC_PLLCFGR_PLLM_Pos) | // PLLM = 2 (код 1)
|
||||
(85U << RCC_PLLCFGR_PLLN_Pos) | // PLLN = 85
|
||||
(0U << RCC_PLLCFGR_PLLR_Pos) | // PLLR = 2 (код 0)
|
||||
RCC_PLLCFGR_PLLREN | // Включаем PLLR
|
||||
RCC_PLLCFGR_PLLSRC_HSE; // Источник HSE
|
||||
|
||||
// 5. Устанавливаем FLASH задержку (4 такта для 170 МГц)
|
||||
FLASH->ACR |= FLASH_ACR_LATENCY_4WS;
|
||||
|
||||
// 6. Включаем PLL
|
||||
RCC->CR |= RCC_CR_PLLON;
|
||||
while (!(RCC->CR & RCC_CR_PLLRDY))
|
||||
;
|
||||
|
||||
// 7. Переключаем системную частоту на PLL
|
||||
RCC->CFGR &= ~RCC_CFGR_SW;
|
||||
RCC->CFGR |= RCC_CFGR_SW_PLL;
|
||||
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL)
|
||||
;
|
||||
|
||||
// 8. Обновляем переменную SystemCoreClock
|
||||
SystemCoreClockUpdate();
|
||||
}
|
||||
|
||||
void SystemInit(void)
|
||||
{
|
||||
/* FPU settings ------------------------------------------------------------*/
|
||||
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
|
||||
SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */
|
||||
#endif
|
||||
SystemClockInit();
|
||||
/* FPU settings ------------------------------------------------------------*/
|
||||
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
|
||||
SCB->CPACR |= ((3UL << (10 * 2)) | (3UL << (11 * 2))); /* set CP10 and CP11 Full Access */
|
||||
#endif
|
||||
|
||||
/* Configure the Vector Table location add offset address ------------------*/
|
||||
#if defined(USER_VECT_TAB_ADDRESS)
|
||||
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
|
||||
#endif /* USER_VECT_TAB_ADDRESS */
|
||||
#endif /* USER_VECT_TAB_ADDRESS */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update SystemCoreClock variable according to Clock Register Values.
|
||||
* The SystemCoreClock variable contains the core clock (HCLK), it can
|
||||
* be used by the user application to setup the SysTick timer or configure
|
||||
* other parameters.
|
||||
*
|
||||
* @note Each time the core clock (HCLK) changes, this function must be called
|
||||
* to update SystemCoreClock variable value. Otherwise, any configuration
|
||||
* based on this variable will be incorrect.
|
||||
*
|
||||
* @note - The system frequency computed by this function is not the real
|
||||
* frequency in the chip. It is calculated based on the predefined
|
||||
* constant and the selected clock source:
|
||||
*
|
||||
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
|
||||
*
|
||||
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
|
||||
*
|
||||
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
|
||||
* or HSI_VALUE(*) multiplied/divided by the PLL factors.
|
||||
*
|
||||
* (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value
|
||||
* 16 MHz) but the real value may vary depending on the variations
|
||||
* in voltage and temperature.
|
||||
*
|
||||
* (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value
|
||||
* 24 MHz), user has to ensure that HSE_VALUE is same as the real
|
||||
* frequency of the crystal used. Otherwise, this function may
|
||||
* have wrong result.
|
||||
*
|
||||
* - The result of this function could be not correct when using fractional
|
||||
* value for HSE crystal.
|
||||
*
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
* @brief Update SystemCoreClock variable according to Clock Register Values.
|
||||
* The SystemCoreClock variable contains the core clock (HCLK), it can
|
||||
* be used by the user application to setup the SysTick timer or configure
|
||||
* other parameters.
|
||||
*
|
||||
* @note Each time the core clock (HCLK) changes, this function must be called
|
||||
* to update SystemCoreClock variable value. Otherwise, any configuration
|
||||
* based on this variable will be incorrect.
|
||||
*
|
||||
* @note - The system frequency computed by this function is not the real
|
||||
* frequency in the chip. It is calculated based on the predefined
|
||||
* constant and the selected clock source:
|
||||
*
|
||||
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
|
||||
*
|
||||
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
|
||||
*
|
||||
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
|
||||
* or HSI_VALUE(*) multiplied/divided by the PLL factors.
|
||||
*
|
||||
* (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value
|
||||
* 16 MHz) but the real value may vary depending on the variations
|
||||
* in voltage and temperature.
|
||||
*
|
||||
* (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value
|
||||
* 24 MHz), user has to ensure that HSE_VALUE is same as the real
|
||||
* frequency of the crystal used. Otherwise, this function may
|
||||
* have wrong result.
|
||||
*
|
||||
* - The result of this function could be not correct when using fractional
|
||||
* value for HSE crystal.
|
||||
*
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SystemCoreClockUpdate(void)
|
||||
{
|
||||
uint32_t tmp, pllvco, pllr, pllsource, pllm;
|
||||
@ -232,35 +275,35 @@ void SystemCoreClockUpdate(void)
|
||||
/* Get SYSCLK source -------------------------------------------------------*/
|
||||
switch (RCC->CFGR & RCC_CFGR_SWS)
|
||||
{
|
||||
case 0x04: /* HSI used as system clock source */
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
case 0x04: /* HSI used as system clock source */
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
|
||||
case 0x08: /* HSE used as system clock source */
|
||||
SystemCoreClock = HSE_VALUE;
|
||||
break;
|
||||
case 0x08: /* HSE used as system clock source */
|
||||
SystemCoreClock = HSE_VALUE;
|
||||
break;
|
||||
|
||||
case 0x0C: /* PLL used as system clock source */
|
||||
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
|
||||
SYSCLK = PLL_VCO / PLLR
|
||||
*/
|
||||
pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
|
||||
pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U ;
|
||||
if (pllsource == 0x02UL) /* HSI used as PLL clock source */
|
||||
{
|
||||
pllvco = (HSI_VALUE / pllm);
|
||||
}
|
||||
else /* HSE used as PLL clock source */
|
||||
{
|
||||
pllvco = (HSE_VALUE / pllm);
|
||||
}
|
||||
pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8);
|
||||
pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U;
|
||||
SystemCoreClock = pllvco/pllr;
|
||||
break;
|
||||
case 0x0C: /* PLL used as system clock source */
|
||||
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
|
||||
SYSCLK = PLL_VCO / PLLR
|
||||
*/
|
||||
pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
|
||||
pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U;
|
||||
if (pllsource == 0x02UL) /* HSI used as PLL clock source */
|
||||
{
|
||||
pllvco = (HSI_VALUE / pllm);
|
||||
}
|
||||
else /* HSE used as PLL clock source */
|
||||
{
|
||||
pllvco = (HSE_VALUE / pllm);
|
||||
}
|
||||
pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8);
|
||||
pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U;
|
||||
SystemCoreClock = pllvco / pllr;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
/* Compute HCLK clock frequency --------------------------------------------*/
|
||||
/* Get HCLK prescaler */
|
||||
@ -269,17 +312,14 @@ void SystemCoreClockUpdate(void)
|
||||
SystemCoreClock >>= tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
* @}
|
||||
*/
|
||||
|
@ -32,7 +32,7 @@ extern volatile int16_t gyro_x, gyro_y, gyro_z;
|
||||
// Коэффициенты фильтр
|
||||
#define GYRO_SCALE 65.5f // Для +-500: 32768/500
|
||||
#define RAD_TO_DEG 57.29578f // Преобразование радиан в градусы
|
||||
#define DT 0.001f // Период дискретизации (10 мс)
|
||||
#define DT 0.0001f // Период дискретизации (100 мкс)
|
||||
|
||||
// Настраиваемые коэффициенты фильтрации
|
||||
extern volatile float accel_filter_coeff; // Коэффициент ФНЧ для акселерометра (0.1-0.5)
|
||||
|
71
Robot_balancer/PID/Drivers/StepperMotorDriver.c
Normal file
71
Robot_balancer/PID/Drivers/StepperMotorDriver.c
Normal file
@ -0,0 +1,71 @@
|
||||
#include <StepperMotorDriver.h>
|
||||
|
||||
int16_t speedStepper1 = 0;
|
||||
int16_t setSpeed1 = 200;
|
||||
int16_t speedStepper2 = 0;
|
||||
int16_t setSpeed2 = 200;
|
||||
|
||||
#define minSpeed 10.0f
|
||||
#define maxSpeed 400.0f
|
||||
|
||||
void SetStepper1RotateSpeed(int16_t* speedStepper1)
|
||||
{
|
||||
if (abs(*speedStepper1) <= minSpeed | abs(*speedStepper1) >= maxSpeed)
|
||||
{
|
||||
TIM2->CCR1 = 0;
|
||||
TIM2->EGR |= TIM_EGR_UG;
|
||||
return;
|
||||
}
|
||||
// Управление направлением
|
||||
if (*speedStepper1 > 0)
|
||||
GPIOA->BSRR = GPIO_BSRR_BR1; // DIR = 0 (LOW)
|
||||
else
|
||||
GPIOA->BSRR = GPIO_BSRR_BS1; // DIR = 1 (HIGH)
|
||||
|
||||
uint32_t absSpeed = (*speedStepper1 > 0) ? *speedStepper1 : -(*speedStepper1);
|
||||
|
||||
uint32_t F_set = (N_FULL_STEP * MICROSTEPPING * absSpeed) / 60;
|
||||
if (F_set == 0) return;
|
||||
|
||||
uint32_t arr_set = F_CLK / (PWM_PSC * F_set);
|
||||
uint32_t ccr_set = (uint32_t)(arr_set * Duty);
|
||||
|
||||
if (arr_set < 10) arr_set = 10;
|
||||
if (ccr_set < 1) ccr_set = 1;
|
||||
|
||||
TIM2->ARR = arr_set - 1;
|
||||
TIM2->CCR1 = ccr_set;
|
||||
|
||||
TIM2->EGR |= TIM_EGR_UG;
|
||||
}
|
||||
//---------------------------------------------------------------------------------------------------------------
|
||||
void SetStepper2RotateSpeed(int16_t* speedStepper2)
|
||||
{
|
||||
if (abs(*speedStepper2) <= minSpeed & abs(*speedStepper2) >= maxSpeed)
|
||||
{
|
||||
TIM2->CCR1 = 0;
|
||||
TIM2->EGR |= TIM_EGR_UG;
|
||||
return;
|
||||
}
|
||||
// Управление направлением
|
||||
if (*speedStepper2 > 0)
|
||||
GPIOB->BSRR = GPIO_BSRR_BR1; // DIR = 0 (LOW)
|
||||
else
|
||||
GPIOB->BSRR = GPIO_BSRR_BS1; // DIR = 1 (HIGH)
|
||||
|
||||
uint32_t absSpeed = (*speedStepper2 > 0) ? *speedStepper2 : -(*speedStepper2);
|
||||
|
||||
uint32_t F_set = (N_FULL_STEP * MICROSTEPPING * absSpeed) / 60;
|
||||
if (F_set == 0) return;
|
||||
|
||||
uint32_t arr_set = F_CLK / (PWM_PSC * F_set);
|
||||
uint32_t ccr_set = (uint32_t)(arr_set * Duty);
|
||||
|
||||
if (arr_set < 10) arr_set = 10;
|
||||
if (ccr_set < 1) ccr_set = 1;
|
||||
|
||||
TIM3->ARR = arr_set - 1;
|
||||
TIM3->CCR3 = ccr_set;
|
||||
|
||||
TIM3->EGR |= TIM_EGR_UG;
|
||||
}
|
16
Robot_balancer/PID/Drivers/StepperMotorDriver.h
Normal file
16
Robot_balancer/PID/Drivers/StepperMotorDriver.h
Normal file
@ -0,0 +1,16 @@
|
||||
#include "stm32g431xx.h"
|
||||
#include <math.h>
|
||||
|
||||
#define N_FULL_STEP 200
|
||||
#define MICROSTEPPING 8
|
||||
#define F_CLK 170000000
|
||||
#define PWM_PSC 17
|
||||
#define Duty 0.05f
|
||||
|
||||
extern int16_t speedStepper1;
|
||||
extern int16_t setSpeed1;
|
||||
extern int16_t speedStepper2;
|
||||
extern int16_t setSpeed2;
|
||||
|
||||
void SetStepper1RotateSpeed(int16_t* speedStepper1);
|
||||
void SetStepper2RotateSpeed(int16_t* speedStepper2);
|
@ -2233,6 +2233,12 @@
|
||||
<file>
|
||||
<name>$PROJ_DIR$\Drivers\LSM6DS3.h</name>
|
||||
</file>
|
||||
<file>
|
||||
<name>$PROJ_DIR$\Drivers\StepperMotorDriver.c</name>
|
||||
</file>
|
||||
<file>
|
||||
<name>$PROJ_DIR$\Drivers\StepperMotorDriver.h</name>
|
||||
</file>
|
||||
</group>
|
||||
<group>
|
||||
<name>Utils</name>
|
||||
|
@ -2959,6 +2959,12 @@
|
||||
<file>
|
||||
<name>$PROJ_DIR$\Drivers\LSM6DS3.h</name>
|
||||
</file>
|
||||
<file>
|
||||
<name>$PROJ_DIR$\Drivers\StepperMotorDriver.c</name>
|
||||
</file>
|
||||
<file>
|
||||
<name>$PROJ_DIR$\Drivers\StepperMotorDriver.h</name>
|
||||
</file>
|
||||
</group>
|
||||
<group>
|
||||
<name>Utils</name>
|
||||
|
@ -1,8 +1,8 @@
|
||||
#include "PID.h"
|
||||
|
||||
float KP = 100;
|
||||
float KI = 1;
|
||||
float KD = 1;
|
||||
float KI = 0;
|
||||
float KD = 10;
|
||||
float integral = 0;
|
||||
float lastError = 0;
|
||||
float limit = 1000; //ограничение интегральной составляющей
|
||||
@ -41,3 +41,15 @@ float pid_update(float target, float current, float dt)
|
||||
// float output = pPart + iPart + dPart;
|
||||
// return output; //Управляющее воздействие/крутящий момент
|
||||
// }
|
||||
|
||||
#define Mc 0.21f //момент сцепления
|
||||
#define J 820.0f //инерция ротора
|
||||
#define dt 0.0001f
|
||||
|
||||
float integral_perevod = 0;
|
||||
|
||||
float perevod (float M){
|
||||
integral_perevod += (M - Mc)*dt;
|
||||
int omega = (int)(1.0f/J*integral_perevod);
|
||||
return omega * 60.0f / 9.5493f;
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
#include <math.h>
|
||||
#include "StepperMotorDriver.h"
|
||||
float pid_update(float target, float current, float dt);
|
||||
float perevod(float pid_result);
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <math.h>
|
||||
#include "LSM6DS3.h"
|
||||
#include "PID.h"
|
||||
#include <StepperMotorDriver.h>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
@ -9,18 +10,16 @@
|
||||
// PA8 - I2C3_SCL
|
||||
//
|
||||
//---------------------------------------------------------------------------------------------------------------
|
||||
//---------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------
|
||||
//---------------------------------------------------------------------------------------------------------------
|
||||
volatile uint32_t counter = 0;
|
||||
float res;
|
||||
int res;
|
||||
float u;
|
||||
extern "C" void TIM2_IRQHandler(void)
|
||||
extern "C" void TIM4_IRQHandler(void)
|
||||
{
|
||||
if (TIM2->SR & TIM_SR_UIF) // Проверить флаг обновления
|
||||
if (TIM4->SR & TIM_SR_UIF) // Проверить флаг обновления
|
||||
{
|
||||
TIM2->SR &= ~TIM_SR_UIF; // Сбросить флаг
|
||||
TIM4->SR &= ~TIM_SR_UIF; // Сбросить флаг
|
||||
|
||||
counter++;
|
||||
|
||||
@ -36,7 +35,7 @@ extern "C" void TIM2_IRQHandler(void)
|
||||
if (fabsf(u) < 0.001f){
|
||||
u = 0.001f;
|
||||
}
|
||||
res = (48/u);
|
||||
res = perevod(u);
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------------------------------------------
|
||||
@ -46,26 +45,32 @@ int main()
|
||||
{
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN; // тактирование порт A
|
||||
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN; // тактирование порт В
|
||||
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN; // тактирование порт C
|
||||
RCC->APB1ENR1 |= RCC_APB1ENR1_I2C3EN; // тактирование I2C3
|
||||
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM4EN; // Включить тактирование TIM4
|
||||
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM2EN; // Включить тактирование TIM2
|
||||
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM3EN; // Включить тактирование TIM3
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
GPIOC->MODER &= ~GPIO_MODER_MODE6_Msk; // очистка пина C6 - использую под LED
|
||||
GPIOC->MODER &= ~GPIO_MODER_MODE11_Msk; // очистка пина C11
|
||||
GPIOA->MODER &= ~GPIO_MODER_MODE8_Msk; // очистка пина A8
|
||||
GPIOA->MODER &= ~(GPIO_MODER_MODE0_Msk); // очистка пина PA0
|
||||
GPIOB->MODER &= ~(GPIO_MODER_MODE0_Msk); // очистка пина PB0
|
||||
GPIOA->MODER &= ~(GPIO_MODER_MODE1_Msk); // очистка пина PA1
|
||||
GPIOB->MODER &= ~(GPIO_MODER_MODE1_Msk); // очистка пина PB1
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
GPIOC->MODER |= GPIO_MODER_MODE6_0; // установка бита выхода
|
||||
GPIOA->MODER |= GPIO_MODER_MODE8_1; // установка бита режима альтернативной функции
|
||||
GPIOC->MODER |= GPIO_MODER_MODE11_1; // установка бита режима альтернативной функции
|
||||
GPIOA->MODER |= (GPIO_MODER_MODE0_1);// установка бита режима альтернативной функции
|
||||
GPIOB->MODER |= (GPIO_MODER_MODE0_1);// установка бита режима альтернативной функции
|
||||
GPIOA->MODER |= (GPIO_MODER_MODE1_0);// установка бита выхода
|
||||
GPIOB->MODER |= (GPIO_MODER_MODE1_0);// установка бита выхода
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
GPIOA->BSRR = GPIO_BSRR_BR1; // установка в низкий A1
|
||||
GPIOB->BSRR = GPIO_BSRR_BR1; // установка в низкий B1
|
||||
GPIOC->BSRR = GPIO_BSRR_BS6; // установка в высокий C6
|
||||
//------------------------------------------------------------------------- ----------------------------------
|
||||
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM2EN; // Включить тактирование TIM2
|
||||
TIM2->PSC = 16 - 1; // Предделитель 16 МГц / 16 = 1000 кГц
|
||||
TIM2->ARR = 1000 - 1; // Автоматическая перезагрузка (0.001 секунда)
|
||||
TIM2->DIER |= TIM_DIER_UIE; // Разрешить прерывание по обновлению
|
||||
TIM2->CR1 |= TIM_CR1_CEN; // Включить таймер
|
||||
NVIC_EnableIRQ(TIM2_IRQn);
|
||||
NVIC_SetPriority(TIM2_IRQn, 15); // Уровень приоритета
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
GPIOA->OTYPER &= ~GPIO_OTYPER_OT8_Msk; // Сброс режима
|
||||
GPIOC->OTYPER &= ~GPIO_OTYPER_OT11_Msk; // Сброс режима
|
||||
@ -84,18 +89,52 @@ int main()
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
GPIOA->AFR[1] &= ~GPIO_AFRH_AFSEL8_Msk; // сброс альтернативной функции
|
||||
GPIOC->AFR[1] &= ~GPIO_AFRH_AFSEL11_Msk; // сброс альтернативной функции
|
||||
GPIOA->AFR[0] &= ~(GPIO_AFRL_AFRL0); // сброс альтернативной функции
|
||||
GPIOB->AFR[0] &= ~(GPIO_AFRL_AFRL0); // сброс альтернативной функции
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
GPIOA->AFR[1] |= (2U << GPIO_AFRH_AFSEL8_Pos); // установка AF2
|
||||
GPIOC->AFR[1] |= (8U << GPIO_AFRH_AFSEL11_Pos); // Установка AF8
|
||||
GPIOA->AFR[0] |= (1 << GPIO_AFRL_AFSEL0_Pos);// установка бита режима альтернативной функции
|
||||
GPIOB->AFR[0] |= (2 << GPIO_AFRL_AFSEL0_Pos);// установка бита режима альтернативной функции
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
TIM4->PSC = 17 - 1; // Предделитель 170 МГц / 17 = 10000 кГц
|
||||
TIM4->ARR = 1000 - 1; // Автоматическая перезагрузка (0.0001 секунда)
|
||||
TIM4->DIER |= TIM_DIER_UIE; // Разрешить прерывание по обновлению
|
||||
TIM4->CR1 |= TIM_CR1_CEN; // Включить таймер
|
||||
NVIC_EnableIRQ(TIM4_IRQn); // Включение прерывания
|
||||
NVIC_SetPriority(TIM4_IRQn, 15); // Уровень приоритета
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
TIM2->PSC = 17 - 1; // Предделитель 170 МГц / 170 = 1000 кГц
|
||||
TIM2->ARR = 1875 - 1; // Автоматическая перезагрузка
|
||||
TIM2->CCR1 = 900; // Заполненность ШИМ
|
||||
TIM2->CCMR1 &= ~TIM_CCMR1_OC1M_Msk; // Очистка режима работы канала 1
|
||||
TIM2->CCMR1 |= (6 << TIM_CCMR1_OC1M_Pos); //Установка режима канала 1 в режим PWM Mode 1
|
||||
TIM2->CCMR1 |= TIM_CCMR1_OC1PE; // preload для канала 1
|
||||
TIM2->CCER |= TIM_CCER_CC1E; // Выход для канала 1
|
||||
TIM2->CR1 |= TIM_CR1_ARPE; // Автоперезагрузка и счет
|
||||
TIM2->EGR |= TIM_EGR_UG; // Обновления регистров
|
||||
TIM2->CR1 |= TIM_CR1_CEN; // Запуск таймера
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
TIM3->PSC = 17 - 1; // Предделитель 170 МГц / 170 = 1000 кГц
|
||||
TIM3->ARR = 1875 - 1; // Автоматическая перезагрузка
|
||||
TIM3->CCR3 = 900; // Заполненность ШИМ
|
||||
TIM3->CCMR2 &= ~TIM_CCMR2_OC3M_Msk; // Очистка режима работы канала 2
|
||||
TIM3->CCMR2 |= (6 << TIM_CCMR2_OC3M_Pos); //Установка режима канала 2 в режим PWM Mode 1
|
||||
TIM3->CCMR2 |= TIM_CCMR2_OC3PE; // preload для канала 2
|
||||
TIM3->CCER |= TIM_CCER_CC3E; // Выход для канала 2
|
||||
TIM3->CR1 |= TIM_CR1_ARPE; // Автоперезагрузка и счет
|
||||
TIM3->EGR |= TIM_EGR_UG; // Обновления регистров
|
||||
TIM3->CR1 |= TIM_CR1_CEN; // Запуск таймера
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
|
||||
I2C3->CR1 &= ~I2C_CR1_PE; // Отключение I2C3
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
// 400 кГц
|
||||
I2C3->TIMINGR = (0 << I2C_TIMINGR_PRESC_Pos) | // PRESC=0 (делитель 1) -> I2CCLK=16 МГц
|
||||
(3 << I2C_TIMINGR_SCLDEL_Pos) | // SCLDEL=3 -> t_HD;STA=0.25 мкс
|
||||
(1 << I2C_TIMINGR_SDADEL_Pos) | // SDADEL=1 -> t_HD;DAT=0.0625 мкс
|
||||
(15 << I2C_TIMINGR_SCLH_Pos) | // SCLH=15 -> t_HIGH=1 мкс
|
||||
(15 << I2C_TIMINGR_SCLL_Pos); // SCLL=15 -> t_LOW=1 мкс
|
||||
I2C3->TIMINGR = (7 << I2C_TIMINGR_PRESC_Pos) | // PRESC=7 -> I2CCLK=21.25 МГц
|
||||
(4 << I2C_TIMINGR_SCLDEL_Pos) | // SCLDEL=8 -> t_HD;STA=0.38 мкс
|
||||
(0 << I2C_TIMINGR_SDADEL_Pos) | // SDADEL=4 -> t_HD;DAT=0.019 мкс
|
||||
(25 << I2C_TIMINGR_SCLH_Pos) | // SCLH=16 -> t_HIGH=0.75 мкс
|
||||
(25 << I2C_TIMINGR_SCLL_Pos); // SCLL=32 -> t_LOW=1.51 мкс
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
I2C3->CR1 |= I2C_CR1_PE; // Включение I2C3
|
||||
for (volatile int i = 0; i < 100000; ++i)
|
||||
@ -104,8 +143,22 @@ int main()
|
||||
while (I2C3->ISR & I2C_ISR_BUSY)
|
||||
__NOP(); // Ждём освобождения линии I2C3
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
|
||||
LSM6DS3_Init();
|
||||
while (1)
|
||||
{
|
||||
if(setSpeed1 != speedStepper1)
|
||||
{
|
||||
speedStepper1 = setSpeed1;
|
||||
SetStepper1RotateSpeed(&speedStepper1);
|
||||
}
|
||||
|
||||
if(setSpeed2 != speedStepper2)
|
||||
{
|
||||
speedStepper2 = setSpeed2;
|
||||
SetStepper2RotateSpeed(&speedStepper2);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user