Code for Sprint 2

Dependencies:   C12832 mbed rtos RangeFinder

Committer:
Soldier7
Date:
Thu Mar 12 19:57:41 2015 +0000
Revision:
4:70090f3b1f07
Parent:
3:3d53799c2f18
Child:
7:24d62ef1ed34
Child:
9:e5d24b7a921b
Commit based on Ali's changes on 'sprint_update'; ; Not working.  Getting error with operation at servo update.

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 4:70090f3b1f07 10
Soldier7 4:70090f3b1f07 11 AnalogIn p1(p19);
Soldier7 4:70090f3b1f07 12 AnalogIn p2(p20);
Soldier7 0:5edc27224a37 13
Soldier7 0:5edc27224a37 14 // Globel variables
Ali_taher 2:b109a4eb9b0d 15 char cordinates[20];
Ali_taher 3:3d53799c2f18 16 char corHoriz[20];
Soldier7 4:70090f3b1f07 17 char corVert[20];
PaulF 1:28ea653772dc 18 //float corDeep;
Soldier7 4:70090f3b1f07 19 float outVert;
PaulF 1:28ea653772dc 20 //float outHoriz;
Soldier7 0:5edc27224a37 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 0:5edc27224a37 25 void serial_thread(void const *args) {
Soldier7 0:5edc27224a37 26 while (true) {
Soldier7 0:5edc27224a37 27 mutexIn.lock();
Ali_taher 2:b109a4eb9b0d 28 // pc.gets(cordinates,4);
Ali_taher 2:b109a4eb9b0d 29 // cordinates = pc.putc(pc.getc());
Soldier7 4:70090f3b1f07 30 pc.scanf("%s \n %s \n ",corHoriz,corVert);// read from serial port the data
Soldier7 0:5edc27224a37 31 mutexIn.unlock();
Soldier7 0:5edc27224a37 32 Thread::wait(200);
Soldier7 0:5edc27224a37 33 }
Soldier7 0:5edc27224a37 34 }
Soldier7 0:5edc27224a37 35
Soldier7 0:5edc27224a37 36 /* Thread LCD 2 - handles the input data from the sonar sensor, and display on the LCD screen.
Soldier7 0:5edc27224a37 37 @update inData */
Soldier7 0:5edc27224a37 38 void lcd_thread(void const *args) {
Soldier7 0:5edc27224a37 39 while (true) {
Soldier7 0:5edc27224a37 40 mutexIn.lock();
Soldier7 0:5edc27224a37 41 mutexOut.lock();
Soldier7 0:5edc27224a37 42 // Display values on the LCD screen
Soldier7 0:5edc27224a37 43 lcd.cls(); // clear the display
Soldier7 4:70090f3b1f07 44 lcd.locate(0,5); // the location where you want your charater to be displayed
Soldier7 4:70090f3b1f07 45 lcd.printf("Hor:%s",corHoriz);
Ali_taher 3:3d53799c2f18 46 lcd.locate(0,20); // the location where you want your charater to be displayed
Soldier7 4:70090f3b1f07 47 lcd.printf("Ver%s", corVert);
Soldier7 0:5edc27224a37 48 mutexIn.unlock();
Soldier7 0:5edc27224a37 49 mutexOut.unlock();
Soldier7 0:5edc27224a37 50 Thread::wait(25);
Soldier7 0:5edc27224a37 51 }
Soldier7 0:5edc27224a37 52 }
Soldier7 0:5edc27224a37 53
Soldier7 0:5edc27224a37 54 /* Thread Control 3 - handles the input data from the sonar sensor, and display on the LCD screen.
Soldier7 0:5edc27224a37 55 @update inData */
Soldier7 0:5edc27224a37 56 void control_thread(void const *args) {
Soldier7 0:5edc27224a37 57 while (true) {
Soldier7 0:5edc27224a37 58 mutexIn.lock();
Soldier7 0:5edc27224a37 59 // The control code will come here
Soldier7 4:70090f3b1f07 60 if (corHoriz[1]=='1')
Soldier7 4:70090f3b1f07 61 s1 = s1 + 0.25;
Soldier7 4:70090f3b1f07 62 if (corHoriz[1]=='2')
Soldier7 4:70090f3b1f07 63 s1 = s1 - 0.25;
Soldier7 0:5edc27224a37 64 mutexIn.unlock();
Soldier7 0:5edc27224a37 65 Thread::wait(25);
Soldier7 0:5edc27224a37 66 }
Soldier7 0:5edc27224a37 67 }
Soldier7 0:5edc27224a37 68
Soldier7 0:5edc27224a37 69 /* Thread Servo 4 - handles the output data from the control thread, and pass to the servo.
Soldier7 0:5edc27224a37 70 @update s1, s2 */
Soldier7 0:5edc27224a37 71 void servo_thread(void const *args) {
Soldier7 0:5edc27224a37 72 while (true) {
Soldier7 0:5edc27224a37 73 mutexOut.lock();
Soldier7 0:5edc27224a37 74 // s1 = outVert;
Soldier7 0:5edc27224a37 75 // s2 = outHoriz;
Soldier7 0:5edc27224a37 76 mutexOut.unlock();
Soldier7 0:5edc27224a37 77 Thread::wait(200);
Soldier7 0:5edc27224a37 78 }
Soldier7 0:5edc27224a37 79 }
Soldier7 0:5edc27224a37 80
Soldier7 0:5edc27224a37 81 int main() {
Soldier7 0:5edc27224a37 82 Thread thread_1(serial_thread); // Start Serial Thread
Soldier7 0:5edc27224a37 83 Thread thread_2(lcd_thread); // Start LCD Thread
Soldier7 0:5edc27224a37 84 Thread thread_3(control_thread); // Start Control Thread
Soldier7 0:5edc27224a37 85 Thread thread_4(servo_thread); // Start Servo Thread
Soldier7 0:5edc27224a37 86 while(1) {
Soldier7 0:5edc27224a37 87 Thread::wait(10);
Soldier7 0:5edc27224a37 88 }
Soldier7 0:5edc27224a37 89 }