_SKU_ROB0117_切诺基基础套件 - jimaobian/DFRobotWikiCn GitHub Wiki

功能介绍

用超声波作为距离检测装置,舵机作为前方扫描器,实现一个可自动蔽障小车。

STEP1:组装小车

参照安装说明

注意事项: 按照切诺基的安装说明组装小车,完成到第3步的时候,找到套件中的Romeo BLE板子的固定孔,将Romeo BLE固定到小车底板上。如下图所示。

固定Romeo BLE

完成组装后,先不要急着把上面的扩展板固定上去,接下去我们进行第二步调试电机。

STEP2:调试电机

使用贴士 如您是第一次使用Arduino的新手。请先尝试走一遍“新手村”任务。

新手任务1:安装Arduino驱动,点击驱动安装教程

新手任务2:下载Blink代码,点击Blink代码下载

1. 连接电机控制引脚 在STEP1中,我们已经把四个电机(马达)固定到底板上了,想让RoMeo BLE控制小车,还需要连接到控制器上。

接线

warning_yellow.png
注意控制板上的D4、D5、D6、D7要与小车底盘上对应的四根线连接好,如示意图中四根线

2. 下载电机调试代码

既然您已经会下载代码了,请下载电机调试代码。

int speedPin_M1 = 5;     //M1 Speed Control
int speedPin_M2 = 6;     //M2 Speed Control
int directionPin_M1 = 4;     //M1 Direction Control
int directionPin_M2 = 7;     //M1 Direction Control

void setup(){
}

void loop(){
    carAdvance(100,100);
    delay(1000);
    carBack(100,100);
    delay(1000);
    carTurnLeft(250,250);
    delay(1000);
    carTurnRight(250,250);
    delay(1000);
}

void carStop(){                 //  Motor Stop
  digitalWrite(speedPin_M2,0);
  digitalWrite(directionPin_M2,LOW);
  digitalWrite(speedPin_M1,0);
  digitalWrite(directionPin_M1,LOW);
}

void carAdvance(int leftSpeed,int rightSpeed){         //Move FORkward
  analogWrite (speedPin_M2,leftSpeed);              //PWM Speed Control
  digitalWrite(directionPin_M2,LOW);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M1,HIGH);
}

void carBack(int leftSpeed,int rightSpeed){       //Move BACKrward
  analogWrite (speedPin_M2,leftSpeed);
  digitalWrite(directionPin_M2,HIGH);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M1,LOW);
}

void carTurnLeft(int leftSpeed,int rightSpeed){      //Turn Left
  analogWrite (speedPin_M2,leftSpeed);
  digitalWrite(directionPin_M2,HIGH);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M1,HIGH);
}
void carTurnRight(int leftSpeed,int rightSpeed){      //Turn Right
  analogWrite (speedPin_M2,leftSpeed);
  digitalWrite(directionPin_M2,LOW);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M1,LOW);
}

3. 装上电池

所需材料:DC母头 x 1 因为RoMeo BLE不带DC接口。

电源安装

上电后发现小车前进1秒后退1秒左转1秒右转1秒即为安装正确,如果发现效果与代码不匹配,可以对代码做一下微调整,根据电机的连线情况,直到匹配为止,才进行下一步。

STEP3:安装上层板

1. 准备材料 cherokey_RomeoBLE3.png

2. 固定超声波位置

可以参看超声波扫描套件的安装手册

3. 固定舵机位置 注意超声波的头要凸出到固定板外 cherokey_RomeoBLE4.png

cherokey_RomeoBLE5.png

STEP4: 调试超声波和舵机

1. 硬件连接 超声波、舵机控制

实物图

warning_yellow.png
安装超声波传感器和支架时,需要把超声波传感器按到底,探头需要露出。

2. 下载代码 下载代码之前需要安装Metro libray

如何加载库,可见链接

#include <Servo.h>
#include <Metro.h>
Metro measureDistance = Metro(50);
Metro sweepServo = Metro(20);

unsigned long actualDistance = 0;

Servo myservo;  // create servo object to control a servo
int pos = 60;
int sweepFlag = 1;


int URPWM = 3; // PWM Output 0-25000US,Every 50US represent 1cm
int URTRIG= 10; // PWM trigger pin
uint8_t EnPwmCmd[4]={0x44,0x02,0xbb,0x01};    // distance measure command

void setup(){                                 // Serial initialization
  myservo.attach(9);
  Serial.begin(9600);                         // Sets the baud rate to 9600
  SensorSetup();
}

void loop(){
 if(measureDistance.check() == 1){
      actualDistance = MeasureDistance();
//      Serial.println(actualDistance);
//      delay(100);
 }

 if(sweepServo.check() == 1){
      servoSweep();
 }

}

void SensorSetup(){
  pinMode(URTRIG,OUTPUT);                     // A low pull on pin COMP/TRIG
  digitalWrite(URTRIG,HIGH);                  // Set to HIGH
  pinMode(URPWM, INPUT);                      // Sending Enable PWM mode command
  for(int i=0;i<4;i++){
      Serial.write(EnPwmCmd[i]);
   }
}

int MeasureDistance(){        // a low pull on pin COMP/TRIG  triggering a sensor reading
    digitalWrite(URTRIG, LOW);
    digitalWrite(URTRIG, HIGH);               // reading Pin PWM will output pulses
    unsigned long distance=pulseIn(URPWM,LOW);
    if(distance==50000){              // the reading is invalid.
      Serial.print("Invalid");
    }else{
      distance=distance/50;           // every 50us low level stands for 1cm
    }
    return distance;
}

void servoSweep(){
  if(sweepFlag ){
     if(pos>=60 && pos<=120){
        pos=pos+1;                                  // in steps of 1 degree
        myservo.write(pos);                         // tell servo to go to position in variable 'pos'
    }
      if(pos>119)  sweepFlag = false;                       // assign the variable again
  }else {
      if(pos>=60 && pos<=120){
        pos=pos-1;
        myservo.write(pos);
      }
      if(pos<61)  sweepFlag = true;
   }
}

3. 调试舵机位置 方法一:可重新安装舵盘 方法二:代码中角度做相应修改

STEP5: 整机调试

1. 固定上层板 固定上层板

2.下载整机调试代码

#include <Servo.h>
#include <Metro.h>
Metro measureDistance = Metro(50);
Metro sweepServo = Metro(20);

int speedPin_M1 = 5;     //M1 Speed Control
int speedPin_M2 = 6;     //M2 Speed Control
int directionPin_M1 = 4;     //M1 Direction Control
int directionPin_M2 = 7;     //M1 Direction Control
unsigned long actualDistance = 40;

Servo myservo;  // create servo object to control a servo
int pos = 60;
int sweepFlag = 1;

int URPWM = 3; // PWM Output 0-25000US,Every 50US represent 1cm
int URTRIG= 10; // PWM trigger pin
uint8_t EnPwmCmd[4]={0x44,0x02,0xbb,0x01};    // distance measure command

void setup(){                                 // Serial initialization
  myservo.attach(9);
  Serial.begin(9600);                         // Sets the baud rate to 9600
  SensorSetup();
}

void loop(){

 if(measureDistance.check() == 1){
      actualDistance = MeasureDistance();
//      Serial.println(actualDistance);
//      delay(100);
 }

 if(sweepServo.check() == 1){
      servoSweep();
 }

 if(actualDistance <= 30){
            myservo.write(90);
            if(pos>=90){
                  carBack(150,150);
//                  Serial.println("carBack");
                  delay(300);
                  carTurnRight(150,150);
//                  Serial.println("carTurnRight");
                  delay(800);
               }else{
                   carBack(150,150);
//                   Serial.println("carBack");
                   delay(300);
                   carTurnLeft(150,150);
//                   Serial.println("carTurnLeft");
                   delay(800);
               }
    }else{
                   carAdvance(150,150);
//                   Serial.println("carAdvance");
                   delay(300);
     }
}

void SensorSetup(){
  pinMode(URTRIG,OUTPUT);                     // A low pull on pin COMP/TRIG
  digitalWrite(URTRIG,HIGH);                  // Set to HIGH
  pinMode(URPWM, INPUT);                      // Sending Enable PWM mode command
  for(int i=0;i<4;i++){
      Serial.write(EnPwmCmd[i]);
   }
}

int MeasureDistance(){  // a low pull on pin COMP/TRIG  triggering a sensor reading
    digitalWrite(URTRIG, LOW);
    digitalWrite(URTRIG, HIGH);               // reading Pin PWM will output pulses
    unsigned long distance=pulseIn(URPWM,LOW);
    if(distance==50000){              // the reading is invalid.
      Serial.print("Invalid");
    }else{
      distance=distance/50;           // every 50us low level stands for 1cm
    }
    return distance;
}

void carStop(){                 //  Motor Stop
  digitalWrite(speedPin_M2,0);
  digitalWrite(directionPin_M2,LOW);
  digitalWrite(speedPin_M1,0);
  digitalWrite(directionPin_M1,LOW);
}

void carAdvance(int leftSpeed,int rightSpeed){         //Move FORkward
  analogWrite (speedPin_M2,leftSpeed);              //PWM Speed Control
  digitalWrite(directionPin_M2,LOW);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M1,HIGH);
}

void carBack(int leftSpeed,int rightSpeed){       //Move BACKrward
  analogWrite (speedPin_M2,leftSpeed);
  digitalWrite(directionPin_M2,HIGH);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M1,LOW);
}

void carTurnLeft(int leftSpeed,int rightSpeed){      //Turn Left
  analogWrite (speedPin_M2,leftSpeed);
  digitalWrite(directionPin_M2,HIGH);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M1,HIGH);
}
void carTurnRight(int leftSpeed,int rightSpeed){      //Turn Right
  analogWrite (speedPin_M2,leftSpeed);
  digitalWrite(directionPin_M2,LOW);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M1,LOW);
}

void servoSweep(){
  if(sweepFlag){
     if(pos>=60 && pos<=120){
        pos=pos+5;                                  // in steps of 1 degree
        myservo.write(pos);                         // tell servo to go to position in variable 'pos'
    }
      if(pos>119)  sweepFlag = false;                       // assign the variable again
  }
   else {
      if(pos>=60 && pos<=120){
        pos=pos-5;
        myservo.write(pos);
      }
      if(pos<61)  sweepFlag = true;
    }
}

你的专属小车就此诞生!

常见问题

Q:小车装好之后不动 A:检查电源接线是否正确,电池需要为新电池,主控板中需要烧录整机调试代码.

Q:小车装好之后前面没障碍物却一直后退 A:检查超声波的接线,以及超声波模块安装是否到位(参考4.3小节的第一图)

更多问题欢迎通过qq或者论坛联系我们!

| 更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。 |

安卓版蓝牙控制软件goBle

DFshopping_car1.png DFRobot商城购买链接

⚠️ **GitHub.com Fallback** ⚠️