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:
Ali_taher
Date:
Sat Mar 07 22:56:18 2015 +0000
Revision:
3:3d53799c2f18
Parent:
2:b109a4eb9b0d
Child:
4:70090f3b1f07
reading from serial port ; each number separated by carriage return is assigned to one variable

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"
Soldier7 0:5edc27224a37 5 Servo s1(p21);
Soldier7 0:5edc27224a37 6 Servo s2(p22);
Soldier7 0:5edc27224a37 7 Serial pc(USBTX, USBRX);
Soldier7 0:5edc27224a37 8 Mutex mutexIn;
Soldier7 0:5edc27224a37 9 Mutex mutexOut;
Soldier7 0:5edc27224a37 10
Soldier7 0:5edc27224a37 11 // Globel variables
Ali_taher 2:b109a4eb9b0d 12 char cordinates[20];
Ali_taher 3:3d53799c2f18 13 char corHoriz[20];
Ali_taher 3:3d53799c2f18 14 char corVertic[20];
PaulF 1:28ea653772dc 15 //float corDeep;
PaulF 1:28ea653772dc 16 //float outVert;
PaulF 1:28ea653772dc 17 //float outHoriz;
Soldier7 0:5edc27224a37 18 C12832 lcd(p5, p7, p6, p8, p11);// lcd is an object from class c12832 initialised by p5,p7....
Soldier7 0:5edc27224a37 19
Soldier7 0:5edc27224a37 20 /* Thread Serial 1 - handles the output data from the control thread, and pass to the servo.
Soldier7 0:5edc27224a37 21 @update s1, s2 */
Soldier7 0:5edc27224a37 22 void serial_thread(void const *args) {
Soldier7 0:5edc27224a37 23 while (true) {
Soldier7 0:5edc27224a37 24 mutexIn.lock();
Ali_taher 2:b109a4eb9b0d 25 // pc.gets(cordinates,4);
Ali_taher 2:b109a4eb9b0d 26 // cordinates = pc.putc(pc.getc());
Ali_taher 3:3d53799c2f18 27 pc.scanf("%s \n %s \n %s",cordinates,corHoriz,corVertic);// read from serial port the data
Ali_taher 3:3d53799c2f18 28 // look for carriage return and assign the first number of to the cordinates and then the second to corHiz
Ali_taher 3:3d53799c2f18 29
Ali_taher 2:b109a4eb9b0d 30 //corHoriz=cordinates;
Ali_taher 2:b109a4eb9b0d 31 // corHoriz=(cordinates[0]+cordinates[1]+cordinates[2]+cordinates[3]);
Ali_taher 2:b109a4eb9b0d 32 // corVertic=(cordinates[4]+cordinates[5]+cordinates[6]+cordinates[7]);
Ali_taher 2:b109a4eb9b0d 33
Ali_taher 2:b109a4eb9b0d 34 //corVertic = cordinates[1];
PaulF 1:28ea653772dc 35 // corDeep = cordinates[2];
Soldier7 0:5edc27224a37 36 mutexIn.unlock();
Soldier7 0:5edc27224a37 37 Thread::wait(200);
Soldier7 0:5edc27224a37 38 }
Soldier7 0:5edc27224a37 39 }
Soldier7 0:5edc27224a37 40
Soldier7 0:5edc27224a37 41 /* Thread LCD 2 - handles the input data from the sonar sensor, and display on the LCD screen.
Soldier7 0:5edc27224a37 42 @update inData */
Soldier7 0:5edc27224a37 43 void lcd_thread(void const *args) {
Soldier7 0:5edc27224a37 44 while (true) {
Soldier7 0:5edc27224a37 45 mutexIn.lock();
Soldier7 0:5edc27224a37 46 mutexOut.lock();
Soldier7 0:5edc27224a37 47 // Display values on the LCD screen
Soldier7 0:5edc27224a37 48 lcd.cls(); // clear the display
Soldier7 0:5edc27224a37 49 lcd.locate(0,3); // the location where you want your charater to be displayed
Ali_taher 3:3d53799c2f18 50 lcd.printf("Hor:%s",cordinates);
Ali_taher 3:3d53799c2f18 51 lcd.locate(0,10); // the location where you want your charater to be displayed
Ali_taher 3:3d53799c2f18 52 lcd.printf("Ver%s", corHoriz);
Ali_taher 3:3d53799c2f18 53 lcd.locate(0,20); // the location where you want your charater to be displayed
Ali_taher 3:3d53799c2f18 54 lcd.printf("Ver%s", corVertic);
Ali_taher 2:b109a4eb9b0d 55 // lcd.printf("vertical: %s ",cordinates );
Soldier7 0:5edc27224a37 56 mutexIn.unlock();
Soldier7 0:5edc27224a37 57 mutexOut.unlock();
Soldier7 0:5edc27224a37 58 Thread::wait(25);
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();
Soldier7 0:5edc27224a37 67 // The control code will come here
Soldier7 0:5edc27224a37 68
Soldier7 0:5edc27224a37 69 mutexIn.unlock();
Soldier7 0:5edc27224a37 70 Thread::wait(25);
Soldier7 0:5edc27224a37 71 }
Soldier7 0:5edc27224a37 72 }
Soldier7 0:5edc27224a37 73
Soldier7 0:5edc27224a37 74 /* Thread Servo 4 - handles the output data from the control thread, and pass to the servo.
Soldier7 0:5edc27224a37 75 @update s1, s2 */
Soldier7 0:5edc27224a37 76 void servo_thread(void const *args) {
Soldier7 0:5edc27224a37 77 while (true) {
Soldier7 0:5edc27224a37 78 mutexOut.lock();
Soldier7 0:5edc27224a37 79 // s1 = outVert;
Soldier7 0:5edc27224a37 80 // s2 = outHoriz;
Soldier7 0:5edc27224a37 81 mutexOut.unlock();
Soldier7 0:5edc27224a37 82 Thread::wait(200);
Soldier7 0:5edc27224a37 83 }
Soldier7 0:5edc27224a37 84 }
Soldier7 0:5edc27224a37 85
Soldier7 0:5edc27224a37 86 int main() {
Soldier7 0:5edc27224a37 87 Thread thread_1(serial_thread); // Start Serial Thread
Soldier7 0:5edc27224a37 88 Thread thread_2(lcd_thread); // Start LCD Thread
Soldier7 0:5edc27224a37 89 Thread thread_3(control_thread); // Start Control Thread
Soldier7 0:5edc27224a37 90 Thread thread_4(servo_thread); // Start Servo Thread
Soldier7 0:5edc27224a37 91 while(1) {
Soldier7 0:5edc27224a37 92 Thread::wait(10);
Soldier7 0:5edc27224a37 93 }
Soldier7 0:5edc27224a37 94 }