updated codes lads

Dependencies:   C12832 Servo mbed-rtos mbed

Fork of rtos_basic_team by WIT_EmbOS_Gr1

Files at this revision

API Documentation at this revision

Comitter:
Soldier7
Date:
Thu Jan 29 12:55:44 2015 +0000
Parent:
9:f60eeadabcb0
Commit message:
Added comments for more understanding (at least for me :)

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Jan 28 22:48:45 2015 +0000
+++ b/main.cpp	Thu Jan 29 12:55:44 2015 +0000
@@ -2,59 +2,64 @@
 #include "rtos.h"
 #include "Servo.h"
 #include "C12832.h"
-Servo s1(p21);
+
+Servo s1(p21); 
 Servo s2(p22);
- 
-AnalogIn p1(p17);
-//AnalogIn p2(p20); 
-Mutex dataIn_mutex;
+AnalogIn p1(p17); // Sonar sensor 1
+//AnalogIn p2(p20); // Sonar sensor 2 (not used yet)
+Mutex dataIn_mutex; 
 Mutex dataOut_mutex;
 
-// Globel variables 
+// Global variables
 float input_data;
 float output_data;
+C12832 lcd(p5, p7, p6, p8, p11); // lcd is an object from class C12832 initialised by p5,p7....
 
-C12832 lcd(p5, p7, p6, p8, p11);// lcd is an object from class c12832 initialised by p5,p7....
- 
-void sonar_thread(void const *args) {
+/* Thread Sonar - handles the input data from the sonar sensor, and display on the LCD screen.
+    @update input_data 
+    */
+void sonar_thread(void const *args)
+{
     while (true) {
-            dataIn_mutex.lock();
-            input_data= p1.read()*5;
-            lcd.cls();      // clear the display
-            lcd.locate(0,3);// the location where you want your charater to be displayed
-            lcd.printf("Thread one = %f\n", input_data);
-            dataIn_mutex.unlock();
-            Thread::wait(25);
+        dataIn_mutex.lock();
+        // Read the sensor and assign to input_data global variable
+        input_data= p1.read()*5;
+        // Display the input_data
+        lcd.cls();      // clear the display
+        lcd.locate(0,3);// the location where you want your charater to be displayed
+        lcd.printf("Thread one = %f\n", input_data);
+        // Handles the thread
+        dataIn_mutex.unlock();
+        Thread::wait(25);
     }
 }
- 
- void control_thread(void const *args) {
+
+/* Thread Control -  */
+void control_thread(void const *args)
+{
     while (true) {
-            dataIn_mutex.lock();
-            
-           if (input_data>0.8)
-         {
+        dataIn_mutex.lock();
+        // Modification on the input_data and assign to servo2
+        if (input_data>0.8) {
             s2=input_data*0.2;
-            }
-            else if(input_data<0.8)
-            {
+        } else if(input_data<0.8) {
             s2=-input_data*0.5;
-            }
-             
-     s1=input_data;
-    
-            dataIn_mutex.unlock();
-            Thread::wait(20);
+        }
+        // Assign input_data to servo1 without modification
+        s1=input_data; 
+        // Handles the thread
+        dataIn_mutex.unlock();
+        Thread::wait(20);
     }
 }
- 
-int main() {
-    Thread thread(sonar_thread);
-    Thread thread_1(control_thread);
-    
+
+/* Main method - Start threads and delay them by waiting. */
+int main()
+{
+    Thread thread(sonar_thread); // Start Sonar Thread
+    Thread thread_1(control_thread); // Start Control Thread
+    // Delay the threads
     while (true) {
-         
-        
-        Thread::wait(10);
+        Thread::wait(10); 
     }
-}
+}
\ No newline at end of file