当前位置:   article > 正文

STM32:麦克纳姆轮进行循迹任务(库函数程序代码)_麦克纳姆轮循迹控制程序

麦克纳姆轮循迹控制程序

由于麦克纳姆轮的特殊性,每个轮子都需要一个电机进行独立控制。轮子的安装顺序为ABAB(注释中顺序为:B轮A轮D轮C轮),怎么安装网上资料很多,驱动建议使用L298N四路驱动模块。话不多说,直接上程序:


             麦克纳姆轮安装方向

        A轮  \\    ------   //  B轮
			  \\  ------  //
				  ------
				  ------				
				  ------
			 //  ------  \\
	   D轮  //   ------   \\  C轮
				
			//四驱底盘及四轮麦克纳姆轮底盘
			//硬件连接说明:
			timer8 控制ABCD四个轮子的PWM
			TIM8_CH1--PC7--A轮 //左前 ---- 电机驱动1-ENA
			TIM8_CH2--PC6--B轮 //右前 ---- 电机驱动1-ENB
			TIM8_CH3--PC8--C轮 //右后 ---- 电机驱动2-ENA
		    TIM8_CH4--PC9--D轮 //左后 ---- 电机驱动2-ENB
			
			
			A轮:PC1 PC0 控制前后运动   PC1----IN2,PC0----IN1
			B轮:PC3 PC2 控制前后运动   PC3----IN4,PC2----IN3			
			C轮:PC10,PC11控制前后运动 PC10 --- IN1,PC11---IN2
			D轮:PC12,PD2控制前后运动  PC12 --- IN3,PD2 ---IN4
				
		    
*********************************************************************************************************
*/

#include "motor.h"
//***************************配置电机驱动IO口***************************//

void MOTOR_GPIO_Init(void)
{		
		/*定义一个GPIO_InitTypeDef类型的结构体*/
		GPIO_InitTypeDef GPIO_InitStructure;
		
		RCC_APB2PeriphClockCmd(MOTOR_CLK, ENABLE);                                	  /*开启GPIO的外设时钟*/																   
		GPIO_InitStructure.GPIO_Pin = MOTOR_A1 | MOTOR_A2	| MOTOR_B1 | MOTOR_B2 | MOTOR_C1 | MOTOR_C2 | MOTOR_D1;	/*选择要控制的GPIO引脚*/	
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                              /*设置引脚模式为通用推挽输出*/   
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                             /*设置引脚速率为50MHz */ 
		GPIO_Init(MOTOR_PORT, &GPIO_InitStructure); 	/*调用库函数,初始化GPIO*/	
	
		RCC_APB2PeriphClockCmd( MOTOR_CLK2, ENABLE);                                	  /*开启GPIO的外设时钟*/																   
		GPIO_InitStructure.GPIO_Pin = MOTOR_D2;	/*选择要控制的GPIO引脚*/	
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                              /*设置引脚模式为通用推挽输出*/   
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                             /*设置引脚速率为50MHz */ 
		GPIO_Init(MOTOR_PORT2, &GPIO_InitStructure);                                    /*调用库函数,初始化GPIO*/		
}
//左前A电机
void MOTOR_A(char state)
{
	if(state == GO)//左前电机前进
	{
		GPIO_SetBits(MOTOR_PORT,MOTOR_A2);
	  GPIO_ResetBits(MOTOR_PORT,MOTOR_A1);	
	}
	if(state == BACK)//左前电机后退
	{
	

		GPIO_SetBits(MOTOR_PORT,MOTOR_A1);
	  GPIO_ResetBits(MOTOR_PORT,MOTOR_A2);
		
		
	}
	if(state == STOP)//停转
	{
		GPIO_ResetBits(MOTOR_PORT,MOTOR_A1);
	  GPIO_ResetBits(MOTOR_PORT,MOTOR_A2);
	}
		
		
}

//右前B电机
void MOTOR_B(char state)
{
	if(state == GO)//右前电机前进
	{
		GPIO_SetBits(MOTOR_PORT,MOTOR_B1);
	  GPIO_ResetBits(MOTOR_PORT,MOTOR_B2);
	}
	if(state == BACK)//右前电机后退
	{
		
		GPIO_SetBits(MOTOR_PORT,MOTOR_B2);
	  GPIO_ResetBits(MOTOR_PORT,MOTOR_B1);
	
	}
	if(state == STOP)//停转
	{
		GPIO_ResetBits(MOTOR_PORT,MOTOR_B1);
	  GPIO_ResetBits(MOTOR_PORT,MOTOR_B2);
	}
}

//左后C电机
void MOTOR_C(char state)
{
	if(state == GO)//左后电机前进
	{
			GPIO_SetBits(MOTOR_PORT,MOTOR_C2);
	  GPIO_ResetBits(MOTOR_PORT,MOTOR_C1);

	}
	if(state == BACK)//左后电机后退
	{

	
		GPIO_SetBits(MOTOR_PORT,MOTOR_C1);
	  GPIO_ResetBits(MOTOR_PORT,MOTOR_C2);
	
	}
	if(state == STOP)//停转
	{
		GPIO_ResetBits(MOTOR_PORT,MOTOR_C1);
	  GPIO_ResetBits(MOTOR_PORT,MOTOR_C2);
	}
		
}

//右后D电机
void MOTOR_D(char state)
{
	if(state == GO)//右后电机前进
	{
		GPIO_SetBits(MOTOR_PORT2,MOTOR_D2);
	  GPIO_ResetBits(MOTOR_PORT,MOTOR_D1);
		
	}
	if(state == BACK)//右后电机后退
	{

		GPIO_SetBits(MOTOR_PORT,MOTOR_D1);
	  GPIO_ResetBits(MOTOR_PORT2,MOTOR_D2);
	}
	if(state == STOP)//停转
	{
		GPIO_ResetBits(MOTOR_PORT,MOTOR_D1);
	  GPIO_ResetBits(MOTOR_PORT2,MOTOR_D2);
	}
		
}


//***************************前进***************************//
//只要配置INx()的状态就可以改变电机转动方向
void Car_Go(void)
{
	//左前电机 前    //右前电机 前
	MOTOR_A(GO);       MOTOR_B(GO);

	//左后电机 前   //右后电机 前
	MOTOR_D(GO);       MOTOR_C(GO);
	
	
}

void Car_Back(void)
{
		//左前电机 后    //右前电机 后
MOTOR_A(BACK);       MOTOR_B(BACK);

	//左后电机 后   //右后电机 后
MOTOR_D(BACK);       MOTOR_C(BACK);
}

//***************************向左***************************//
void Car_Left(void)
{
	
	//左前电机 后    //右前电机 前
	MOTOR_A(BACK);       MOTOR_B(GO);

	//左后电机 前   //右后电机 后
  MOTOR_D(GO);       MOTOR_C(BACK);
	
}

//***************************向左前45度***************************//
void Car_LeftQ45(void)
{
	
	//左前电机 停    //右前电机 前
	MOTOR_A(STOP);   MOTOR_B(GO);

	//左后电机 前   //右后电机 停
 MOTOR_D(GO);       MOTOR_C(STOP);
	
}

//***************************向左后45度***************************//
void Car_LeftH45(void)
{
	//左前电机 后    //右前电机 停
MOTOR_A(BACK);       MOTOR_B(STOP);

	//左后电机 停   //右后电机 后
  MOTOR_D(STOP);       MOTOR_C(BACK);
	
}

//***************************向左转圈***************************//
void Car_Turn_Left(void)
{
	
	//左前电机 后    //右前电机 前
MOTOR_A(BACK);       MOTOR_B(GO);

	//左后电机 后   //右后电机 前
MOTOR_D(BACK);       MOTOR_C(GO);
	
}

//***************************向右***************************//
void Car_Right(void)
{
	//左前电机 前    //右前电机 后
MOTOR_A(GO);       MOTOR_B(BACK);

	//左后电机 后   //右后电机 前
  MOTOR_D(BACK);       MOTOR_C(GO);
	
}

//***************************向右前45度***************************//
void Car_RightQ45(void)
{
	//左前电机 前    //右前电机 停
MOTOR_A(GO);       MOTOR_B(STOP);

	//左后电机 停   //右后电机 前
 MOTOR_D(STOP);       MOTOR_C(GO);
	
}
//***************************向右后45度***************************//
void Car_RightH45(void)
{
	//左前电机 停    //右前电机 后
MOTOR_A(STOP);       MOTOR_B(BACK);

	//左后电机 后   //右后电机 停
  MOTOR_D(BACK);       MOTOR_C(STOP);
	
}
//***************************向右转圈***************************//
void Car_Turn_Right(void)
{
	//左前电机 前    //右前电机 后
MOTOR_A(GO);       MOTOR_B(BACK);

	//左后电机 前   //右后电机 后
  MOTOR_D(GO);       MOTOR_C(BACK);
	
}



//***************************停车***************************//
void Car_Stop(void)
{
	//左前电机 停    //右前电机 停
MOTOR_A(STOP);       MOTOR_B(STOP);

	//左后电机 停   //右后电机 停
  MOTOR_D(STOP);       MOTOR_C(STOP);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267

以上为几种车子运动的函数构造,接下来是用四路循迹模块完成循迹任务程序代码:

#include "find.h"
#include "bsp_timer8.h"
#include "bsp_sys.h"
#include "delay.h"

//循迹IO初始化
//寻迹传感器从右到左以此O1 O2 O3 O4 
//硬件连接 O1-PA4,O2-PA5,O3-PA6,O4-PA7,
//要初始化为输入模式
void Find_IO_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	//开启GPIO时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6 | GPIO_Pin_7;//选择IO端口
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//配置为上拉输入
	GPIO_Init(GPIOA, &GPIO_InitStructure);//根据GPIO_InitStructure中指定的参数初始化外设GPIOD寄存器

} 


//循迹、、循迹模块 黑线输出高电平1  白线低电平0
//小车最左端是O4----最右端是O1
//循迹路面:白色路面黑色引导线,即寻黑线。
//黑线 传感器输出1,白线输出0
void Find(void)
{
	//全是白线,前进
	if((Find_O4 == 0)&&(Find_O3 == 0)&&(Find_O2 == 0)&&(Find_O1 == 0))// 白线,前进
	{
			Car_Go();
	}
	O2在黑线  右边有黑线 小车偏左
	//应向右转调整到前进方向	。即左轮加速右轮减速
	if((Find_O4 == 0)&&(Find_O3 == 0)&&(Find_O2 == 1)&&(Find_O1 == 0))// O2寻到黑线
	{
			
			Car_Turn_Right();
	}

	O2在黑线 O1黑线  右边有黑线 小车偏偏左
	//应向右转调整到前进方向	。即左轮加速右轮减速
	if((Find_O4 == 0)&&(Find_O3 == 0)&&(Find_O2 == 1)&&(Find_O1 == 1))// O2 O1寻到黑线
	{
			Car_Turn_Right();
	}
	O1在黑线  右边有黑线 小车偏偏偏左
	//应向右转调整到前进方向	。即左轮加速右轮减速
	if((Find_O4 == 0)&&(Find_O3 == 0)&&(Find_O2 == 0)&&(Find_O1 == 1))// O1寻到黑线
	{
		Car_Turn_Right();
	}
	O3在黑线  左边边有黑线 小车偏右
	//应向左转调整到前进方向	。即右轮加速左轮减速
	if((Find_O4 == 0)&&(Find_O3 == 1)&&(Find_O2 == 0)&&(Find_O1 == 0))// O3寻到黑线
	{
		Car_Turn_Left();
	}
	O3,O4在黑线  左边边有黑线 小车偏偏右
	//应向左转调整到前进方向	。即右轮加速左轮减速
	if((Find_O4 == 1)&&(Find_O3 == 1)&&(Find_O2 == 0)&&(Find_O1 == 0))// O3 O4寻到黑线
	{
		Car_Turn_Left();
	}
	
	O4在黑线  左边边有黑线 小车偏偏偏右
	//应向左转调整到前进方向	。即右轮加速左轮减速
	if((Find_O4 == 1)&&(Find_O3 == 0)&&(Find_O2 == 0)&&(Find_O1 == 0))// O4寻到黑线
	{
		Car_Turn_Left();
			
	}
	
	
	//停车
	if((Find_O4 == 1)&&(Find_O3 == 1)&&(Find_O2 == 1)&&(Find_O1 == 1))// 所以传感器都在黑线
	{
		 Car_Stop();
	}
	
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/1012968
推荐阅读
相关标签
  

闽ICP备14008679号