Code for Sprint 2
Dependencies: C12832 mbed rtos RangeFinder
Diff: main.cpp
- Revision:
- 21:f098ffcd192a
- Parent:
- 20:5f6df7ae19a3
- Parent:
- 17:974430ee2fbb
- Child:
- 22:00a577f64930
diff -r 5f6df7ae19a3 -r f098ffcd192a main.cpp --- a/main.cpp Thu Apr 23 10:40:09 2015 +0000 +++ b/main.cpp Thu Apr 23 10:50:12 2015 +0000 @@ -2,27 +2,36 @@ #include "rtos.h" #include "Servo.h" #include "C12832.h" +#include "RangeFinder.h"// header files for sonar sensor Servo tiltServo(p24); Servo panServo(p25); Serial pc(USBTX, USBRX); Servo vertServo(p23); - +Mutex mutexIn;// protect global variables +Mutex mutexOut;// protect global variables +Mutex mutex_sonar; AnalogIn sonar(p19); // temporary changed to potmeter -AnalogIn corVert(p20); // temporary +AnalogIn corVert(p20); // temporary changed to potmeter Mutex mutexIn; Mutex mutexOut; // Global variables float corHoriz = 0.5; // horizontal variable arrives from OpenCV -//float corVert = 0.5; // vertical variable arrives from OpenCV -float corVert1 = 0.5; // temporary -float distance = 0.5; // distance from the sonar sensor +float corVert = 0.5; // vertical variable arrives from OpenCV +float distance = 0.5;// variable holds the distance in meters 0 to 3.3 +float norm=0; // variable holds the normalised values form the sonar sensor float outVert; // output to vertical servo float outTilt; // output to tilt servo float outHoriz; // output to horizontal servo -float search = .9; // set current position of pan servo to find the movement direction -C12832 lcd(p5, p7, p6, p8, p11);// lcd is an object from class c12832 initialised by p5, p7.... +C12832 lcd(p5, p7, p6, p8, p11);// lcd is an object from class c12832 initialised by p5,p7.... + +/* parallax ultrasound range finder +p21 pin the range finder is connected to. +10 is Time of pulse to send to the rangefinder to trigger a measurement, in microseconds. +5800 is Scaling of the range finder's output pulse from microseconds to metres. +100000 Time to wait for a pulse from the range finder before giving up */ +RangeFinder rf(p26, 10, 5800.0, 100000); /* Thread Serial 1 - handles the output data from the control thread, and pass to the servo. @update s1, s2 */ @@ -38,22 +47,20 @@ void lcd_thread(void const *args) { while (true) { + mutex_sonar.lock(); // Display values on the LCD screen lcd.cls(); // clear the display lcd.locate(0,1); // the location where you want your charater to be displayed lcd.printf("Sonar: %0.3f, OutTilt: %0.3f", sonar.read(), outTilt); lcd.locate(0,9); // the location where you want your charater to be displayed lcd.printf("Vert: %0.3f, OutVert: %0.3f", corVert.read(), outVert); - //lcd.locate(0,18); // the location where you want your charater to be displayed - //lcd.printf("Sonar: %0.3f", sonar.read()); Thread::wait(250); } } /* Thread Control 3 - handles the input data from the sonar sensor, and display on the LCD screen. @update inData */ -void control_thread(void const *args) -{ +void control_thread(void const *args) { while (true) { mutexIn.lock(); if (corVert > 0 && corVert < 1) { // if corVert is valid (between 0 - 1) then do movements @@ -78,31 +85,42 @@ }*/ } mutexIn.unlock(); - Thread::wait(25); + Thread::wait(250); } } /* Thread Servo 4 - handles the output data from the control thread, and pass to the servo. @update s1, s2 */ -void servo_thread(void const *args) -{ +void servo_thread(void const *args) { while (true) { mutexOut.lock(); tiltServo = outTilt; panServo = outHoriz; vertServo = outVert; mutexOut.unlock(); - Thread::wait(200); + Thread::wait(250); } } -int main() -{ +/* Thread sonar 5 - handles the sonar values which can be in meter or normailsed value to one */ +void sonar_thread(void const *args) { + while (true) { + mutex_sonar.lock(); + distance = rf.read_m(); // read the distance from the sonar sensor in meter + norm= distance/3.3; // normalised value from the sonar sensor + printf("dis: %0.2f", distance);// Display the distance in meters from the sonar + mutex_sonar.unlock(); + Thread::wait(250); + } +} + +int main() { Thread thread_1(serial_thread); // Start Serial Thread Thread thread_2(lcd_thread); // Start LCD Thread Thread thread_3(control_thread); // Start Servo Thread Thread thread_4(servo_thread); // Start Servo Thread - while(1) { + Thread thread_5(sonar_thread); // Start Servo Thread + while(1) { wait(1); } }