Wifi Controlled Robot is done as a mini project (Lab4) for ECE 4180 class (Georgia Tech). Robot is assembled in Sparkfun's Shadow chasis robot kit. This robot takes the command from the webpage to move forward, reverse without colliding. The distance sensor is used as a mechanism for collision detection and play a short beep sound. It turn 90 degree with magnetometer when the turn command is sent.

Dependencies:   Motordriver mbed

/media/uploads/pkoirala3/capture.jpg Showing the setup and calibration: Part1 In this part the Wifi setups and attains an IP address so we can control it through a webpage on our phone/PC. After it obtains an IP address it calibrates the IMU so we can have accurate compass headings for precise turns.

The parts that we use are as follows:

  • Mbed
  • 2 DC motors
  • H-Bridge (to drive the DC motors)
  • Speaker
  • Class D Amp (to drive the Speaker)
  • IMU (use the magnometer for compass headings)
  • Wifi card (ESP8266)

Showing webpage functionality and Robot Functionality: In this part we demonstrate the functionality of the robot and the webpage to control it.

Final Code

[Repository '/teams/Prana-Koirala/code/Wifi_controlled_Robot_ECE4180/docs/tip/main_8cpp_source.html' not found]

Revision:
4:166570fa7fda
Parent:
3:fff9904d2ecb
Child:
5:e9f1030a5bbb
--- a/main.cpp	Mon Mar 13 05:23:32 2017 +0000
+++ b/main.cpp	Mon Mar 13 17:53:47 2017 +0000
@@ -1,7 +1,7 @@
 #include "mbed.h"
 #include "motordriver.h"
-#include "ultrasonic.h"
 #include "LSM9DS1.h"
+#include "Speaker.h"
 #define PI 3.14159
 #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA.
 
@@ -14,13 +14,15 @@
 Serial pc(USBTX, USBRX);
 Serial esp(p28, p27); // tx, rx
 
-PwmOut mySpeaker(p24);
+Speaker mySpeaker(p24);
 
 // LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
 
 AnalogIn DistSensorFwd(p19);
 AnalogIn DistSensorRev(p20);
 
+BusOut myleds(LED1, LED2, LED3, LED4);
+
 // variables for sending/receiving data over serial
 volatile int tx_in=0;
 volatile int tx_out=0;
@@ -47,7 +49,7 @@
 int SERVtimeout = 5;    // set server timeout in seconds in case link breaks.
 
 int main()
-{
+{   
     pc.baud(9600);
     esp.baud(9600);
     esp.attach(&Rx_interrupt, Serial::RxIrq); //serial interrupt to receive data
@@ -56,15 +58,15 @@
     wait(0.01);
     DataRX=0;
 
-    mySpeaker.period(1.0/200.0); // 500hz period
+// Initially stop the robot 
     A.stop(0.0);
     B.stop(0.0);
-    /*
-        IMU.begin();
-        IMU.calibrate(1);
-        IMU.calibrateMag(0);
-        pc.printf("Final Calibration done, prana");
-    */
+/*
+    IMU.begin();
+    IMU.calibrate(1);
+    IMU.calibrateMag(0);
+    pc.printf("Final Calibration done, Ready to move...\r\n");
+*/
     while(1) {
         if(DataRX==1) {
             ReadWebData();
@@ -76,8 +78,8 @@
         char temp_buff[50];
         if(abs(stateA) > 1  && abs(stateB) > 1) strcpy(temp_buff, "Robot Stopped!");
         else if((stateA > 0 && stateA <= 1)&&(stateB > 0 && stateB <= 1)) { // obstacle within 10 cm and robot is still moving
-            float Dist = DistSensorFwd;
-            ObjDetect(Dist);
+            // float Dist = DistSensorFwd;
+            // ObjDetect(Dist);
             strcpy(temp_buff, "Robot Moving Forward!");
         } else if((stateA < 0 && stateA >= -1)&&(stateB < 0 && stateB >= -1)) { // If moving rev
             float Dist = DistSensorRev;
@@ -111,7 +113,7 @@
     rx_in = 0;
     rx_out = 0;
     // check web data for form information
-    if( strstr(webdata, "check=BotFwd") != NULL ) {
+    if( strstr(webdata, "select=BotFwd") != NULL ) {
         if(abs(A.state()) <= 1) A.stop(0.0);
         if(abs(B.state()) <= 1) B.stop(0.0);
         A.speed(0.75);
@@ -119,7 +121,7 @@
         ledRev = 0;
         ledFwd = 1;
     }
-    if( strstr(webdata, "check=BotRev") != NULL ) {
+    if( strstr(webdata, "select=BotRev") != NULL ) {
         if(abs(A.state()) <= 1) A.stop(0.0);
         if(abs(B.state()) <= 1) B.stop(0.0);
         A.speed(-0.75);
@@ -127,24 +129,26 @@
         ledFwd = 0;
         ledRev = 1;
     }
-    if( strstr(webdata, "check=BotStop") != NULL ) {
+    if( strstr(webdata, "select=BotStop") != NULL ) {
         A.stop(0.0);
         B.stop(0.0);
         ledFwd = 0;
         ledRev = 0;
     }
-    if( strstr(webdata, "check=BotLeft") != NULL ) {
-        B.stop(0.0);
-        A.speed(0.75);
+    if( strstr(webdata, "select=BotLeft") != NULL ) {
+        A.speed(0.5);
+        B.stop(-0.5);
         // ImuCheck();
         wait(1.0);
         A.stop(0.0);
+        B.stop(0.0);
     }
-    if( strstr(webdata, "check=BotRight") != NULL ) {
-        A.stop(0.0);
+    if( strstr(webdata, "select=BotRight") != NULL ) {
+        A.speed(-0.5);
         B.speed(0.5);
         // ImuCheck();
         wait(1.0);
+        A.stop(0.0);
         B.stop(0.0);
     }
     if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
@@ -206,23 +210,23 @@
     SendCMD();
     getreply();
     wait(0.3);
-    strcpy(cmdbuff, "conn:send('<p><input type=\"radio\" name=\"check\" value=\"BotFwd\"> Move Robot Forward')\r\n");
+    strcpy(cmdbuff, "conn:send('<p><input type=\"radio\" name=\"select\" value=\"BotFwd\"> Move Robot Forward')\r\n");
     SendCMD();
     getreply();
     wait(0.3);
-    strcpy(cmdbuff, "conn:send('<p><input type=\"radio\" name=\"check\" value=\"BotRev\"> Move Robot Reverse')\r\n");
+    strcpy(cmdbuff, "conn:send('<p><input type=\"radio\" name=\"select\" value=\"BotRev\"> Move Robot Reverse')\r\n");
     SendCMD();
     getreply();
     wait(0.3);
-    strcpy(cmdbuff, "conn:send('<p><input type=\"radio\" name=\"check\" value=\"BotStop\"> Stop Robot')\r\n");
+    strcpy(cmdbuff, "conn:send('<p><input type=\"radio\" name=\"select\" value=\"BotStop\"> Stop Robot')\r\n");
     SendCMD();
     getreply();
     wait(0.3);
-    strcpy(cmdbuff, "conn:send('<p><input type=\"radio\" name=\"check\" value=\"BotLeft\"> Turn Robot Left')\r\n");
+    strcpy(cmdbuff, "conn:send('<p><input type=\"radio\" name=\"select\" value=\"BotLeft\"> Turn Robot Left')\r\n");
     SendCMD();
     getreply();
     wait(0.3);
-    strcpy(cmdbuff, "conn:send('<p><input type=\"radio\" name=\"check\" value=\"BotRight\"> Turn Robot Right')\r\n");
+    strcpy(cmdbuff, "conn:send('<p><input type=\"radio\" name=\"select\" value=\"BotRight\"> Turn Robot Right')\r\n");
     SendCMD();
     getreply();
     wait(0.3);
@@ -384,6 +388,7 @@
     return;
 }
 
+/*
 // To make Robot Turn approximately 90 degree using IMU mag
 void ImuCheck()
 {
@@ -411,17 +416,20 @@
         if(abs(heading - initial_heading) > 90) rotFlag = 1;
     }
 }
+*/
 
 // Checking for obstacles in the way while moving Fwd and Rev
 void ObjDetect(float currDist)
 {
-    if(currDist <= 0.5f) {
+    if(currDist > 0.98f) { // 1.0=4 cm, 0.0=30 cm
+        myleds = 1;
+        pc.printf("Obj detected at: %f cm\r\n",(1-currDist)*26+4);
+        wait(0.001);
         A.stop(0.0);
         B.stop(0.0);
         ledFwd = 0;
         ledRev = 0;
-        mySpeaker = 0.5;  // Play Beep sound
-        wait(1.0);
-        mySpeaker=0.0;    // turn off audio
+        mySpeaker.PlayNote(500.0,0.5,0.5);
+        myleds = 0;
     }
 }
\ No newline at end of file