/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2022 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_delay.h"
#include "bsp_key.h"
#include "bsp_motor.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim8;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM8_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM8_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
delay_init(168);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET); /* 设置方向 */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); /* 使能ENABLE */
g_motor_state = MOTOR_STOP;
uint32_t tick_start_run; /* 开始运行时刻 */
uint32_t tick;
uint8_t buffer_usart_send[16];
uint8_t i;
uint16_t sum;
uint32_t speed_step_per_second;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(KEY1_StateRead()==KEY_DOWN)
{
motor_s_shape_acceleration_init(300, 100, 10);
tick_start_run = HAL_GetTick();
}
if (KEY2_StateRead() == KEY_DOWN)
{
motor_s_shape_acceleration_init(-300, 100, 10);
tick_start_run = HAL_GetTick();
}
tick = HAL_GetTick();
if (MOTOR_STOP != g_motor_state)
{
speed_step_per_second = TIM_CLK / g_step_timer_pulse_num;
buffer_usart_send[0] = 0x01; /* 帧头 */
buffer_usart_send[1] = 0x67; /* 帧头 */
buffer_usart_send[2] = 0x42; /* 帧头 */
buffer_usart_send[3] = 0xc0; /* 帧头 */
buffer_usart_send[4] = (tick - tick_start_run) >> 8; /* 时刻 */
buffer_usart_send[5] = (tick - tick_start_run) & 0xff; /* 时刻 */
buffer_usart_send[6] = speed_step_per_second >> 24; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[7] = speed_step_per_second >> 16; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[8] = speed_step_per_second >> 8; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[9] = speed_step_per_second & 0xff; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[10] = g_steps_done >> 24; /* 电机已经运行的步数 */
buffer_usart_send[11] = g_steps_done >> 16; /* 电机已经运行的步数 */
buffer_usart_send[12] = g_steps_done >> 8; /* 电机已经运行的步数 */
buffer_usart_send[13] = g_steps_done & 0xff; /* 电机已经运行的步数 */
sum = 0;
for (i = 4; i < 14; i++)
{
sum += buffer_usart_send[i];
}
buffer_usart_send[14] = sum >> 8;
buffer_usart_send[15] = sum & 0xff;
HAL_UART_Transmit(&huart1, buffer_usart_send, 16, 1000);
//delay_ms(10);
}
else
{
tick_start_run = HAL_GetTick();
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief TIM8 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM8_Init(void)
{
/* USER CODE BEGIN TIM8_Init 0 */
/* USER CODE END TIM8_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM8_Init 1 */
/* USER CODE END TIM8_Init 1 */
htim8.Instance = TIM8;
htim8.Init.Prescaler = 20;
htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
htim8.Init.Period = 65535;
htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim8.Init.RepetitionCounter = 0;
htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim8) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim8) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OnePulse_Init(&htim8, TIM_OPMODE_SINGLE) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 65535;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
__HAL_TIM_DISABLE_OCxPRELOAD(&htim8, TIM_CHANNEL_1);
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim8, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM8_Init 2 */
__HAL_TIM_ENABLE_IT(&htim8, TIM_IT_UPDATE);
/* USER CODE END TIM8_Init 2 */
HAL_TIM_MspPostInit(&htim8);
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3|GPIO_PIN_7, GPIO_PIN_RESET);
/*Configure GPIO pins : PE2 PE3 PE4 PE0
PE1 */
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_0
|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/*Configure GPIO pins : PD3 PD7 */
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @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 */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#ifndef BSP_MOTOR_H
#define BSP_MOTOR_H
#include "main.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MOTOR_LEAD 40 /* 电机模组导程,单位0.1mm */
#define STEP_PER_CIRCLE 200 /* 细分为1时,电机走一圈需要的步数 */
#define MOTOR_DRIVER_SUBDIVISION 32 /*电机驱动器细分*/
#define TIM_CLK 8000000 /* 定时器时钟频率 */
#define TIM_ARR_MAX 65535
#define MOTOR_S_SHAPE_ACCELERATION_STEP_MAX 20480 /* 电机S曲线加速最长的步数 */
#define MOTOR_DIR_FORWARD \
do { \
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET); \
} while(0)
#define MOTOR_DIR_REVERSE \
do { \
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET); \
} while(0)
#define MOTOR_ENABLE \
do { \
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); \
} while(0)
#define MOTOR_DISABLE \
do { \
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); \
} while(0)
typedef enum
{
MOTOR_STOP = 0,
MOTOR_ACCELERATE,
MOTOR_UNIFORM,
MOTOR_DECELERATE
}motor_run_state_typedef;
typedef enum
{
MOTOR_FORWARD = 0,
MOTOR_REVERSE
}motor_dir_typedef;
extern motor_run_state_typedef g_motor_state;
extern uint32_t g_steps_done;
extern uint32_t g_steps_total;
extern uint32_t g_step_timer_pulse_num;
extern uint32_t g_step_timer_pulse_num_min;
extern uint16_t motor_s_shape_acceleration_tim_arr[MOTOR_S_SHAPE_ACCELERATION_STEP_MAX];
extern uint32_t g_motor_s_shape_acceleration_step;
uint8_t motor_s_shape_acceleration_init(int32_t distance, uint32_t speed, uint8_t accel_time);
#ifdef __cplusplus
}
#endif
#endif /* BSP_MOTOR_H */
#include "bsp_motor.h"
#include <math.h>
/**
* 电机运行状态
* 在main函数中会引用到
* 每当电机为运行状态时下位机通过串口将电机状态发送给上位机
*/
motor_run_state_typedef g_motor_state;
motor_dir_typedef motor_dir; /* 电机运行方向 */
uint32_t g_steps_total; /* 电机总共需要运行的步数 */
uint32_t g_steps_done; /* 电机已经运行的步数 */
uint32_t g_step_timer_pulse_num; /* 电机运行一步需要定时器的脉冲数 */
uint32_t g_step_timer_pulse_num_min; /* 电机运行一步需要定时器的脉冲数的最小值 */
uint16_t motor_s_shape_acceleration_tim_arr[MOTOR_S_SHAPE_ACCELERATION_STEP_MAX];
uint32_t g_motor_s_shape_acceleration_step; /* 电机总共需要运行的步数 */
/**
* @brief 电机s曲线加速运行初始化
* @param distance:总共运行的距离,单位:0.1mm,有符号数,正表示方向向前,负数表示方向向后
* @param speed:匀速运行的速度,单位:0.1mm/s
* @param accel_time:加速时间,单位:0.1s
* @note:
* @retval 0:执行成功
*/
uint8_t motor_s_shape_acceleration_init(int32_t distance, uint32_t speed, uint8_t accel_time)
{
#define INCACCEL(Vo,V,T) ( ( 2 * ((V) - (Vo)) ) / pow((T),2) ) /* 加加速度:加速度增加量 V - V0 = 1/2 * J * t^2 */
#define INCACCELSTEP(J,T) ( ( (J) * pow( (T) , 3 ) ) / 6 ) /* 加加速段的位移量(步数) S = 1/6 * J * t^3 */
uint8_t cnt = 0;
uint32_t speed_step_per_sec; /* speed对应的速度,单位:1s多少步 */
double jerk_time; /* 加加速时间,单位0.1s */
uint32_t intermediate_speed_step_per_sec; /* 中间速度,单位:1s多少步 */
double jerk = 0; /* 加加速度 */
uint32_t jerk_step; /* 加加速所需的步数 */
uint32_t deceleration_step; /* 减加速所需的步数 */
uint32_t acceleration_step; /* 加速段的步数,单位:步 */
double sum_time = 0; /* 时间累加量 */
double time_interval_cube = 0; /* 时间间隔的立方 */
double time_interval = 0; /* 时间间隔 dt */
double Vi = 0; /* 时间点t的速度 */
uint32_t time_arr;
uint32_t i = 0;
if (distance > 0)
{
motor_dir = MOTOR_FORWARD;
MOTOR_DIR_FORWARD;
}
else if (distance < 0)
{
motor_dir = MOTOR_REVERSE;
MOTOR_DIR_REVERSE;
distance = 0 - distance;
}
else /* distance == 0,参数有问题 */
{
return 1;
}
if(speed == 0) /* 不允许速度为0 */
{
return 1;
}
if(accel_time == 0) /* 不允许加速时间为0 */
{
return 1;
}
g_steps_total = distance * STEP_PER_CIRCLE * MOTOR_DRIVER_SUBDIVISION / MOTOR_LEAD; /* 将距离转换为步数 */
g_step_timer_pulse_num_min = TIM_CLK * MOTOR_LEAD * 1.0 / speed / STEP_PER_CIRCLE / MOTOR_DRIVER_SUBDIVISION; /* 将速度转换为定时器的ARR值 */
cnt = 0;
while(1)
{
speed_step_per_sec = speed * 1.0 * STEP_PER_CIRCLE * MOTOR_DRIVER_SUBDIVISION / MOTOR_LEAD;
jerk_time = accel_time * 1.0 / 10 / 2; /* 将时间单位由0.1s转换为1s */
intermediate_speed_step_per_sec = speed_step_per_sec / 2;
jerk = fabs(INCACCEL(0, intermediate_speed_step_per_sec, jerk_time)); /* 根据中点速度计算加加速度 */
jerk_step = (uint32_t)INCACCELSTEP(jerk, jerk_time); /* 加加速需要的步数 */
deceleration_step = (uint32_t)(speed_step_per_sec * jerk_time - jerk_step); /* 减加速需要的步数 */
acceleration_step = deceleration_step + jerk_step; /* 加速需要的步数 */
if (acceleration_step % 2 != 0) /* 由于浮点型数据转换成整形数据带来了误差,所以这里加1 */
{
acceleration_step += 1;
}
if(acceleration_step * 2 > g_steps_total) /* 如果加减速走的步数超过了全程步数,则按比例将速度降下来 */
{
speed_step_per_sec = g_steps_total * 1.0 / acceleration_step / 2 * speed_step_per_sec;
if(0 == speed_step_per_sec)
{
return 1;
}
else
{
intermediate_speed_step_per_sec = speed_step_per_sec / 2; /* 计算中点速度 */
jerk = fabs(INCACCEL(0, intermediate_speed_step_per_sec, jerk_time)); /* 根据中点速度计算加加速度 */
jerk_step = (uint32_t)INCACCELSTEP(jerk, jerk_time); /* 加加速需要的步数 */
deceleration_step = (uint32_t)(speed_step_per_sec * jerk_time - jerk_step); /* 减加速需要的步数 */
acceleration_step = deceleration_step + jerk_step; /* 加速需要的步数 */
if (acceleration_step % 2 != 0) /* 由于浮点型数据转换成整形数据带来了误差,所以这里加1 */
{
acceleration_step += 1;
}
}
}
if(++cnt >= 2)
{
return 1;
}
else
{
if(acceleration_step > MOTOR_S_SHAPE_ACCELERATION_STEP_MAX) /* 如果加速曲线的步数超过缓存的最大值,则将速度按比例降下来 */
{
speed = MOTOR_S_SHAPE_ACCELERATION_STEP_MAX * 1.0 / acceleration_step * speed;
}
else
{
break;
}
}
}
sum_time = 0;
time_interval_cube = 6.0f * 1.0f / jerk; /* 根据位移和时间的公式S = 1/6 * J * Ti^3 第1步的时间:Ti^3 = 6 * 1 / Jerk ; */
time_interval = pow(time_interval_cube, (1 / 3.0f));
sum_time += time_interval;
Vi = 0.5f * jerk * pow(sum_time, 2); /* 第一步的速度 */
time_arr = TIM_CLK * 1.0 / Vi;
if(time_arr > TIM_ARR_MAX) time_arr = TIM_ARR_MAX;
else if(time_arr < 10) time_arr = 10; /* 防止速度太快,stm32反应不过来 */
motor_s_shape_acceleration_tim_arr[0] = time_arr;
for (i = 1; i < acceleration_step; i++)
{
/* 步进电机的速度就是定时器脉冲输出频率,可以计算出每一步的时间 */
/* 得到第i-1步的时间 */
time_interval = 1.0f / Vi; /* 电机每走一步的时间 Ti = 1 / Vn-1 */
/* 加加速段速度计算 */
if (i < jerk_step)
{
sum_time += time_interval; /* 从0开始到i的时间累积 */
Vi = 0.5f * jerk * pow(sum_time, 2); /* 速度的变化量: dV = 1/2 * Jerk * Ti^2; */
time_arr = TIM_CLK * 1.0 / Vi; /* 得到加加速段每一步对应的速度 */
if(time_arr > TIM_ARR_MAX) time_arr = TIM_ARR_MAX;
else if(time_arr < 10) time_arr = 10;
motor_s_shape_acceleration_tim_arr[i] = time_arr;
/* 当最后一步的时候,时间并不严格等于Time,所以这里要稍作处理,作为减加速段的时间 */
if (i == jerk_step - 1)
sum_time = fabs(sum_time - accel_time * 1.0 / 10);
}
/* 减加速段速度计算 */
else
{
sum_time += time_interval; /* 时间累计 */
Vi = 0.5f * jerk * pow(fabs(accel_time * 1.0 / 10 - sum_time), 2); /* dV = 1/2 * Jerk *(T-t)^2; */
Vi = speed_step_per_sec - Vi;
time_arr = TIM_CLK * 1.0 / Vi; /* 得到加加速段每一步对应的速度 */
if(time_arr > TIM_ARR_MAX) time_arr = TIM_ARR_MAX;
else if(time_arr < 10) time_arr = 10;
motor_s_shape_acceleration_tim_arr[i] = time_arr;
}
}
if (acceleration_step * 2 > g_steps_total) /* 如果加减速的步数超过全程步数,则报错 */
{
return 1;
}
g_motor_s_shape_acceleration_step = acceleration_step;
g_step_timer_pulse_num = motor_s_shape_acceleration_tim_arr[0];
g_motor_state = MOTOR_ACCELERATE;
g_steps_done = 0;
__HAL_TIM_CLEAR_IT(&htim8, TIM_IT_UPDATE);
TIM8->CCR1 = g_step_timer_pulse_num >> 1;
TIM8->ARR = g_step_timer_pulse_num;
HAL_TIM_PWM_Start_IT(&htim8, TIM_CHANNEL_1);
return 0;
}
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f4xx_it.c
* @brief Interrupt Service Routines.
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2022 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_motor.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim8;
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
/******************************************************************************/
/* Cortex-M4 Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */
/* USER CODE END NonMaskableInt_IRQn 0 */
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
while (1)
{
}
/* USER CODE END NonMaskableInt_IRQn 1 */
}
/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
/* USER CODE END W1_HardFault_IRQn 0 */
}
}
/**
* @brief This function handles Memory management fault.
*/
void MemManage_Handler(void)
{
/* USER CODE BEGIN MemoryManagement_IRQn 0 */
/* USER CODE END MemoryManagement_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
/* USER CODE END W1_MemoryManagement_IRQn 0 */
}
}
/**
* @brief This function handles Pre-fetch fault, memory access fault.
*/
void BusFault_Handler(void)
{
/* USER CODE BEGIN BusFault_IRQn 0 */
/* USER CODE END BusFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_BusFault_IRQn 0 */
/* USER CODE END W1_BusFault_IRQn 0 */
}
}
/**
* @brief This function handles Undefined instruction or illegal state.
*/
void UsageFault_Handler(void)
{
/* USER CODE BEGIN UsageFault_IRQn 0 */
/* USER CODE END UsageFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_UsageFault_IRQn 0 */
/* USER CODE END W1_UsageFault_IRQn 0 */
}
}
/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
/* USER CODE BEGIN SVCall_IRQn 0 */
/* USER CODE END SVCall_IRQn 0 */
/* USER CODE BEGIN SVCall_IRQn 1 */
/* USER CODE END SVCall_IRQn 1 */
}
/**
* @brief This function handles Debug monitor.
*/
void DebugMon_Handler(void)
{
/* USER CODE BEGIN DebugMonitor_IRQn 0 */
/* USER CODE END DebugMonitor_IRQn 0 */
/* USER CODE BEGIN DebugMonitor_IRQn 1 */
/* USER CODE END DebugMonitor_IRQn 1 */
}
/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
/* USER CODE BEGIN PendSV_IRQn 0 */
/* USER CODE END PendSV_IRQn 0 */
/* USER CODE BEGIN PendSV_IRQn 1 */
/* USER CODE END PendSV_IRQn 1 */
}
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
/******************************************************************************/
/* STM32F4xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32f4xx.s). */
/******************************************************************************/
/**
* @brief This function handles USART1 global interrupt.
*/
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
/**
* @brief This function handles TIM8 update interrupt and TIM13 global interrupt.
*/
void TIM8_UP_TIM13_IRQHandler(void)
{
/* USER CODE BEGIN TIM8_UP_TIM13_IRQn 0 */
/* USER CODE END TIM8_UP_TIM13_IRQn 0 */
HAL_TIM_IRQHandler(&htim8);
/* USER CODE BEGIN TIM8_UP_TIM13_IRQn 1 */
/* USER CODE END TIM8_UP_TIM13_IRQn 1 */
}
/* USER CODE BEGIN 1 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
HAL_TIM_PWM_Stop_IT(&htim8, TIM_CHANNEL_1);
g_steps_done++;
if (g_steps_done >= g_steps_total) /* 运行完成 */
{
g_motor_state = MOTOR_STOP;
return;
}
if (g_steps_done < g_motor_s_shape_acceleration_step) /* 加速阶段 */
{
g_step_timer_pulse_num = motor_s_shape_acceleration_tim_arr[g_steps_done];
}
else if (g_steps_total - g_steps_done <= g_motor_s_shape_acceleration_step) /* 减速阶段 */
{
g_step_timer_pulse_num = motor_s_shape_acceleration_tim_arr[g_steps_total - g_steps_done - 1];
}
else /* 匀速阶段 */
{
g_step_timer_pulse_num = g_step_timer_pulse_num_min;
}
TIM8->CCR1 = g_step_timer_pulse_num >> 1;
TIM8->ARR = g_step_timer_pulse_num;
HAL_TIM_PWM_Start_IT(&htim8, TIM_CHANNEL_1);
}
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
总结:
1、
2、修改为motor_s_shape_acceleration_init(30, 100, 10);
3、
4、
5、
6、