Code for Sprint 2

Dependencies:   C12832 mbed rtos RangeFinder

Committer:
Argensis
Date:
Thu Apr 23 14:39:38 2015 +0000
Revision:
23:7d29f2132197
Parent:
22:00a577f64930
Child:
26:71288f42dbc6
Add enum

Who changed what in which revision?

UserRevisionLine numberNew 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
Soldier7 14:74733a28eb80 7 Servo tiltServo(p24);
Soldier7 14:74733a28eb80 8 Servo panServo(p25);
Soldier7 22:00a577f64930 9 Servo vertServo(p23);
Soldier7 0:5edc27224a37 10 Serial pc(USBTX, USBRX);
Soldier7 21:f098ffcd192a 11 Mutex mutexIn;// protect global variables
Soldier7 21:f098ffcd192a 12 Mutex mutexOut;// protect global variables
Ali_taher 16:af76305da577 13 Mutex mutex_sonar;
Soldier7 22:00a577f64930 14 AnalogIn sonar(p17); // correct is p17. TEMPORARY changed to potmeter p19
Soldier7 22:00a577f64930 15 //AnalogIn corVert(p20); // TO REMOVE. Temporary changed to potmeter
Soldier7 0:5edc27224a37 16
Soldier7 8:fe434a018d96 17 // Global variables
Soldier7 15:4730277de7c2 18 float corHoriz = 0.5; // horizontal variable arrives from OpenCV
Soldier7 21:f098ffcd192a 19 float corVert = 0.5; // vertical variable arrives from OpenCV
Soldier7 21:f098ffcd192a 20 float distance = 0.5;// variable holds the distance in meters 0 to 3.3
Ali_taher 16:af76305da577 21 float norm=0; // variable holds the normalised values form the sonar sensor
Soldier7 14:74733a28eb80 22 float outVert; // output to vertical servo
Soldier7 14:74733a28eb80 23 float outTilt; // output to tilt servo
Soldier7 14:74733a28eb80 24 float outHoriz; // output to horizontal servo
Soldier7 0:5edc27224a37 25 C12832 lcd(p5, p7, p6, p8, p11);// lcd is an object from class c12832 initialised by p5,p7....
Argensis 17:974430ee2fbb 26
Argensis 23:7d29f2132197 27 typedef enum {
Argensis 23:7d29f2132197 28 searching,
Argensis 23:7d29f2132197 29 tracking,
Argensis 23:7d29f2132197 30 retreating
Argensis 23:7d29f2132197 31 } LampState;
Argensis 23:7d29f2132197 32
Argensis 23:7d29f2132197 33 LampState lamp;
Argensis 23:7d29f2132197 34
Soldier7 21:f098ffcd192a 35 /* parallax ultrasound range finder
Ali_taher 16:af76305da577 36 p21 pin the range finder is connected to.
Ali_taher 16:af76305da577 37 10 is Time of pulse to send to the rangefinder to trigger a measurement, in microseconds.
Ali_taher 16:af76305da577 38 5800 is Scaling of the range finder's output pulse from microseconds to metres.
Soldier7 21:f098ffcd192a 39 100000 Time to wait for a pulse from the range finder before giving up */
Soldier7 21:f098ffcd192a 40 RangeFinder rf(p26, 10, 5800.0, 100000);
Ali_taher 16:af76305da577 41
Soldier7 0:5edc27224a37 42 /* Thread Serial 1 - handles the output data from the control thread, and pass to the servo.
Soldier7 0:5edc27224a37 43 @update s1, s2 */
Soldier7 12:f6d4980c48d6 44 void serial_thread(void const *args)
Soldier7 12:f6d4980c48d6 45 {
Soldier7 0:5edc27224a37 46 while (true) {
Soldier7 22:00a577f64930 47 pc.scanf("%f,%f", &corHoriz, &corVert);// read from serial port the data // temporary Vert1
Soldier7 0:5edc27224a37 48 }
Soldier7 0:5edc27224a37 49 }
Soldier7 12:f6d4980c48d6 50
Soldier7 0:5edc27224a37 51 /* Thread LCD 2 - handles the input data from the sonar sensor, and display on the LCD screen.
Soldier7 0:5edc27224a37 52 @update inData */
Soldier7 12:f6d4980c48d6 53 void lcd_thread(void const *args)
Soldier7 12:f6d4980c48d6 54 {
Soldier7 0:5edc27224a37 55 while (true) {
Ali_taher 16:af76305da577 56 mutex_sonar.lock();
Soldier7 0:5edc27224a37 57 // Display values on the LCD screen
Soldier7 0:5edc27224a37 58 lcd.cls(); // clear the display
Soldier7 18:5dfebb54cdbd 59 lcd.locate(0,1); // the location where you want your charater to be displayed
Soldier7 20:5f6df7ae19a3 60 lcd.printf("Sonar: %0.3f, OutTilt: %0.3f", sonar.read(), outTilt);
Soldier7 18:5dfebb54cdbd 61 lcd.locate(0,9); // the location where you want your charater to be displayed
Soldier7 22:00a577f64930 62 lcd.printf("Vert: %0.3f, OutVert: %0.3f", corVert, outVert);
Argensis 9:e5d24b7a921b 63 Thread::wait(250);
Argensis 23:7d29f2132197 64 mutex_sonar.unlock();
Soldier7 0:5edc27224a37 65 }
Soldier7 0:5edc27224a37 66 }
Soldier7 0:5edc27224a37 67
Soldier7 0:5edc27224a37 68 /* Thread Control 3 - handles the input data from the sonar sensor, and display on the LCD screen.
Soldier7 0:5edc27224a37 69 @update inData */
Soldier7 21:f098ffcd192a 70 void control_thread(void const *args) {
Soldier7 0:5edc27224a37 71 while (true) {
Soldier7 0:5edc27224a37 72 mutexIn.lock();
Soldier7 18:5dfebb54cdbd 73 if (corVert > 0 && corVert < 1) { // if corVert is valid (between 0 - 1) then do movements
Soldier7 12:f6d4980c48d6 74 // moves lamp down by the fraction of the difference from the middle
Soldier7 20:5f6df7ae19a3 75 if (corVert >= .5) {
Soldier7 20:5f6df7ae19a3 76 outVert = corVert - ((corVert - .5) * sonar);
Soldier7 20:5f6df7ae19a3 77 } else {
Soldier7 20:5f6df7ae19a3 78 outVert = corVert + ((.5 - corVert) * sonar);
Soldier7 20:5f6df7ae19a3 79 }
Soldier7 20:5f6df7ae19a3 80 outTilt = corVert;
Soldier7 20:5f6df7ae19a3 81 outHoriz = corHoriz;
Soldier7 20:5f6df7ae19a3 82 } else { // Else this is the case when there is no input from the OpenCV
Soldier7 14:74733a28eb80 83 outVert = 1;
Soldier7 18:5dfebb54cdbd 84 outTilt = corVert;
Soldier7 14:74733a28eb80 85 // TODO Pan search code here. (Searching personality.)
Soldier7 20:5f6df7ae19a3 86 /*if (search < outHoriz && search < .9) {
Soldier7 20:5f6df7ae19a3 87 outHoriz = outHoriz + .05;
Soldier7 20:5f6df7ae19a3 88 search = search + .05;
Soldier7 20:5f6df7ae19a3 89 } else if (search > outHoriz && search > .1) {
Soldier7 20:5f6df7ae19a3 90 outHoriz =
Soldier7 20:5f6df7ae19a3 91 search =
Soldier7 20:5f6df7ae19a3 92 }*/
Soldier7 12:f6d4980c48d6 93 }
Soldier7 0:5edc27224a37 94 mutexIn.unlock();
Ali_taher 16:af76305da577 95 Thread::wait(250);
Soldier7 0:5edc27224a37 96 }
Soldier7 0:5edc27224a37 97 }
Soldier7 0:5edc27224a37 98
Soldier7 0:5edc27224a37 99 /* Thread Servo 4 - handles the output data from the control thread, and pass to the servo.
Soldier7 0:5edc27224a37 100 @update s1, s2 */
Soldier7 21:f098ffcd192a 101 void servo_thread(void const *args) {
Soldier7 0:5edc27224a37 102 while (true) {
Soldier7 0:5edc27224a37 103 mutexOut.lock();
Argensis 10:ca6f2769964e 104 tiltServo = outTilt;
Argensis 10:ca6f2769964e 105 panServo = outHoriz;
Argensis 10:ca6f2769964e 106 vertServo = outVert;
Soldier7 0:5edc27224a37 107 mutexOut.unlock();
Ali_taher 16:af76305da577 108 Thread::wait(250);
Soldier7 0:5edc27224a37 109 }
Soldier7 0:5edc27224a37 110 }
Soldier7 0:5edc27224a37 111
Ali_taher 16:af76305da577 112 /* Thread sonar 5 - handles the sonar values which can be in meter or normailsed value to one */
Ali_taher 16:af76305da577 113 void sonar_thread(void const *args) {
Ali_taher 16:af76305da577 114 while (true) {
Ali_taher 16:af76305da577 115 mutex_sonar.lock();
Ali_taher 16:af76305da577 116 distance = rf.read_m(); // read the distance from the sonar sensor in meter
Ali_taher 16:af76305da577 117 norm= distance/3.3; // normalised value from the sonar sensor
Ali_taher 16:af76305da577 118 printf("dis: %0.2f", distance);// Display the distance in meters from the sonar
Ali_taher 16:af76305da577 119 mutex_sonar.unlock();
Ali_taher 16:af76305da577 120 Thread::wait(250);
Ali_taher 16:af76305da577 121 }
Ali_taher 16:af76305da577 122 }
Argensis 17:974430ee2fbb 123
Soldier7 0:5edc27224a37 124 int main() {
Soldier7 0:5edc27224a37 125 Thread thread_1(serial_thread); // Start Serial Thread
Soldier7 0:5edc27224a37 126 Thread thread_2(lcd_thread); // Start LCD Thread
Argensis 10:ca6f2769964e 127 Thread thread_3(control_thread); // Start Servo Thread
Soldier7 0:5edc27224a37 128 Thread thread_4(servo_thread); // Start Servo Thread
Ali_taher 16:af76305da577 129 Thread thread_5(sonar_thread); // Start Servo Thread
Argensis 17:974430ee2fbb 130 while(1) {
Argensis 9:e5d24b7a921b 131 wait(1);
Soldier7 0:5edc27224a37 132 }
Soldier7 0:5edc27224a37 133 }