The FollowMeBot follows the user based on the color of his or her shirt. The device hosts a webcam on a servo to find the object and orient the robot. The color is chosen through the user interface with push buttons. Currently, the MATLAB code supports red and green detection. The purpose of FollowMeBot is to be able to follow the user in order to be helpful for daily tasks.

Dependencies:   Rectangle Servo TextLCD mbed

Fork of FollowMeBot3 by Rahul Shetty

Revision:
8:7758b1db4d5b
Parent:
7:41b5307e4c03
Child:
9:72e899c35484
--- a/main.cpp	Fri Dec 06 19:06:50 2013 +0000
+++ b/main.cpp	Fri Dec 06 20:40:17 2013 +0000
@@ -5,10 +5,10 @@
 #include "TextLCD.h"
 
 // Macros/Constants
-#define MAX_VIEW_X 640 // maximum X value input from camera
-#define MAX_VIEW_Y 480 // maximum Y value input from camera
-//#define MAX_VIEW_X 1600 //maximum X value input from camera
-//#define MAX_VIEW_Y 1200 // maximum Y value input from camera
+//#define MAX_VIEW_X 640 // maximum X value input from camera
+//#define MAX_VIEW_Y 480 // maximum Y value input from camera
+#define MAX_VIEW_X 1600 //maximum X value input from camera
+#define MAX_VIEW_Y 1200 // maximum Y value input from camera
 #define CENTER_BOX_TOLLERANCE 100 // Size of our box
 #define TO_SERVO_DIVISOR 4000.0 // Value to divide by to get the amount to move the servo by
 #define NO_COLOR_MAX_COUNT 10
@@ -17,10 +17,13 @@
 #define SERVO_HOME .5
 #define SERVO_HOME_TOL .2
 #define SPEED_CONST 65535 // Used with the blob area to determine how fast the robot should move toward the target
+#define GREEN 0
+#define RED 1
 
 // Hardware sensors and devices
 DigitalOut myled(LED1);
 DigitalOut myled2(LED2);
+DigitalIn colorPB(p30);
 DigitalOut greenLED(p29);
 DigitalOut redLED(p28);
 iRobot followMeBot(p9, p10);
@@ -31,7 +34,6 @@
 //AnalogIn irSensorRight(p18);
 Serial pc(USBTX, USBRX); // tx, rx
 TextLCD lcd(p14, p16, p17, p18, p19, p20); // rs, e, d4-d7
-InterruptIn colorPB(p30);
 
 //float irVal = irSensorFront;
 
@@ -44,32 +46,25 @@
                     (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
 int noColorCounter; // Counts how long it has been since we have seen a color to follow
 bool colorLost = true; // boolean to represent if the color is confirmed "lost" (aka noColorCounter > NO_COLOR_MAX_COUNT)
-#define GREEN 0
-#define RED 1
-int color = GREEN; // Current color selected by the pushbutton
-int colorPast;
 
 // Function Prototypes
 void getXYpos();
 float moveCamera();
 void moveBot();
 int servoIsInHome();
-void colorSelect();
+void changeColor();
+char color = 0;
+int PBPast;
 
 int main()
 {
     followMeBot.start();
     servoHor = .5;
-    colorPast = color;
-    greenLED = 1;
-    colorPB.fall(&colorSelect);
+
     while(1) {
         getXYpos();
         moveBot();
-        if(colorPast != color) {
-            pc.printf("%d\n",color);
-            colorPast = color;
-        }
+        changeColor();
     }
 }
 
@@ -82,7 +77,7 @@
 {
     /*lcd.cls();
     lcd.locate(0,0);
-    lcd.printf("%d;%d;%f", xpos, ypos, servoHor.read());*/
+  lcd.printf("%d;%d;%f", xpos, ypos, servoHor.read());*/
     float temp = 0;
     if(xpos == 0) { // If we recieve a 0 for the location
         if(!colorLost && (++noColorCounter > NO_COLOR_MAX_COUNT)) { // Check to see if we have seen enough to consider the color lost
@@ -133,16 +128,16 @@
         myled = 0;
     }
 
-    /*  float irVal = irSensorFront;
-      lcd.locate(0,0);
-      lcd.printf("irVal: %f", irVal);
-      lcd.locate(0,1);
-      lcd.printf("blob: %d", blobArea);*/
+  /*  float irVal = irSensorFront;
+    lcd.locate(0,0);
+    lcd.printf("irVal: %f", irVal);
+    lcd.locate(0,1);
+    lcd.printf("blob: %d", blobArea);*/
 }
 
 void moveBot()
 {
-    // irVal = irSensorFront;                aaaaaaaaaaa
+   // irVal = irSensorFront;                aaaaaaaaaaa
 
     //
     // colorLost = false;
@@ -166,21 +161,22 @@
             followMeBot.left();
 
         } else if(servoIsInHome() == 0) {//n
-
+        
             //if (irVal < COLLISION_DIST) {
             if (blobArea < 180000) {              //just testing          aaaaaa
                 //lcd.cls();
                 //lcd.printf("forward: %f", irVal);
                 followMeBot.forward();
-            } else {
+            }
+            else {
                 followMeBot.stop();
                 lcd.cls();
                 lcd.locate(0,0);
                 lcd.printf("blobArea: %d ", blobArea);
                 //wait(1);
-                // lcd.printf("stop: %f ", irVal);
-            }
-
+               // lcd.printf("stop: %f ", irVal);
+            } 
+            
             /*
             //followMeBot.changeSpeed(SPEED_CONST/(blobArea/100));
             followMeBot.stop();
@@ -189,8 +185,8 @@
     } else {
         lcd.printf("Color Lost");
         followMeBot.left();
-        //  followMeBot.stop();//n
-        // followMeBot.right();
+      //  followMeBot.stop();//n
+       // followMeBot.right();
     }
 
 }
@@ -206,14 +202,18 @@
     }
 }
 
-void colorSelect()
-{
-    color = !color;
-    if(color == GREEN) {
-        greenLED = 1;
-        redLED = 0;
-    } else {
-        greenLED = 0;
-        redLED = 1;
+void changeColor(){
+    int PBval = colorPB;
+    if(PBval != PBPast && PBval == 1){
+        color = !color;
+        if(color == GREEN){
+            greenLED = 1;
+            redLED = 0;
+        }
+        else{
+            greenLED = 0;
+            redLED = 1;        
+        }
     }
+    PBPast = PBval;
 }