My First ever Robot. It tracks objects using Infra-red sensors, and has basic AI that follows and avoid objects.
I will describe the how I made this robot, and upload some videos.
Version 1 (16 Dec 2011):
Version 2 (20 Dec 2011):
—————–The components used ———————-
I actually planned this robot before making
it. I didn’t use all the parts expected. here https://oscarliang.com/ir-…
I only used these compenents:
Arduino
2 x Servo
IR detector
Motor Driver
2 x Motors
4 x AA battery
2 x Wheels
some capacitors/resistors
———————- How did you implement the code ———————-
I divided the robot into 3 functional parts,
write the code and tested each part separately, and finally put them together
and integrate them to work together. For each part see:
———————- Explaining to me the whole idea of this robot ———————-
You should see what this robot can do from the videos. It’s
very simple, and it’s very responsive to interactions, therefore making it very
fun to play with (my friends love it!
18/12/2011
This is the detailed structure:
So this is the final result:
|
Summary
To Make Wally a better robot, I will do some modifications to it. Let’s get started.
1. My styrene plastic sheets finally arrived. So I re-built my tracking head!
IR Sensor h file
Oscar’s projectIR 4 position sensor12/12/2011 decided add in servo control (from attach to actual control)11/12/2011 this library is used to control a 4 position (up down left right) IR sensor, which contains both emitters and detectors.*/#ifndef WPROGRAM_H #define WPROGRAM_H #include “WProgram.h” //standard types and constants of the Arduino language #endif class IRSensor { private: // Pins byte leftIRPin; // IR values public: int leftValue; int distance; // from 0 – 1000, the larger the closer public: IRSensor(byte _irLEDPin, byte _leftIRPin, byte _rightIRPin, byte _upIRPin, byte _downIRPin); }; |
/* Oscar’s project IR 4 position sensor this library is used to control a 4 position (up down left right) IR sensor, */ #include “IRSensor.h” IRSensor::IRSensor(byte _irLEDPin, byte _leftIRPin, byte _rightIRPin, byte _upIRPin, byte _downIRPin) { // setup all parameters irLEDPin = _irLEDPin; calON_LEDPin = 13; pinMode(irLEDPin, OUTPUT); } void IRSensor::ReadIR(){ digitalWrite(irLEDPin, HIGH); // total values digitalWrite(irLEDPin, LOW); leftValueBG = analogRead(leftIRPin); leftValue = leftValue – leftValueBG; distance = (leftValue + rightValue + upValue +downValue)/4; } |
/*Oscar’s projectMotor Driver control LibraryThis is used to simply motor operation, so easy commands such as ‘forward, turn right’ etc can be used to instruct robot what to do12/12/2011 added basic instructions (directions), with duration as inputs*/ #ifndef WPROGRAM_H class MotorControl { private: public: void Forward(); void Backward(); void TurnLeft(); void TurnRight(); void TurnLeftDish(); void TurnRightDish(); void Stop(); }; |
#include “MotorControl.h”MotorControl::MotorControl(){lcPin1 = 2; lcPin2 = 3; rcPin1 = 4; rcPin2 = 5;pinMode(lcPin1, OUTPUT); pinMode(lcPin2, OUTPUT); pinMode(rcPin1, OUTPUT); pinMode(rcPin2, OUTPUT);} MotorControl::MotorControl(byte _lcPin1, byte _lcPin2, byte _rcPin1, byte _rcPin2){lcPin1 = _lcPin1; lcPin2 = _lcPin2; rcPin1 = _rcPin1; rcPin2 = _rcPin2; pinMode(lcPin1, OUTPUT); } void MotorControl::Forward(){ void MotorControl::Backward(){ void MotorControl::TurnLeft(){ void MotorControl::TurnRight(){ void MotorControl::TurnLeftDish(){ void MotorControl::TurnRightDish(){ void MotorControl::Stop(){ void MotorControl::Brake(){ |
/*Oscar’s IR Robot V2 18 Dec 2011This is an open source project feel free to modify and distribute*/#ifndef SERVO_H #define SERVO_H #include #endif#include “IRSensor.h” #include “MotorControl.h” IRSensor sensor(8,A0,A1,A2,A3); // modes // Servos byte lrServirLEDPin = 2; byte followLEDPin = 13; int lrServoPos = 1500; int lrServoPosMin = 600; int lrServoPosMax = 2400; int lrServoPosMid = (lrServoPosMin + lrServoPosMax) / 2; boolean assending = true; // ============ Performance Parameters ============ const int distanceMin = 230; const int distanceClosest = 650; // ============= End Of Parameters ============ void setup() pinMode(followLEDPin, OUTPUT); lrServo.attach(lrServirLEDPin); delay(1000); //Serial.begin(9600); } void loop() if (mode == FOLLOWOBJECT){ FollowObject(); // ========= turn body if head turns too much ===== if (lrServoPos < 1100) motors.TurnLeftDish(); // ========= go forward or backward depends on ===== else { else if (mode == AVOIDOBJECT){ AvoidObject(); } } // =============== Object tracking ============== void FollowObject(){ sensor.ReadIR(); // when Object is detected if (sensor.distance > distanceMin){ if ((sensor.rightValue – sensor.leftValue) > minDif) if ((sensor.upValue – sensor.downValue) > minDif) } // when Object is NOT detected else { // horizontal servo move toward centre // vertical servo move toward centre } lrServoPos = constrain(lrServoPos,600,2300); lrServo.writeMicroseconds(lrServoPos); delay(20); } // =============== Object Avoiding ============== void AvoidObject(){ boolean detectedLeft = false; sensor.ReadIR(); // object not detected motors.Forward(); } // object detected // check where the obstacle is // if it’s too large motors.Stop(); // stop movement to allow analysis // look around, analyse location of obstacles // check right side // check left side // reset head position // if obstacle is too big, back off and turn around void TurnHead(Servo &servo, int X, int Y){ int temp = servo.read(); if (temp > X){ for (temp; temp>X; temp=temp–30){ } for (temp; temp<X; temp=temp+30){ } } |