控制任务

对2驱动轮1从动轮的小车基本行走控制方法进行设计

实物图与电路设计

                       

图1 双轮驱动小车1

 

图2 双轮驱动小车2

 

图3 双轮驱动小车3

  

图4 双轮驱动小车控制系统接线图

实际接线

Arduino的引脚6,8,7分别接L298N驱动板的ENA,IN1,IN2,控制左电机,Arduino的引脚3,2,4分别接L298N驱动板的ENB,IN3,IN4,控制右电机,

程序设计

  1 //控制板引脚定义
  2 int motor_L_ENA = 6; //左电机控制端连接于Arduino板的6,8,7引脚
  3 int motor_L_IN1 = 8;
  4 int motor_L_IN2 = 7;
  5 int motor_R_ENB = 2; //右电机控制端连接于Arduino板的2,3,4引脚
  6 int motor_R_IN3 = 3;
  7 int motor_R_IN4 = 4;
  8 int low_speed = 100; //左转右转时的高速低速设定,可调整
  9 int high_speed = 200;
 10 
 11 
 12 
 13 //子函数定义
 14 void go_forward_high_speed()   //小车前进 全速
 15 {
 16   digitalWrite(motor_L_ENA, 1);  //左电机正转
 17   digitalWrite(motor_L_IN1, 0);
 18   digitalWrite(motor_L_IN2, 1);
 19 
 20   digitalWrite(motor_R_ENB, 1);   //右电机正转
 21   digitalWrite(motor_R_IN3, 0);
 22   digitalWrite(motor_R_IN4, 1);
 23 }
 24 
 25 void go_backward_high_speed()  //小车后退 全速
 26 {
 27   digitalWrite(motor_L_ENA, 1);  //左电机反转
 28   digitalWrite(motor_L_IN1, 1);
 29   digitalWrite(motor_L_IN2, 0);
 30 
 31   digitalWrite(motor_R_ENB, 1);  //右电机反转
 32   digitalWrite(motor_R_IN3, 1);
 33   digitalWrite(motor_R_IN4, 0);
 34 }
 35 
 36 void stop_with_brake()  //小车制动
 37 {
 38   digitalWrite(motor_L_ENA, 1);  //左电机制动
 39   digitalWrite(motor_L_IN1, 1);
 40   digitalWrite(motor_L_IN2, 1) ;
 41 
 42   digitalWrite(motor_R_ENB, 1);  //右电机制动
 43   digitalWrite(motor_R_IN3, 1);
 44   digitalWrite(motor_R_IN4, 1) ;
 45 }
 46 
 47 void stop_free()    //小车自由停止
 48 {
 49   digitalWrite(motor_L_ENA, 0); //左电机自由停止
 50   digitalWrite(motor_L_IN1, 0);
 51   digitalWrite(motor_L_IN2, 0) ;
 52 
 53   digitalWrite(motor_R_ENB, 0);   //右电机自由停止
 54   digitalWrite(motor_R_IN3, 0);
 55   digitalWrite(motor_R_IN4, 0) ;
 56 }
 57 
 58 void go_forward_speed_up()  //小车前进 加速
 59 {
 60   digitalWrite(motor_L_IN1, 0); //左轮加速
 61   digitalWrite(motor_L_IN2, 1);
 62   for (int a = 100; a <= 255; a++)
 63   {
 64     analogWrite(motor_L_ENA, a);
 65     delay(200);
 66   }
 67 
 68   digitalWrite(motor_R_IN3, 0); //右轮加速
 69   digitalWrite(motor_R_IN4, 1);
 70   for (int a = 100; a <= 255; a++)
 71   {
 72     analogWrite(motor_R_ENB, a);
 73     delay(200);
 74   }
 75 }
 76 
 77 void go_forward_speed_down()  //小车前进 减速
 78 {
 79   digitalWrite(motor_L_IN1, 0);  //左轮减速
 80   digitalWrite(motor_L_IN2, 1);
 81   for (int a = 255; a <= 100; a--)
 82   {
 83     analogWrite(motor_L_ENA, a);
 84     delay(200);
 85   }
 86 
 87   digitalWrite(motor_R_IN3, 0);  //右轮减速
 88   digitalWrite(motor_R_IN4, 1);
 89   for (int a = 255; a <= 100; a--)
 90   {
 91     analogWrite(motor_R_ENB, a);
 92     delay(200);
 93   }
 94 }
 95 
 96 void go_forward_left()  //小车左转
 97 {
 98   digitalWrite(motor_L_IN1, 0);
 99   digitalWrite(motor_L_IN2, 1);
100   analogWrite(motor_L_ENA, low_speed); //左轮低速
101 
102   digitalWrite(motor_R_IN3, 0);
103   digitalWrite(motor_R_IN4, 1);
104   analogWrite(motor_R_ENB, high_speed);  //右轮高速
105 }
106 
107 void go_forward_right()  //小车右转
108 {
109   digitalWrite(motor_L_IN1, 0);
110   digitalWrite(motor_L_IN2, 1);
111   analogWrite(motor_L_ENA, high_speed); //左轮高速
112 
113   digitalWrite(motor_R_IN3, 0);
114   digitalWrite(motor_R_IN4, 1);
115   analogWrite(motor_R_ENB, low_speed);  //右轮低速
116 }
117 
118 
119 void setup()
120 {
121   pinMode(motor_L_ENA, OUTPUT); //左电机使能和PWM调速口
122   pinMode(motor_L_IN1, OUTPUT);  //左电机控制口1
123   pinMode(motor_L_IN2, OUTPUT);  //左电机控制口2
124 
125   pinMode(motor_R_ENB, OUTPUT); //右电机使能和PWM调速口
126   pinMode(motor_R_IN3, OUTPUT);  //右电机控制口3
127   pinMode(motor_R_IN4, OUTPUT);  //左电机控制口4
128 }
129 
130 void loop()
131 {
132   stop_with_brake(); //制动2秒
133   delay(2000);
134 
135   go_forward_speed_up();   //前进 加速 50秒
136   delay(50000);
137   go_forward_high_speed();   //前进 全速10秒
138   delay(10000);
139   go_forward_speed_down();   //前进 减速50秒
140   delay(50000);
141   stop_free();    //自由停车5秒
142   delay(5000);
143 
144   go_forward_left();  //小车左转10秒,时间可调整
145   delay(10000);
146   go_forward_right();  //小车右转10秒,时间可调整
147   delay(10000);
148   stop_free();    //自由停车5秒
149   delay(5000);
150 }

运行效果

包括下列动作:小车制动,加速前进,全速前进,减速前进,自由停车,小车左转,小车右转,自由停车,循环

进一步练习

设计程序控制小车走圆形曲线或者走口字