receiver code which helps the vehicle to move forward, backward, left and right

Dependencies:   mbed Servo mbed-rtos XBeeTest Motor emic2 X_NUCLEO_53L0A1

Revision:
1:95f5ac2f045d
Parent:
0:25aca1983704
Child:
2:112ca6a4fc73
--- a/main.cpp	Tue Sep 29 21:31:14 2009 +0000
+++ b/main.cpp	Wed Dec 05 22:46:10 2018 +0000
@@ -1,36 +1,121 @@
-/**
- * XBee Example Test
- * A test application that demonstrates the ability
- * of transmitting serial data via an XBee module with
- * an mbed microprocesor.
- * By: Vlad Cazan
- * Date: Tuesday, September 29th 2009
- */
-
+// basic xbee example
+// - take chars from the terminal, push them out xbee1
+// - listen on xbee2, and print value + 1 to terminal
+#include "Motor.h"
 #include "mbed.h"
+#include "emic2.h"
+#include "XNucleo53L0A1.h"
+#include <stdio.h>
+Serial pc(USBTX, USBRX);
+DigitalOut shdn(p26);
 
-Serial xbee1(p9, p10); //Creates a variable for serial comunication through pin 9 and 10
-
-DigitalOut rst1(p11); //Digital reset for the XBee, 200ns for reset
+//I2C sensor pins
+#define VL53L0_I2C_SDA   p28
+#define VL53L0_I2C_SCL   p27
 
-DigitalOut myled(LED3);//Create variable for Led 3 on the mbed
-DigitalOut myled2(LED4);//Create variable for Led 4 on the mbed
+static XNucleo53L0A1 *board=NULL;
+DigitalOut shdn(p26);
+emic2 myTTS(p13, p14);
+Serial xbee1(p9, p10);
+DigitalOut rst1(p11);
 
-Serial pc(USBTX, USBRX);//Opens up serial communication through the USB port via the computer
+Motor B(p23, p6, p5); // pwm, fwd, rev, can brake, right
+Motor A(p24, p7, p8); // left motor
+
+char input;
+int counter;
 
 int main() {
-    rst1 = 0; //Set reset pin to 0
-    myled = 0;//Set LED3 to 0
-    myled2= 0;//Set LED4 to 0
-    wait_ms(1);//Wait at least one millisecond
-    rst1 = 1;//Set reset pin to 1
-    wait_ms(1);//Wait another millisecond
-
-    while (1) {//Neverending Loop
-        if (pc.readable()) {//Checking for serial comminication
-            myled = 0; //Turn Led 3 Off
-            xbee1.putc(pc.getc()); //XBee write whatever the PC is sending
-            myled = 1; //Turn Led 3 on for succcessfull communication
+    //initialize distance sensor
+    int status;
+    uint32_t distance;
+    DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
+    /* creates the 53L0A1 expansion board singleton obj */
+    board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
+    shdn = 0; //must reset sensor for an mbed reset to work
+    wait(0.1);
+    shdn = 1;
+    wait(0.1);
+    /* init the 53L0A1 board with default values */
+    status = board->init_board();
+    while (status) {
+        pc.printf("Failed to init board! \r\n");
+        status = board->init_board();
+    }
+    //loop taking and printing distance
+    while (1) {
+        status = board->sensor_centre->get_distance(&distance);
+        if (status == VL53L0X_ERROR_NONE) {
+            pc.printf("D=%ld mm\r\n", distance);
+        }
+    }
+    
+    
+    
+    myTTS.volume(2);
+    myTTS.voice(3);
+    counter = 0;
+    // reset the xbees (at least 200ns)
+    rst1 = 0;
+    wait_ms(1); 
+    rst1 = 1;
+    wait_ms(1); 
+    input = 'I';
+    A.speed(0);
+    B.speed(0);
+    while(1) {
+        if(xbee1.readable()) {
+            input = xbee1.getc();
+        }
+        A.speed(0);
+        B.speed(0);
+        
+        if (input == 'A')
+        {
+            myTTS.speakf("S");//Speak command starts with "S"
+            myTTS.speakf("forward");
+            myTTS.speakf("\r"); //marks end of speak command
+            myTTS.ready();
+            A.speed(0.5);
+            B.speed(-0.5);
+            wait(0.5);
+        } else if (input == 'B')
+        {
+            myTTS.speakf("S");//Speak command starts with "S"
+            myTTS.speakf("back");
+            myTTS.speakf("\r"); //marks end of speak command
+            myTTS.ready();
+            A.speed(-0.5);
+            B.speed(0.5);
+            wait(0.5);
+        } else if (input == 'C')
+        {
+            myTTS.speakf("S");//Speak command starts with "S"
+            myTTS.speakf("left");
+            myTTS.speakf("\r"); //marks end of speak command
+            myTTS.ready();
+            A.speed(0.5);
+            B.speed(0.5);
+            wait(0.5);
+        } else if (input == 'D')
+        {
+            myTTS.speakf("S");//Speak command starts with "S"
+            myTTS.speakf("right");
+            myTTS.speakf("\r"); //marks end of speak command
+            myTTS.ready();
+            A.speed(-0.5);
+            B.speed(-0.5);
+            wait(0.5);
+        } else if (input == 'E')
+        {
+            myTTS.speakf("S");//Speak command starts with "S"
+            myTTS.speakf("stop, open vicinity warning system");
+            myTTS.speakf("\r"); //marks end of speak command
+            myTTS.ready();
+            A.speed(0);
+            B.speed(0);
+            wait(0.5);
+            
         }
     }
 }
\ No newline at end of file