本文主要是介绍ROS2+STM32智能小车底盘转向代码(C++版/HAL库版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这个代码还没有测试通过,哈哈原理是不会错的你看吧
extern TIM_HandleTypeDef htim2;extern TIM_HandleTypeDef htim3;void adjustSpeed(TIM_HandleTypeDef htimx,/*引脚号*/unsigned int TIM_CHANNEL_X,/*速度*/int speed,int rotation){//获取分母值int motherPWM=htimx.Init.Period;float rate=motherPWM/100.0;if(rotation<0){HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_4);HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_6);HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_7); speed=0-speed;}__HAL_TIM_SET_COMPARE(&htimx,TIM_CHANNEL_X,(speed*rate-1>0?speed*rate-1:0)); }void rotateLeft(){//要怎么样左转?左边轮胎=0,右边轮胎转就实现转向adjustSpeed(htim2,TIM_CHANNEL_1,0,0);adjustSpeed(htim2,TIM_CHANNEL_3,0,0);adjustSpeed(htim2,TIM_CHANNEL_2,30,0);adjustSpeed(htim2,TIM_CHANNEL_4,30,0);}void rotateRight(){//要怎么样左转?左边轮胎=0,右边轮胎转就实现转向adjustSpeed(htim2,TIM_CHANNEL_1,30,0);adjustSpeed(htim2,TIM_CHANNEL_3,30,0);adjustSpeed(htim2,TIM_CHANNEL_2,0,0);adjustSpeed(htim2,TIM_CHANNEL_4,0,0);}void goAhead(int speed){//要怎么样左转?左边轮胎=0,右边轮胎转就实现转向adjustSpeed(htim2,TIM_CHANNEL_1,speed,0);adjustSpeed(htim2,TIM_CHANNEL_3,speed,0);adjustSpeed(htim2,TIM_CHANNEL_2,speed,0);adjustSpeed(htim2,TIM_CHANNEL_4,speed,0);}void goBack(){//要怎么样左转?左边轮胎=0,右边轮胎转就实现转向adjustSpeed(htim2,TIM_CHANNEL_1,30,-1);adjustSpeed(htim2,TIM_CHANNEL_3,30,-1);adjustSpeed(htim2,TIM_CHANNEL_2,30,-1);adjustSpeed(htim2,TIM_CHANNEL_4,30,-1);}
这篇关于ROS2+STM32智能小车底盘转向代码(C++版/HAL库版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!