基于arduino的5路循迹小车(4)
arduino与舵机控制板的通信控制
以及语音播报模块
接第一篇链接 https://blog.csdn.net/weixin_45984029/article/details/103437347
1.舵机控制板
开发板与舵机控制板的TX,RX交叉相连,VCC,GND对应相连
2.语音播报模块
VCC,GND与开发板对应相连
语音播报模块的每一个触发端子与开发板的数字端相连(具体位置对应代码)
#include <LobotServoController.h>
LobotServoController myse;
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
//循迹红外引脚定义
//TrackSensorLeftPin1 TrackSensorLeftPin2 TrackSensorMid TrackSensorRightPin1 TrackSensorRightPin2
// A1 A2 A3 A4 A5
const int TrackSensorLeftPin1 = A1; //定义第一个循迹红外传感器引脚为A1
const int TrackSensorLeftPin2 = A2; //定义第二个循迹红外传感器引脚为A2
const int TrackSensorMid = A3; //定义第三个循迹红外传感器引脚为A3
const int TrackSensorRightPin1 = A4; //定义第四个循迹红外传感器引脚为A4
const int TrackSensorRightPin2 = A5; //定义第五个循迹红外传感器引脚为A5
// TrackSensorRightPin3 进行计数
// A6
const int TrackSensorRightPin3 = A6; //右主计数
//定义计数变量
int JS2;
//定义各个循迹红外引脚采集的数据的变量
int SLL;
int SL;
int SM;
int SR;
int SRR;
int echoPin = 32; //超声波
int trigPin = 33;
unsigned int S;
int a;
int i;
int G1 = 26;
int G2 = 27;
int G3 = 28;
int G4 = 29;
int G5 = 30;
void setup()
{
Serial.begin(9600);//超声波
Serial1.begin(9600);
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
while(!Serial1);
digitalWrite(13,HIGH);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//初始化语音输出
pinMode(G1, OUTPUT);
pinMode(G2, OUTPUT);
pinMode(G3, OUTPUT);
pinMode(G4, OUTPUT);
pinMode(G5, OUTPUT);
digitalWrite(G1, HIGH);
digitalWrite(G2, HIGH);
digitalWrite(G3, HIGH);
digitalWrite(G4, HIGH);
digitalWrite(G5, HIGH);
//初始化电机驱动IO口为输出方式
pinMode(Left_motor_go1, OUTPUT);
pinMode(Left_motor_back1, OUTPUT);
pinMode(Right_motor_go1, OUTPUT);
pinMode(Right_motor_back1, OUTPUT);
//定义四路循迹红外传感器为输入接口
pinMode(TrackSensorLeftPin1, INPUT);
pinMode(TrackSensorLeftPin2, INPUT);
pinMode(TrackSensorMid,INPUT);
pinMode(TrackSensorRightPin1, INPUT);
pinMode(TrackSensorRightPin2, INPUT);
//四路循迹红外传感器初始化为高电平
digitalWrite(TrackSensorLeftPin1, HIGH);
digitalWrite(TrackSensorLeftPin2, HIGH);
digitalWrite(TrackSensorMid,HIGH);
digitalWrite(TrackSensorRightPin1, HIGH);
digitalWrite(TrackSensorRightPin2, HIGH);
digitalWrite(G1, LOW);//播报第一条语音
}
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 barke(int left_speed, int right_speed)
{
//左电机停止
digitalWrite(Left_motor_go1, HIGH); //左电机前进使能
digitalWrite(Left_motor_back1, HIGH); //左电机后退禁止
analogWrite(Left_motor_pwm1, left_speed);
analogWrite(Left_motor_pwm2, left_speed);
//右电机停止
digitalWrite(Right_motor_go1, HIGH ); //右电机前进禁止
digitalWrite(Right_motor_back1,HIGH); //右电机后退禁止
analogWrite(Right_motor_pwm1, right_speed);
analogWrite(Right_motor_pwm2, right_speed);
}
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 xunji()//循迹
{
a = 0;
//检测到黑线时循迹模块相应的指示灯灭,端口电平为HIGH
//未检测到黑线时循迹模块相应的指示灯亮,端口电平为LOW
SLL = digitalRead(TrackSensorLeftPin1);
SL = digitalRead(TrackSensorLeftPin2);
SM = digitalRead(TrackSensorMid);
SR = digitalRead(TrackSensorRightPin1);
SRR = digitalRead(TrackSensorRightPin2);
//计数
JS2 = digitalRead(TrackSensorRightPin3);
//循迹
if( SM == HIGH )
{
run(45,37);
}
if( SL == HIGH && SM == LOW)
{
left(35,57);
}
if( SLL == HIGH && SM == LOW)
{
left(35,57);
}
if( SR == HIGH && SM == LOW)
{
right(50,32);
}
if( SRR == HIGH && SM == LOW)
{
right(50,32);
}
if(SR == HIGH && SRR == HIGH)
{
right(50,32);
}
if(SL == HIGH && SLL == HIGH)
{
left(30,57);
}
if(SRR == HIGH && SR == HIGH && SM == HIGH && SL == HIGH)
{
right(50,32);
}
if(SM == HIGH && (SL == HIGH && SLL == HIGH) || (SR == HIGH && SL == HIGH) || (SR == HIGH && SRR == HIGH))
{
run(45,37);
}
}
void loop()
{
xunji();//调用循迹
if(JS2 == HIGH)
{
a++;
i++;
if(a == 1)
{
barke(120,120);//停车
delay(650);
bank(0,0);
delay(1000);
if(i == 1)
{
digitalWrite(G2, LOW);//语音播报第二条语音
myse.runActionGroup(100,1); //运行100号识别1组
delay(2000);
/*scan();//识别
shangp();*/
myse.runActionGroup(26,1);
delay(16000);
}
if(i == 2)
{
myse.runActionGroup(100,1); //运行100号识别1组
delay(2000);
/*scan();//识别
shangp();*/
myse.runActionGroup(27,1);
delay(16000);
}
if(i == 3)
{
myse.runActionGroup(100,1); //运行100号识别1组
delay(2000);
/*scan();//识别
shangp();*/
myse.runActionGroup(28,1);
delay(16000);
myse.runActionGroup(110,1); //运行110号动作组
delay(4000);
bank(0,0);
delay(1000);
run(42,34);
delay(300);
run(15,8);
delay(500);
}
if(i == 4)
{
myse.runActionGroup(98,1); //运行98号动作组
delay(2000);
run(50,42);
delay(400);
return;
}
if(i == 5)//下料
{
/*scan();//识别
fangp();*/
}
if(i == 6)
{
/*scan();//识别
fangp();*/
}
if(i == 7)
{
/*scan();//识别
fangp();*/
}
if(i == 8)
{
digitalWrite(G5, LOW);
myse.runActionGroup(81,1); //运行101号识别2组
delay(16000);
/*myse.runActionGroup(101,1); //运行101号识别2组
delay(4000);
scan();//识别
fangp();*/
}
if(i == 9)
{
myse.runActionGroup(82,1); //运行101号识别2组
delay(16000);
/*myse.runActionGroup(101,1); //运行101号识别2组
delay(4000);
scan();//识别
fangp();*/
}
if(i == 10)
{
myse.runActionGroup(83,1); //运行101号识别2组
delay(16000);
/*myse.runActionGroup(101,1); //运行101号识别2组
delay(4000);
scan();//识别
fangp();*/
}
run(100,90);
delay(200);
return;
}
}
range();//超声波检测
}
void range()
{
digitalWrite(trigPin,LOW); //测距
delayMicroseconds(2); //延时2微秒
digitalWrite(trigPin,HIGH);
delayMicroseconds(20);
digitalWrite(trigPin,LOW);
int distance = pulseIn(echoPin,HIGH); //读取高电平时间
distance = distance/58;
S = distance; //把值赋给S
if( S < 16 )//小于16厘米时,调用避障程序
{
BZ();
}
}
void BZ()//避障程序
{
barke(120,120);
delay(600);
bank(0,0);
delay(300);
back(45,37);
delay(300);
barke(120,120);
delay(300);
bank(0,0);
delay(300);
left(55,65);
delay(700);
run(55,55);
delay(1000);
right(55,45);
delay(400);
run(55,55);
delay(1000);
right(55,45);
delay(400);
run(55,55);
delay(1046);
left(55,55);
delay(550);
}
在IDE中必须要加载舵机控制板的库,不然无法控制舵机
动作组直接保存在舵机控制板中,开发板直接调用
语音播报模块低电平触发一次,播报一次对应音频文件