Catalin Craciun / ZumoRobotBluetoothControlled

Dependencies:   ZumoRobotUtilities mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
catalincraciun7
Date:
Thu Nov 13 15:43:35 2014 +0000
Parent:
2:53d1b48743a3
Child:
4:9bed0d58673d
Commit message:
Minor fixes and documentation added

Changed in this revision

Libs/ZumoRobotUtilities.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Libs/ZumoRobotUtilities.lib	Sun Nov 02 11:35:22 2014 +0000
+++ b/Libs/ZumoRobotUtilities.lib	Thu Nov 13 15:43:35 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/catalincraciun7/code/ZumoRobotUtilities/#036da44b023e
+http://mbed.org/users/catalincraciun7/code/ZumoRobotUtilities/#b1fde0759c94
--- a/main.cpp	Sun Nov 02 11:35:22 2014 +0000
+++ b/main.cpp	Thu Nov 13 15:43:35 2014 +0000
@@ -19,19 +19,68 @@
 Timer inputTimer;
 Serial bluetooth(PTC4, PTC3);
 
-char bluetoothInput[10];
+/**
+ *  The string receiver from bluetooth
+ */
+char bluetoothInput[20];
 float velocity;
 
+/**
+ *  This method waits for the robot's password in order to unlock it
+ */
+void waitForPassword();
+/**
+ *  Method for updating the robot
+ */
+void update();
+/**
+ *  Method for initialising the robot
+ *  It initialises the bluetooth's baud rate, the leds, creates a new instance of ZumoRobotManager and sets the 
+ * timer for controlling the bluetooth's transmission flow
+ */
+void init();
+
+void waitForPassword() {
+
+    // Turning off the leds
+    LEDBLUE = RGBLED_OFF; LEDRED = RGBLED_OFF; LEDGREEN = RGBLED_OFF;
+    while (true) {
+        // Waiting to read the password of the bluetooth
+        if (bluetooth.readable()) {
+            bluetooth.gets(bluetoothInput, 15);
+            if (zumoRobotManager -> checkPassword(bluetoothInput)) {
+                // Sending a "successfuly message" back to the device which sent the password
+                bluetooth.printf("$suc");
+                break;
+            } else {
+                // Sending a "failure message" back to the device which sent the password
+                bluetooth.printf("$fai");   
+            }
+        }
+    }
+    
+    update();
+}
+
 void update() {
+    
     // Updating loop
+
+    // Setting the timer for controlling the bluetooth flow
+    inputTimer.start(); Serial pc(USBTX, USBRX);
+    // Lighting the green led
+    LEDBLUE = RGBLED_OFF; LEDRED = RGBLED_OFF; LEDGREEN = RGBLED_ON;
     while (true) {
         if (inputTimer.read_ms() >= 10 && bluetooth.readable()) {
             inputTimer.reset();
-            bluetooth.gets(bluetoothInput, 8);
+            bluetooth.gets(bluetoothInput, 15);
+            // Checking the type of input received
             if ((bluetoothInput[0] == '+' || bluetoothInput[0] == '-') && 
                 (bluetoothInput[1] == '+' || bluetoothInput[1] == '-') &&
                 bluetoothInput[2] >= '0' && bluetoothInput[2] <='9' &&
-                bluetoothInput[3] >= '0' && bluetoothInput[3] <='9') {
+                bluetoothInput[3] >= '0' && bluetoothInput[3] <='9' &&
+                bluetoothInput[4] >= '0' && bluetoothInput[4] <='9' &&
+                bluetoothInput[5] >= '0' && bluetoothInput[5] <='9') {
                     /// Got a "velocity descriptor" string
                     velocity = ((bluetoothInput[2] - '0') * 10.0f + (bluetoothInput[3] - '0') * 1.0f) * 0.01f;
                     if (bluetoothInput[0] == '-')
@@ -42,41 +91,53 @@
                     if (bluetoothInput[1] == '-')
                         velocity = -velocity;
                     zumoRobotManager -> setVelocityY(velocity);
-            } else if (bluetoothInput[0] == 'c') {
-                /// Got a "colour descriptor" string
-                char colour = bluetoothInput[1];
-                
-                if (colour == 'r') {
+            } else if (bluetoothInput[0] == 'c' &&
+                       bluetoothInput[1] == '$') {
+                // Got a "colour macro command" (macro command referring to the color of the RGB led on robot)
+                char colour = bluetoothInput[2];
+                if (colour == 'r') { 
                     LEDRED = RGBLED_ON; LEDBLUE = RGBLED_OFF; LEDGREEN = RGBLED_OFF;   
                 }  else if (colour == 'g') {
                     LEDRED = RGBLED_OFF; LEDBLUE = RGBLED_OFF; LEDGREEN = RGBLED_ON;
                 } else if (colour == 'b') {
                     LEDRED = RGBLED_OFF; LEDBLUE = RGBLED_ON; LEDGREEN = RGBLED_OFF;
                 }  
+            } else if (bluetoothInput[0] == '$') {
+                // Received a "macro command" (macro command = a command controlling the status of the device or so)
+                if (bluetoothInput[1] == 'o' &&
+                    bluetoothInput[2] == 'u' &&
+                    bluetoothInput[3] == 't') {
+                    // Connection should be ended
+                    break;   
+                }
             }
         }
     }
+    
+    // Connection was terminated, retrieving the "locked" status
+    waitForPassword();
 }
 
 void init() {
 
-    // Initialising the controller    
+    // Initialising the robot
     
+    // Creating a new instance of ZumoRobotManager
     zumoRobotManager = new ZumoRobotManager();
     
-    LEDBLUE = RGBLED_OFF;
-    LEDRED = RGBLED_OFF;
-    LEDGREEN = RGBLED_ON;
+    // Turning off the leds
+    LEDBLUE = RGBLED_OFF; LEDRED = RGBLED_OFF; LEDGREEN = RGBLED_OFF;
 
+    // Setting the bluetoot's baud rate for communication
     bluetooth.baud(9600);
-    
-    inputTimer.start();
 }
 
 int main() {
+    
+    // Calling init
+    init();
+    // Locking the robot
+    waitForPassword();
 
-    // Updating loop
-    
-    init();
-    update();
+    return 0;
 }