emic2

Dependencies:   mbed SoftSerial

main.cpp

Committer:
rickyqqj
Date:
2020-06-19
Revision:
3:677be555b290
Parent:
2:32f26fe1d8ad

File content as of revision 3:677be555b290:

#include "mbed.h"
#include "string"
Timer getctimer;

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 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;
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;
string slave="4444,10,059266";
void readerRX() {    //to recieve UART sent from smart cam
       count = 0;
       sentence[0] = 'S';
         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(){  // to recieve UART from RFID
  
     countc = 0;
       sentencec[0] = 'S';
       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 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'); 
        myTTS.putc('S'); 
         myTTS.printf("Your location "); 
      sentence[0] = 'S';
       for(int i=0; i<count+1; i++)
       {myTTS.putc(sentence[i]);}
       myTTS.putc('\r');
  
  rfidspeak = 0;
  }
  
  
  else if (smartcamspeak == 1)
  {   myTTS.putc('X'); //stop talking if reset and not a power on
     myTTS.putc('\r'); // Send a CR in case the system is already up
     myTTS.printf("S");//Speak command starts with "S"
    myTTS.printf("Hellow");  // Send the desired string to convert to speech
    myTTS.printf("\r"); //marks end of speak command
    smartcamspeak = 0;
  }
 else
 { wait(0.5);
    myTTS.putc('X'); 
    myTTS.putc('\r'); 
     }
     */
}

int main()
{    
       BLSB.mode(PullDown);
       BMSB.mode(PullDown);
       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 = 20;
        myTTS.printf("V%d\r",volume);
        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("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);  
       BMSB.mode(PullDown);
       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;      //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)     //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;                
         timedout = 0;
        }
        
  //    frx.fall(&speakIRQ);
   //   myTTS.putc('X'); 
  //  myTTS.putc('\r'); 
      //stop talking if reset and not a power on
if(frx == 0)
 {inspeech = 0;}
  //    frx.fall(NULL);

if(rfidspoken == 1 && smartcamspeak == 1)
{jump = 1;
rfidspoken = 0;}
if(smartcamspoken == 1)
{jump = 0;
smartcamspoken = 0;}

 if(rfidspeak == 1 && inspeech == 0 && jump == 0)
 {     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 "); 
      sentence[0] = 'S';
       for(int i=1; i<count+1; i++)
       {myTTS.putc(sentence[i]);}
       myTTS.putc('\r');
     // while(frx == 1);
  rfidspeak = 0;
  inspeech = 1;
  rfidspoken = 1;
  }
  
  
  else if (smartcamspeak == 1 && inspeech == 0)
  {  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 "); 
      sentencec[0] = 'S';
       for(int i=1; i<countc+1; i++)
       {myTTS.putc(sentencec[i]);}
       myTTS.putc('\r');
   //   while(frx == 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(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("Voice!"); 
          myTTS.putc('\r'); 
          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 && 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("Obstacle!"); 
          myTTS.putc('\r'); 
          inspeech = 1;
    //      while(frx == 1);
      }
      //    while(frx == 1); 
      }
      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("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;
         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
        myTTS.printf("\r"); //marks end of speak command
        while (frx == 1);  //ready waits for speech to finish from last command with a ":" response
        */
        
    /*    //Song Demo
        myTTS.speakf("D1\r");//Sing Song Demo
        myTTS.ready(); //member function wait
        myled = 0;
        //Voice Demo
        for (int i=0; i<9 ; i++) { //demo different voices
            myTTS.voice(i);
            myTTS.speakf("SHello this is a sample of voice number, %D\r",i);
            myTTS.ready();
        }
        myTTS.voice(0); //back to default voice
        //Number demo
        for (int i=10; i>=0 ; i--) { //demo different numbers
            myTTS.speakf("S%D.\r",i);
            myTTS.ready();
        }
        //Read Time (RTC) demo
        set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
        wait(2); //let RTC advance two seconds after setting to confirm it works
        time_t seconds = time(NULL);//read current time
        myTTS.speakf("STime in seconds since January 1, 1970 =, %d\r", seconds);
        myTTS.ready();
        myTTS.speakf("STime as a basic string = %s\r", ctime(&seconds));
        myTTS.ready();
        char buffer[32];
        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        myTTS.speakf("SThe current time is %s\r", buffer);
        myTTS.ready();
*/
    }
}