emic2

Dependencies:   mbed SoftSerial

Files at this revision

API Documentation at this revision

Comitter:
rickyqqj
Date:
Fri Jun 19 09:29:45 2020 +0000
Parent:
2:32f26fe1d8ad
Commit message:
This is the central program for the Caecus central MCU

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Mar 02 12:43:18 2020 +0000
+++ b/main.cpp	Fri Jun 19 09:29:45 2020 +0000
@@ -1,47 +1,93 @@
 #include "mbed.h"
 #include "string"
-
-//Emic 2 Hello Speech World Demo
-
+Timer getctimer;
 
-InterruptIn frx(PF_1);
-DigitalIn BLSB(PB_0);
-DigitalIn BMSB(PA_8);
-DigitalOut myled(LED1);
-Serial myTTS(PA_9, PA_10); //serial RX,TX pins to emic
+InterruptIn frx(PF_1); //a virtual UART RX for TTS
+DigitalIn BLSB(PB_0);  //LSB from LIDAR
+DigitalIn BMSB(PA_8);  //MSB from LIDAR
+DigitalIn SPD(PB_4);  //For speech speed seting button
+DigitalIn VOI(PA_5);  // for speech voice setting button
+DigitalOut BLTEN(PB_3);  //Pins to control the mode of bluetooth module HC_05
+Serial myTTS(PA_9, PA_10); //serial RX,TX pins to emic  however the RX is used to recieve signal from RFID module, TTS tx is connected to a digitalpin
 
-Serial myCAM(PA_2, PA_3); //serial RX,TX pins to emic
-
-char sentence [255];
-char sentencec [255];
+Serial myCAM(PA_2, PA_3); //serial RX,TX pins to ble
+//due to hardware reason,  all functions and variable with "Cam" is connect to RFID,  all functions and variable with "RFID" is connect to CAM, 
+char sentence [1024];  //buffers to recieve UART from Cam
+char sentencec [1024];  //buffers to recieve UART from RFID
 uint8_t count;
 uint8_t countc;
-bool rfidspeak = 0;
-bool smartcamspeak = 0;
+uint16_t confirmblt = 0;
+bool rfidspeak = 0;       //if there is someting to speak from Cam, it goes high
+bool smartcamspeak = 0;  //if there is someting to speak from RFID, it goes high
 bool inspeech = 0;
-void readerRX() {
-       frx.fall(NULL);
+string slave="4444,10,059266";
+void readerRX() {    //to recieve UART sent from smart cam
        count = 0;
        sentence[0] = 'S';
-       while(sentence[count]!='!' && count!=255)
-       {count++;
-        sentence[count]=myTTS.getc();
+         getctimer.reset();
+       getctimer.start();
+       while(sentence[count]!='!' && count!=1023 && getctimer.read()<2)
+       {if(frx == 0){inspeech = 0;}
+        if(myTTS.readable())             //My TTS Rx is used to recieve Cam, we have another digital pins for TTS.
+        {count++;
+        sentence[count]=myTTS.getc();}
        }
+         getctimer.stop();
      rfidspeak = 1;
     }
- void camRX(){
-     frx.fall(NULL);
+ void camRX(){  // to recieve UART from RFID
+  
      countc = 0;
        sentencec[0] = 'S';
-       while(sentencec[countc]!='!' && countc!=255)
-       {
-        countc++;
-         sentencec[countc]=myCAM.getc();
+       getctimer.reset();
+       getctimer.start();
+       while(sentencec[countc]!='!' && countc!=1023 && getctimer.read()<2)  //timed out after 2 seconds recieving
+       {if(frx == 0){inspeech = 0;}
+         if(myCAM.readable())   // we make sure we get all the bit sent.
+         {countc++;
+         sentencec[countc]=myCAM.getc();}
        }
+       getctimer.stop();
      smartcamspeak = 1;
+     
+ 
  }
-void speakIRQ()
-{frx.fall(NULL);
+
+void setblt()   //to program the bluetooth master module for RFGID
+{BLTEN = 1;
+  wait (5);
+ myTTS.printf("Sset up bluetooth\r");
+  wait (5);
+  myCAM.printf("AT+RMAAD\r\n");   //AT+RMAAD AT+ROLE=1 AT+RESET
+        wait(1);
+        myCAM.printf("AT+ROLE=1\r\n"); 
+    wait(1); 
+    myCAM.printf("AT+PSWD=");
+    myCAM.putc('"');
+    myCAM.printf("1234");
+    myCAM.putc('"');
+    myCAM.printf("\r\n");
+    wait(1);
+    myCAM.printf("AT+PAIR=%s,9\r\n",slave);
+    wait(9);
+    myCAM.printf("AT+BIND=%s\r\n",slave);
+    wait(2);
+    myCAM.printf("AT+CMODE=1\r\n");
+    wait(1);
+     myCAM.printf("AT+LINK=%s\r\n",slave);
+    wait(2);
+          myTTS.printf("Sfinish set up\r");
+          myCAM.printf("AT+RESET\r\n"); 
+          wait(2);
+          
+          BLTEN = 0;
+}
+
+void speakIRQ()  //tracking interrupt for TTS
+{inspeech = 0;
+   
+ /*
+ frx.fall(NULL);
  if(rfidspeak == 1)
  {      myTTS.putc('X'); 
         myTTS.putc('\r'); 
@@ -69,49 +115,104 @@
     myTTS.putc('X'); 
     myTTS.putc('\r'); 
      }
+     */
 }
 
 int main()
-{      BLSB.mode(PullDown);
+{    
+       BLSB.mode(PullDown);
        BMSB.mode(PullDown);
-      frx.fall(NULL);
-       myTTS.attach(&readerRX,Serial::RxIrq);
+       SPD.mode(PullUp);
+       VOI.mode(PullUp);
+       bool SET_SPD=0;
+       bool SET_VOI=0;
+       bool VOIP=0;   //avoid multiple press issue(only one release with one press with one release will be counted as one press)
+       bool SPDP=0;
+  frx.fall(speakIRQ);  //interrupt to track TTS status
+       myTTS.attach(&readerRX,Serial::RxIrq);  //Serial Rx interrupt for UART
       myCAM.attach(&camRX,RawSerial::RxIrq);
-        int volume = 18;
-        myTTS.printf("V%D\r",volume);
+        int volume = 20;
+        myTTS.printf("V%d\r",volume);
         while (frx == 1); 
-    
-        int voice = 0;
-        myTTS.printf("N%D\r",voice);
-       while (frx == 1); 
        
      
      
         //Plain text demo
+         myTTS.printf("W%d\r",150);
+           while (frx == 1); 
+         myTTS.printf("N%d\r",1);
         myTTS.printf("S");//Speak command starts with "S"
-        myTTS.printf("Hellow");  // Send the desired string to convert to speech
+        myTTS.printf("Keyecus! ");  // Send the desired string to convert to speech
         myTTS.printf("\r"); //marks end of speak command
          while (frx == 1); 
          //ready waits for speech to finish from last command with a ":" response
-
+     int speed = 200;
+       int speedfast=speed+120;
+         myTTS.printf("W%d\r",speed);
+       while (frx == 1); 
+         int voice = 2;
+        myTTS.printf("N%d\r",voice);
+       while (frx == 1); 
 
    //     frx.fall(&speakIRQ);
    int timedout = 0;
-      BLSB.mode(PullDown);
+      BLSB.mode(PullDown);  
        BMSB.mode(PullDown);
-      frx.fall(NULL);
        myTTS.attach(&readerRX,Serial::RxIrq);
       myCAM.attach(&camRX,RawSerial::RxIrq);
+  //    myCAM.baud(9600);
+    /*   myCAM.printf("AT+ORGL");
+         wait(0.5);
+       
+     //      myCAM.printf("AT+CMODE=0");
+           
+     //   wait(10); 
+        
+      //     myCAM.printf("AT+INQM=0,5,5"); 
+    //    wait(0.5);
+     /*   X   myCAM.printf("AT+CPSWD=");
+           myCAM.putc('"');
+            myCAM.printf("9999");
+             myCAM.putc('"'); */
+   /*     wait(0.5);              //changemode
+           myTTS.printf("Sdisconnect the power!\r");
+           wait (10);
+            myCAM.baud(9600);
+           myTTS.printf("Spairing\r");
+         myCAM.printf("AT+PAIR=4444,10,059266,9\r\n");
+         wait(9);
+        
+          myTTS.printf("SBinding\r"); */
+    /*     myCAM.printf("AT+BIND=4444,10,059266\r\n");
+         wait(10);
+         myTTS.printf("Sseting mode\r");
+         wait(2);
+          myTTS.printf("Slinking\r");
+          myCAM.printf("AT+LINK=4444,10,059266\r\n");
+          wait(10);
+          myTTS.printf("Sfinish set up\r");
+          myCAM.printf("AT+RESET\r\n"); */
+     //      myCAM.printf("AT+NAMErfid\r\n");
+  //    wait(2);
+  //    myCAM.printf("AT+PIN1234\r\n");
+ //     wait(2);
       bool jump = 0;
-      bool rfidspoken = 0;
-      bool smartcamspoken = 0;
+      bool rfidspoken = 0;      //used to balance the priority between SmartCam and RFID
+      bool smartcamspoken = 0;  //used to balance the priority between SmartCam and RFID
+
+      uint16_t head=0;
+      uint16_t waist=0;
+      uint16_t foot=0;
+      uint16_t confirm = 25000;  //smaller=faster response, N of Cycle comfirmed before telling the user about the position of the obstacle  bigger(>30000 make a huge different)=more accurate
+
+      myCAM.baud(9600); 
     while(1) {
-        if(inspeech == 1)
+        if(inspeech == 1)     //just incase if the MCU failed to track the TTS due to other interrupt(unlikely to happen unless circuit broke), we make sure it does not wait forever.
         {timedout++;}
         else
         {timedout = 0;}
         if(timedout > 10000000)
-        {inspeech = 0;
+        {inspeech = 0;                
          timedout = 0;
         }
         
@@ -131,7 +232,11 @@
 smartcamspoken = 0;}
 
  if(rfidspeak == 1 && inspeech == 0 && jump == 0)
- {      myTTS.putc('X'); 
+ {     head = 0;
+     waist = 0;
+     foot = 0;
+     myTTS.printf("W%d\r",speed);
+       myTTS.putc('X'); 
         myTTS.putc('\r'); 
         myTTS.putc('S'); 
          myTTS.printf("I see the "); 
@@ -147,7 +252,11 @@
   
   
   else if (smartcamspeak == 1 && inspeech == 0)
-  {   myTTS.putc('X'); 
+  {  head = 0;
+     waist = 0;
+     foot = 0;
+     myTTS.printf("W%d\r",speed);
+      myTTS.putc('X'); 
         myTTS.putc('\r'); 
         myTTS.putc('S'); 
          myTTS.printf("Your location is "); 
@@ -156,39 +265,157 @@
        {myTTS.putc(sentencec[i]);}
        myTTS.putc('\r');
    //   while(frx == 1);
-  smartcamspeak = 0;
-  inspeech = 1;
-  smartcamspoken = 1;
+  smartcamspeak = 0;    //used to balance the priority between SmartCam and RFID
+  inspeech = 1;         //record the state of the TTS
+  smartcamspoken = 1;   //used to balance the priority between SmartCam and RFID
   }
- 
-     else  if(BMSB == 1 && BLSB == 1 && inspeech == 0)
-       { myTTS.putc('X'); 
+     else if(SET_VOI == 1 && inspeech == 0)
+    { head = 0;
+     waist = 0;
+     foot = 0;
+      voice++;
+      if(voice == 9)
+      {voice = 0;}
+        myTTS.printf("N%d\r",voice);
+         myTTS.printf("W%d\r",speed);
+        myTTS.putc('X'); 
         myTTS.putc('\r'); 
         myTTS.putc('S'); 
-         myTTS.printf("Barriers at head");  
+         myTTS.printf("Voice!"); 
           myTTS.putc('\r'); 
-         inspeech = 1;
+          inspeech = 1;
+        inspeech = 1;
+        SET_VOI=0;
+    }
+ 
+     else  if(BMSB == 1 && BLSB == 1 && foot<confirm && waist < confirm && head < confirm)
+       { if(foot<confirm && waist < confirm)
+        {head++;
+        foot =0;
+        waist = 0;
+        }
+       if(head < confirm && inspeech == 0)
+       { myTTS.printf("W%d\r",speedfast);
+        myTTS.putc('X'); 
+        myTTS.putc('\r'); 
+        myTTS.putc('S'); 
+         myTTS.printf("Obstacle!"); 
+          myTTS.putc('\r'); 
+          inspeech = 1;
+    //      while(frx == 1);
+      }
        //   while(frx == 1);
           }
-      else  if(BMSB == 1 && BLSB == 0 && inspeech == 0)
-        {  myTTS.putc('X'); 
+      else  if(BMSB == 1 && BLSB == 0 && foot<confirm && waist < confirm && head < confirm)
+        {if(foot<confirm && head < confirm)
+        {waist++;
+         head = 0;
+         foot = 0;}
+       if(waist< confirm && inspeech == 0)
+       {myTTS.printf("W%d\r",speedfast);
+        myTTS.putc('X'); 
         myTTS.putc('\r'); 
         myTTS.putc('S'); 
-         myTTS.printf("barriers at waist");
+         myTTS.printf("Obstacle!"); 
           myTTS.putc('\r'); 
-         inspeech = 1;
+          inspeech = 1;
+    //      while(frx == 1);
+      }
       //    while(frx == 1); 
       }
-      else  if(BMSB == 0 && BLSB == 1 && inspeech == 0)
-         {  myTTS.putc('X'); 
+      else  if(BMSB == 0 && BLSB == 1 && foot<confirm && waist < confirm && head < confirm)
+         {if(head<confirm && waist < confirm)
+          {foot++;
+          waist = 0;
+          head = 0;  
+          }
+       if(foot < confirm && inspeech == 0)
+       {myTTS.printf("W%d\r",speedfast);
+        myTTS.putc('X'); 
+        myTTS.putc('\r'); 
+        myTTS.putc('S'); 
+         myTTS.printf("Obstacle!"); 
+          myTTS.putc('\r'); 
+          inspeech = 1;
+    //      while(frx == 1);
+      }
+    }
+       else if(SET_SPD == 1 && inspeech == 0)
+    { head = 0;
+     waist = 0;
+     foot = 0;
+      speed=speed+75;
+     if(speed > 600)
+      {speed = 75;}
+     speedfast=speed+120;
+      if(speedfast>600)
+      {speedfast = 600;}
+        myTTS.printf("W%d\r",speed);
+        myTTS.putc('X'); 
+        myTTS.putc('\r'); 
+        myTTS.putc('S'); 
+         myTTS.printf("speed!"); 
+          myTTS.putc('\r'); 
+          inspeech = 1;
+        inspeech = 1;
+        SET_SPD=0;
+      }
+    else if(head >= confirm  && inspeech == 0)
+        {myTTS.printf("W%d\r",speedfast);
+            myTTS.putc('X'); 
         myTTS.putc('\r'); 
         myTTS.putc('S'); 
-         myTTS.printf("Barriers at foot"); 
+         myTTS.printf("head");  
+          myTTS.putc('\r'); 
+         inspeech = 1;
+         head = 0;
+         }
+    else  if(waist >= confirm && inspeech == 0)
+        {myTTS.printf("W%d\r",speedfast); 
+        myTTS.putc('X'); 
+        myTTS.putc('\r'); 
+        myTTS.putc('S'); 
+         myTTS.printf("waist");
+          myTTS.putc('\r'); 
+         inspeech = 1;
+          waist = 0;
+         }
+     else if(foot >= confirm && inspeech == 0)
+        {myTTS.printf("W%d\r",speedfast); 
+        myTTS.putc('X'); 
+        myTTS.putc('\r'); 
+        myTTS.putc('S'); 
+         myTTS.printf("foot"); 
           myTTS.putc('\r'); 
          inspeech = 1;
-    //      while(frx == 1);
+         foot = 0;
+         }
+  if(SPD == 0)
+     {if(VOI == 0)
+        {confirmblt++;
+         if(confirmblt >= 60000)
+         {setblt();
+         confirmblt = 0;
+         }
+           SPDP = 0;
+          VOIP = 0;
+        }
+     else
+     {confirmblt = 0;}
     }
-         
+     if(SPD == 0 && SPDP == 0) 
+     {SPDP =1;}   
+      if(SPD == 1 && SPDP ==1)
+      {SET_SPD = 1;
+       SPDP =0;
+      }
+     if(VOI == 0 && VOIP == 0) 
+     {VOIP =1;
+      }   
+      if(VOI == 1 && VOIP ==1)
+      {SET_VOI = 1;
+       VOIP =0;
+      }
        /*
        myTTS.printf("S");//Speak command starts with "S"
         myTTS.printf("Hellow");  // Send the desired string to convert to speech