GIU\ZF
Dependencies: MCP23017 WattBob_TextLCD mbed-rtos mbed
Fork of rtos_basic by
tasks/MailTasks.cpp@17:a29ce6fc667c, 2018-03-28 (annotated)
- Committer:
- ihexx
- Date:
- Wed Mar 28 00:26:55 2018 +0000
- Revision:
- 17:a29ce6fc667c
- Parent:
- tasks/sendMail.cpp@16:0ada6cbd41e2
- Child:
- 19:2044bb5d7f29
Fixed deadlock bug on debug framework
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 | namespace mailData{ |
ihexx | 12:90b5d8eae5ec | 4 | Mail<mail_t, 100> mailBox; |
ihexx | 12:90b5d8eae5ec | 5 | } |
ihexx | 12:90b5d8eae5ec | 6 | |
ihexx | 13:ab52f46c98ab | 7 | namespace enqueueMail{ |
ihexx | 13:ab52f46c98ab | 8 | //Send speed, accelerometer and brake values to a 100 element MAIL queue |
ihexx | 12:90b5d8eae5ec | 9 | Thread thread; |
ihexx | 13:ab52f46c98ab | 10 | const float freq = 0.2; //hz |
ihexx | 12:90b5d8eae5ec | 11 | |
ihexx | 12:90b5d8eae5ec | 12 | //I/O |
ihexx | 12:90b5d8eae5ec | 13 | void runTask(){ |
ihexx | 14:8a6c20435523 | 14 | |
ihexx | 14:8a6c20435523 | 15 | |
ihexx | 13:ab52f46c98ab | 16 | Timer executionTimer,sleepTimer; |
ihexx | 13:ab52f46c98ab | 17 | executionTimer.reset(); |
ihexx | 13:ab52f46c98ab | 18 | sleepTimer.reset(); |
ihexx | 13:ab52f46c98ab | 19 | |
ihexx | 13:ab52f46c98ab | 20 | const int const_delay = int((1000.0f/freq)+0.5f); |
ihexx | 13:ab52f46c98ab | 21 | int dynamic_delay = const_delay; |
ihexx | 14:8a6c20435523 | 22 | int tick = 0; |
ihexx | 13:ab52f46c98ab | 23 | |
ihexx | 12:90b5d8eae5ec | 24 | while(1){ |
ihexx | 14:8a6c20435523 | 25 | sleepTimer.stop(); |
ihexx | 13:ab52f46c98ab | 26 | executionTimer.start(); |
ihexx | 13:ab52f46c98ab | 27 | int sleepTime = sleepTimer.read_ms(); |
ihexx | 13:ab52f46c98ab | 28 | const int drift = ((sleepTime - dynamic_delay) > 0)? |
ihexx | 13:ab52f46c98ab | 29 | (sleepTime - dynamic_delay) : 0; |
ihexx | 13:ab52f46c98ab | 30 | //Core Loop: |
ihexx | 12:90b5d8eae5ec | 31 | using namespace mailData; |
ihexx | 12:90b5d8eae5ec | 32 | mail_t *mail = mailBox.alloc(); |
ihexx | 12:90b5d8eae5ec | 33 | |
ihexx | 12:90b5d8eae5ec | 34 | runTimeParams::liveAccess.lock(); |
ihexx | 12:90b5d8eae5ec | 35 | |
ihexx | 12:90b5d8eae5ec | 36 | mail->speed = runTimeParams::avgSpeed; |
ihexx | 12:90b5d8eae5ec | 37 | mail->accel = runTimeParams::accelForce; |
ihexx | 12:90b5d8eae5ec | 38 | mail->brake = runTimeParams::brakeForce; |
ihexx | 12:90b5d8eae5ec | 39 | |
ihexx | 12:90b5d8eae5ec | 40 | runTimeParams::liveAccess.unlock(); |
ihexx | 12:90b5d8eae5ec | 41 | |
ihexx | 12:90b5d8eae5ec | 42 | mailBox.put(mail); |
ihexx | 13:ab52f46c98ab | 43 | //End of Core loop |
ihexx | 12:90b5d8eae5ec | 44 | |
ihexx | 14:8a6c20435523 | 45 | tick++; |
ihexx | 13:ab52f46c98ab | 46 | executionTimer.stop(); |
ihexx | 13:ab52f46c98ab | 47 | int exec_time = executionTimer.read_ms(); |
ihexx | 13:ab52f46c98ab | 48 | |
ihexx | 13:ab52f46c98ab | 49 | #if DEBUG_MODE |
ihexx | 14:8a6c20435523 | 50 | const int debug_log_interval = int(freq/dequeueMail::freq); |
ihexx | 14:8a6c20435523 | 51 | if (!(tick%debug_log_interval)){ |
ihexx | 17:a29ce6fc667c | 52 | runTimeParams::debugAccess.lock(); |
ihexx | 17:a29ce6fc667c | 53 | *runTimeParams::debugLog += "Enqueue Mail," + to_string(exec_time) + "," |
ihexx | 14:8a6c20435523 | 54 | + to_string(sleepTime) + "," |
ihexx | 14:8a6c20435523 | 55 | + to_string(drift) + "\n\r"; |
ihexx | 17:a29ce6fc667c | 56 | runTimeParams::debugAccess.unlock(); |
ihexx | 14:8a6c20435523 | 57 | tick = 0; |
ihexx | 14:8a6c20435523 | 58 | } |
ihexx | 13:ab52f46c98ab | 59 | #endif |
ihexx | 13:ab52f46c98ab | 60 | |
ihexx | 13:ab52f46c98ab | 61 | executionTimer.reset(); |
ihexx | 13:ab52f46c98ab | 62 | sleepTimer.reset(); |
ihexx | 13:ab52f46c98ab | 63 | sleepTimer.start(); |
ihexx | 14:8a6c20435523 | 64 | |
ihexx | 13:ab52f46c98ab | 65 | dynamic_delay = const_delay - (exec_time + drift); |
ihexx | 13:ab52f46c98ab | 66 | Thread::wait(dynamic_delay); |
ihexx | 12:90b5d8eae5ec | 67 | } |
ihexx | 12:90b5d8eae5ec | 68 | |
ihexx | 12:90b5d8eae5ec | 69 | } |
ihexx | 12:90b5d8eae5ec | 70 | } |
ihexx | 12:90b5d8eae5ec | 71 | |
ihexx | 13:ab52f46c98ab | 72 | namespace dequeueMail{ |
ihexx | 13:ab52f46c98ab | 73 | //Dump contents of feature_7 MAIL queue to the serial connection to the PC |
ihexx | 12:90b5d8eae5ec | 74 | Thread thread; |
ihexx | 17:a29ce6fc667c | 75 | |
ihexx | 16:0ada6cbd41e2 | 76 | const float freq = 0.05; //hz |
ihexx | 17:a29ce6fc667c | 77 | |
ihexx | 13:ab52f46c98ab | 78 | |
ihexx | 15:524de2b2ef8e | 79 | |
ihexx | 12:90b5d8eae5ec | 80 | Serial pc(USBTX, USBRX); // tx, rx |
ihexx | 13:ab52f46c98ab | 81 | |
ihexx | 12:90b5d8eae5ec | 82 | |
ihexx | 12:90b5d8eae5ec | 83 | void runTask(){ |
ihexx | 13:ab52f46c98ab | 84 | Timer executionTimer,sleepTimer; |
ihexx | 13:ab52f46c98ab | 85 | executionTimer.reset(); |
ihexx | 13:ab52f46c98ab | 86 | sleepTimer.reset(); |
ihexx | 13:ab52f46c98ab | 87 | |
ihexx | 13:ab52f46c98ab | 88 | const int const_delay = int((1000.0f/freq)+0.5f); |
ihexx | 13:ab52f46c98ab | 89 | int dynamic_delay = const_delay; |
ihexx | 13:ab52f46c98ab | 90 | |
ihexx | 13:ab52f46c98ab | 91 | pc.printf("speed,acceleration,brake\n\r"); |
ihexx | 12:90b5d8eae5ec | 92 | |
ihexx | 13:ab52f46c98ab | 93 | while(true){ |
ihexx | 13:ab52f46c98ab | 94 | |
ihexx | 13:ab52f46c98ab | 95 | sleepTimer.stop(); |
ihexx | 13:ab52f46c98ab | 96 | executionTimer.start(); |
ihexx | 13:ab52f46c98ab | 97 | int sleepTime = sleepTimer.read_ms(); |
ihexx | 13:ab52f46c98ab | 98 | const int drift = ((sleepTime - dynamic_delay) > 0)? |
ihexx | 13:ab52f46c98ab | 99 | (sleepTime - dynamic_delay) : 0; |
ihexx | 13:ab52f46c98ab | 100 | |
ihexx | 13:ab52f46c98ab | 101 | /* Mail */ |
ihexx | 13:ab52f46c98ab | 102 | using namespace mailData; |
ihexx | 13:ab52f46c98ab | 103 | osEvent evt = mailBox.get(1); |
ihexx | 13:ab52f46c98ab | 104 | while (evt.status == osEventMail){ |
ihexx | 13:ab52f46c98ab | 105 | mail_t * mail = (mail_t*)evt.value.p; |
ihexx | 13:ab52f46c98ab | 106 | pc.printf("%.2f,%.2f,%.2f\n\r",mail->speed,mail->accel,mail->brake); |
ihexx | 13:ab52f46c98ab | 107 | mailBox.free(mail); |
ihexx | 13:ab52f46c98ab | 108 | evt = mailBox.get(1); |
ihexx | 13:ab52f46c98ab | 109 | } |
ihexx | 12:90b5d8eae5ec | 110 | |
ihexx | 13:ab52f46c98ab | 111 | executionTimer.stop(); |
ihexx | 13:ab52f46c98ab | 112 | int exec_time = executionTimer.read_ms(); |
ihexx | 12:90b5d8eae5ec | 113 | |
ihexx | 13:ab52f46c98ab | 114 | #if DEBUG_MODE |
ihexx | 17:a29ce6fc667c | 115 | runTimeParams::debugAccess.lock(); |
ihexx | 17:a29ce6fc667c | 116 | *runTimeParams::debugLog += "Dequeue Mail," + to_string(exec_time) + "," |
ihexx | 13:ab52f46c98ab | 117 | + to_string(sleepTime) + "," |
ihexx | 13:ab52f46c98ab | 118 | + to_string(drift) + "\n\r"; |
ihexx | 17:a29ce6fc667c | 119 | string * message; |
ihexx | 17:a29ce6fc667c | 120 | if (runTimeParams::debugLog == & runTimeParams::debugLogBuffer1){ |
ihexx | 17:a29ce6fc667c | 121 | runTimeParams::debugLog = & runTimeParams::debugLogBuffer2; |
ihexx | 17:a29ce6fc667c | 122 | message = &runTimeParams::debugLogBuffer1; |
ihexx | 17:a29ce6fc667c | 123 | } |
ihexx | 17:a29ce6fc667c | 124 | else{ |
ihexx | 17:a29ce6fc667c | 125 | runTimeParams::debugLog = & runTimeParams::debugLogBuffer1; |
ihexx | 17:a29ce6fc667c | 126 | message = &runTimeParams::debugLogBuffer2; |
ihexx | 17:a29ce6fc667c | 127 | } |
ihexx | 17:a29ce6fc667c | 128 | *runTimeParams::debugLog = ""; |
ihexx | 17:a29ce6fc667c | 129 | runTimeParams::debugAccess.unlock(); |
ihexx | 17:a29ce6fc667c | 130 | |
ihexx | 17:a29ce6fc667c | 131 | pc.printf(message->c_str()); |
ihexx | 17:a29ce6fc667c | 132 | |
ihexx | 17:a29ce6fc667c | 133 | |
ihexx | 13:ab52f46c98ab | 134 | #endif |
ihexx | 13:ab52f46c98ab | 135 | |
ihexx | 13:ab52f46c98ab | 136 | executionTimer.reset(); |
ihexx | 13:ab52f46c98ab | 137 | sleepTimer.reset(); |
ihexx | 13:ab52f46c98ab | 138 | sleepTimer.start(); |
ihexx | 13:ab52f46c98ab | 139 | dynamic_delay = const_delay - (exec_time + drift); |
ihexx | 13:ab52f46c98ab | 140 | Thread::wait(dynamic_delay); |
ihexx | 13:ab52f46c98ab | 141 | |
ihexx | 13:ab52f46c98ab | 142 | } |
ihexx | 12:90b5d8eae5ec | 143 | } |
ihexx | 12:90b5d8eae5ec | 144 | } |
ihexx | 12:90b5d8eae5ec | 145 | |
ihexx | 12:90b5d8eae5ec | 146 |