The IoTBot is a WiFi-enabled rover built from the Shadow Robot kit. It is controlled from a web interface running on the Adafruit Huzzah ESP8266 WiFi module and implements a pair of Hall-effect sensors on the motors to keep the wheels spinning at the same speed. Additionally, it uses a Sharp IR sensor to detect walls in front of the robot and prevent it from crashing.

Dependencies:   mbed-dev mbed-rtos

Fork of huzzah_helloWorld by ECE 4180 Team Who

Revision:
1:046dd94c57ce
Parent:
0:57cec3469a80
Child:
2:2d87957b577b
--- a/main.cpp	Thu Oct 22 16:18:21 2015 +0000
+++ b/main.cpp	Mon Oct 31 07:33:45 2016 +0000
@@ -1,227 +1,30 @@
 #include "mbed.h"
+#include "rtos.h"
  
-Serial pc(USBTX, USBRX);
-Serial esp(p28, p27); // tx, rx
-DigitalOut reset(p26);
+RawSerial  pc(USBTX, USBRX);
+RawSerial  dev(p28,p27);
 DigitalOut led1(LED1);
 DigitalOut led4(LED4);
+
+// Set up the motor pins
+PwmOut motorACtrl(p21);
+PwmOut motorBCtrl(p22);
+DigitalOut backA(p20);
+DigitalOut fwdA(p19);
+DigitalOut stby(p18);
+DigitalOut fwdB(p17);
+DigitalOut backB(p16);
 Timer t;
- 
-int  count,ended,timeout;
-char buf[2024];
-char snd[1024];
- 
-char ssid[32] = "SwagInABag";     // enter WiFi router ssid inside the quotes
-char pwd [32] = "yoloswag"; // enter WiFi router password inside the quotes
- 
-void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate();
- void dev_recv()
-{
-    led1 = !led1;
-    while(esp.readable()) {
-        pc.putc(esp.getc());
-    }
-}
- 
-void pc_recv()
-{
-    led4 = !led4;
-    while(pc.readable()) {
-        esp.putc(pc.getc());
-    }
-}
- 
- 
-int main()
-{
-    reset=0; //hardware reset for 8266
-    pc.baud(9600);  // set what you want here depending on your terminal program speed
-    pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
-    wait(0.5);
-    reset=1;
-    timeout=2;
-    getreply();
- 
-    esp.baud(9600);   // change this to the new ESP8266 baudrate if it is changed at any time.
- 
-    //ESPsetbaudrate();   //******************  include this routine to set a different ESP8266 baudrate  ******************
- 
-    ESPconfig();        //******************  include Config to set the ESP8266 configuration  ***********************
- 
- 
- 
-    pc.attach(&pc_recv, Serial::RxIrq);
-    esp.attach(&dev_recv, Serial::RxIrq);
-    
-    // continuosly get AP list and IP
-    while(1) {
-        sleep();
-    }
- 
-}
- 
-// Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
-void ESPsetbaudrate()
-{
-    strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
-    SendCMD();
-}
- 
-//  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
-void ESPconfig()
-{
+float speed = 0.9f;
 
-    wait(5);
-    pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
-        strcpy(snd,".\r\n.\r\n");
-    SendCMD();
-        wait(1);
-    pc.printf("---------- Reset & get Firmware ----------\r\n");
-    strcpy(snd,"node.restart()\r\n");
-    SendCMD();
-    timeout=5;
-    getreply();
-    pc.printf(buf);
- 
-    wait(2);
- 
-    pc.printf("\n---------- Get Version ----------\r\n");
-    strcpy(snd,"print(node.info())\r\n");
-    SendCMD();
-    timeout=4;
-    getreply();
-    pc.printf(buf);
- 
-    wait(3);
- 
-    // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
-    pc.printf("\n---------- Setting Mode ----------\r\n");
-    strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
-    SendCMD();
-    timeout=4;
-    getreply();
-    pc.printf(buf);
- 
-    wait(2);
- 
-   
+float timeout = 0.05f;
+int  count,ended;
+char buf[256];
+char motorCmd[] = "fwrd";
  
-    pc.printf("\n---------- Listing Access Points ----------\r\n");
-    strcpy(snd, "function listap(t)\r\n");
-        SendCMD();
-        wait(1);
-        strcpy(snd, "for k,v in pairs(t) do\r\n");
-        SendCMD();
-        wait(1);
-        strcpy(snd, "print(k..\" : \"..v)\r\n");
-        SendCMD();
-        wait(1);
-        strcpy(snd, "end\r\n");
-        SendCMD();
-        wait(1);
-        strcpy(snd, "end\r\n");
-        SendCMD();
-        wait(1);
-        strcpy(snd, "wifi.sta.getap(listap)\r\n");
-    SendCMD();
-    wait(1);
-        timeout=15;
-    getreply();
-    pc.printf(buf);
- 
-    wait(2);
- 
-    pc.printf("\n---------- Connecting to AP ----------\r\n");
-    pc.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
-    strcpy(snd, "wifi.sta.config(\"");
-    strcat(snd, ssid);
-    strcat(snd, "\",\"");
-    strcat(snd, pwd);
-    strcat(snd, "\")\r\n");
-    SendCMD();
-    timeout=10;
-    getreply();
-    pc.printf(buf);
- 
-    wait(5);
- 
-    pc.printf("\n---------- Get IP's ----------\r\n");
-    strcpy(snd, "print(wifi.sta.getip())\r\n");
-    SendCMD();
-    timeout=3;
-    getreply();
-    pc.printf(buf);
- 
-    wait(1);
- 
-    pc.printf("\n---------- Get Connection Status ----------\r\n");
-    strcpy(snd, "print(wifi.sta.status())\r\n");
-    SendCMD();
-    timeout=5;
-    getreply();
-    pc.printf(buf);
- 
-    pc.printf("\n\n\n  If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
-    pc.printf("  Run this if you want to reconfig the ESP8266 at any time.\r\n");
-    pc.printf("  It saves the SSID and password settings internally\r\n");
-    wait(10);
-        
-        
-          pc.printf("\n---------- Setting up http server ----------\r\n");
-    strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
-        SendCMD();
-        wait(1);
-        strcpy(snd, "srv:listen(80,function(conn)\r\n");
-        SendCMD();
-        wait(1);
-        strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
-        SendCMD();
-        wait(1);
-        strcpy(snd, "print(payload)\r\n");
-        SendCMD();
-        wait(1);
-        
-        strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
-        SendCMD();
-      wait(1);
-        
-        strcpy(snd, "conn:send(\"<html>\")\r\n");
-        SendCMD();
-      wait(1);
-        
-        strcpy(snd, "conn:send(\"<h1> Hi James, NodeMcu.</h1>\")\r\n");
-      SendCMD();
-        wait(1);
-        
-        strcpy(snd, "conn:send(\"<h2> test</h2>\")\r\n");
-        SendCMD();
-        wait(1);
-        
-        strcpy(snd, "conn:send(\"</html>\")\r\n");
-    SendCMD();
-    wait(1);
-        
-        strcpy(snd, "end)\r\n");
-    SendCMD();
-    wait(1);
-        
-        strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n");
-    SendCMD();
-    wait(1);
-        strcpy(snd, "end)\r\n");
-    SendCMD();
-    wait(1);
-        timeout=17;
-    getreply();
-    pc.printf(buf);
-        pc.printf("\r\nDONE");
-}
- 
-void SendCMD()
-{
-    esp.printf("%s", snd);
-}
- 
+/**
+ *  Get the reply string from the WiFi module
+ */
 void getreply()
 {
     memset(buf, '\0', sizeof(buf));
@@ -229,8 +32,8 @@
     ended=0;
     count=0;
     while(!ended) {
-        if(esp.readable()) {
-            buf[count] = esp.getc();
+        if(dev.readable()) {
+            buf[count] = dev.getc();
             count++;
         }
         if(t.read() > timeout) {
@@ -240,5 +43,103 @@
         }
     }
 }
+
+/**
+ *
+ */
+void getMotorCommand() {
+    int index = 3;
+    // iterate backwards through the message buffer
+    for (int i = sizeof(buf) - 1; i > 2; i--){
+        // Check if the current character is a null
+        if (buf[i] != '\0') {
+            motorCmd[index] = buf[i - 2];   // Copy the character
+            if (index == 0){
+                break;      // Break if we have completed getting the command
+            } else {
+                index--;    // Decrement otherwise
+            }
+        }
+    }
+}
+
+/**
+ *  Run the motor based on the command passed through the serial interface.
+ */
+void runMotor(){
+    if (strcmp(motorCmd, (char *)"fwrd") == 0) {
+           stby = 1;        // Start H-bridge driver
+           fwdA = 1;
+           fwdB = 1;
+           backA = 0;
+           backB = 0;
+           motorACtrl = speed;
+           motorBCtrl = speed;
+    } else if (strcmp(motorCmd, (char *)"back") == 0) {
+           stby = 1;        // Start H-bridge driver
+           fwdA = 0;
+           fwdB = 0;
+           backA = 1;
+           backB = 1;
+           motorACtrl = speed;
+           motorBCtrl = speed;
+    } else if (strcmp(motorCmd, (char *)"left") == 0) {
+           stby = 1;        // Start H-bridge driver
+           fwdA = 1;
+           fwdB = 0;
+           backA = 0;
+           backB = 1;
+           motorACtrl = speed;
+           motorBCtrl = speed;
+    } else if (strcmp(motorCmd, (char *)"rght") == 0) {
+           stby = 1;        // Start H-bridge driver
+           fwdA = 0;
+           fwdB = 1;
+           backA = 1;
+           backB = 0;
+           motorACtrl = speed;
+           motorBCtrl = speed;
+    } else if (strcmp(motorCmd, (char *)"stop") == 0) {
+           stby = 1;        // Start H-bridge driver
+           fwdA = 0;
+           fwdB = 0;
+           backA = 0;
+           backB = 0;
+           motorACtrl = 0.0f;   // Set speed to 0
+           motorBCtrl = 0.0f;
+           stby = 0;        // Turn off H-bridge driver
+    }
+}
+
+void dev_recv()
+{
+    led1 = !led1;
+    getreply();         // Get the serial message
+    getMotorCommand();  // Extract the motor command from the message
+    runMotor();         // Run the motor based on the command
+}
  
-    
\ No newline at end of file
+void pc_recv()
+{
+    led4 = !led4;
+    while(pc.readable()) {
+        dev.putc(pc.getc());
+    }
+}
+ 
+int main()
+{
+    pc.baud(115200);
+    dev.baud(115200);
+ 
+    pc.attach(&pc_recv, Serial::RxIrq);
+    dev.attach(&dev_recv, Serial::RxIrq);
+    
+    // Initialize the motors
+    motorACtrl.period(0.01f);
+    motorBCtrl.period(0.01f);
+ 
+    while(1) {
+        sleep();
+    }
+}
\ No newline at end of file