Xbee robot with telemetry, controller code

Dependencies:   4DGL-uLCD-SE DebounceIn PinDetect mbed-rtos mbed

Fork of 2Xbee_Controller by Hanbin Ying

Revision:
2:887ff47839ed
Parent:
1:2c67639795a4
--- a/main.cpp	Fri Apr 29 19:08:24 2016 +0000
+++ b/main.cpp	Fri Apr 29 19:42:40 2016 +0000
@@ -18,19 +18,19 @@
 //reverse   18
 //right     19
 
-Serial pc(USBTX, USBRX);
-DigitalOut led1(LED1);
+DigitalOut led1(LED1); //four leds to display the command users send in
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
-Mutex xbee_lock;
-Mutex uLCD_lock;
-int a;
+Mutex uLCD_lock; //mutex lock for the uLCD communication
 volatile int voltage;
 volatile int distance;
 
 
-void read_battery()
+void read_battery() 
+//read battery information back from the robot, need a mutex lock when 
+//the information is written to the variable, otherwise "display_battery" 
+//thread will try to access the the variable while it's changed
 {
 
     xbee1.putc(15);
@@ -42,7 +42,10 @@
     //pc.printf("voltage: %i\n",voltage);
 }
 
-void read_distance()
+void read_distance() 
+//read distance information back from the robot, need a mutex lock when 
+//the information is written to the variable, otherwise "display_distance" 
+//thread will try to access the the variable while it's changed
 {
     xbee1.putc(16);
     //if (xbee1.readable()) {
@@ -52,13 +55,14 @@
     //}
     //pc.printf("distance: %i\n",distance);
 }
-void display_distance(void const *args)
+void display_distance(void const *args) //update the distance sensor information in the LCD once every 0.3s
 {
     while(1) {
 
         uLCD_lock.lock();
         uLCD.locate(1,5);
-        float temp = float(1/(float(distance)/232*3.3))*30.53-2.64;
+        float temp = float(1/(float(distance)/232*3.3))*30.53-2.64; //distance*232 scales reading back to a float between 0-1. Then a linear
+                                                                    //approximation is used to convert float reading to cm reading
         uLCD.printf("%3.3fcm",temp);
         uLCD_lock.unlock();
         //pc.printf("%i\n",distance);
@@ -67,7 +71,7 @@
     }
 }
 
-void display_battery(void const *args)
+void display_battery(void const *args) //update the battery information in the LCD once every 0.3s
 {
     while(1) {
         uLCD_lock.lock();
@@ -91,21 +95,18 @@
     wait_ms(100);
     rst1 = 1;
     wait_ms(100);
-    uLCD.baudrate(3000000);
+    uLCD.baudrate(3000000); //update the 
     uLCD.cls();
     wait(.01);
     uLCD.locate(1,2);
     uLCD.printf("Battery:\n");
     uLCD.locate(1,4);
     uLCD.printf("Distance:\n");
-    //Thread thread2(read_distance);
-    //Thread thread3(read_battery);
     Thread thread4(display_distance);
     Thread thread5(display_battery);
 
-    //led1=1;
-    //}
     while (1) {
+        // loop to check if each pushbutton is pushed. If pushed, send a corresponding command to the robot
         if (forward==0) {
             led1 = 1;
             xbee1.putc(11);
@@ -120,9 +121,9 @@
             xbee1.putc(14);
         } else {
             xbee1.putc(0);
-            led1=led2=led3=led4=0;
+            led1=led2=led3=led4=0; //reset all LED at the end of loop
         }
-        read_distance();
-        read_battery();
+        read_distance(); //request distance information at the end of every loop
+        read_battery(); //request battery information at the end of every loop
     }
 }
\ No newline at end of file