Code for Sprint 2

Dependencies:   C12832 mbed rtos RangeFinder

Committer:
Soldier7
Date:
Wed Feb 25 17:33:41 2015 +0000
Revision:
0:5edc27224a37
Child:
1:28ea653772dc
Child:
5:4354b585dcab
Initial commit; ; Four threads: for serial input, LDC display, control and servo.; There is bug in line 25 to declare cordinates variable and line 67-68.

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
Soldier7 0:5edc27224a37 12 float cordinates[3];
Soldier7 0:5edc27224a37 13 float corHoriz;
Soldier7 0:5edc27224a37 14 float corVertic;
Soldier7 0:5edc27224a37 15 float corDeep;
Soldier7 0:5edc27224a37 16 float outVert;
Soldier7 0:5edc27224a37 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();
Soldier7 0:5edc27224a37 25 // cordinates = pc.putc(pc.getc());
Soldier7 0:5edc27224a37 26 corHoriz = cordinates[0];
Soldier7 0:5edc27224a37 27 corVertic = cordinates[1];
Soldier7 0:5edc27224a37 28 corDeep = cordinates[2];
Soldier7 0:5edc27224a37 29 mutexIn.unlock();
Soldier7 0:5edc27224a37 30 Thread::wait(200);
Soldier7 0:5edc27224a37 31 }
Soldier7 0:5edc27224a37 32 }
Soldier7 0:5edc27224a37 33
Soldier7 0:5edc27224a37 34 /* Thread LCD 2 - handles the input data from the sonar sensor, and display on the LCD screen.
Soldier7 0:5edc27224a37 35 @update inData */
Soldier7 0:5edc27224a37 36 void lcd_thread(void const *args) {
Soldier7 0:5edc27224a37 37 while (true) {
Soldier7 0:5edc27224a37 38 mutexIn.lock();
Soldier7 0:5edc27224a37 39 mutexOut.lock();
Soldier7 0:5edc27224a37 40 // Display values on the LCD screen
Soldier7 0:5edc27224a37 41 lcd.cls(); // clear the display
Soldier7 0:5edc27224a37 42 lcd.locate(0,3); // the location where you want your charater to be displayed
Soldier7 0:5edc27224a37 43 lcd.printf("Hor: %f, Vert: %f, Deep: %f", corHoriz, corVertic, corDeep);
Soldier7 0:5edc27224a37 44 mutexIn.unlock();
Soldier7 0:5edc27224a37 45 mutexOut.unlock();
Soldier7 0:5edc27224a37 46 Thread::wait(25);
Soldier7 0:5edc27224a37 47 }
Soldier7 0:5edc27224a37 48 }
Soldier7 0:5edc27224a37 49
Soldier7 0:5edc27224a37 50 /* Thread Control 3 - handles the input data from the sonar sensor, and display on the LCD screen.
Soldier7 0:5edc27224a37 51 @update inData */
Soldier7 0:5edc27224a37 52 void control_thread(void const *args) {
Soldier7 0:5edc27224a37 53 while (true) {
Soldier7 0:5edc27224a37 54 mutexIn.lock();
Soldier7 0:5edc27224a37 55 // The control code will come here
Soldier7 0:5edc27224a37 56
Soldier7 0:5edc27224a37 57 mutexIn.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 Servo 4 - handles the output data from the control thread, and pass to the servo.
Soldier7 0:5edc27224a37 63 @update s1, s2 */
Soldier7 0:5edc27224a37 64 void servo_thread(void const *args) {
Soldier7 0:5edc27224a37 65 while (true) {
Soldier7 0:5edc27224a37 66 mutexOut.lock();
Soldier7 0:5edc27224a37 67 // s1 = outVert;
Soldier7 0:5edc27224a37 68 // s2 = outHoriz;
Soldier7 0:5edc27224a37 69 mutexOut.unlock();
Soldier7 0:5edc27224a37 70 Thread::wait(200);
Soldier7 0:5edc27224a37 71 }
Soldier7 0:5edc27224a37 72 }
Soldier7 0:5edc27224a37 73
Soldier7 0:5edc27224a37 74 int main() {
Soldier7 0:5edc27224a37 75 Thread thread_1(serial_thread); // Start Serial Thread
Soldier7 0:5edc27224a37 76 Thread thread_2(lcd_thread); // Start LCD Thread
Soldier7 0:5edc27224a37 77 Thread thread_3(control_thread); // Start Control Thread
Soldier7 0:5edc27224a37 78 Thread thread_4(servo_thread); // Start Servo Thread
Soldier7 0:5edc27224a37 79 while(1) {
Soldier7 0:5edc27224a37 80 Thread::wait(10);
Soldier7 0:5edc27224a37 81 }
Soldier7 0:5edc27224a37 82 }