Follow me bot application!

Dependencies:   Rectangle Servo TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "iRobot.h"
00003 #include "Servo.h"
00004 #include "Rectangle.h"
00005 #include "TextLCD.h"
00006 
00007 // Macros/Constants
00008 #define MAX_VIEW_X 1600 // maximum X value input from camera
00009 #define MAX_VIEW_Y 1200 // maximum Y value input from camera
00010 #define CENTER_BOX_TOLLERANCE 200 // Size of our box
00011 #define TO_SERVO_DIVISOR 3000.0 // Value to divide by to get the amount to move the servo by
00012 #define NO_COLOR_MAX_COUNT 10
00013 #define COLLISION_DIST .4
00014 #define BLOB_CLOSE_DIST 200
00015 #define SERVO_HOME .5
00016 #define SERVO_HOME_TOL .2
00017 #define SPEED_CONST 65535
00018 
00019 // Hardware sensors and devices
00020 DigitalOut myled(LED1);
00021 DigitalOut myled2(LED2);
00022 iRobot followMeBot(p9, p10);
00023 Servo servoHor(p22);
00024 Servo servoVer(p21);
00025 AnalogIn irSensorFront(p15);
00026 //AnalogIn irSensorLeft(p19);
00027 //AnalogIn irSensorRight(p18);
00028 Serial pc(USBTX, USBRX); // tx, rx
00029 TextLCD lcd(p14, p16, p17, p18, p19, p20); // rs, e, d4-d7
00030 
00031 // Software variables
00032 char serial_rx_buffer[256]; // Input buffer for data from the PC
00033 int xpos, ypos, blobArea; // x and y positions read from matlab
00034 Rectangle centerBox((MAX_VIEW_X/2)-CENTER_BOX_TOLLERANCE, (MAX_VIEW_Y/2)-CENTER_BOX_TOLLERANCE,
00035                     (MAX_VIEW_X/2)+CENTER_BOX_TOLLERANCE,(MAX_VIEW_Y/2)+CENTER_BOX_TOLLERANCE); // Creates a box to examine if the camera is well enough centered
00036 int noColorCounter; // Counts how long it has been since we have seen a color to follow
00037 bool colorLost; // boolean to represent if the color is confirmed "lost" (aka noColorCounter > NO_COLOR_MAX_COUNT)
00038 
00039 // Function Prototypes
00040 void getXYpos();
00041 float moveCamera();
00042 void moveBot();
00043 int servoIsInHome();
00044 
00045 int main()
00046 {
00047     //followMeBot.start();
00048     //wait(1);
00049     while(1) {
00050         getXYpos();
00051         moveBot();
00052     }
00053 }
00054 
00055 /**
00056 *   Moves the servo to move the camera based upon where the
00057 *   color is located on the reported x and y
00058 *
00059 */
00060 float moveCamera()
00061 {
00062     float temp = 0;
00063     if(xpos == 0) { // If we recieve a 0 for the location
00064         if(!colorLost && (++noColorCounter > NO_COLOR_MAX_COUNT)) { // Check to see if we have seen enough to consider the color lost
00065             servoHor = .5; // If the color is lost, return servo to home
00066             colorLost = true; // Set colorLost to true
00067             noColorCounter = 0; // Reset counter
00068         }
00069     } else if(!centerBox.is_touch(xpos, (MAX_VIEW_Y/2))) { // If we have a location
00070         noColorCounter = 0; // Reset counter
00071         colorLost = false; // We have found the color!
00072         temp = servoHor.read() - (centerBox.getCenterX() - xpos)/TO_SERVO_DIVISOR; // Calculate location to move servo to
00073         if(temp > 0 && temp <= 1) { // If the value is within the servo range
00074             servoHor = temp; // Set the servo equal to the position
00075             //sprintf(serial_rx_buffer, "%f\n", temp);
00076         }
00077         /*temp = servoVer.read() + ((float)centerBox.getCenterY() - (float)ypos)/TO_SERVO_DIVISOR;
00078         if(temp > 0 && temp <= 1) {
00079             servoVer = temp;
00080         }*/
00081     }
00082     //pc.puts(serial_rx_buffer);
00083     return temp; // return the servo position
00084 }
00085 
00086 // Will return the number of degrees to turn the irobot by
00087 void getXYpos()
00088 {
00089     char * temp;
00090     const char del = ';';
00091     if(pc.readable()) { // See if matlab has data for us
00092         myled = 1;
00093         pc.gets(serial_rx_buffer, 256); // Get position data
00094         pc.puts(serial_rx_buffer);
00095         temp = strtok(serial_rx_buffer, &del);
00096         xpos = atoi(temp); // Convert data to xposition int
00097         temp = strtok(NULL, &del);
00098         ypos = atoi(temp);
00099         temp = strtok(NULL, &del);
00100         blobArea = atoi(temp);
00101         lcd.cls();
00102         lcd.locate(0,0);
00103         lcd.printf("%d;%d", xpos, ypos);
00104         moveCamera(); // Move the camera
00105     } else {
00106         myled = 0;
00107     }
00108 }
00109 
00110 void moveBot()
00111 {
00112     float irVal = irSensorFront;
00113     lcd.locate(0,1);
00114     if(!colorLost) {
00115         if(irVal > COLLISION_DIST && blobArea > BLOB_CLOSE_DIST) {
00116             //followMeBot.stop();
00117             lcd.printf("stop");
00118         } else if(servoIsInHome() > 0) {
00119             //followMeBot.right();
00120             lcd.printf("right");
00121         } else if(servoIsInHome() < 0) {
00122             //followMeBot.left();
00123             lcd.printf("left");
00124         } else {
00125             //followMeBot.changeSpeed(SPEED_CONST/(blobArea/100));
00126             //followMeBot.forward();
00127             lcd.printf("for sp: %d", (SPEED_CONST/(blobArea/100)));
00128         }
00129     }
00130     else{
00131         lcd.printf("Color Lost");
00132     }
00133 }
00134 
00135 int servoIsInHome()
00136 {
00137     if(servoHor.read() > SERVO_HOME + SERVO_HOME_TOL) {
00138         return 1;
00139     } else if(servoHor.read() < SERVO_HOME - SERVO_HOME_TOL) {
00140         return -1;
00141     } else {
00142         return 0;
00143     }
00144 }