本帖最后由 vickywong 于 2016-11-20 10:52 编辑
小车总结: 1 转接板的问题:电机D9和舵机库D10冲突,超声波和红外测距都用D7D8。这个转接板完全不行 2 红外遥控的接收头在前边,人得后退着遥控。遥控器也不好使,没有前后左右键,自己定义的容易按错。 3 两个红外测距传感器角度分的太开了,基本上没用处 3 不知是电机歪了还是轮子斜了,转起来就像是在跳舞 4 电线插进卡槽就拔不出来了,想改一下都困难,拿钳子改锥捅半天才拔出来。 总的来说:长的很漂亮,毛病一大堆。 听说都已经改好出新版的了,还给我们旧版的评测个毛啊 -------------------------------------------- 以上是吐槽,下面是小车总程序。说明: 1 遥控用的是自己做的手机蓝牙APP,没有用红外遥控。 2 D9D10冲突的问题没有改,D9没有PWM输出了反正就是HIGHT和LOW了,也能跑。 3 超声波另外接到D12D13了 4 能遥控着跑、跟随、避障、寻迹同时实现。
[mw_shl_code=cpp,true]#include <Servo.h>
Servo myServo;
const byte IR_trailing_left_pin = 4;
const byte IR_trailing_right_pin = 2;
const byte echo_pin = 12;
const byte trig_pin = 13;
const byte rangingLeft_pin = 7;
const byte rangingRight_pin = 8;
const byte serove_pin = 10;
const byte inPin_leftB = 6;
const byte inPin_leftA = 9;
const byte inPin_rightA = 3;
const byte inPin_rightB = 5;
int Command = 0;
uint8_t tempBuffer;
uint8_t readBuffer[4];
uint8_t num = 0;
void getCommand();
void goUp(int up_speed);
void goLeft(int left_speed);
void goRight(int right_speed);
void goBack(int back_speed);
void goStop();
int getDistance();
void control();
void trailing();
void follow();
void avoidance();
void setup()
{
pinMode(IR_trailing_left_pin,INPUT);
pinMode(IR_trailing_right_pin,INPUT);
pinMode(echo_pin,INPUT);
pinMode(trig_pin,OUTPUT);
pinMode(rangingLeft_pin,INPUT);
pinMode(rangingRight_pin,INPUT);
pinMode(inPin_leftA,OUTPUT);
pinMode(inPin_leftB,OUTPUT);
pinMode(inPin_rightA,OUTPUT);
pinMode(inPin_rightB,OUTPUT);
myServo.attach(serove_pin);
myServo.write(90);
delay(800);
Serial.begin(9600);
}
void loop()
{
Command = 0;
getCommand();
switch(Command)
{
case 0x5553:
goUp(160);
break;
case 0x4453:
goBack(100);
break;
case 0x4C53:
goLeft(100);
break;
case 0x5253:
goRight(100);
break;
case 0x5552:
goStop();
break;
case 0x4141:
control();
break;
case 0x4242:
trailing();
break;
case 0x4343:
follow();
break;
case 0x4444:
avoidance();
break;
}
// Serial.println(Command);
}
void getCommand()
{
if (Serial.available()){
tempBuffer = Serial.read();
readBuffer[num] = tempBuffer;
num++;
if (tempBuffer == 0x0D) {
Command = (readBuffer[0] << 8) | (readBuffer[1]);
num=0;
Serial.println(Command, HEX);
}
}
}
void goUp(int up_speed)
{
analogWrite(inPin_leftA,0);
analogWrite(inPin_rightA,0);
analogWrite(inPin_leftB,up_speed);
analogWrite(inPin_rightB,up_speed);
}
void goLeft(int left_speed)
{
analogWrite(inPin_leftA,left_speed);
analogWrite(inPin_rightB,left_speed);
analogWrite(inPin_leftB,255);
analogWrite(inPin_rightA,255);
}
void goRight(int right_speed)
{
analogWrite(inPin_leftB,right_speed);
analogWrite(inPin_rightA,right_speed);
analogWrite(inPin_leftA,255);
analogWrite(inPin_rightB,255);
}
void goBack(int back_speed)
{
analogWrite(inPin_leftB,back_speed);
analogWrite(inPin_rightB,back_speed);
analogWrite(inPin_leftA,255);
analogWrite(inPin_rightA,255);
}
void goStop()
{
analogWrite(inPin_leftA,0);
analogWrite(inPin_leftB,0);
analogWrite(inPin_rightA,0);
analogWrite(inPin_rightB,0);
}
int getDistance()
{
int dist;
digitalWrite(trig_pin,LOW);
delayMicroseconds(2);
digitalWrite(trig_pin,HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin,LOW);
dist=pulseIn(echo_pin,HIGH)/58;
return dist;
}
void control()
{
goStop();
delay(10);
}
void trailing()
{
Command = 0;
byte irLeft;
byte irRight;
while(Command == 0x4242 || Command == 0 || Command == 0x5353)
{
goStop();
irLeft = digitalRead(IR_trailing_left_pin);
irRight = digitalRead(IR_trailing_right_pin);
if(irLeft == 0 && irRight == 0)
{
goUp(100);
delay(100);
}
else if(irLeft == 0 && irRight == 1)
{
goRight(80);
delay(100);
}
else if(irLeft == 1 && irRight == 0)
{
goLeft(80);
delay(100);
}
else if(irLeft == 1 && irRight == 1)
{
int i = 0;
while(i<=1000 || irLeft == 1 || irRight == 1)
{
goRight(80);
irLeft = digitalRead(IR_trailing_left_pin);
irRight = digitalRead(IR_trailing_right_pin);
i++;
goStop();
}
}
goStop();
getCommand();
}
}
void follow()
{
getCommand();
Command = 0;
byte rangingA = digitalRead(rangingLeft_pin);
byte rangingB = digitalRead(rangingRight_pin);
int distan = getDistance();
while(Command == 0x4343 || Command == 0 || Command == 0x5353)
{
distan = getDistance();
rangingA = digitalRead(rangingLeft_pin);
rangingB = digitalRead(rangingRight_pin);
distan = getDistance();
if(distan >= 2 && distan <= 10 && rangingA == 0 && rangingB == 0)
{
goUp(100);
}
else if(distan >= 2 && distan <= 10 && rangingA == 1 && rangingB == 0)
{
goRight(100);
delay(100);
goStop();
}
else if(distan >= 2 && distan <= 10 && rangingA == 0 && rangingB == 1)
{
goLeft(100);
delay(100);
goStop();
}
getCommand();
}
}
void avoidance()
{
getCommand();
Command = 0;
int distan;
while(Command == 0x4444 || Command == 0 || Command == 0x5353)
{
myServo.write(90);
delay(800);
distan = getDistance();
if(distan > 20)
{
goUp(100);
}
else if(distan < 20)
{
myServo.write(10);
delay(800);
distan = getDistance();
if(distan > 20)
{
myServo.write(90);
delay(800);
goLeft(100);
}
else if(distan < 20)
{
myServo.write(170);
delay(800);
distan = getDistance();
if(distan > 20)
{
myServo.write(90);
delay(800);
goRight(100);
}
else
{
myServo.write(90);
delay(800);
int Time = 0;
while(distan < 20 || Time <= 1000)
{
distan = getDistance();
goRight(100);
Time++;
}
goStop();
}
}
}
getCommand();
}
}[/mw_shl_code]
|