![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
updated codes lads
Dependencies: C12832 Servo mbed-rtos mbed
Fork of rtos_basic_updated by
Diff: main.cpp
- Revision:
- 15:1e53fa116948
- Parent:
- 13:aef2afe73bc6
--- a/main.cpp Mon Feb 02 13:02:36 2015 +0000 +++ b/main.cpp Mon Feb 09 16:22:25 2015 +0000 @@ -5,76 +5,67 @@ Servo s1(p21); Servo s2(p22); -AnalogIn p1(p19); // Sonar sensor 1 +AnalogIn p1(p19); // Sonar sensor 1 17 for the sensor or 19 for the potential meter. //AnalogIn p2(p20); // Sonar sensor 2 (not used yet) -Mutex input_data_mutex; -Mutex output_data_mutex; +Mutex inData_mutex; +Mutex outData_mutex; // Globel variables -float input_data; -float output_data; +float inData; +float outData; C12832 lcd(p5, p7, p6, p8, p11);// lcd is an object from class c12832 initialised by p5,p7.... /* Thread Sonar - handles the input data from the sonar sensor, and display on the LCD screen. - @update input_data - */ -void sonar_thread(void const *args) -{ - float anVolt; - float sum=0; //Create sum variable so it can be averaged - float avg=60.00; //Quantity of values to average (sample size) - // Loop for + @update inData */ +void sonar_thread(void const *args) { while (true) { - input_data_mutex.lock(); -// // Average the calculation -// if (avg>=60) { -// sum=0; -// } -// // Read input and return -// for(int i = 0; i < avg ; i++) { -// anVolt = p1.read(); / -// sum += anVolt; -// } -// // Update output_data global variable -// output_data = sum/avg; - output_data=p1.read()*5; - // Display values on the LCD screen - lcd.cls(); // clear the display - lcd.locate(0,3);// the location where you want your charater to be displayed - //lcd.printf("Input = %f\n", input_data); - lcd.printf("Output = %f\n", output_data); - // Handles the thread - input_data_mutex.unlock(); + inData_mutex.lock(); + inData = p1.read(); + inData_mutex.unlock(); Thread::wait(25); } } -/* Thread Control - */ -void control_thread(void const *args) -{ +/* Thread Control - + @update outData */ +void control_thread(void const *args) { while (true) { - input_data_mutex.lock(); - output_data_mutex.lock(); - // Modification on the input_data and assign to servo2 - if (output_data<4) { - s2 = -output_data; // move backwards - } else if(output_data<2) { - s2=output_data; // keep tracking the object - } - // Assign input_data to servo1 without modification - s1=input_data; + inData_mutex.lock(); + outData_mutex.lock(); + // The servo works from 0 to 1 volt and in approx 110 degree + float voltage = inData; // local variable to play with + // Logic code comes here + + outData = voltage; // Update output_data global variable + // Display values on the LCD screen + lcd.cls(); // clear the display + lcd.locate(0,3);// the location where you want your charater to be displayed + lcd.printf("In: %f, Out: %f", inData, outData); // Handles the thread - input_data_mutex.unlock(); - output_data_mutex.unlock(); + inData_mutex.unlock(); + outData_mutex.unlock(); Thread::wait(20); } } +/* Thread Servo - handles the output data from the control thread, and pass to the servo. + @update s1, s2 */ +void servo_thread(void const *args) { + while (true) { + outData_mutex.lock(); + s1 = outData; + s2 = outData; + outData_mutex.unlock(); + Thread::wait(25); + } +} + /* Main method - Start threads and delay them by waiting. */ int main() { - Thread thread(sonar_thread); // Start Sonar Thread - Thread thread_1(control_thread); // Start Control Thread + Thread thread_1(sonar_thread); // Start Sonar Thread + Thread thread_2(control_thread); // Start Control Thread + Thread thread_3(servo_thread); // Start Servo Thread // Delay the threads while (true) { Thread::wait(10);