STM32F407 直流有刷电机PID速度环控制(位置式)

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; 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"
#include "bsp_usart.h"
#include "bsp_pid.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 htim1;
TIM_HandleTypeDef htim3;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

uint16_t g_encoder_update_cnt = 0;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_TIM3_Init(void);
static void MX_TIM1_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_USART1_UART_Init();
  MX_TIM3_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */
	
	delay_init(168);
	
	usart_init();
	pid_init();
	
	__HAL_TIM_SET_COUNTER(&htim3, 0);
	__HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
	__HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE);
	HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
	
	
	int32_t velocity_current;				/* 单位:0.001r/min */
	int32_t pos_current;							/* 单位:0.001r */
	uint8_t buffer_usart_send[16];
	uint32_t tick;
	uint16_t sum;
	uint8_t i;
	uint8_t run = 0;	
	uint32_t tick_start_run;	/* 开始运行时刻 */
	
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
		usart_task();
		
		if(KEY1_StateRead()==KEY_DOWN)
    {
			__HAL_TIM_SET_COUNTER(&htim1, 0);
			HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
			HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
			
			run = 1;
    }
		
		if (run)
		{
			pid_ctrl(get_pid_velocity().target);
		}
		
		if (run)
		{
			pos_current = ((int32_t)(g_encoder_update_cnt * (htim3.Init.Period + 1) + __HAL_TIM_GET_COUNTER(&htim3))) * 1.0 / STEP_ENCODER_CIRCLE * 1000;
			velocity_current = get_velocity_current() * 1000;
			tick = HAL_GetTick();
			
			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] = pos_current >> 24;										/* 电机位置,0.001r */
			buffer_usart_send[7] = pos_current >> 16;										/* 电机位置,0.001r */
			buffer_usart_send[8] = pos_current >> 8;										/* 电机位置,0.001r */
			buffer_usart_send[9] = pos_current & 0xff;									/* 电机位置,0.001r */
			buffer_usart_send[10] = velocity_current >> 24;							/* 编码器速度,0.001r/min */
			buffer_usart_send[11] = velocity_current >> 16;							/* 编码器速度,0.001r/min */
			buffer_usart_send[12] = velocity_current >> 8;							/* 编码器速度,0.001r/min */
			buffer_usart_send[13] = velocity_current & 0xff;						/* 编码器速度,0.001r/min */
			
			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_OscInitTypeDe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值