Code for Sprint 2
Dependencies: C12832 mbed rtos RangeFinder
main.cpp@16:af76305da577, 2015-04-17 (annotated)
- Committer:
- Ali_taher
- Date:
- Fri Apr 17 10:12:20 2015 +0000
- Revision:
- 16:af76305da577
- Parent:
- 10:ca6f2769964e
- Child:
- 17:974430ee2fbb
- Child:
- 24:de4d6a06011b
Adding the sonar sensor reading to the code and display the distance in meters
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Soldier7 | 0:5edc27224a37 | 1 | #include "mbed.h" |
Soldier7 | 0:5edc27224a37 | 2 | #include "rtos.h" |
Soldier7 | 0:5edc27224a37 | 3 | #include "Servo.h" |
Soldier7 | 0:5edc27224a37 | 4 | #include "C12832.h" |
Ali_taher | 16:af76305da577 | 5 | #include "RangeFinder.h"// header files for sonar sensor |
Argensis | 9:e5d24b7a921b | 6 | |
Ali_taher | 16:af76305da577 | 7 | Servo tiltServo(p22); |
Argensis | 9:e5d24b7a921b | 8 | Servo panServo(p22); |
Ali_taher | 16:af76305da577 | 9 | |
Soldier7 | 0:5edc27224a37 | 10 | Serial pc(USBTX, USBRX); |
Argensis | 10:ca6f2769964e | 11 | Servo vertServo(p23); |
Ali_taher | 16:af76305da577 | 12 | AnalogIn sonar(p19); //not any more used |
Ali_taher | 16:af76305da577 | 13 | |
Ali_taher | 16:af76305da577 | 14 | Mutex mutexIn;// protect globel variables |
Ali_taher | 16:af76305da577 | 15 | Mutex mutexOut;// protect globel variables |
Ali_taher | 16:af76305da577 | 16 | Mutex mutex_sonar; |
Soldier7 | 0:5edc27224a37 | 17 | |
Argensis | 9:e5d24b7a921b | 18 | // Global variables |
Argensis | 10:ca6f2769964e | 19 | float corHoriz = 0; // horizontal variable arrives from OpenCV |
Argensis | 10:ca6f2769964e | 20 | float corVert = 0; // vertical variable arrives from OpenCV |
Ali_taher | 16:af76305da577 | 21 | float distance = 0;// variable holds the distance in meters 0 to 3.3 |
Ali_taher | 16:af76305da577 | 22 | float norm=0; // variable holds the normalised values form the sonar sensor |
Soldier7 | 8:fe434a018d96 | 23 | float outVert; // rr |
Soldier7 | 8:fe434a018d96 | 24 | float outTilt; |
Soldier7 | 8:fe434a018d96 | 25 | float outHoriz; |
Soldier7 | 0:5edc27224a37 | 26 | C12832 lcd(p5, p7, p6, p8, p11);// lcd is an object from class c12832 initialised by p5,p7.... |
Soldier7 | 0:5edc27224a37 | 27 | |
Ali_taher | 16:af76305da577 | 28 | /*parallax ultrasound range finder |
Ali_taher | 16:af76305da577 | 29 | p21 pin the range finder is connected to. |
Ali_taher | 16:af76305da577 | 30 | 10 is Time of pulse to send to the rangefinder to trigger a measurement, in microseconds. |
Ali_taher | 16:af76305da577 | 31 | 5800 is Scaling of the range finder's output pulse from microseconds to metres. |
Ali_taher | 16:af76305da577 | 32 | 100000 Time to wait for a pulse from the range finder before giving up |
Ali_taher | 16:af76305da577 | 33 | */ |
Ali_taher | 16:af76305da577 | 34 | |
Ali_taher | 16:af76305da577 | 35 | RangeFinder rf(p26, 10, 5800.0, 100000); |
Soldier7 | 0:5edc27224a37 | 36 | /* Thread Serial 1 - handles the output data from the control thread, and pass to the servo. |
Soldier7 | 0:5edc27224a37 | 37 | @update s1, s2 */ |
Soldier7 | 0:5edc27224a37 | 38 | void serial_thread(void const *args) { |
Soldier7 | 0:5edc27224a37 | 39 | while (true) { |
Argensis | 9:e5d24b7a921b | 40 | pc.scanf("%f,%f", &corHoriz, &corVert);// read from serial port the data |
Soldier7 | 0:5edc27224a37 | 41 | } |
Soldier7 | 0:5edc27224a37 | 42 | } |
Soldier7 | 0:5edc27224a37 | 43 | |
Soldier7 | 0:5edc27224a37 | 44 | /* Thread LCD 2 - handles the input data from the sonar sensor, and display on the LCD screen. |
Soldier7 | 0:5edc27224a37 | 45 | @update inData */ |
Soldier7 | 0:5edc27224a37 | 46 | void lcd_thread(void const *args) { |
Soldier7 | 0:5edc27224a37 | 47 | while (true) { |
Ali_taher | 16:af76305da577 | 48 | mutex_sonar.lock(); |
Soldier7 | 0:5edc27224a37 | 49 | // Display values on the LCD screen |
Soldier7 | 0:5edc27224a37 | 50 | lcd.cls(); // clear the display |
Soldier7 | 4:70090f3b1f07 | 51 | lcd.locate(0,5); // the location where you want your charater to be displayed |
Argensis | 9:e5d24b7a921b | 52 | lcd.printf("Hor: %0.3f", corHoriz); |
Ali_taher | 16:af76305da577 | 53 | lcd.locate(0,10); // the location where you want your charater to be displayed |
Ali_taher | 16:af76305da577 | 54 | lcd.printf("Ver: %0.3f", corVert); |
Ali_taher | 3:3d53799c2f18 | 55 | lcd.locate(0,20); // the location where you want your charater to be displayed |
Ali_taher | 16:af76305da577 | 56 | lcd.printf("dis: %0.2f", distance);// Display the distance in meters from the sonar |
Ali_taher | 16:af76305da577 | 57 | mutex_sonar.unlock(); |
Argensis | 9:e5d24b7a921b | 58 | Thread::wait(250); |
Soldier7 | 0:5edc27224a37 | 59 | } |
Soldier7 | 0:5edc27224a37 | 60 | } |
Soldier7 | 0:5edc27224a37 | 61 | |
Soldier7 | 0:5edc27224a37 | 62 | /* Thread Control 3 - handles the input data from the sonar sensor, and display on the LCD screen. |
Soldier7 | 0:5edc27224a37 | 63 | @update inData */ |
Soldier7 | 0:5edc27224a37 | 64 | void control_thread(void const *args) { |
Soldier7 | 0:5edc27224a37 | 65 | while (true) { |
Soldier7 | 0:5edc27224a37 | 66 | mutexIn.lock(); |
Ali_taher | 16:af76305da577 | 67 | |
Soldier7 | 8:fe434a018d96 | 68 | float differ; |
Soldier7 | 8:fe434a018d96 | 69 | differ = exp(corVert + sonar * outTilt) / (1 + exp(corVert + sonar * outTilt)); |
Soldier7 | 7:24d62ef1ed34 | 70 | if (corVert > .5) { // check if face is below the half of the camera view |
Soldier7 | 8:fe434a018d96 | 71 | if (outTilt > .5) { // check if lamp head is facing down |
Soldier7 | 8:fe434a018d96 | 72 | // moves lamp down by the fraction of the difference from the middle |
Argensis | 10:ca6f2769964e | 73 | } |
Argensis | 10:ca6f2769964e | 74 | } |
Soldier7 | 0:5edc27224a37 | 75 | mutexIn.unlock(); |
Ali_taher | 16:af76305da577 | 76 | Thread::wait(250); |
Soldier7 | 0:5edc27224a37 | 77 | } |
Soldier7 | 0:5edc27224a37 | 78 | } |
Soldier7 | 0:5edc27224a37 | 79 | |
Soldier7 | 0:5edc27224a37 | 80 | /* Thread Servo 4 - handles the output data from the control thread, and pass to the servo. |
Soldier7 | 0:5edc27224a37 | 81 | @update s1, s2 */ |
Soldier7 | 0:5edc27224a37 | 82 | void servo_thread(void const *args) { |
Soldier7 | 0:5edc27224a37 | 83 | while (true) { |
Soldier7 | 0:5edc27224a37 | 84 | mutexOut.lock(); |
Argensis | 10:ca6f2769964e | 85 | tiltServo = outTilt; |
Argensis | 10:ca6f2769964e | 86 | panServo = outHoriz; |
Argensis | 10:ca6f2769964e | 87 | vertServo = outVert; |
Soldier7 | 0:5edc27224a37 | 88 | mutexOut.unlock(); |
Ali_taher | 16:af76305da577 | 89 | Thread::wait(250); |
Soldier7 | 0:5edc27224a37 | 90 | } |
Soldier7 | 0:5edc27224a37 | 91 | } |
Ali_taher | 16:af76305da577 | 92 | /* Thread sonar 5 - handles the sonar values which can be in meter or normailsed value to one */ |
Ali_taher | 16:af76305da577 | 93 | void sonar_thread(void const *args) { |
Ali_taher | 16:af76305da577 | 94 | while (true) { |
Ali_taher | 16:af76305da577 | 95 | mutex_sonar.lock(); |
Ali_taher | 16:af76305da577 | 96 | distance = rf.read_m(); // read the distance from the sonar sensor in meter |
Ali_taher | 16:af76305da577 | 97 | norm= distance/3.3; // normalised value from the sonar sensor |
Ali_taher | 16:af76305da577 | 98 | printf("dis: %0.2f", distance);// Display the distance in meters from the sonar |
Ali_taher | 16:af76305da577 | 99 | mutex_sonar.unlock(); |
Ali_taher | 16:af76305da577 | 100 | Thread::wait(250); |
Ali_taher | 16:af76305da577 | 101 | } |
Ali_taher | 16:af76305da577 | 102 | } |
Soldier7 | 0:5edc27224a37 | 103 | int main() { |
Soldier7 | 0:5edc27224a37 | 104 | Thread thread_1(serial_thread); // Start Serial Thread |
Soldier7 | 0:5edc27224a37 | 105 | Thread thread_2(lcd_thread); // Start LCD Thread |
Argensis | 10:ca6f2769964e | 106 | Thread thread_3(control_thread); // Start Servo Thread |
Soldier7 | 0:5edc27224a37 | 107 | Thread thread_4(servo_thread); // Start Servo Thread |
Ali_taher | 16:af76305da577 | 108 | Thread thread_5(sonar_thread); // Start Servo Thread |
Soldier7 | 0:5edc27224a37 | 109 | while(1) { |
Ali_taher | 16:af76305da577 | 110 | |
Argensis | 9:e5d24b7a921b | 111 | wait(1); |
Soldier7 | 0:5edc27224a37 | 112 | } |
Soldier7 | 0:5edc27224a37 | 113 | } |