Embedded Software Assignment 3

Dependencies:   MCP23017 Servo WattBob_TextLCD mbed-rtos mbed

Fork of ES_Assignment_3_Pub by M L

Revision:
1:15edb3b6763a
Parent:
0:6b3496e7a954
--- a/main.cpp	Fri May 27 15:56:36 2016 +0000
+++ b/main.cpp	Fri May 27 17:02:52 2016 +0000
@@ -11,6 +11,8 @@
                     For the Display it uses an LCD Screen
                     For the Speedometer it uses an Servo
                     The Emulation and Synchronisation is implemented using TimerThreads and Semaphores
+                    for the reason of existing thread limits, conventional timers are helping to slicing
+                    timeslots
 
 @version 1.4
 @updateDate 30.03.2016
@@ -57,7 +59,7 @@
 void Timer3_void(void const *args);                                 // Timer 3
 
 
-// Task Functions
+                                                                    // Task Functions
 void task1_break_accelerate();
 void task2_read_show_engine_state();
 void task3_show_odometer();
@@ -70,6 +72,8 @@
 void task10_calc_avg_speed();
 void task11_emulate_car();
 
+
+                                                                    // Init Variables
 int Convert_Hz_to_Ms(double Hz);
 
 float accerlator(0);
@@ -87,11 +91,12 @@
 bool sw_timer11(0);
 bool sw_timer2(0);
 bool sw_timer21(0);
-int sw_timer3(4);                                                   // initalize with a first run
+int sw_timer3(4);                                                   // sw_timer3 initalize with a first run
 
 std::deque<float> AvgSpeedDB;                                       // used for storing the average speed
 
-Semaphore SemAvgSpeedDB(1);
+                                                        
+Semaphore SemAvgSpeedDB(1);                                         // declare used Semaphores
 Semaphore SemAvgSpeed(1);
 Semaphore SemSpeed(1);
 Semaphore SemBreak_Accelerate(1);
@@ -127,7 +132,7 @@
     lcd->cls();                                                     // clear display
     lcd->locate(0,0);                                               // set cursor to location (0,0) - top left corner
 
-    RtosTimer Timer1(Timer1_void,osTimerPeriodic,(void *)NULL);
+    RtosTimer Timer1(Timer1_void,osTimerPeriodic,(void *)NULL);     // create the necesarry timers to overcome a thread issue (max threads)
     Timer1.start(Convert_Hz_to_Ms(20.0));
 
     RtosTimer Timer2(Timer2_void,osTimerPeriodic,(void *)NULL);
@@ -156,7 +161,7 @@
     if(sw_timer1) {                                                 // runs just every second time, so at 10 hz
         task1_break_accelerate();
         sw_timer11 = !sw_timer11;
-        if(sw_timer11) {                                            // runs just every second time, so at 5 hz
+        if(sw_timer11) {                                            // runs just every fourth time, so at 5 hz
             task10_calc_avg_speed();
         }
     }
@@ -197,7 +202,7 @@
         task8_read_single_side_light();
         sw_timer21 = !sw_timer21;
 
-        if (!indicator_R && indicator_L) {
+        if (!indicator_R && indicator_L) {                          // switch the left / right indicator
             DoutLEDRight=0;
             DoutLEDLeft=!DoutLEDLeft;
         } else if(indicator_R && !indicator_L) {
@@ -226,8 +231,8 @@
 {
     task6_fill_mail_queue();
     if((sw_timer3%4)==0) {                                          // task runs at 0.05 Hz
-        task7_dump_mail_to_serial();
-        sw_timer3=0;
+        task7_dump_mail_to_serial();                                // dump the queue to serial
+        sw_timer3=0;                                                // reset the timer
     }
     sw_timer3++;
 }
@@ -238,9 +243,12 @@
 */
 void task1_break_accelerate()
 {
+                                                                        // Let the Semaphores wait
     SemBreak_Accelerate.wait();
-    accerlator = AinAccel;
-    brake = AinBreak;
+    
+    accerlator = AinAccel;                                              // save the accerlator value
+    brake = AinBreak;                                                   // save the brake value
+                                                                        // Let the Semaphores release
     SemBreak_Accelerate.release();
 }
 
@@ -250,9 +258,12 @@
 */
 void task2_read_show_engine_state()
 {
+                                                                        // Let the Semaphores wait
     SemEngine.wait();
-    engine = DinSwitchEngine;
-    DoutLEDEngine = engine;
+    
+    engine = DinSwitchEngine;                                           // read the engine state
+    DoutLEDEngine = engine;                                             // write the engine state
+                                                                        // Let the Semaphores release
     SemEngine.release();
 }
 
@@ -262,8 +273,10 @@
 */
 void task3_show_odometer()
 {
-    SemAvgSpeed.wait();
-    Odometer = avgSpeed/250.0;
+                                                                        // Let the Semaphores wait
+    SemAvgSpeed.wait();                                                 
+    Odometer = avgSpeed/250.0;                                          // Calculate the odometer
+                                                                        // Let the Semaphores release
     SemAvgSpeed.release();
 }
 
@@ -273,11 +286,13 @@
 */
 void task4_speed_warning()
 {
+                                                                        // Let the Semaphores wait
     SemAvgSpeed.wait();
-    if(avgSpeed>75.0)
-        LEDSpeedWarning = !LEDSpeedWarning;
+    if(avgSpeed>75.0)                                                   // check our speed
+        LEDSpeedWarning = !LEDSpeedWarning;                             // and switch the Warning on/off
     else
         LEDSpeedWarning = 0;
+                                                                         // Let the Semaphores release
     SemAvgSpeed.release();
 }
 
@@ -287,7 +302,7 @@
 */
 void task5_update_odometer()
 {
-    // Let the Semaphores wait
+                                                                    // Let the Semaphores wait
     SemDistance.wait();
     SemAvgSpeed.wait();
 
@@ -295,8 +310,7 @@
     lcd->printf("s: %5.0f",avgSpeed);
     lcd->locate(1,0);
     lcd->printf("d: %5.0f",dist);
-
-    // Let the Semaphores release
+                                                                    // Let the Semaphores release
     SemDistance.release();
     SemAvgSpeed.release();
 }
@@ -308,19 +322,19 @@
 */
 void task6_fill_mail_queue()
 {
-    // Let the Semaphores wait
+                                                                                // Let the Semaphores wait
     SemMailCnT.wait();
     SemBreak_Accelerate.wait();
     SemSpeed.wait();
 
-    mail_t *mail = mail_box.alloc();
-    mail->speed = speed;
+    mail_t *mail = mail_box.alloc();                                            // reserve the space for our new message
+    mail->speed = speed;                                                        // fill with values
     mail->accel = accerlator;
     mail->brake = brake;
-    mail_box.put(mail);
+    mail_box.put(mail);                                                         // put the new message into the mail queue
     mailcounter++;
 
-    // Let the Semaphores release
+                                                                                // Let the Semaphores release
     SemBreak_Accelerate.release();
     SemSpeed.release();
     SemMailCnT.release();
@@ -331,7 +345,7 @@
 */
 void task7_dump_mail_to_serial()
 {
-    // Let the Semaphores wait
+                                                                                // Let the Semaphores wait
     SemMailCnT.wait();
 
     while(mailcounter) {                                                        // as long as we got mail
@@ -347,7 +361,7 @@
         mailcounter--;
     }
 
-    // Release the Semaphores
+                                                                                // Release the Semaphores
     SemMailCnT.release();
 }
 
@@ -356,7 +370,7 @@
 */
 void task8_read_single_side_light()
 {
-    DoutLEDLight = DinSwitchLight;
+    DoutLEDLight = DinSwitchLight;                                             // Reading the value
 }
 
 
@@ -365,8 +379,8 @@
 */
 void task9_read_indicators()
 {
-    indicator_R = DinSwitchRindic;
-    indicator_L = DinSwitchLindic;
+    indicator_R = DinSwitchRindic;                                            // Reading the value
+    indicator_L = DinSwitchLindic;                                            // Reading the value
 }
 
 
@@ -376,16 +390,16 @@
 void task10_calc_avg_speed()
 {
 
-    // Let the Semaphores wait
+                                                                              // Let the Semaphores wait
     SemAvgSpeed.wait();
     SemAvgSpeedDB.wait();
 
     float sum(0);
     for(deque<float>::const_iterator i = AvgSpeedDB.begin(); i != AvgSpeedDB.end(); ++i)
-        sum+= *i;                                                    // calculate the average by iterating over the queue
+        sum+= *i;                                                             // calculate the average by iterating over the queue
     avgSpeed = sum/AvgSpeedDB.size();
 
-    // Release the Semaphores
+                                                                              // Release the Semaphores
     SemAvgSpeedDB.release();
     SemAvgSpeed.release();
 }
@@ -396,7 +410,7 @@
 */
 void task11_emulate_car()
 {
-    // Let the Semaphores wait
+                                                                              // Let the Semaphores wait
     SemAvgSpeed.wait();
     SemAvgSpeedDB.wait();
     SemDistance.wait();
@@ -404,19 +418,19 @@
     SemSpeed.wait();
     SemEngine.wait();
 
-    if(accerlator<=brake || !engine)
+    if(accerlator<=brake || !engine)                                        // are we braking more than accelerating? is the engine on?
         speed = 0;
     else
         speed = (accerlator-brake) *0.5 +speed;
     if(speed>250)
-        speed=250; //maximum speed
-    if(AvgSpeedDB.size()>=4)                                        // if we already got 4 values, we have to
-        AvgSpeedDB.pop_front();                                     // make space by deleting the oldest value
-    AvgSpeedDB.push_back(speed);                                    // safe a new reading
+        speed=250;                                                           // maximum speed
+    if(AvgSpeedDB.size()>=4)                                                 // if we already got 4 values, we have to
+        AvgSpeedDB.pop_front();                                              // make space by deleting the oldest value
+    AvgSpeedDB.push_back(speed);                                             // safe a new reading
 
-    dist += speed * 1.0/20.0;                                       // runs at 20 Hz so we have to take this into account
+    dist += speed * 1.0/20.0;                                                // runs at 20 Hz so we have to take this into account
 
-    // Release the Semaphores
+                                                                             // Release the Semaphores
     SemDistance.release();
     SemAvgSpeed.release();
     SemAvgSpeedDB.release();