GIU\ZF

Dependencies:   MCP23017 WattBob_TextLCD mbed-rtos mbed

Fork of rtos_basic by mbed official

Revision:
19:2044bb5d7f29
Parent:
17:a29ce6fc667c
Child:
20:202e0046527e
--- a/tasks/task_group1.cpp	Wed Mar 28 00:47:12 2018 +0000
+++ b/tasks/task_group1.cpp	Wed Mar 28 01:32:01 2018 +0000
@@ -1,9 +1,7 @@
 #include "core.h"
 namespace display{
-    //Display on MBED text display • odometer value • average speed
+    //Display on MBED text display: odometer value, average speed
     const float freq = 2.0f; //hz
-    
-    //I/O
     MCP23017 *port;
     WattBob_TextLCD *lcd;
     
@@ -69,11 +67,8 @@
     }
 namespace turnSignal{
     //Read the two turn indicator switches and flash appropriate
-        //indicator LEDs at a rate of 1Hz. If both switches are switched on
-        //then flash both indicator LEDs at a rate of 2Hz (hazard mode).
+        //indicator LEDs at a rate of 1Hz
     static const float freq = 0.5; //hz
-    
-    //I/O
     DigitalIn lSwitch(PORT_TURN_SIGNAL_SWITCH_LEFT);
     DigitalIn rSwitch(PORT_TURN_SIGNAL_SWITCH_RIGHT);
     
@@ -83,6 +78,9 @@
     static inline void hotLoop(){
         int a = lSwitch.read();
         int b = rSwitch.read();
+        
+        //If both switches are switched on
+        //then flash both indicator LEDs at a rate of 2Hz (hazard mode)
         if(a&&b){
              lLed.period(2.0f);
              rLed.period(2.0f);
@@ -115,12 +113,18 @@
         brakeIndicator::init();
         speedIndicator::init();
         
-        const int const_delay = int((1000.0f/freq)+0.5f);
-        int dynamic_delay = const_delay;
-        int tick = 0;
-        int max_exec_time = 0;
+        const int const_delay = int((1000.0f/freq)+0.5f); //ideal sched delay
+        int dynamic_delay = const_delay; //real sched delay (updated in loop)
+        
+        int tick = 0; //freq subsampler
+        #if DEBUG_MODE
+        int max_exec_time = 0; //for logging
+        #endif
         
         while(true){
+            //Determine scheduling compensators:
+                //1: release time drift 
+                //2: execution time 
             sleepTimer.stop();
             executionTimer.start();
             int sleepTime = sleepTimer.read_ms();
@@ -128,8 +132,7 @@
                                         (sleepTime - dynamic_delay) : 0;
             
             
-            // Run all tasks
-           
+            // Run all tasks--------------
             brakeIndicator::hotLoop();
             
             static const int tick_interval_sIndicator = int((freq/speedIndicator::freq)+0.5f);
@@ -141,15 +144,17 @@
             if (!(tick%tick_interval_tSignal)){
                 turnSignal::hotLoop();
             }
-            
             display::hotLoop();
+            //--------------Completed tasks
             
             tick++;
             executionTimer.stop();
             int exec_time = executionTimer.read_ms();
-            if (exec_time > max_exec_time) max_exec_time=exec_time;
+            
             
             #if DEBUG_MODE
+            //Debug Logs (once per dequeue call to avoid memory issues)
+            if (exec_time > max_exec_time) max_exec_time=exec_time;
             static const int tick_interval_debug_log = int((freq/dequeueMail::freq)+0.5f);
             if (!(tick%tick_interval_debug_log)){
                 runTimeParams::debugAccess.lock();
@@ -161,7 +166,7 @@
             #else
             static const int tick_interval_debug_log = 1;
             #endif
-            
+            //Reset tick count
             static const int tick_LCM = 
                 tick_interval_debug_log*
                 tick_interval_sIndicator*
@@ -169,9 +174,11 @@
                 
             if (tick==tick_LCM) tick=0;
             
+            
             executionTimer.reset();
             sleepTimer.reset();
             sleepTimer.start();
+            //compensate for delays
             dynamic_delay = const_delay - (exec_time + drift);
             Thread::wait(dynamic_delay);
         }