Brandon Weiner and Carlos Tallard / Mbed 2 deprecated Bluetooth-Controlled_LIDAR_Robot_LiveStreamingRaspberryPi

Dependencies:   4DGL-uLCD-SE1 Motor SDFileSystem X_NUCLEO_53L0A1 mbed-rtos mbed BotwithWavePlayerLevel

Fork of BotWithBluetoothLIDARV2 by Brandon Weiner and Carlos Tallard

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "Motor.h"
00004 #include "rtos.h"
00005 
00006 #define SD_DBG             1
00007 
00008 #include "SDFileSystem.h"
00009 SDFileSystem sd(p5, p6, p7, p29, "sd");
00010 
00011 
00012 #include "wave_player.h"
00013 AnalogOut DACout(p18);
00014 wave_player waver(&DACout);
00015 
00016 #include "XNucleo53L0A1.h"
00017 #include <stdio.h>
00018 Serial pc(USBTX,USBRX);
00019 DigitalOut shdn(p26);
00020 
00021 //I2C sensor pins
00022 #define VL53L0_I2C_SDA   p28
00023 #define VL53L0_I2C_SCL   p27
00024 static XNucleo53L0A1 *board=NULL;
00025 DigitalOut myled (LED2);
00026 Serial blue(p9,p10);
00027 Motor RW(p22, p13, p12); // pwm, fwd, rev
00028 Motor LW(p21, p8, p11); // pwm, fwd, rev
00029 Thread lidar;
00030 Thread siren;
00031 uint32_t distance;
00032 int level = 8; //Volume level is turned off
00033 
00034 
00035 //speaker thread
00036 void t3(){
00037    
00038     while(1){
00039                
00040     FILE *wave_file=fopen("/sd/LoudSound.wav","r");
00041     waver.play(wave_file);
00042     fclose(wave_file);
00043     
00044 }
00045 }
00046 
00047 //lidar thread
00048 void t2() {
00049     
00050     int status;
00051     DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
00052     /* creates the 53L0A1 expansion board singleton obj */
00053     board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
00054     shdn = 0; //must reset sensor for an mbed reset to work
00055     Thread::wait(100);
00056     //lidar power off
00057     shdn = 1;
00058     Thread::wait(100);
00059     /* init the 53L0A1 board with default values */
00060     status = board->init_board();
00061     while (status) {
00062         pc.printf("Failed to init board! \r\n");
00063         status = board->init_board();
00064     }
00065     //loop taking and printing distance
00066     while (1) {
00067         status = board->sensor_centre->get_distance(&distance);
00068         if (status == VL53L0X_ERROR_NONE) {
00069             pc.printf("D=%ld mm\r\n", distance);
00070             if(distance > 240) level = 8;
00071             if(distance < 90) level = 0;
00072             if (distance >=90 && distance <=240){
00073                  int value=floor(static_cast<double>(8*(distance-90)/150));
00074                  if(value >=0 && value<=8){
00075                      level=value;
00076                  } 
00077             }
00078         }
00079     myled=!myled;
00080     Thread::wait(500);
00081     }
00082     
00083     }
00084 
00085 
00086 //bot movement using bluetooth thread
00087 int main()
00088 {
00089     char previousButton='0';
00090     char current;
00091     myled=0;
00092     pc.printf("in main\n\r");
00093     lidar.start(t2);
00094     siren.start(t3);
00095     char bnum=0;
00096     char bhit=0;
00097     while(1) {
00098          
00099     if(blue.readable()==1){
00100         if (blue.getc()=='!') {
00101             if (blue.getc()=='B') { //button data packet
00102                 bnum = blue.getc(); //button number
00103                 bhit = blue.getc(); //1=hit, 0=release
00104                 current=bnum;  
00105                 previousButton = current; 
00106                 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { 
00107                     switch (bnum) {
00108                         case '1': //number button 1
00109                             if (bhit=='1') {
00110                                 RW.speed(1);
00111                             } else {
00112                                 //add release code here
00113                             break;
00114                         case '2': //number button 2
00115                             if (bhit=='1') {
00116                                 RW.speed(0);
00117                                 LW.speed(0);
00118                                 //add hit code here
00119                             } else {
00120                                 //add release code here
00121                             }
00122                             break;
00123                         case '3': //number button 3
00124                             if (bhit=='1') {
00125                                 //add hit code here
00126                             } else {
00127                                 //add release code here
00128                             }
00129                             break;
00130                         case '4': //number button 4
00131                             if (bhit=='1') {
00132                                 //add hit code here
00133                             } else {
00134                                 //add release code here
00135                             }
00136                             break;
00137                         case '5': //button 5 up arrow
00138                             if (bhit=='1') {
00139                                 RW.speed(1.0);
00140                                 LW.speed(1.0);  
00141                             } else {
00142                                 RW.speed(0); 
00143                                 LW.speed(0); 
00144                             }
00145                             break;
00146                         case '6': //button 6 down arrow
00147                             if (bhit=='1') {
00148                                 RW.speed(-1.0);
00149                                 LW.speed(-1.0);  
00150                                 //add hit code here
00151                             } else {
00152                                 RW.speed(0);
00153                                 LW.speed(0);
00154                             }
00155                             break;
00156                         case '7': //button 7 left arrow
00157                             if (bhit=='1') {
00158                                 RW.speed(-1.0);
00159                                 LW.speed(1.0);
00160                             } else {
00161                                 RW.speed(0);
00162                                 LW.speed(0);
00163                             }
00164                             break;
00165                         case '8': //button 8 right arrow
00166                             if (bhit=='1') {
00167                                 RW.speed(1.0);
00168                                 LW.speed(-1.0);
00169                             } else {
00170                                 RW.speed(0);
00171                                 LW.speed(0);
00172                             }
00173                             break;
00174                         default:
00175                             break;
00176                     }
00177                     }
00178                     }
00179                 }
00180           
00181             }
00182            
00183             Thread::yield();
00184             
00185         }
00186     }
00187    }