Magician Chassis: Obstacle Avoidance


MC w/ Obstacle AvoidanceAlright, first things first, pardon the ghetto build pictured to the left. I’m missing materials I thought I had and my solution was not quite done to perfection. That said, this is a good starting point for obstacle avoidance with any roaming robotic chassis and if your budget is very limited then this project is definitely for you. Listed below is the bill of materials and where I found the parts. Of course you can get an Arduino from just about anywhere nowadays, but my focus was for a inexpensive solution.

Before continuing further please note that the following libraries will be needed for this tutorial.

Motor Shield Library – DOWNLOAD
New Ping Library – DOWNLOAD

All the code for this project can be found HERE.

NOTE: If you desperately want to build this project now then the materials below are probably not for you. Delivery will be anywhere between 2-4 weeks because China is pretty far away.

Magician Chassis     – $14.95 – Sparkfun.com (shipping not included in price)
Arduino Uno (clone) – $14.78 – eBay (seller:besram-tech)
Motor Driver Shield  – $5.15   – eBay.com (seller:hittime_hk)
Ultrasonic Module    – $2.20   – eBay.com (seller:eorder0612)

The chassis also requires 4 AA batteries. There are several ways of connecting the ultrasonic module to the motor shield; breadboard, soldering etc. I don’t recommend connecting it the way I did. That will definitely change as soon as the parts come in. Anyways the important part is that you’ve connected it accordingly with the code. but I’m getting ahead of myself.

Before assembling everything make sure you download Adafruits motor shield library from github. If you’re asking yourself whether this library will work with a Chinese made shield out of the box the answer is yes. If you want to know more about the motor shield Adafruit has an easy to follow tutorial here.

Once you have the chassis assembled make sure you have the motors connected in the connectors pictured below. The left motor will be attached to M1 and the right motor is attached to M2.

Motor Wiring

 If at this point you want to test everything to ensure it’s working then plug in the code below.

// Motor Shield library file

#include 

AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
// the speed can be set anywhere between 200 and 255
motor1.setSpeed(255);
motor2.setSpeed(255);
}

void loop() {
// moves the motors forward. Other options include BACKWARD
// and RELEASE which stops the motors
motor1.run(FORWARD);
motor2.run(FORWARD);
}

ERROR ALERT: At this stage I encountered an issue. For whatever reason the left motor was operating at a higher speed than the right causing it to turn instead of moving straight. If you have this issue a quick fix is to simply adjust the speed for the left motor.

Now that the motors are working lets take a look at the ultrasonic range module.

NOTE: This is not the parallax ping module. The range finder used in this project has four pins instead of only three. The four pin module is said to work much better than the three pin ping module. Because of this the ping library packaged with the Arduino IDE will not work.

First thing to look for is where you’ll be wiring the ultrasonic module to on the motor shield. In one of the corners (depending on it’s orientation) there are three rows of through holes. One row is +5 below it is Gnd and below that is A0-A5.

Ping Wiring

 

As mentioned before the ultrasonic module has 4 pins; Vcc, Trig, Echo, Gnd. The ultrasonic module operates at 5v so attach Vcc on the module to any one of the holes on the +5 row and attach the Gnd to any one of the holes in the center row. Trig will attach to the A0 spot which is closest to the 9V through hole. Echo attaches right next to it in the A1 spot.Ping Module

Once everything is attached correctly plug in the code below and watch your robot avoid obstacles.

#include 
#include 

// Arduino pin tied to trigger pin on the ultrasonic sensor.
#define TRIGGER_PIN  A0  
// Arduino pin tied to echo pin on the ultrasonic sensor.
#define ECHO_PIN     A1  
// Maximum distance we want to ping for (in centimeters). 
// Maximum sensor distance is rated at 400-500cm.
#define MAX_DISTANCE 200 

// NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 

//speed variables for the left and right motor
int Lspeed = 255;
int Rspeed = 255;
// sets minimum distance to robot before avoiding the obstacle
int distance = 15;

//declaring the left and right motors and attaching them to 
//their corresponding connector on the motor shield
AF_DCMotor leftMotor(1, MOTOR12_64KHZ);
AF_DCMotor rightMotor(2, MOTOR12_64KHZ);

void setup() {
  //sets initial speed
  leftMotor.setSpeed(Lspeed);
  rightMotor.setSpeed(Rspeed);
}

void loop() {
  // Send ping, get ping time in microseconds (uS)
  unsigned int uS = sonar.ping(); 
  // Translates ping signal to centimeters for easy of use
  unsigned int CM = uS / US_ROUNDTRIP_CM; 

  // Checks whether the robot is 15 cm from an object and turns right
  // if the condition is true. The new ping library defaults to 0 at max
  // range this is why the condition includes CM > 0
  if (CM < distance && CM > 0) {
    int iteration = 0;
    while( iteration == 0){
      leftMotor.run(FORWARD);
      rightMotor.run(BACKWARD);
      iteration = 1;
    }
    // The rotation will continue for 600 milliseconds before starting the
    // next portion of the code
    delay(600);
  }

  //starts motors going forward
  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);
}

1 thought on “Magician Chassis: Obstacle Avoidance”

Leave a Reply