GIU\ZF
Dependencies: MCP23017 WattBob_TextLCD mbed-rtos mbed
Fork of rtos_basic by
Revision 17:a29ce6fc667c, committed 2018-03-28
- Comitter:
- ihexx
- Date:
- Wed Mar 28 00:26:55 2018 +0000
- Parent:
- 16:0ada6cbd41e2
- Child:
- 18:d48324fd3440
- Commit message:
- Fixed deadlock bug on debug framework
Changed in this revision
--- a/main.cpp Tue Mar 27 22:03:07 2018 +0000 +++ b/main.cpp Wed Mar 28 00:26:55 2018 +0000 @@ -8,12 +8,16 @@ Mutex liveAccess; float brakeForce = 0.0f; float accelForce = 0.0f; - float newSpeed = 0.0f; + float speed[3] = {0.0f}; float avgSpeed = 0.0f; float odometer = 0.0f; #if DEBUG_MODE - string debugLog = "task,execution_time_ms,lastSleep,drift\n\r"; + Mutex debugAccess; + + string debugLogBuffer1 = "task,execution_time_ms,lastSleep,drift\n\r"; + string debugLogBuffer2 = ""; + string * debugLog = &debugLogBuffer1; #endif }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tasks/MailTasks.cpp Wed Mar 28 00:26:55 2018 +0000 @@ -0,0 +1,146 @@ +#include "core.h" + +namespace mailData{ + Mail<mail_t, 100> mailBox; +} + +namespace enqueueMail{ + //Send speed, accelerometer and brake values to a 100 element MAIL queue + Thread thread; + const float freq = 0.2; //hz + + //I/O + void runTask(){ + + + Timer executionTimer,sleepTimer; + executionTimer.reset(); + sleepTimer.reset(); + + const int const_delay = int((1000.0f/freq)+0.5f); + int dynamic_delay = const_delay; + int tick = 0; + + while(1){ + sleepTimer.stop(); + executionTimer.start(); + int sleepTime = sleepTimer.read_ms(); + const int drift = ((sleepTime - dynamic_delay) > 0)? + (sleepTime - dynamic_delay) : 0; + //Core Loop: + using namespace mailData; + mail_t *mail = mailBox.alloc(); + + runTimeParams::liveAccess.lock(); + + mail->speed = runTimeParams::avgSpeed; + mail->accel = runTimeParams::accelForce; + mail->brake = runTimeParams::brakeForce; + + runTimeParams::liveAccess.unlock(); + + mailBox.put(mail); + //End of Core loop + + tick++; + executionTimer.stop(); + int exec_time = executionTimer.read_ms(); + + #if DEBUG_MODE + const int debug_log_interval = int(freq/dequeueMail::freq); + if (!(tick%debug_log_interval)){ + runTimeParams::debugAccess.lock(); + *runTimeParams::debugLog += "Enqueue Mail," + to_string(exec_time) + "," + + to_string(sleepTime) + "," + + to_string(drift) + "\n\r"; + runTimeParams::debugAccess.unlock(); + tick = 0; + } + #endif + + executionTimer.reset(); + sleepTimer.reset(); + sleepTimer.start(); + + dynamic_delay = const_delay - (exec_time + drift); + Thread::wait(dynamic_delay); + } + + } +} + +namespace dequeueMail{ + //Dump contents of feature_7 MAIL queue to the serial connection to the PC + Thread thread; + + const float freq = 0.05; //hz + + + + Serial pc(USBTX, USBRX); // tx, rx + + + void runTask(){ + Timer executionTimer,sleepTimer; + executionTimer.reset(); + sleepTimer.reset(); + + const int const_delay = int((1000.0f/freq)+0.5f); + int dynamic_delay = const_delay; + + pc.printf("speed,acceleration,brake\n\r"); + + while(true){ + + sleepTimer.stop(); + executionTimer.start(); + int sleepTime = sleepTimer.read_ms(); + const int drift = ((sleepTime - dynamic_delay) > 0)? + (sleepTime - dynamic_delay) : 0; + + /* Mail */ + using namespace mailData; + osEvent evt = mailBox.get(1); + while (evt.status == osEventMail){ + mail_t * mail = (mail_t*)evt.value.p; + pc.printf("%.2f,%.2f,%.2f\n\r",mail->speed,mail->accel,mail->brake); + mailBox.free(mail); + evt = mailBox.get(1); + } + + executionTimer.stop(); + int exec_time = executionTimer.read_ms(); + + #if DEBUG_MODE + runTimeParams::debugAccess.lock(); + *runTimeParams::debugLog += "Dequeue Mail," + to_string(exec_time) + "," + + to_string(sleepTime) + "," + + to_string(drift) + "\n\r"; + string * message; + if (runTimeParams::debugLog == & runTimeParams::debugLogBuffer1){ + runTimeParams::debugLog = & runTimeParams::debugLogBuffer2; + message = &runTimeParams::debugLogBuffer1; + } + else{ + runTimeParams::debugLog = & runTimeParams::debugLogBuffer1; + message = &runTimeParams::debugLogBuffer2; + } + *runTimeParams::debugLog = ""; + runTimeParams::debugAccess.unlock(); + + pc.printf(message->c_str()); + + + #endif + + executionTimer.reset(); + sleepTimer.reset(); + sleepTimer.start(); + dynamic_delay = const_delay - (exec_time + drift); + Thread::wait(dynamic_delay); + + } + } +} + + \ No newline at end of file
--- a/tasks/brakeIndicator.cpp Tue Mar 27 22:03:07 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -#include "core.h" - -namespace brakeIndicator{ - Thread thread; - const float freq = 2.0f; //hz - - //I/O - PwmOut led2(PORT_REDBOX_LED1); - - void runTask(){ -// Show use of the brake on a LED on the RedBox Unit - led2.period_ms(50); - while(1){ - runTimeParams::liveAccess.lock(); - led2.write(runTimeParams::brakeForce); - runTimeParams::liveAccess.unlock(); - wait(1/freq); - } - } -}
--- a/tasks/core.h Tue Mar 27 22:03:07 2018 +0000 +++ b/tasks/core.h Wed Mar 28 00:26:55 2018 +0000 @@ -37,9 +37,12 @@ extern float accelForce; extern float avgSpeed; extern float odometer; - extern float newSpeed; + extern float speed[3]; #if DEBUG_MODE - extern string debugLog; + extern Mutex debugAccess; + extern string debugLogBuffer1; + extern string debugLogBuffer2 ; + extern string * debugLog ; #endif } @@ -69,41 +72,6 @@ extern Mail<mail_t, 100> mailBox; } -namespace filterSpeed{ - //Filter speed with averaging filter - extern Thread thread; - extern const float freq; - void runTask(); -} - -namespace brakeIndicator{ - //Read brake and accelerator values from variable resistors - extern Thread thread; - extern const float freq; - void runTask(); -} - -namespace speedIndicator{ - //Read brake and accelerator values from variable resistors - extern Thread thread; - extern const float freq; - void runTask(); -} - -namespace sideLights{ - //Read brake and accelerator values from variable resistors - extern Thread thread; - extern const float freq; - void runTask(); -} - -namespace turnSignal{ - //Read brake and accelerator values from variable resistors - extern Thread thread; - extern const float freq; - void runTask(); -} - namespace enqueueMail{ //Send speed, accelerometer and brake values to a 100 element //MAIL queue
--- a/tasks/sendMail.cpp Tue Mar 27 22:03:07 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,136 +0,0 @@ -#include "core.h" - -namespace mailData{ - Mail<mail_t, 100> mailBox; -} - -namespace enqueueMail{ - //Send speed, accelerometer and brake values to a 100 element MAIL queue - Thread thread; - const float freq = 0.2; //hz - - //I/O - void runTask(){ - - - Timer executionTimer,sleepTimer; - executionTimer.reset(); - sleepTimer.reset(); - - const int const_delay = int((1000.0f/freq)+0.5f); - int dynamic_delay = const_delay; - int tick = 0; - - while(1){ - sleepTimer.stop(); - executionTimer.start(); - int sleepTime = sleepTimer.read_ms(); - const int drift = ((sleepTime - dynamic_delay) > 0)? - (sleepTime - dynamic_delay) : 0; - //Core Loop: - using namespace mailData; - mail_t *mail = mailBox.alloc(); - - runTimeParams::liveAccess.lock(); - - mail->speed = runTimeParams::avgSpeed; - mail->accel = runTimeParams::accelForce; - mail->brake = runTimeParams::brakeForce; - - runTimeParams::liveAccess.unlock(); - - mailBox.put(mail); - //End of Core loop - - tick++; - executionTimer.stop(); - int exec_time = executionTimer.read_ms(); - - #if DEBUG_MODE - const int debug_log_interval = int(freq/dequeueMail::freq); - if (!(tick%debug_log_interval)){ - runTimeParams::liveAccess.lock(); - runTimeParams::debugLog += "Enqueue Mail," + to_string(exec_time) + "," - + to_string(sleepTime) + "," - + to_string(drift) + "\n\r"; - runTimeParams::liveAccess.unlock(); - tick = 0; - } - #endif - - executionTimer.reset(); - sleepTimer.reset(); - sleepTimer.start(); - - dynamic_delay = const_delay - (exec_time + drift); - Thread::wait(dynamic_delay); - } - - } -} - -namespace dequeueMail{ - //Dump contents of feature_7 MAIL queue to the serial connection to the PC - Thread thread; - #if DEBUG_MODE - const float freq = 0.2; //hz - #else - const float freq = 0.05; //hz - #endif - - - Serial pc(USBTX, USBRX); // tx, rx - - - void runTask(){ - Timer executionTimer,sleepTimer; - executionTimer.reset(); - sleepTimer.reset(); - - const int const_delay = int((1000.0f/freq)+0.5f); - int dynamic_delay = const_delay; - - pc.printf("speed,acceleration,brake\n\r"); - - while(true){ - - sleepTimer.stop(); - executionTimer.start(); - int sleepTime = sleepTimer.read_ms(); - const int drift = ((sleepTime - dynamic_delay) > 0)? - (sleepTime - dynamic_delay) : 0; - - /* Mail */ - using namespace mailData; - osEvent evt = mailBox.get(1); - while (evt.status == osEventMail){ - mail_t * mail = (mail_t*)evt.value.p; - pc.printf("%.2f,%.2f,%.2f\n\r",mail->speed,mail->accel,mail->brake); - mailBox.free(mail); - evt = mailBox.get(1); - } - - executionTimer.stop(); - int exec_time = executionTimer.read_ms(); - - #if DEBUG_MODE - runTimeParams::liveAccess.lock(); - runTimeParams::debugLog += "Dequeue Mail," + to_string(exec_time) + "," - + to_string(sleepTime) + "," - + to_string(drift) + "\n\r"; - pc.printf(runTimeParams::debugLog.c_str()); - runTimeParams::debugLog = ""; - runTimeParams::liveAccess.unlock(); - #endif - - executionTimer.reset(); - sleepTimer.reset(); - sleepTimer.start(); - dynamic_delay = const_delay - (exec_time + drift); - Thread::wait(dynamic_delay); - - } - } -} - - \ No newline at end of file
--- a/tasks/sideLights.cpp Tue Mar 27 22:03:07 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -#include "core.h" - -namespace sideLights{ - Thread thread; - static const float freq = 1; //hz - - //I/O - DigitalIn lightSwitch(PORT_BRAKE); - DigitalOut led(PORT_SIDE_LIGHTS); - void runTask(){ - //Read a single side light switch and set side lights accordingly - while(1){ - wait(1/freq); - led = lightSwitch; - } - - } -}
--- a/tasks/speedIndicator.cpp Tue Mar 27 22:03:07 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -#include "core.h" - -namespace speedIndicator{ - Thread thread; - static const float freq = 1; //hz - - //I/O - DigitalOut led(PORT_REDBOX_LED1); - - void runTask(){ - //Monitor speed and if it goes over 88 mph switch on a LED on - //the RedBox unit - led = 0; - while(1){ - - - runTimeParams::liveAccess.lock(); - led = (runTimeParams::avgSpeed>88); - runTimeParams::liveAccess.unlock(); - wait(1/freq); - } - } -}
--- a/tasks/task_group1.cpp Tue Mar 27 22:03:07 2018 +0000 +++ b/tasks/task_group1.cpp Wed Mar 28 00:26:55 2018 +0000 @@ -28,6 +28,78 @@ lcd->printf("Speed=%.2f",avgSpeed); } } +namespace brakeIndicator{ + //Show use of the brake on a LED on the RedBox Unit + const float freq = 2.0f; //hz + PwmOut led2(PORT_REDBOX_LED1); + static inline void init(){ + led2.period_ms(50); + } + static inline void hotLoop(){ + runTimeParams::liveAccess.lock(); + led2.write(runTimeParams::brakeForce); + runTimeParams::liveAccess.unlock(); + + } +} +namespace speedIndicator{ + //Monitor speed and if it goes over 88 mph switch on a LED on + //the RedBox unit + static const float freq = 1; //hz + DigitalOut led(PORT_REDBOX_LED1); + static inline void init(){ + led = 0; + } + static inline void hotLoop(){ + runTimeParams::liveAccess.lock(); + led = (runTimeParams::avgSpeed>88); + runTimeParams::liveAccess.unlock(); + + } +} +namespace sideLights{ + //Read a single side light switch and set side lights accordingly + static const float freq = 1; //hz + DigitalIn lightSwitch(PORT_BRAKE); + DigitalOut led(PORT_SIDE_LIGHTS); + static inline void hotLoop(){ + led = lightSwitch; + } + + } +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). + static const float freq = 0.5; //hz + + //I/O + DigitalIn lSwitch(PORT_TURN_SIGNAL_SWITCH_LEFT); + DigitalIn rSwitch(PORT_TURN_SIGNAL_SWITCH_RIGHT); + + PwmOut lLed(PORT_TURN_SIGNAL_LED_LEFT); + PwmOut rLed(PORT_TURN_SIGNAL_LED_RIGHT); + + static inline void hotLoop(){ + int a = lSwitch.read(); + int b = rSwitch.read(); + if(a&&b){ + lLed.period(2.0f); + rLed.period(2.0f); + } + else{ + lLed.period(1.0f); + rLed.period(1.0f); + } + a ? lLed.write(0.5f) : lLed.write(0.0f); + b ? rLed.write(0.5f) : rLed.write(0.0f); + } + +} + + + + namespace task_group_1{ Thread thread; @@ -38,16 +110,19 @@ Timer executionTimer,sleepTimer; executionTimer.reset(); sleepTimer.reset(); + display::init(); + 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; + while(true){ sleepTimer.stop(); executionTimer.start(); - - int sleepTime = sleepTimer.read_ms(); const int drift = ((sleepTime - dynamic_delay) > 0)? (sleepTime - dynamic_delay) : 0; @@ -55,26 +130,45 @@ // Run all tasks - display::hotLoop(); + brakeIndicator::hotLoop(); + static const int tick_interval_sIndicator = int((freq/speedIndicator::freq)+0.5f); + if (!(tick%tick_interval_sIndicator)){ + speedIndicator::hotLoop(); + sideLights::hotLoop(); + } + static const int tick_interval_tSignal = int((freq/turnSignal::freq)+0.5f); + if (!(tick%tick_interval_tSignal)){ + turnSignal::hotLoop(); + } + display::hotLoop(); tick++; executionTimer.stop(); int exec_time = executionTimer.read_ms(); + if (exec_time > max_exec_time) max_exec_time=exec_time; #if DEBUG_MODE static const int tick_interval_debug_log = int((freq/dequeueMail::freq)+0.5f); if (!(tick%tick_interval_debug_log)){ - runTimeParams::liveAccess.lock(); - runTimeParams::debugLog += "task_group_1," + to_string(exec_time) + "," + runTimeParams::debugAccess.lock(); + *runTimeParams::debugLog += "task_group_1," + to_string(max_exec_time) + "," + to_string(sleepTime) + "," + to_string(drift) + "\n\r"; - runTimeParams::liveAccess.unlock(); + runTimeParams::debugAccess.unlock(); } - if (tick==tick_interval_debug_log*1) tick=0; + #else + static const int tick_interval_debug_log = 1; #endif + static const int tick_LCM = + tick_interval_debug_log* + tick_interval_sIndicator* + tick_interval_tSignal; + + if (tick==tick_LCM) tick=0; + executionTimer.reset(); sleepTimer.reset(); sleepTimer.start();
--- a/tasks/task_group2.cpp Tue Mar 27 22:03:07 2018 +0000 +++ b/tasks/task_group2.cpp Wed Mar 28 00:26:55 2018 +0000 @@ -32,43 +32,44 @@ static inline void hotLoop(){ - runTimeParams::liveAccess.lock(); - //a = (v2-v1)/t - //v2 = at+v1 + static int i = 0; + const int i_prev = i; + i = (i>=3)? 0: i+1; + const float friction = 0.1f; - const float accel = (getIgnition::ignition.read())? (runTimeParams::accelForce - - (runTimeParams::brakeForce+friction)) - : - -(runTimeParams::brakeForce+friction); + + runTimeParams::liveAccess.lock(); - float tmpSpeed = accel * + - runTimeParams::newSpeed; - runTimeParams::newSpeed = (tmpSpeed>0)?tmpSpeed:0; + //v2 = at+v1 + float accel; + if (getIgnition::ignition.read()){ + accel = runTimeParams::accelForce - + (runTimeParams::brakeForce+friction); + } + else{ + accel= -(runTimeParams::brakeForce+friction); + } + + float tmpSpeed = accel * + runTimeParams::speed[i_prev]; + runTimeParams::speed[i] = (tmpSpeed>0)?tmpSpeed:0; runTimeParams::liveAccess.unlock(); + } } namespace filterSpeed{ + //Filter speed with averaging filter static const float freq = 5; //hz - - //I/O - float speed[3] = {0}; - - void runTask(){ - //Filter speed with averaging filter - //• average of last ‘n’ readings (e.g. n =3) - //(raw speed value will be computed by the “car simulator” - //process) - while(1){ - wait(1/freq); - float avgSpeed = (speed[0] + speed[1] +speed[2])/3; + static inline void hotLoop(){ runTimeParams::liveAccess.lock(); - runTimeParams::avgSpeed = avgSpeed; + runTimeParams::avgSpeed = (runTimeParams::speed[0] + + runTimeParams::speed[1] + + runTimeParams::speed[2])/3; runTimeParams::liveAccess.unlock(); - } - } + } } + namespace task_group_2{ Thread thread; const float freq = 20.0f; //hz @@ -82,7 +83,7 @@ const int const_delay = int((1000.0f/freq)+0.5f); int dynamic_delay = const_delay; int tick = 0; - + int max_exec_time = 0; while(true){ @@ -98,6 +99,9 @@ // Run all tasks-------------- carSimulator::hotLoop(); + static const int tick_interval_filter = int((freq/filterSpeed::freq)+0.5f); + if (!(tick%tick_interval_filter)) filterSpeed::hotLoop(); + static const int tick_interval_controls = int((freq/getControls::freq)+0.5f); if (!(tick%tick_interval_controls)) getControls::hotLoop(); @@ -108,26 +112,35 @@ tick++; executionTimer.stop(); int exec_time = executionTimer.read_ms(); + if (exec_time > max_exec_time) max_exec_time=exec_time; #if DEBUG_MODE - const int debug_log_interval = int(freq/dequeueMail::freq); + static const int debug_log_interval = int(freq/dequeueMail::freq); if (!(tick%debug_log_interval)){ - runTimeParams::liveAccess.lock(); - runTimeParams::debugLog += "task_group_2," + to_string(exec_time) + "," + runTimeParams::debugAccess.lock(); + *runTimeParams::debugLog += "task_group_2," + to_string(max_exec_time) + "," + to_string(sleepTime) + "," + to_string(drift) + "\n\r"; - - runTimeParams::liveAccess.unlock(); + exec_time = 0; + runTimeParams::debugAccess.unlock(); } - static const int tick_LCM = (debug_log_interval*tick_interval_ignitionLED*tick_interval_controls); + #else + static const int debug_log_interval = 1; + #endif + + static const int tick_LCM = + debug_log_interval* + tick_interval_ignitionLED* + tick_interval_controls* + tick_interval_filter; + if (tick==tick_LCM) tick=0; - #endif executionTimer.reset(); sleepTimer.reset(); sleepTimer.start(); - dynamic_delay = const_delay -drift; + dynamic_delay = const_delay -(exec_time + drift); Thread::wait(dynamic_delay); } }
--- a/tasks/turnSignal.cpp Tue Mar 27 22:03:07 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -#include "core.h" - -namespace turnSignal{ - Thread thread; - static const float freq = 0.5; //hz - - //I/O - DigitalIn lSwitch(PORT_TURN_SIGNAL_SWITCH_LEFT); - DigitalIn rSwitch(PORT_TURN_SIGNAL_SWITCH_RIGHT); - - PwmOut lLed(PORT_TURN_SIGNAL_LED_LEFT); - PwmOut rLed(PORT_TURN_SIGNAL_LED_RIGHT); - - void runTask(){ - //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). - - lLed.period(1.0f); - rLed.period(1.0f); - while(1){ - wait(1.0f/freq); - lSwitch.read() ? lLed.write(0.5f) : lLed.write(0.0f); - rSwitch.read() ? rLed.write(0.5f) : rLed.write(0.0f); - } - - } -}