基于arduino的5路循迹小车(1)
初步小车运动
1.开发板使用arduino 2560
2.电机驱动板使用 大功率电机驱动板(PWM由两个端子控制)
3.电源12V锂电池
4.降压板(需要降压给开发板供电)
5.直流减速电机12V
12V电源直接给通过电机驱动板给电机供电
12V电源通过降压板给开发板供电
其余控制电路接线均按代码接线
(注意:12V电源接出的线因电流过大,需要使用漆包线,其余均可用杜邦线)
int Left_motor_go1 = 24; //左电机前进 AIN1
int Left_motor_back1 = 25; //左电机后退 AIN2
int Right_motor_go1 = 22; //右电机前进 BIN1
int Right_motor_back1 = 23; //右电机后退 BIN2
int Left_motor_pwm1 = 3; //左电机控速 PWMA1
int Right_motor_pwm1= 5; //右电机控速 PWMB1
int Left_motor_pwm2 = 4; //左电机控速 PWMA2
int Right_motor_pwm2= 6; //右电机控速 PWMB2
void setup()
{
//初始化电机驱动IO口为输出方式
pinMode(Left_motor_go1, OUTPUT);
pinMode(Left_motor_back1, OUTPUT);
pinMode(Right_motor_go1, OUTPUT);
pinMode(Right_motor_back1, OUTPUT);
}
void run(int left_speed, int right_speed)
{
//左电机前进
digitalWrite(Left_motor_go1, LOW); //左电机前进使能
digitalWrite(Left_motor_back1, HIGH); //左电机后退禁止
analogWrite(Left_motor_pwm1, LOW);
analogWrite(Left_motor_pwm2, left_speed);
//右电机前进
digitalWrite(Right_motor_go1, LOW); //右电机前进使能
digitalWrite(Right_motor_back1, HIGH); //右电机后退禁止
analogWrite(Right_motor_pwm1, LOW);
analogWrite(Right_motor_pwm2, right_speed);
}
void left(int left_speed, int right_speed)
{
//左电机后退
digitalWrite(Left_motor_go1, HIGH); //左电机前进禁止
digitalWrite(Left_motor_back1,LOW); //左电机后退禁止
analogWrite(Left_motor_pwm2, left_speed);
analogWrite(Left_motor_pwm1,LOW);
//右电机前进
digitalWrite(Right_motor_go1, LOW); //右电机前进使能
digitalWrite(Right_motor_back1, HIGH); //右电机后退禁止
analogWrite(Right_motor_pwm2, LOW);
analogWrite(Right_motor_pwm1, right_speed);
}
void right(int left_speed, int right_speed)
{
//左电机前进
digitalWrite(Left_motor_go1, LOW); //左电机前进使能
digitalWrite(Left_motor_back1, HIGH); //左电机后退禁止
analogWrite(Left_motor_pwm2, LOW);
analogWrite(Left_motor_pwm1, left_speed);
//右电机后退
digitalWrite(Right_motor_go1, HIGH ); //右电机前进禁止
digitalWrite(Right_motor_back1,LOW); //右电机后退禁止
analogWrite(Right_motor_pwm2, right_speed);
analogWrite(Right_motor_pwm1, LOW );
}
void back(int left_speed, int right_speed)
{
//左电机后退
digitalWrite(Left_motor_go1, HIGH); //左电机前进禁止
digitalWrite(Left_motor_back1, LOW); //左电机后退使能
analogWrite(Left_motor_pwm1, left_speed);
analogWrite(Left_motor_pwm2, LOW);
//右电机后退
digitalWrite(Right_motor_go1, HIGH); //右电机前进禁止
digitalWrite(Right_motor_back1, LOW); //右电机后退使能
analogWrite(Right_motor_pwm1, right_speed);
analogWrite(Right_motor_pwm2, LOW);
}
void bank(int left_speed, int right_speed)//惯性停车
{
//左电机空
digitalWrite(Left_motor_go1, LOW); //左电机前进禁止
digitalWrite(Left_motor_back1, LOW); //左电机后退禁止
analogWrite(Left_motor_pwm1, left_speed);
analogWrite(Left_motor_pwm2, left_speed);
//右电机空
digitalWrite(Right_motor_go1, LOW); //右电机前进禁止
digitalWrite(Right_motor_back1,LOW); //右电机后退禁止
analogWrite(Right_motor_pwm1, right_speed);
analogWrite(Right_motor_pwm2, right_speed);
}
void loop()
{
}
在loop中输入需要测试的小车运行状态
1.若电机转向相反,请检查接线
2.运行时有剧烈抖动或电机发出较大异响,请使用万用表排查问题