INTRODUCTION TO AI
N45 Avoidance Robot Design Document Nicola Gordon 11012129 1/25/2014
Contents Introduction ........................................................................................................................... 1 Goal ...................................................................................................................................... 2 Research .............................................................................................................................. 2 Project Solution – Block Diagram .......................................................................................... 3 Pseudo Code ........................................................................................................................ 4 Concept Art ........................................................................................................................... 5 Circuit Wiring Diagram .......................................................................................................... 5 Breadboard Diagram ............................................................................................................. 6 Testing the Servos ................................................................................................................ 7 our sensor ............................................................................................................................ 7 What is it? ......................................................................................................................... 7 Testing the Sensor ............................................................................................................ 8 Constructing a body .............................................................................................................. 9 Adding the Arduino Uno ........................................................................................................ 9 our project Testing all components together ......................................................................................... 11 Source Code ....................................................................................................................... 11 Goal achieved ..................................................................................................................... 11 Bibliography ........................................................................................................................ 12 Appendix ............................................................................................................................. 13 Appendix A: Research Slides .......................................................................................... 13 Appendix B: Ping Ultra Sonic Sensor data sheet ............................................................. 14 Appendix C: Final Source code ....................................................................................... 15
Introduction This design document has been taken and modified from the blog the author set up to document each stage of the build www.robotsbuiltbynikkig.wordpress.com. Further information including pictures and videos can be found at the above address
1|Page
Goal The goal for my project was to make a robot as cheap as possible to build and run, yet still capable of having its own personality. The robot will not be a remote-controlled bot – it will think for its self, trying to avoid obstacles and determine its own direction.
Research Initial research was conducted into obstacle avoidance robots and robotics in general. A copy of the research presentation slides that I presented is located in Appendix A.
2|Page
Project Solution – Block Diagram
Project Flow Chart
3|Page
Pseudo Code Set constants Right Forward at 0o Set constants Right Backward at 180o Set constants Left Forward same as Right Backward Set constants Left Backward same as Right Forward Set constants Right Motor at 90o Set constants Left Motor at 90o Set constants for ping))) Set threshold for obstacles (in cm) Set distances on either side Set constants for motors Set time it takes to receive PING))) signal setup() attach motors to pins 10 and 11
loop() Set constant for Forward Distance equals ping sensor If distance forward is more than the threshold Move Forward else If distance forward is blocked Turn off motors Delay for a few seconds to think Turn to the left and scan Turn to the right and scan Compare the distance compareDistance() if left Distance more than right Distance Turn left else if (right Distance less than leftDistance) Turn right else if they are equally obstructed turn 180 degrees long ping() Send out PING))) signal pulse Get duration it takes to receive echo Convert duration into distance
4|Page
Concept Art
Circuit Wiring Diagram
5|Page
Breadboard Diagram
6|Page
Testing the Servos Please visit blog for videos of testing of servos.
Ping))) Ultra-Sonic Sensor What is it? The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent range accuracy and stable readings in an easy-to-use package.
I ordered a 4 pin ultra-sonic sensor from china for only £1.00 including postage and only had to wait 21 days for it to arrive. Once I received I looked at the data sheet to work out what pins done what.
Using the HC-SR04 with an Arduino There is an Arduino library for the HC-SR04 that offers two ways to use the sensor, See Appendix B for the 3 Ping))) ultra-sonic data sheet. The library includes 3 functions that I will use: 1. Ultrasonic(int TP, int EP) This is an initial function for ultrasonic ranging module, choose the pins for module TRIG and ECHO pin. For example: Ultrasonic (13,12);
Defines the digital pin 13 of Arduino as the TRIG pin of HC-SR04 and pin 12 for the ECHO pin. 7|Page
2. long Timing() This function triggers the ultrasonic module and returns the duration that the ECHO pin was held high. For example: long time; Ultrasonic hcsr; time = hcsr.Timing();
The distance of the object corelates to the time the ECHO pin is held high. The distance formula is: Distance = ((Duration of high level)*(Sonic :340m/s))/2
3. long Ranging(int sys) — (sys : CM / INC) As I don’t want to change the time into distance, this function helps you get the distance immediately. This function has a parameter (using CM or ICN) that shows the distance in centimetres or inches. This function will call Timing() and you don’t need to use the Timing() before it. For example: long distance; Ultrasonic hcsr; distance = hcsr.Ranging(CM);
returns the distance in centimetres.
Testing the Sensor
8|Page
Constructing a body
Adding the Arduino Uno
Lucky Find – Old RC Car no Remote
9|Page
Motor Shield The motor shield can drive up to 4 DC motors bi-directionally. That means they can be driven forwards and backwards. The speed can also be varied at 0.5% increments using the high-quality built in PWM. This means the speed is very smooth and won't vary! Note that the H-bridge chip is not meant for driving loads over 0.6A or that peak over 1.2A so this is for small motors. Check the datasheet for information about the motor to verify its OK. To connect a motor, simply solder two wires to the terminals and then connect them to either theM1, M2, M3, or M4. Then follow these steps in your sketch Make sure you #include
Create the AF_DCMotor object with AF_DCMotor(motor#, frequency), to setup the motor H-bridge and latches. The constructor takes two arguments. The first is which port the motor is connected to, 1, 2, 3 or 4. frequency is how fast the speed controlling signal is. For motors 1 and 2 you can choose MOTOR12_64KHZ, MOTOR12_8KHZ, MOTOR12_2KHZ, orMOTOR12_1KHZ. A high speed like 64KHzwont be audible but a low speed like 1KHz will use less power. Motors 3 & 4 are only possible to run at 1KHz and will ignore any setting given Then you can set the speed of the motor using setSpeed(speed) where the speed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want. To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE. Of course, the Arduino doesn't actually know if the motor is 'forward' or 'backward', so if you want to change which way it thinks is forward; simply swap the two wires from the motor to the shield.
10 | P a g e
Modifying the RC The first thing I done was unconnected all wires, there was three wires in total, two for the motors and one for the antenna for the controller. As I did not have any controllers I just took it all apart and added in my own motors with the Arduino. Instead of throwing the old brains out and purchasing a battery compartment, I used the RC’s battery component to power the Arduino and motors.
Testing all components together Please visit blog to see all the testing videos.
Source Code The final source code can be found in appendix C.
P.I.M.P My Ride Once all the components were in place and working correct I purchased some spray paints and letter stencils and started to customised my robot.
Goal achieved Overall the goal I set myself at the start of the project has been 98% achieved, the only missing component was an on/off switch.
11 | P a g e
Bibliography Book: Karvinen, K, Karvien, T. 2011, Make: Arduino Bots and Gadgets, Maker Media, Inc, USA Websites: Arduino Playground http://playground.arduino.cc/ [Last accessed 03 January 2014] Hacking RC Car http://www.instructables.com/id/Autonomous-Control-of-RC-Car-UsingArduino/ [Last accessed 03 January 2014] Lady Ada http://www.ladyada.net/make/mshield/[Last accessed 13 January 2014] Lets make robots http://letsmakerobots.com/node/26905 [Last accessed 10 January 2014] Make ICT Workshop http://cratel.wichita.edu/blogs/eecsseniordesignspring2013fall2013/files/2013/02/ Hacking-a-RC-Car-(1).pdf [Last accessed 10 January 2014] Maker Shed http://www.makershed.com/Articles.asp?ID=263 [Last accessed 06 January 2014] Parallax http://www.parallax.com/sites/default/files/downloads/28015-PING-Sensor-ProductGuide-v2.0.pdf [Last accessed 13 January 2014] Robot Tutorials using the Arduino http://arthursrobotorial.blogspot.co.uk/ [Last accessed 13 November 2013]
12 | P a g e
Appendix Appendix A: Research Slides
13 | P a g e
Appendix B: Ping Ultra Sonic Sensor data sheet
14 | P a g e
Appendix C: Final Source code #include #define trigPin3 // Ping))) Sensor #define echoPin 2 // Ping))) Sensor AF_DCMotormotor1(1, MOTOR12_8KHZ); // create motor #1, 8KHz pwm AF_DCMotor motor3(3, MOTOR34_8KHZ); // create motor #2, 8KHz pwm void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); motor1.setSpeed(255); // set the speed to 200/255 motor3.setSpeed(255); // set the speed to 200/255 } intCheckDistance() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; return distance; } void MotorForward(intdelaytime) { motor1.run(FORWARD); motor3.run(FORWARD); delay(delaytime); } void MotorBackward(intdelaytime) { motor1.run(BACKWARD); motor3.run(BACKWARD); delay(delaytime); } void MotorRelease() { motor1.run(RELEASE); motor3.run(RELEASE); 15 | P a g e
delay(1000); } voidMotorLeft() { motor1.run(FORWARD); motor3.run(BACKWARD); delay(600); } voidMotorRight() { motor1.run(BACKWARD); motor3.run(FORWARD); delay(500); } void loop() { inttestDistance = CheckDistance(); Serial.print(testDistance); Serial.println(‖ test‖); if (testDistance>= 50|| testDistance<= 0){ Serial.println(―Out of range‖); //go forward and check range again MotorForward(700); MotorRelease(); } else { Serial.print(testDistance); Serial.println(‖ cm‖); //object in path, reverse and turn to avoid MotorBackward(600); MotorRelease(); MotorRight(); MotorRelease(); MotorForward(600); MotorRelease(); MotorLeft(); MotorRelease(); } delay(500); } 16 | P a g e