vickywong 发表于 2016-11-19 20:35

[*Altar小车动起来*]5. 跟随

本帖最后由 vickywong 于 2016-12-10 12:00 编辑

    小车跟随功能是跟着一个物体一直走,物体转弯时小车也跟着转弯。用超声波传感器检测前面物体的距离,在一定距离范围内就向前走,转弯是用两边的2个红外测距传感器,发现一边距离大了就向相反的方向转弯。有一个大问题:这个Altar小车的扩展板上,超声波传感器用的是D7、D8引脚,两个红外测距传感器也是D7、D8引脚,别的孔插不进去!这是小车设计的错误,只好把超声波传感器的线拔了,echo、trig插2根公公头杜邦线到D12、D13,Vcc、GND插2根公母头杜邦线到原来的Vcc和GND。注意:现在这个程序是没有遥控的,就会跟着走。
const byte echo_pin = 12;
const byte trig_pin = 13;
const byte rangingLeft_pin = 7;
const byte rangingRight_pin = 8;
const byte inPin_leftB = 6;
const byte inPin_leftA = 9;
const byte inPin_rightA = 3;
const byte inPin_rightB = 5;

int distan;
byte rangingA;
byte rangingB;

void setup() {
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);
}
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 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);
}
void loop(){
distan = getDistance();
rangingA = digitalRead(rangingLeft_pin);
rangingB = digitalRead(rangingRight_pin);
    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();
    }
    else
    {
      goStop();
    }
}

vickywong 发表于 2016-11-19 20:42

避障功能和跟随功能基本是一回事,原理差不多。吴老师已经写过避障了,这里就不写避障了。
还有个问题就是避障最好用舵机,结果舵机接到D10,用了舵机库,影响了电机D9的PWM输出。不好搞,还是这个小车扩展板设计的错误问题!

6464 发表于 2016-11-20 09:54

楼主可以上视频吗

vickywong 发表于 2016-11-20 10:06

6464 发表于 2016-11-20 09:54
楼主可以上视频吗

就跟原来蓝宙的视频差不多的
http://v.youku.com/v_show/id_XMTcyMzUyODMxMg==.html

byran620 发表于 2016-12-9 19:32

樓主,提否把以上的編程複製,然後上傳到ARDUINOUNO 板裡,開著車子就可跟隨?   

byran620 发表于 2016-12-9 19:34

,複製了,但運得時出現多個ERROR,是否我複製錯了??

vickywong 发表于 2016-12-9 19:58

byran620 发表于 2016-12-9 19:34
,複製了,但運得時出現多個ERROR,是否我複製錯了??

前边不要复制,这不是程序里的。另外,这个程序你不一定能直接用,需要一样的电机驱动、一样的接线才行。

byran620 发表于 2016-12-10 07:57

const byte echo_pin = 12;
const byte trig_pin = 13;
const byte rangingLeft_pin = 7;
const byte rangingRight_pin = 8;
const byte inPin_leftB = 6;
const byte inPin_leftA = 9;
const byte inPin_rightA = 3;
const byte inPin_rightB = 5;

int distan;
byte rangingA;
byte rangingB;

void setup() {
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);
}
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 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);
}
void loop(){
distan = getDistance();
rangingA = digitalRead(rangingLeft_pin);
rangingB = digitalRead(rangingRight_pin);
    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();
    }
}

是的,我复制了,开著車子,它只在原地轉,可能是你所說的PIN 位不對,這个程序你是否用Altar小车原裝电机驱动 及 馬達測試?   因為之前聽你說過有些pin 位 有衝突, 因此現在有些PIN位都改了,超聲波接D12 & D13 ,舵機接D3, 紅外線循跡接D2 & D4, 左車頭的紅外線感應接A0, 右車頭的紅外線感應接D8, 紅外線接收模塊D11, 电机驱动模塊接D5,D6,D9,D10,這樣有問題嗎? 請師兄指點小弟。

vickywong 发表于 2016-12-10 11:48

byran620 发表于 2016-12-10 07:57
const byte echo_pin = 12;
const byte trig_pin = 13;
const byte rangingLeft_pin = 7;


我上传调试了一下,程序我少写了一部分,改成这样:
const byte echo_pin = 12;
const byte trig_pin = 13;
const byte rangingLeft_pin = 7;
const byte rangingRight_pin = 8;
const byte inPin_leftB = 6;
const byte inPin_leftA = 9;
const byte inPin_rightA = 3;
const byte inPin_rightB = 5;

int distan;
byte rangingA;
byte rangingB;

void setup() {
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);
}
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 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);
}
void loop(){
distan = getDistance();
rangingA = digitalRead(rangingLeft_pin);
rangingB = digitalRead(rangingRight_pin);
    if(distan >= 2 && distan <= 10 && rangingA == 0 && rangingB == 0)
    {
      goUp(100);
    }
    else if(distan >= 2 && distan <= 10 && rangingA == 0 && rangingB == 1)
    {
      goRight(100);
      delay(100);
      goStop();
    }
    else if(distan >= 2 && distan <= 10 && rangingA == 1 && rangingB == 0)
    {
      goLeft(100);
      delay(100);
      goStop();
    }
    else
    {
      goStop();
    }
}


vickywong 发表于 2016-12-10 11:53

byran620 发表于 2016-12-10 07:57
const byte echo_pin = 12;
const byte trig_pin = 13;
const byte rangingLeft_pin = 7;


如果按你的接线,应该把前面的接线改成这样:
const byte echo_pin = 12;
const byte trig_pin = 13;
const byte rangingLeft_pin = A0;
const byte rangingRight_pin = 8;
const byte inPin_leftB = 5;
const byte inPin_leftA = 6;
const byte inPin_rightA = 9;
const byte inPin_rightB = 10;
页: [1] 2
查看完整版本: [*Altar小车动起来*]5. 跟随