updated codes lads
Dependencies: C12832 Servo mbed-rtos mbed
Fork of rtos_basic_team by
main.cpp@10:372c5d608b5c, 2015-01-29 (annotated)
- Committer:
- Soldier7
- Date:
- Thu Jan 29 12:55:44 2015 +0000
- Revision:
- 10:372c5d608b5c
- Parent:
- 9:f60eeadabcb0
Added comments for more understanding (at least for me :)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
emilmont | 1:491820ee784d | 2 | #include "rtos.h" |
PaulF | 7:c8e192e2c80c | 3 | #include "Servo.h" |
Ali_taher | 8:507111cc659c | 4 | #include "C12832.h" |
Soldier7 | 10:372c5d608b5c | 5 | |
Soldier7 | 10:372c5d608b5c | 6 | Servo s1(p21); |
PaulF | 7:c8e192e2c80c | 7 | Servo s2(p22); |
Soldier7 | 10:372c5d608b5c | 8 | AnalogIn p1(p17); // Sonar sensor 1 |
Soldier7 | 10:372c5d608b5c | 9 | //AnalogIn p2(p20); // Sonar sensor 2 (not used yet) |
Soldier7 | 10:372c5d608b5c | 10 | Mutex dataIn_mutex; |
Ali_taher | 9:f60eeadabcb0 | 11 | Mutex dataOut_mutex; |
Ali_taher | 9:f60eeadabcb0 | 12 | |
Soldier7 | 10:372c5d608b5c | 13 | // Global variables |
Ali_taher | 9:f60eeadabcb0 | 14 | float input_data; |
Ali_taher | 9:f60eeadabcb0 | 15 | float output_data; |
Soldier7 | 10:372c5d608b5c | 16 | C12832 lcd(p5, p7, p6, p8, p11); // lcd is an object from class C12832 initialised by p5,p7.... |
Ali_taher | 9:f60eeadabcb0 | 17 | |
Soldier7 | 10:372c5d608b5c | 18 | /* Thread Sonar - handles the input data from the sonar sensor, and display on the LCD screen. |
Soldier7 | 10:372c5d608b5c | 19 | @update input_data |
Soldier7 | 10:372c5d608b5c | 20 | */ |
Soldier7 | 10:372c5d608b5c | 21 | void sonar_thread(void const *args) |
Soldier7 | 10:372c5d608b5c | 22 | { |
Ali_taher | 9:f60eeadabcb0 | 23 | while (true) { |
Soldier7 | 10:372c5d608b5c | 24 | dataIn_mutex.lock(); |
Soldier7 | 10:372c5d608b5c | 25 | // Read the sensor and assign to input_data global variable |
Soldier7 | 10:372c5d608b5c | 26 | input_data= p1.read()*5; |
Soldier7 | 10:372c5d608b5c | 27 | // Display the input_data |
Soldier7 | 10:372c5d608b5c | 28 | lcd.cls(); // clear the display |
Soldier7 | 10:372c5d608b5c | 29 | lcd.locate(0,3);// the location where you want your charater to be displayed |
Soldier7 | 10:372c5d608b5c | 30 | lcd.printf("Thread one = %f\n", input_data); |
Soldier7 | 10:372c5d608b5c | 31 | // Handles the thread |
Soldier7 | 10:372c5d608b5c | 32 | dataIn_mutex.unlock(); |
Soldier7 | 10:372c5d608b5c | 33 | Thread::wait(25); |
Ali_taher | 9:f60eeadabcb0 | 34 | } |
Ali_taher | 9:f60eeadabcb0 | 35 | } |
Soldier7 | 10:372c5d608b5c | 36 | |
Soldier7 | 10:372c5d608b5c | 37 | /* Thread Control - */ |
Soldier7 | 10:372c5d608b5c | 38 | void control_thread(void const *args) |
Soldier7 | 10:372c5d608b5c | 39 | { |
emilmont | 1:491820ee784d | 40 | while (true) { |
Soldier7 | 10:372c5d608b5c | 41 | dataIn_mutex.lock(); |
Soldier7 | 10:372c5d608b5c | 42 | // Modification on the input_data and assign to servo2 |
Soldier7 | 10:372c5d608b5c | 43 | if (input_data>0.8) { |
Ali_taher | 9:f60eeadabcb0 | 44 | s2=input_data*0.2; |
Soldier7 | 10:372c5d608b5c | 45 | } else if(input_data<0.8) { |
Ali_taher | 9:f60eeadabcb0 | 46 | s2=-input_data*0.5; |
Soldier7 | 10:372c5d608b5c | 47 | } |
Soldier7 | 10:372c5d608b5c | 48 | // Assign input_data to servo1 without modification |
Soldier7 | 10:372c5d608b5c | 49 | s1=input_data; |
Soldier7 | 10:372c5d608b5c | 50 | // Handles the thread |
Soldier7 | 10:372c5d608b5c | 51 | dataIn_mutex.unlock(); |
Soldier7 | 10:372c5d608b5c | 52 | Thread::wait(20); |
emilmont | 1:491820ee784d | 53 | } |
emilmont | 1:491820ee784d | 54 | } |
Soldier7 | 10:372c5d608b5c | 55 | |
Soldier7 | 10:372c5d608b5c | 56 | /* Main method - Start threads and delay them by waiting. */ |
Soldier7 | 10:372c5d608b5c | 57 | int main() |
Soldier7 | 10:372c5d608b5c | 58 | { |
Soldier7 | 10:372c5d608b5c | 59 | Thread thread(sonar_thread); // Start Sonar Thread |
Soldier7 | 10:372c5d608b5c | 60 | Thread thread_1(control_thread); // Start Control Thread |
Soldier7 | 10:372c5d608b5c | 61 | // Delay the threads |
emilmont | 1:491820ee784d | 62 | while (true) { |
Soldier7 | 10:372c5d608b5c | 63 | Thread::wait(10); |
emilmont | 1:491820ee784d | 64 | } |
Soldier7 | 10:372c5d608b5c | 65 | } |