ROS2智能小车代码备份

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 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.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#ifdef __cplusplus
extern "C" {
#include "cal.h"
#endif

/* 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 ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
void delay_us(uint32_t ii)
{
	uint32_t temp;
	SysTick->LOAD=ii;
	SysTick->VAL=0x00;
	SysTick->CTRL=0x01;//2号位1使用内核时钟
	do
	{
		temp=SysTick->CTRL;
	}
	while(temp&0x01&&!(temp&(1<<16)));
	SysTick->CTRL=0X00;
	SysTick->VAL=0X00;
}
void measDist()
{//超声波的发射引脚是:PB5,接收引脚是PB6
	uint32_t GPIO_PIN_TX=GPIO_PIN_5;
	uint32_t GPIO_PIN_RX=GPIO_PIN_6;
	int beginTime=0,endTime=0;
	int radari=0;
	int distance_cm;
  while (1)
  {
		radari=0;
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,1);
		delay_us(12);  //实际花费了20us 
		while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2)==0);
		while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2)==1)
		{
			radari++;
			delay_us(50);
		}
		distance_cm=340.0/1000000*(50*radari)/2;//这个就是测量的距离
		radari=0;
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,0);
  }
  /* USER CODE END 3 */
}
	
void adjustSpeed(TIM_HandleTypeDef htimx,/*引脚叿*/unsigned int TIM_CHANNEL_X,/*速度*/int speed,int rotation)
	{
		//获取分母倿
		int motherPWM=htimx.Init.Period;
		float rate=motherPWM/50.0;
		GPIO_TypeDef *GPIOX=GPIOA;
		int i=0,j=0;
		uint16_t GPIO_PIN_1_P=GPIO_PIN_4;
		uint16_t GPIO_PIN_1_N=GPIO_PIN_4;
		uint16_t GPIO_PIN_2_P=GPIO_PIN_4;
		uint16_t GPIO_PIN_2_N=GPIO_PIN_4;
		uint16_t GPIO_PIN_3_P=GPIO_PIN_4;
		uint16_t GPIO_PIN_3_N=GPIO_PIN_4;
		uint16_t GPIO_PIN_4_P=GPIO_PIN_4;
		uint16_t GPIO_PIN_4_N=GPIO_PIN_4;
		if(rotation<0)
		{
			HAL_GPIO_TogglePin(GPIOX,GPIO_PIN_1_P);
			HAL_GPIO_TogglePin(GPIOX,GPIO_PIN_1_N);
						HAL_GPIO_TogglePin(GPIOX,GPIO_PIN_2_P);
			HAL_GPIO_TogglePin(GPIOX,GPIO_PIN_2_N);
						HAL_GPIO_TogglePin(GPIOX,GPIO_PIN_2_P);
			HAL_GPIO_TogglePin(GPIOX,GPIO_PIN_2_N);
						HAL_GPIO_TogglePin(GPIOX,GPIO_PIN_2_P);
			HAL_GPIO_TogglePin(GPIOX,GPIO_PIN_2_N);
			speed=0-speed;
		}
		__HAL_TIM_SET_COMPARE(&htimx,TIM_CHANNEL_X,(speed*rate-1>0?speed*rate-1:0));			

	}
	void movCar(char c,int speed)
	{
		uint32_t TIM_CHANNEL_LA=TIM_CHANNEL_1;
		uint32_t TIM_CHANNEL_RA=TIM_CHANNEL_2;
		uint32_t TIM_CHANNEL_LB=TIM_CHANNEL_3;
		uint32_t TIM_CHANNEL_RB=TIM_CHANNEL_4;
		switch(c)
		{//左转
			case 'L':
				adjustSpeed(htim2,TIM_CHANNEL_LA,0,0);
				adjustSpeed(htim2,TIM_CHANNEL_LB,0,0);
				adjustSpeed(htim2,TIM_CHANNEL_RA,30,0);
				adjustSpeed(htim2,TIM_CHANNEL_RB,30,0);				
			break;
			//右转
			case 'R':
				adjustSpeed(htim2,TIM_CHANNEL_LA,30,0);
				adjustSpeed(htim2,TIM_CHANNEL_LB,30,0);		
				adjustSpeed(htim2,TIM_CHANNEL_RA,0,0);
				adjustSpeed(htim2,TIM_CHANNEL_RB,0,0);	
			break;
			//直行
			case 'A':
				adjustSpeed(htim2,TIM_CHANNEL_LA,speed,0);
				adjustSpeed(htim2,TIM_CHANNEL_LB,speed,0);
				adjustSpeed(htim2,TIM_CHANNEL_RA,speed,0);
				adjustSpeed(htim2,TIM_CHANNEL_RB,speed,0);		
			break;
			//倒车
			case 'B':
				adjustSpeed(htim2,TIM_CHANNEL_LA,30,-1);
				adjustSpeed(htim2,TIM_CHANNEL_LB,30,-1);
				adjustSpeed(htim2,TIM_CHANNEL_RA,30,-1);
				adjustSpeed(htim2,TIM_CHANNEL_RB,30,-1);				
			break;
		}
	}
void rotateSG90(int angle,TIM_HandleTypeDef *htim)
{
	//输出20ms的PWM信号,然后关闭这个PWM
	int times_=50;//1秒钟内重复的次数,例妿20um,就是50欿
	uint32_t TIM_CHANNEL_X=TIM_CHANNEL_3;
	static int first=1;
	int sysClockFreq=HAL_RCC_GetSysClockFreq();;
	int Prescaler=0;//预分频数
	int pulse=0;//PWM高电平持续时闿
	TIM_OC_InitTypeDef sConfigOC = {0};
	
	uint32_t Period=0;
	if(first==1)
	{
		htim->Init.Period=4000-1;//初始化period
	}
	Prescaler=htim->Init.Prescaler+1;
	if(first==1)
	{//调整Period倿--》调整PWM周期,但是无敿--》原因是虽然数忼改了,但是没有应用
		htim->Init.Period=sysClockFreq/(float)Prescaler/(float)times_-1;		
		first=0;
	}
	//设置占空毿
	pulse=htim->Init.Period +1;
		//使设置生敿
	HAL_TIM_PWM_Init(htim);
	
	switch(angle)
	{
		case 0:
			pulse=0.025*pulse;
            //这里100是设置占空比_100/4000就是占空比了100/4000*20ms=0.5ms
			break;
		case 45:
			pulse=0.05*pulse;
			break;
		case 90:
			pulse=0.075*pulse;
			break;
		case 135:
			pulse=0.1*pulse;
		break;
		case 180:
			pulse=0.125*pulse;
			break;
	}
	__HAL_TIM_SET_COMPARE(htim,TIM_CHANNEL_X,pulse);
//	HAL_TIM_PWM_Start(htim,TIM_CHANNEL_X);
	HAL_Delay(20);
}
//时钟频率霿要设置为8MHZ,另外霿要先随便设置丿个TIM定时器,推荐timer2
/* 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_TIM2_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
	HAL_TIM_Base_Start_IT(&htim2);
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_4);

	HAL_TIM_Base_Start_IT(&htim3);
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_3);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		rotateSG90(0,&htim3);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  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)
  {
    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_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)
  {
    Error_Handler();
  }
}

/* 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 */

你可能感兴趣的:(单片机,嵌入式硬件)