sonar sensor is working in the version of code but the reading are not correct

Dependencies:   C12832 Pulse RangeFinder Servo mbed rtos

Fork of Team_Sprint2 by WIT_EmbOS_Gr1

Committer:
Soldier7
Date:
Thu Apr 23 11:00:40 2015 +0000
Revision:
24:00a577f64930
Parent:
23:f098ffcd192a
This is the working one after the merge.

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