$USD
  • EUR€
  • £GBP
  • $USD
PROJECTS RoboticsArduino

How to Make a Motion Following Robot

DFRobot Aug 18 2015 549
Project by MagicByCalvin via Instructables.
 





After messing around with an Arduino for a couple months and avidly reading instructables, I decided that I would finally publish my own. I wanted to create something that moves by itself. I also wanted to create a system that reacts to the outside environment. After some thought I decided on a simple motion follower.

Potential Applications:


• Motion following camera
• Robots that will acknowledge their environments
• Motion following turret
• Educational projects
• Obstacle avoiding robots

Required Materials:


• Breadboard 
• Ultrasonic Range Sensor x2 (I used the four pin version so if you are using the ping version you'll have to change the code a bit) 
• Servo (I used a micro size) 
• Arduino UNO or similar microcontroller 
• Jumper Cables (female to male and male to male) 
• Particle Board 
• 9v Battery and connector 
• Something to mount the servo on (I used some poster mounting putty) 

Required Tools:


• Hot glue gun 
• Xacto knife 
• Computer with Arduino IDE 
• Electrical tape or similar

 

Step 1: Build the Sensor Mount






First, we will need to build the sensor mount. For this I built a prototype out of cardboard and then built my final project in particle board. In the future I plan to 3d print a perfect mount for the sensor.

Cut a rectangle that is about the same height as one of the sensors and an 1/8th inch (about 3mm) longer than two of them together. Set the rectangle aside and cut out two identical isosceles triangles with the odd angle out being 120 degrees. I cut it at 120 degrees because the range sensors have a 15 degree cone that they measure, this allows for no blind spots while optimizing the area that is sensed.

Finally, place the sensors on the mount to determine where to cut the rectangular holes for them to fit in. Make the hole as small as possible because this snug fit makes up for not using any other adhesives or connectors. Now cut another small rectangular hole on the bottom triangle of the mount to allow for jumper cables to be passed through. Once the jumper cables are plugged in, the sensors should stay in place. If they don't, you will need to fasten them with some hot glue or pins.

When you are done with the mount, attach it to the servo.


Step 2: Wire Everything!




Now all you need to do is connect everything! I used Fritzing to create a circuit diagram. I have also provided some pictures of the final product.

The code provided uses digital pins 9 through 13. Pin 9 is the data pin for the servo. Pins 10 and 11 are the echo and trig pins, respectively, of the left sensor. Pins 12 and 13 are the echo and trig pins, respectively, of the right sensor. I connected the 5v and gnd pins from the Arduino to the breadboard and then used jumpers to connect the servo and sensors.


Step 3: Code



The code that I provided allows for the changing of the distance threshold. So in other words, how far the sensors can see. I will be uploading a new instructable soon to show how you can make the sensors even more accurate using a temperature sensor. Finally, my friend showed me a cool way of debugging really quick. All you do is use a boolean and some if statements. If the boolean is true, then serial communication will be on and communicating. If the boolean is false, then the program will run much faster but not communicate.

I will attach the .ino file, otherwise you can copy it from here:

/*************************************************************************************************
**************************************************************************************************

Motion Follow Created by Calvin Kielas-Jensen

Using an Arduino UNO, check Instructables.com for the circuit diagram.

This script allows two ultrasonic range sensors to follow movement while mounted on the top of a servo. The distance threshold can be changed but should not be set too far as the sensors will begin to fail.

Anyone is welcome to use and modify this code as long as I am given credit. Thank you for respecting the open source movement!

************************************************************************************************** *************************************************************************************************/

#include

Servo myservo;

const int Lin = 10, Rin = 12, Lout = 11, Rout = 13, serv = 9; //setting sensor pins and servo pin

// establish variables for duration // and the distance result in inches long Rduration, Lduration, Rinches, Linches;

int threshold = 10; //Sensor threshold in inches

int angle = 80; //Initial angle

boolean debug = false; //Serial communication for debuging. Set to true for serial communication.

void setup() { // initialize serial communication: if (debug) { Serial.begin(9600); } myservo.attach(9); //attach servo to pin 9 }

void loop() { //Most of the sensor code has been taken from David Mellis's PING sensor code //I modified it for a 4 pin sensor as oppsed to the 3 pin sensor // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(Rout, OUTPUT); digitalWrite(Rout, LOW); delayMicroseconds(2); digitalWrite(Rout, HIGH); delayMicroseconds(5); digitalWrite(Rout, LOW);

Rduration = pulseIn(Rin, HIGH); pinMode(Lout, OUTPUT); digitalWrite(Lout, LOW); delayMicroseconds(2); digitalWrite(Lout, HIGH); delayMicroseconds(5); digitalWrite(Lout, LOW);

Lduration = pulseIn(Lin, HIGH);

// convert the time into a distance Rinches = microsecondsToInches(Rduration); Linches = microsecondsToInches(Lduration); if (debug) { Serial.print("Left: "); Serial.print(Linches); Serial.println(" in"); Serial.print("Right: "); Serial.print(Rinches); Serial.println(" in"); } follow(); }

long microsecondsToInches(long microseconds) {

// According to Parallax's datasheet for the PING))), there are

// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

// second). This gives the distance travelled by the ping, outbound

// and return, so we divide by 2 to get the distance of the obstacle.

// See: http://www.parallax.com/dl/docs/prod/acc/28015-PI...

return microseconds / 74 / 2; }

void follow() { if (Linches <= threshold || Rinches <= threshold) { if (Linches + 2 < Rinches) { angle = angle - 2; } if (Rinches + 2 < Linches) { angle = angle + 2; } } if (angle > 160) { angle = 160; } if (angle < 0) { angle = 0; } myservo.write(angle); }
 

Step 4: Conclusion




Once the code has been loaded, plug in the 9v battery and watch it start following anything that comes within the threshold range! 

Troubleshooting:

  • If the sensors keep turning the wrong way, try switching the cables in pins 10 and 11 with the cables in pins 12 and 13.
  • If the sensors don't move at all, or just a little bit, check the wiring. It is really easy to accidentally move the jumper cables over one spot on the breadboard.
  • If the sensors are moving really slowly, go back to the code and make sure that the debug is false. The serial communications can really slow down the reaction time of the robot.
  • If you are still having issues, first make sure that the Arduino is on and that all jumper cables are in the correct spots. Switch the debug to true and check to see if the range sensors are working. You might also want to test the servo to make sure that it is also in working condition.

Some improvements I am planning on for the future:

  • Improving the accuracy of the sensors with the use of a temperature sensor
  • 3d printing the sensor mount and servo mount
  • Adding another servo and sensor to allow for vertical movement
  • Switching out the servos with stepper motors to allow for 360 degree following
 
REVIEW