Wifi controlled robot that uses ESP8266 wifi chip.

Dependencies:   Motordriver Servo mbed-dev mbed-rtos

Fork of ESP8266_wifi_robot by Kairi Kozuma

Files at this revision

API Documentation at this revision

Comitter:
K2Silver
Date:
Sat Oct 29 15:35:26 2016 +0000
Commit message:
Initial commit of wifi controlled robot

Changed in this revision

ESP8266.cpp Show annotated file Show diff for this revision Revisions of this file
ESP8266.h Show annotated file Show diff for this revision Revisions of this file
Motordriver.lib Show annotated file Show diff for this revision Revisions of this file
Servo.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-dev.lib Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r df754b773321 ESP8266.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266.cpp	Sat Oct 29 15:35:26 2016 +0000
@@ -0,0 +1,99 @@
+#include "ESP8266.h"
+
+
+ESP8266::ESP8266(PinName tx, PinName rx, PinName reset, char * SSID, char * password) :
+    _esp(tx, rx), _reset(reset) {
+    strcpy(_ssid, SSID);
+    strcpy(_pwd, password);        
+}
+ 
+//  Setup for ESP8266
+void ESP8266::setup() {
+    
+    /* Set wifi mode */
+    send("wifi.setmode(wifi.STATION)\r\n");
+    
+    /* Set SSID and password */
+    strcpy(_snd, "wifi.sta.config(\"");
+    strcat(_snd, _ssid);
+    strcat(_snd, "\",\"");
+    strcat(_snd, _pwd);
+    strcat(_snd, "\")\r\n");
+    send();
+    
+    wait(7);
+
+    /* Set up HTTP server */
+    send("srv=net.createServer(net.TCP)\r\n");
+    send("srv:listen(80,function(conn)\r\n");
+    send("conn:on(\"receive\",function(conn,payload)\r\n");
+    send("print(payload:match(\"Z%u\"))\r\n");
+    send("conn:send(\"<!DOCTYPE html>\")\r\n");
+    send("conn:send(\"<html>\")\r\n");
+    send("conn:send(\"<h1>Control Robot</h1>\")\r\n");
+    send("conn:send('<form action=\"\" method=\"POST\">\r\n')");
+    send("conn:send('<input type=\"submit\" name=\"ZF\" value=\"Forward\">\r\n')");
+    send("conn:send('<input type=\"submit\" name=\"ZB\" value=\"Back\">\r\n')");
+    send("conn:send('<input type=\"submit\" name=\"ZL\" value=\"Left\">\r\n')");
+    send("conn:send('<input type=\"submit\" name=\"ZR\" value=\"Right\">\r\n')");
+    send("conn:send('<input type=\"submit\" name=\"ZS\" value=\"Servo Left\">\r\n')");
+    send("conn:send('<input type=\"submit\" name=\"ZP\" value=\"Servo Right\">\r\n')");
+    send("conn:send('</form>\r\n')");
+    send("conn:send(\"</html>\")\r\n"); 
+    send("end)\r\n");
+    send("conn:on(\"sent\",function(conn) conn:close() end)\r\n");
+    send("end)\r\n");
+    wait(10);
+}
+ 
+void ESP8266::send()
+{
+    _esp.printf("%s", _snd);
+    Thread::wait(500);
+}
+
+void ESP8266::send(char * command) {
+    _esp.printf("%s", command);
+    Thread::wait(500);
+}
+
+void ESP8266::reset() {
+    _reset = 0;
+    wait(1); /* Not RTOS friendly */
+    _reset = 1;
+}
+
+void ESP8266::baud(int baudrate) {
+    char buffer[32];
+    sprintf(buffer, "AT+CIOBAUD=%d\r\n", baudrate);
+    send(buffer);
+    
+    _esp.baud(baudrate);
+}
+
+void ESP8266::getIP() {
+    send("\r\nprint(wifi.sta.getip())\r\n");  
+}
+
+void ESP8266::getMAC() {
+    send("\r\nprint(wifi.sta.getmac())\r\n");  
+}
+
+int ESP8266::putc(int c) {
+    return _esp.putc(c);
+}
+
+int ESP8266::getc() {
+    return _esp.getc();
+} 
+    
+int ESP8266::readable() {
+    return _esp.readable();
+}
+int ESP8266::writeable() {
+    return _esp.writeable();
+}
+
+void ESP8266::attach(Callback< void()> func, SerialBase::IrqType type) {
+    _esp.attach(func, type);
+}
diff -r 000000000000 -r df754b773321 ESP8266.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266.h	Sat Oct 29 15:35:26 2016 +0000
@@ -0,0 +1,34 @@
+#ifndef ESP8266_H
+#define ESP8266_H
+
+#include "mbed.h"
+
+#define SND_BUF_SIZE 1024
+
+class ESP8266 
+{
+private:
+    char _ssid[32];
+    char _pwd[32];
+    char _snd[SND_BUF_SIZE];
+    RawSerial _esp;
+    DigitalOut _reset;
+    
+public:
+    ESP8266(PinName tx, PinName rx, PinName reset, char * SSID, char * password); 
+    void send();
+    void send(char * command);
+    void setup();
+    void reset();
+    void baud(int baudrate);
+    void getMAC();
+    void getIP();
+    
+    int putc(int c);
+    int getc();
+    int readable();
+    int writeable();
+    void attach(Callback< void()> func, SerialBase::IrqType type);
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r df754b773321 Motordriver.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motordriver.lib	Sat Oct 29 15:35:26 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/littlexc/code/Motordriver/#3110b9209d3c
diff -r 000000000000 -r df754b773321 Servo.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Sat Oct 29 15:35:26 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r 000000000000 -r df754b773321 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Oct 29 15:35:26 2016 +0000
@@ -0,0 +1,186 @@
+#include "mbed.h"
+#include "Servo.h"
+#include "rtos.h"
+#include "ESP8266.h"
+#include "motordriver.h"
+
+/* Left and right motors */
+Motor motor_l(p22, p11, p12, 1); // pwm, fwd, rev, can brake
+Motor motor_r(p21, p13, p14, 1); // pwm, fwd, rev, can brake
+
+/* Virtual USB serial port */
+RawSerial pc(USBTX, USBRX); // tx, rx
+
+/* ESP8266 wifi chip */
+ESP8266 esp(p28, p27, p26, "SSID", "password"); // tx, rx, reset, SSID, password
+#define RCV_BUF_SIZE 1024
+char rcv[RCV_BUF_SIZE];
+volatile int rcv_tail = 0;
+volatile int rcv_head = 0;
+    
+/* Speaker */
+PwmOut speaker(p24);
+
+/* Servo with IR sensor mounted */
+Servo servo_ir(p23);
+
+/* IR sensor */
+AnalogIn ir_sensor(p20);
+
+/* mbed LEDs */
+DigitalOut led1(LED1);
+DigitalOut led4(LED4);
+
+/* IR sensor thread */
+void ir_thread(void const * args) {
+    while (1) {
+        if (ir_sensor > 0.8) {
+            speaker = 1;
+        } else {
+            speaker = 0;
+        }
+        Thread::wait(50);
+    }
+}
+
+/* Circular buffer to hold commands */
+static int COM_BUF_SIZE = 100;
+volatile char * command_queue;
+volatile int queue_tail = 0;
+volatile int queue_head = 0;
+
+/* Command execute thread */
+void command_exec_thread(void const *args) {
+    while (1) {
+        if (queue_tail < queue_head) {
+            /* Save comand in temporary char variable */
+            char command = command_queue[queue_tail];
+            
+            /* Null the command consumed */
+            command_queue[queue_tail] = '\0';
+            
+            /* Execute the command */
+            switch(command) {
+            case '\0': /* null command */
+                break;
+            case 'F': /* Move forward */
+                motor_r.speed(1); 
+                motor_l.speed(1); 
+                Thread::wait(400);
+                motor_r.stop(1);
+                motor_l.stop(1);
+                break;
+            case 'B': /* Move backwards */
+                motor_r.speed(-1); 
+                motor_l.speed(-1); 
+                Thread::wait(400);
+                motor_r.stop(1);
+                motor_l.stop(1);
+                break;
+            case 'R': /* Move right */
+                motor_l.speed(1); 
+                Thread::wait(400);
+                motor_l.stop(1);
+                break;
+            case 'L': /* Move left */
+                motor_r.speed(1); 
+                Thread::wait(400);
+                motor_r.stop(1);
+                break;
+            case 'S': /* Turn servo right */
+                if (servo_ir > 0.0) {
+                    float val = (servo_ir - .2);
+                    servo_ir = val > 0.0 ? val : 0.0;
+                }
+                break;
+            case 'P': /* Turn servo left */
+                if (servo_ir < 1.0) {
+                    float val = (servo_ir + .2);
+                    servo_ir = val < 1.0 ? val : 1.0;
+                }
+                break;
+            default:
+                break;
+            }
+            /* Increment command index in circular buffer */
+            queue_tail = (queue_tail + 1) % COM_BUF_SIZE;
+        }
+        Thread::wait(200);
+    }
+}
+ 
+ 
+/* Queue command thread*/
+void command_queue_thread(void const *args) {
+    while(1) {
+        if (rcv_tail < rcv_head) {
+            if (rcv[rcv_tail] == 'Z') {
+                /* Loop until valid value found */
+                while (rcv[(rcv_tail + 1) % RCV_BUF_SIZE] == '\0') {
+                    Thread::wait(100);    
+                }
+                char let = rcv[(rcv_tail + 1) % RCV_BUF_SIZE];
+                command_queue[queue_head] = let;
+                queue_head = (queue_head + 1) % COM_BUF_SIZE;   
+            }
+            rcv[rcv_tail] = '\0'; /* Clear value */
+            rcv_tail = (rcv_tail + 1) % RCV_BUF_SIZE;
+        }
+        Thread::wait(10);
+    }
+}
+    
+/* PC receive interrupt routine */
+void pc_recv() {
+    led1 = 1;
+    while(pc.readable()) {
+        /* Print out to ESP8266 hardware */
+        esp.putc(pc.getc());
+    }
+    led1 = 0;
+}
+
+/* ESP83266 receive interrupt routine */
+void dev_recv() {
+    led4 = 1;
+    while(esp.readable()) {
+        char let = esp.getc();
+        /* Echo to pc to see output */
+        pc.putc(let);
+        /* Put in receive buffer */
+        rcv[rcv_head] = let;
+        rcv_head = (rcv_head + 1) % RCV_BUF_SIZE;
+    }
+    led4 = 0;
+}
+ 
+/* Main code */
+int main() {
+    /* Set pc serial baud rate */
+    pc.baud(9600);
+    
+    /* Set up esp */
+    esp.reset();
+    esp.baud(9600);
+    esp.setup();
+    
+    /* Initialize command buffer */
+    command_queue = (char*) calloc(sizeof(char), COM_BUF_SIZE);
+    
+    /* Attach interrupts */
+    pc.attach(&pc_recv, Serial::RxIrq);
+    esp.attach(&dev_recv, Serial::RxIrq);
+    
+    /* Print IP and MAC */
+    esp.getIP();
+    esp.getMAC();
+    
+    /* Start threads */
+    Thread command_exec_thr(command_exec_thread);
+    Thread command_queue_thr(command_queue_thread);
+    Thread ir_thr(ir_thread);
+    
+    while(1) {
+        Thread::wait(10000);
+    }
+}
diff -r 000000000000 -r df754b773321 mbed-dev.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-dev.lib	Sat Oct 29 15:35:26 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/mbed-dev/#156823d33999
diff -r 000000000000 -r df754b773321 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sat Oct 29 15:35:26 2016 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed-rtos/#3da5f554d8bf