Asha Harris / Mbed 2 deprecated RoboNav

Dependencies:   HC_SR04_Ultrasonic_Library Motor mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Motor.h"
00003 #include "rtos.h"
00004 #include "ultrasonic.h"
00005 
00006 //Account for time (approx. 2min, see serial output in terminal) for wi-fi server to set up before navigation code begins!!
00007 
00008 Motor ma(p25, p14, p13);
00009 Motor mb(p24, p11, p12); // pwm, fwd, rev
00010 DigitalOut myled(LED1);
00011 DigitalOut led4(LED4);
00012 DigitalOut led3(LED3);
00013 
00014 //speaker/sd set up
00015 /*AnalogOut DACout(p18); //currently in pwm 26
00016 wave_player waver(&DACout);
00017 SDFileSystem sd(p5, p6, p7, p8, "sd");*/
00018 
00019 Serial pc(USBTX, USBRX);
00020 Serial esp(p28, p27); // tx, rx
00021 DigitalOut reset(p26);
00022 Timer t;
00023 
00024 int  count,ended,timeout;
00025 char buf[2024];
00026 char snd[1024];
00027  
00028 char ssid[32] = "iPhone";     // enter WiFi router ssid inside the quotes
00029 char pwd [32] = "Jordan23#"; // enter WiFi router password inside the quotes
00030  
00031 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate();
00032  void dev_recv()
00033 {
00034     myled = myled;
00035     while(esp.readable()) {
00036         pc.putc(esp.getc());
00037     }
00038 }
00039  
00040 void pc_recv()
00041 {
00042     led4 = !led4;
00043     while(pc.readable()) {
00044         esp.putc(pc.getc());
00045     }
00046 }
00047  
00048 
00049 DigitalIn Enc1(p21); //right motor... 
00050 DigitalIn Enc2(p22); //left motor
00051 int oldEnc1 = 0; 
00052 int oldEnc2 = 0;
00053 
00054 int ticksR = 0; //# times right wheel encoder changed state
00055 int ticksL = 0; //" "
00056 
00057 void dist1(int distance)
00058 {
00059     //put code here to execute when the distance has changed
00060     printf("Distance1 %d mm\r\n", distance);
00061 }
00062 void dist2(int distance2)
00063 {
00064     //put code here to execute when the distance has changed
00065     printf("Distance2 %d mm\r\n", distance2);
00066 }
00067 
00068 //Sonar1 variables
00069 ultrasonic mu1(p15, p16, .1, 1, &dist1);
00070 
00071 //Sonar2 variables
00072 ultrasonic mu2(p22, p21, .1, 1, &dist2);
00073 
00074 void wheelEnc(void const *argument){
00075     while(1){
00076         if(Enc1 != oldEnc1) { // Increment ticks every time the state has switched. 
00077                 ticksR++;
00078                 oldEnc1 = Enc1;
00079 
00080             }
00081        if(Enc2 != oldEnc2) {
00082                 ticksL++;
00083                 oldEnc2 = Enc2;
00084             }
00085         pc.printf("Left ticks: %d \n", ticksL);
00086         pc.printf("Right ticks: %d \n", ticksR);
00087         }
00088     }
00089     
00090 void sonarThread(void const *argument){
00091     while(1){
00092         mu1.checkDistance(); //front sonar
00093         mu2.checkDistance(); //right side sonar
00094         int muDist1 = mu1.getCurrentDistance();
00095         int muDist2 = mu2.getCurrentDistance();
00096 
00097         //always move straight forward
00098         ma.speed(-.8); //Left motor
00099         mb.speed(-.8); //Right motor
00100         myled =0;
00101 
00102         //Front sensor is not detecting a wall, but right side is too close
00103         if ((muDist1 > 250) && (muDist2 < 170)){
00104             ma.speed(-.2);
00105             mb.speed(-.9); 
00106         }
00107         //Front sensor is not detecting a wall, but right side is too far
00108         else if ((muDist1 > 250) && (muDist2 > 250)){
00109             ma.speed(-.6); //turn
00110             mb.speed(-0.4);
00111         }
00112         else if (muDist1 < 280){
00113                 ma.speed(1); //
00114                 mb.speed(-1);
00115                 wait(0.3);
00116         }
00117         else if ((muDist1 > 250) && (muDist2 > 500)){
00118                 ma.speed(-.8);
00119                 mb.speed(-.8);
00120                 wait(0.1);
00121                 mb.speed(1);
00122                 wait(0.1);
00123         }
00124         else {
00125         ma.speed(-.8);
00126         mb.speed(-.8);
00127         wait(.1);
00128         }
00129         }
00130     }
00131 ///////////////////////////
00132 /*void audio_thread(void const *argument)
00133 {
00134     ultra_mutex.lock();
00135     FILE *wave_file;
00136     wave_file=fopen("/sd/imperial_marchEdit.wav","r");
00137     waver.play(wave_file);
00138     fclose(wave_file);
00139     ultra_mutex.unlock();
00140 }*/
00141 
00142 
00143 int main()
00144 {
00145     led3 = 1;
00146     wait(2);
00147     reset=0; //hardware reset for 8266
00148     pc.baud(9600);  // set what you want here depending on your terminal program speed
00149     pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
00150     wait(0.5);
00151     reset=1;
00152     timeout=2;
00153     getreply();
00154  
00155     esp.baud(9600);   // change this to the new ESP8266 baudrate if it is changed at any time.
00156  
00157     //ESPsetbaudrate();   //******************  include this routine to set a different ESP8266 baudrate  ******************
00158  
00159     ESPconfig();        //******************  include Config to set the ESP8266 configuration  ***********************
00160  
00161  
00162  
00163     pc.attach(&pc_recv, Serial::RxIrq);
00164     esp.attach(&dev_recv, Serial::RxIrq);
00165     //start measuring the distance
00166     mu1.startUpdates();
00167     mu2.startUpdates();
00168     
00169     Enc1.mode(PullUp); // requires a pullup resistor so i just used embed's feature.
00170     Enc2.mode(PullUp);
00171     
00172     Thread thread1(wheelEnc);
00173     Thread thread2(sonarThread);
00174     //Thread thread3(audio_thread);
00175     //Thread thread4(wifiThread);
00176 
00177     while(1) {
00178         
00179         Thread::wait(100);        
00180 
00181     }
00182 }
00183 
00184 
00185 
00186 ///WIFI SERVER GUI SETUP
00187 
00188 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
00189 void ESPsetbaudrate()
00190 {
00191     strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
00192     SendCMD();
00193 }
00194  
00195 //  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
00196 void ESPconfig()
00197 {
00198 
00199     wait(5);
00200     pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
00201         strcpy(snd,".\r\n.\r\n");
00202     SendCMD();
00203         wait(1);
00204     pc.printf("---------- Reset & get Firmware ----------\r\n");
00205     strcpy(snd,"node.restart()\r\n");
00206     SendCMD();
00207     timeout=5;
00208     getreply();
00209     pc.printf(buf);
00210  
00211     wait(2);
00212  
00213     pc.printf("\n---------- Get Version ----------\r\n");
00214     strcpy(snd,"print(node.info())\r\n");
00215     SendCMD();
00216     timeout=4;
00217     getreply();
00218     pc.printf(buf);
00219  
00220     wait(3);
00221  
00222     // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
00223     pc.printf("\n---------- Setting Mode ----------\r\n");
00224     strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
00225     SendCMD();
00226     timeout=4;
00227     getreply();
00228     pc.printf(buf);
00229  
00230     wait(2);
00231  
00232    
00233  
00234     pc.printf("\n---------- Listing Access Points ----------\r\n");
00235     strcpy(snd, "function listap(t)\r\n");
00236         SendCMD();
00237         wait(1);
00238         strcpy(snd, "for k,v in pairs(t) do\r\n");
00239         SendCMD();
00240         wait(1);
00241         strcpy(snd, "print(k..\" : \"..v)\r\n");
00242         SendCMD();
00243         wait(1);
00244         strcpy(snd, "end\r\n");
00245         SendCMD();
00246         wait(1);
00247         strcpy(snd, "end\r\n");
00248         SendCMD();
00249         wait(1);
00250         strcpy(snd, "wifi.sta.getap(listap)\r\n");
00251     SendCMD();
00252     wait(1);
00253         timeout=15;
00254     getreply();
00255     pc.printf(buf);
00256  
00257     wait(2);
00258  
00259     pc.printf("\n---------- Connecting to AP ----------\r\n");
00260     pc.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
00261     strcpy(snd, "wifi.sta.config(\"");
00262     strcat(snd, ssid);
00263     strcat(snd, "\",\"");
00264     strcat(snd, pwd);
00265     strcat(snd, "\")\r\n");
00266     SendCMD();
00267     timeout=10;
00268     getreply();
00269     pc.printf(buf);
00270  
00271     wait(5);
00272  
00273     pc.printf("\n---------- Get IP's ----------\r\n");
00274     strcpy(snd, "print(wifi.sta.getip())\r\n");
00275     SendCMD();
00276     timeout=3;
00277     getreply();
00278     pc.printf(buf);
00279  
00280     wait(1);
00281  
00282     pc.printf("\n---------- Get Connection Status ----------\r\n");
00283     strcpy(snd, "print(wifi.sta.status())\r\n");
00284     SendCMD();
00285     timeout=5;
00286     getreply();
00287     pc.printf(buf);
00288  
00289     pc.printf("\n\n\n  If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
00290     pc.printf("  Run this if you want to reconfig the ESP8266 at any time.\r\n");
00291     pc.printf("  It saves the SSID and password settings internally\r\n");
00292     pc.printf(" Asha Harris and Teferi Lab 2 ");
00293     wait(10);
00294         
00295         
00296           pc.printf("\n---------- Setting up http server ----------\r\n");
00297     strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
00298         SendCMD();
00299         wait(1);
00300         strcpy(snd, "srv:listen(80,function(conn)\r\n");
00301         SendCMD();
00302         wait(1);
00303         strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
00304         SendCMD();
00305         wait(1);
00306         strcpy(snd, "print(payload)\r\n");
00307         SendCMD();
00308         wait(1);
00309         
00310         strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
00311         SendCMD();
00312       wait(1);
00313         
00314         strcpy(snd, "conn:send(\"<html>\")\r\n");
00315         SendCMD();
00316       wait(1);
00317         
00318         strcpy(snd, "conn:send(\"<h1> 4180 IOT Self Nav Robot </h1>\")\r\n");
00319       SendCMD();
00320         wait(1);
00321         
00322         sprintf(snd, "conn:send(\"%s'\")\r\n", ticksR);
00323         SendCMD();
00324         wait(1);
00325         
00326         strcpy(snd, "conn:send(\"<h2> cm.. </h2>\")\r\n");
00327         SendCMD();
00328         wait(1);
00329         
00330         strcpy(snd, "conn:send(\"</html>\")\r\n");
00331     SendCMD();
00332     wait(1);
00333         
00334         strcpy(snd, "end)\r\n");
00335     SendCMD();
00336     wait(1);
00337         
00338         strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n");
00339     SendCMD();
00340     wait(1);
00341         strcpy(snd, "end)\r\n");
00342     SendCMD();
00343     wait(1);
00344         timeout=17;
00345     getreply();
00346     pc.printf(buf);
00347         pc.printf("\r\nDONE");
00348 }
00349  
00350 void SendCMD()
00351 {
00352     esp.printf("%s", snd);
00353 }
00354  
00355 void getreply()
00356 {
00357     memset(buf, '\0', sizeof(buf));
00358     t.start();
00359     ended=0;
00360     count=0;
00361     while(!ended) {
00362         if(esp.readable()) {
00363             buf[count] = esp.getc();
00364             count++;
00365         }
00366         if(t.read() > timeout) {
00367             ended = 1;
00368             t.stop();
00369             t.reset();
00370         }
00371     }
00372 }
00373  
00374