GIU\ZF
Dependencies: MCP23017 WattBob_TextLCD mbed-rtos mbed
Fork of rtos_basic by
main.cpp@12:90b5d8eae5ec, 2018-03-27 (annotated)
- Committer:
- ihexx
- Date:
- Tue Mar 27 14:35:09 2018 +0000
- Revision:
- 12:90b5d8eae5ec
- Parent:
- 11:0309bef74ba8
- Child:
- 13:ab52f46c98ab
core loop functional.; Display task tested.; Debug frameowrk conceptualised.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ihexx | 12:90b5d8eae5ec | 1 | #include "core.h" |
ihexx | 12:90b5d8eae5ec | 2 | |
ihexx | 12:90b5d8eae5ec | 3 | DigitalOut led1(LED3); |
emilmont | 1:491820ee784d | 4 | DigitalOut led2(LED2); |
ihexx | 12:90b5d8eae5ec | 5 | Thread thread,thread_group_2HZ,thread_group_1HZ; |
ihexx | 12:90b5d8eae5ec | 6 | |
ihexx | 12:90b5d8eae5ec | 7 | //Merge tasks with same frequency |
ihexx | 12:90b5d8eae5ec | 8 | namespace runTimeParams{ |
ihexx | 12:90b5d8eae5ec | 9 | Mutex liveAccess; |
ihexx | 12:90b5d8eae5ec | 10 | float brakeForce = 0.0f; |
ihexx | 12:90b5d8eae5ec | 11 | float accelForce = 0.0f; |
ihexx | 12:90b5d8eae5ec | 12 | float avgSpeed = 0.0f; |
ihexx | 12:90b5d8eae5ec | 13 | float odometer = 0.0f; |
ihexx | 12:90b5d8eae5ec | 14 | #if DEBUG_MODE |
ihexx | 12:90b5d8eae5ec | 15 | string debugLog = "task,execution_time_ms,lastSleep,drift\n"; |
ihexx | 12:90b5d8eae5ec | 16 | #endif |
ihexx | 12:90b5d8eae5ec | 17 | } |
ihexx | 12:90b5d8eae5ec | 18 | |
ihexx | 12:90b5d8eae5ec | 19 | |
ihexx | 12:90b5d8eae5ec | 20 | namespace task_group_1{ |
ihexx | 12:90b5d8eae5ec | 21 | Thread thread; |
ihexx | 12:90b5d8eae5ec | 22 | Timer executionTimer,sleepTimer; |
ihexx | 12:90b5d8eae5ec | 23 | bool tick = false; |
ihexx | 12:90b5d8eae5ec | 24 | |
ihexx | 12:90b5d8eae5ec | 25 | void init(){ |
ihexx | 12:90b5d8eae5ec | 26 | executionTimer.reset(); |
ihexx | 12:90b5d8eae5ec | 27 | sleepTimer.reset(); |
ihexx | 12:90b5d8eae5ec | 28 | display::init(); |
ihexx | 12:90b5d8eae5ec | 29 | } |
ihexx | 12:90b5d8eae5ec | 30 | void runTask(){ |
ihexx | 12:90b5d8eae5ec | 31 | const int const_delay = int(1000.0f/display::freq); |
ihexx | 12:90b5d8eae5ec | 32 | int dynamic_delay = const_delay; |
ihexx | 12:90b5d8eae5ec | 33 | while(true){ |
ihexx | 12:90b5d8eae5ec | 34 | sleepTimer.stop(); |
ihexx | 12:90b5d8eae5ec | 35 | executionTimer.start(); |
ihexx | 12:90b5d8eae5ec | 36 | |
ihexx | 12:90b5d8eae5ec | 37 | |
ihexx | 12:90b5d8eae5ec | 38 | int sleepTime = sleepTimer.read_ms(); |
ihexx | 12:90b5d8eae5ec | 39 | const int drift = ((sleepTime - dynamic_delay) > 0)? |
ihexx | 12:90b5d8eae5ec | 40 | (sleepTime - dynamic_delay) : 0; |
ihexx | 12:90b5d8eae5ec | 41 | |
ihexx | 12:90b5d8eae5ec | 42 | |
ihexx | 12:90b5d8eae5ec | 43 | // Run all tasks |
ihexx | 12:90b5d8eae5ec | 44 | display::hotLoop(); |
ihexx | 12:90b5d8eae5ec | 45 | |
ihexx | 12:90b5d8eae5ec | 46 | |
ihexx | 12:90b5d8eae5ec | 47 | executionTimer.stop(); |
ihexx | 12:90b5d8eae5ec | 48 | int exec_time = executionTimer.read_ms(); |
ihexx | 12:90b5d8eae5ec | 49 | |
ihexx | 12:90b5d8eae5ec | 50 | #if DEBUG_MODE |
ihexx | 12:90b5d8eae5ec | 51 | runTimeParams::liveAccess.lock(); |
ihexx | 12:90b5d8eae5ec | 52 | //runTimeParams::debugLog += "GROUP_1," + to_string(executionTimer.read_ms()) + "," |
ihexx | 12:90b5d8eae5ec | 53 | // + to_string(sleepTimer.read_ms()) + ", \n"; |
ihexx | 12:90b5d8eae5ec | 54 | runTimeParams::odometer = float(sleepTime); |
ihexx | 12:90b5d8eae5ec | 55 | runTimeParams::avgSpeed = float(exec_time); |
ihexx | 12:90b5d8eae5ec | 56 | runTimeParams::liveAccess.unlock(); |
ihexx | 12:90b5d8eae5ec | 57 | #endif |
ihexx | 12:90b5d8eae5ec | 58 | |
ihexx | 12:90b5d8eae5ec | 59 | executionTimer.reset(); |
ihexx | 12:90b5d8eae5ec | 60 | sleepTimer.reset(); |
ihexx | 12:90b5d8eae5ec | 61 | sleepTimer.start(); |
ihexx | 12:90b5d8eae5ec | 62 | dynamic_delay = const_delay - (exec_time + drift); |
ihexx | 12:90b5d8eae5ec | 63 | Thread::wait(dynamic_delay); |
ihexx | 12:90b5d8eae5ec | 64 | } |
ihexx | 12:90b5d8eae5ec | 65 | } |
ihexx | 12:90b5d8eae5ec | 66 | } |
ihexx | 12:90b5d8eae5ec | 67 | |
ihexx | 12:90b5d8eae5ec | 68 | int init(){ |
ihexx | 12:90b5d8eae5ec | 69 | //Run task initializers |
ihexx | 12:90b5d8eae5ec | 70 | task_group_1::init(); |
ihexx | 12:90b5d8eae5ec | 71 | |
ihexx | 12:90b5d8eae5ec | 72 | //Start task hotloops |
ihexx | 12:90b5d8eae5ec | 73 | |
ihexx | 12:90b5d8eae5ec | 74 | task_group_1::thread.start(task_group_1::runTask); |
ihexx | 12:90b5d8eae5ec | 75 | // getControls::thread.start(getControls::runTask); |
ihexx | 12:90b5d8eae5ec | 76 | return 0; |
ihexx | 12:90b5d8eae5ec | 77 | } |
emilmont | 1:491820ee784d | 78 | |
geky | 7:8d9919175929 | 79 | void led2_thread() { |
emilmont | 1:491820ee784d | 80 | while (true) { |
emilmont | 1:491820ee784d | 81 | led2 = !led2; |
mbed_official | 11:0309bef74ba8 | 82 | Thread::wait(1000); |
emilmont | 1:491820ee784d | 83 | } |
emilmont | 1:491820ee784d | 84 | } |
emilmont | 1:491820ee784d | 85 | |
emilmont | 1:491820ee784d | 86 | int main() { |
ihexx | 12:90b5d8eae5ec | 87 | init(); |
geky | 7:8d9919175929 | 88 | thread.start(led2_thread); |
emilmont | 1:491820ee784d | 89 | |
emilmont | 1:491820ee784d | 90 | while (true) { |
emilmont | 1:491820ee784d | 91 | led1 = !led1; |
mbed_official | 11:0309bef74ba8 | 92 | Thread::wait(500); |
emilmont | 1:491820ee784d | 93 | } |
emilmont | 1:491820ee784d | 94 | } |