Code for Sprint 2
Dependencies: C12832 mbed rtos RangeFinder
main.cpp@14:74733a28eb80, 2015-04-16 (annotated)
- Committer:
- Soldier7
- Date:
- Thu Apr 16 16:16:10 2015 +0000
- Revision:
- 14:74733a28eb80
- Parent:
- 13:4ef3d9835b13
- Child:
- 15:4730277de7c2
Raw version of accepting inputs, but the outTilt variable is in a loop and decreasing below 0.
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" |
Argensis | 9:e5d24b7a921b | 5 | |
Soldier7 | 14:74733a28eb80 | 6 | Servo tiltServo(p24); |
Soldier7 | 14:74733a28eb80 | 7 | Servo panServo(p25); |
Soldier7 | 0:5edc27224a37 | 8 | Serial pc(USBTX, USBRX); |
Argensis | 10:ca6f2769964e | 9 | Servo vertServo(p23); |
Soldier7 | 14:74733a28eb80 | 10 | AnalogIn sonar(p19); // temporary changed to potmeter |
Soldier7 | 0:5edc27224a37 | 11 | Mutex mutexIn; |
Soldier7 | 0:5edc27224a37 | 12 | Mutex mutexOut; |
Soldier7 | 0:5edc27224a37 | 13 | |
Soldier7 | 8:fe434a018d96 | 14 | // Global variables |
Argensis | 10:ca6f2769964e | 15 | float corHoriz = 0; // horizontal variable arrives from OpenCV |
Argensis | 10:ca6f2769964e | 16 | float corVert = 0; // vertical variable arrives from OpenCV |
Soldier7 | 14:74733a28eb80 | 17 | float distance = 0; // distance from the sonar sensor |
Soldier7 | 14:74733a28eb80 | 18 | float outVert; // output to vertical servo |
Soldier7 | 14:74733a28eb80 | 19 | float outTilt; // output to tilt servo |
Soldier7 | 14:74733a28eb80 | 20 | float outHoriz; // output to horizontal servo |
Soldier7 | 14:74733a28eb80 | 21 | C12832 lcd(p5, p7, p6, p8, p11);// lcd is an object from class c12832 initialised by p5, p7.... |
Soldier7 | 0:5edc27224a37 | 22 | |
Soldier7 | 0:5edc27224a37 | 23 | /* Thread Serial 1 - handles the output data from the control thread, and pass to the servo. |
Soldier7 | 0:5edc27224a37 | 24 | @update s1, s2 */ |
Soldier7 | 12:f6d4980c48d6 | 25 | void serial_thread(void const *args) |
Soldier7 | 12:f6d4980c48d6 | 26 | { |
Soldier7 | 0:5edc27224a37 | 27 | while (true) { |
Argensis | 9:e5d24b7a921b | 28 | pc.scanf("%f,%f", &corHoriz, &corVert);// read from serial port the data |
Soldier7 | 0:5edc27224a37 | 29 | } |
Soldier7 | 0:5edc27224a37 | 30 | } |
Soldier7 | 12:f6d4980c48d6 | 31 | |
Soldier7 | 0:5edc27224a37 | 32 | /* Thread LCD 2 - handles the input data from the sonar sensor, and display on the LCD screen. |
Soldier7 | 0:5edc27224a37 | 33 | @update inData */ |
Soldier7 | 12:f6d4980c48d6 | 34 | void lcd_thread(void const *args) |
Soldier7 | 12:f6d4980c48d6 | 35 | { |
Soldier7 | 0:5edc27224a37 | 36 | while (true) { |
Soldier7 | 0:5edc27224a37 | 37 | // Display values on the LCD screen |
Soldier7 | 0:5edc27224a37 | 38 | lcd.cls(); // clear the display |
Soldier7 | 4:70090f3b1f07 | 39 | lcd.locate(0,5); // the location where you want your charater to be displayed |
Soldier7 | 14:74733a28eb80 | 40 | lcd.printf("Sonar: %0.3f, OutTilt: %0.3f", sonar.read(), outTilt); |
Ali_taher | 3:3d53799c2f18 | 41 | lcd.locate(0,20); // the location where you want your charater to be displayed |
Soldier7 | 14:74733a28eb80 | 42 | lcd.printf("Vert: %0.3f, OutVert: %0.3f", corVert, outVert); |
Argensis | 9:e5d24b7a921b | 43 | Thread::wait(250); |
Soldier7 | 0:5edc27224a37 | 44 | } |
Soldier7 | 0:5edc27224a37 | 45 | } |
Soldier7 | 0:5edc27224a37 | 46 | |
Soldier7 | 0:5edc27224a37 | 47 | /* Thread Control 3 - handles the input data from the sonar sensor, and display on the LCD screen. |
Soldier7 | 0:5edc27224a37 | 48 | @update inData */ |
Soldier7 | 12:f6d4980c48d6 | 49 | void control_thread(void const *args) |
Soldier7 | 12:f6d4980c48d6 | 50 | { |
Soldier7 | 0:5edc27224a37 | 51 | while (true) { |
Soldier7 | 0:5edc27224a37 | 52 | mutexIn.lock(); |
Soldier7 | 8:fe434a018d96 | 53 | float differ; |
Soldier7 | 8:fe434a018d96 | 54 | differ = exp(corVert + sonar * outTilt) / (1 + exp(corVert + sonar * outTilt)); |
Soldier7 | 14:74733a28eb80 | 55 | if (0.3 < corVert < 0.7) { // if corVert is valid (between 0 - 1) then do movements |
Soldier7 | 12:f6d4980c48d6 | 56 | // moves lamp down by the fraction of the difference from the middle |
Soldier7 | 14:74733a28eb80 | 57 | outVert = outVert + ((outVert - .5) * differ); |
Soldier7 | 14:74733a28eb80 | 58 | // tilt down by the fraction of the difference from the middle |
Soldier7 | 14:74733a28eb80 | 59 | outTilt = outTilt + ((outVert - .5) * (1 - differ)); |
Soldier7 | 14:74733a28eb80 | 60 | } else if (0 < corVert < 0.3) { |
Soldier7 | 14:74733a28eb80 | 61 | outVert = 0; |
Soldier7 | 14:74733a28eb80 | 62 | outTilt = corVert * .9; // Follow the low movement only with the tilt servo. |
Soldier7 | 14:74733a28eb80 | 63 | // (.9 is the correction of the side of the screen at OpenCV) |
Soldier7 | 14:74733a28eb80 | 64 | } else if (corVert > 0.7 && corVert < 1) { |
Soldier7 | 14:74733a28eb80 | 65 | outVert = 1; |
Soldier7 | 14:74733a28eb80 | 66 | outTilt = corVert * 1.1; |
Soldier7 | 14:74733a28eb80 | 67 | } else { // Else this is the case when there is no input from the OpenCV |
Soldier7 | 14:74733a28eb80 | 68 | outVert = .5; |
Soldier7 | 14:74733a28eb80 | 69 | // TODO Pan search code here. (Searching personality.) |
Soldier7 | 12:f6d4980c48d6 | 70 | } |
Soldier7 | 0:5edc27224a37 | 71 | mutexIn.unlock(); |
Soldier7 | 0:5edc27224a37 | 72 | Thread::wait(25); |
Soldier7 | 0:5edc27224a37 | 73 | } |
Soldier7 | 0:5edc27224a37 | 74 | } |
Soldier7 | 0:5edc27224a37 | 75 | |
Soldier7 | 0:5edc27224a37 | 76 | /* Thread Servo 4 - handles the output data from the control thread, and pass to the servo. |
Soldier7 | 0:5edc27224a37 | 77 | @update s1, s2 */ |
Soldier7 | 12:f6d4980c48d6 | 78 | void servo_thread(void const *args) |
Soldier7 | 12:f6d4980c48d6 | 79 | { |
Soldier7 | 0:5edc27224a37 | 80 | while (true) { |
Soldier7 | 0:5edc27224a37 | 81 | mutexOut.lock(); |
Argensis | 10:ca6f2769964e | 82 | tiltServo = outTilt; |
Argensis | 10:ca6f2769964e | 83 | panServo = outHoriz; |
Argensis | 10:ca6f2769964e | 84 | vertServo = outVert; |
Soldier7 | 0:5edc27224a37 | 85 | mutexOut.unlock(); |
Soldier7 | 0:5edc27224a37 | 86 | Thread::wait(200); |
Soldier7 | 0:5edc27224a37 | 87 | } |
Soldier7 | 0:5edc27224a37 | 88 | } |
Soldier7 | 0:5edc27224a37 | 89 | |
Soldier7 | 12:f6d4980c48d6 | 90 | int main() |
Soldier7 | 12:f6d4980c48d6 | 91 | { |
Soldier7 | 0:5edc27224a37 | 92 | Thread thread_1(serial_thread); // Start Serial Thread |
Soldier7 | 0:5edc27224a37 | 93 | Thread thread_2(lcd_thread); // Start LCD Thread |
Argensis | 10:ca6f2769964e | 94 | Thread thread_3(control_thread); // Start Servo Thread |
Soldier7 | 0:5edc27224a37 | 95 | Thread thread_4(servo_thread); // Start Servo Thread |
Soldier7 | 0:5edc27224a37 | 96 | while(1) { |
Argensis | 9:e5d24b7a921b | 97 | wait(1); |
Soldier7 | 0:5edc27224a37 | 98 | } |
Soldier7 | 0:5edc27224a37 | 99 | } |