Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832 mbed rtos RangeFinder
Revision 24:de4d6a06011b, committed 2015-04-20
- Comitter:
- Ali_taher
- Date:
- Mon Apr 20 13:06:40 2015 +0000
- Parent:
- 16:af76305da577
- Commit message:
- adding the sonar reading and servo movements when there is any object within the 3 meters
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp.orig | Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Apr 17 10:12:20 2015 +0000
+++ b/main.cpp Mon Apr 20 13:06:40 2015 +0000
@@ -26,18 +26,22 @@
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.
+p23 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);
+RangeFinder rf(p23, 10, 5800.0, 100000);
/* Thread Serial 1 - handles the output data from the control thread, and pass to the servo.
@update s1, s2 */
void serial_thread(void const *args) {
while (true) {
+ // I have problem in this thread the sonar reading is not correct if this thread is used
+
pc.scanf("%f,%f", &corHoriz, &corVert);// read from serial port the data
+
+
}
}
@@ -45,7 +49,7 @@
@update inData */
void lcd_thread(void const *args) {
while (true) {
- mutex_sonar.lock();
+ mutex_sonar.lock();
// Display values on the LCD screen
lcd.cls(); // clear the display
lcd.locate(0,5); // the location where you want your charater to be displayed
@@ -81,33 +85,52 @@
@update s1, s2 */
void servo_thread(void const *args) {
while (true) {
+ // mutex_sonar.lock();
mutexOut.lock();
tiltServo = outTilt;
panServo = outHoriz;
vertServo = outVert;
+
+ /* if the distance more than 3 meters the servo will mover full range CW and CCW*/
+ for(int i=0; i<100; i++) {
+ if (distance>=2)
+ {
+ tiltServo = i/100.0;
+ Thread::wait(25);
+ }
+ }
+ for(int i=100; i>0; i--) {
+ if (distance>=2)
+ {
+ tiltServo = i/100.0;
+ Thread:: wait(25);
+ }
+ }
+ tiltServo.position(0.0);
+ // mutex_sonar.unlock();
mutexOut.unlock();
Thread::wait(250);
}
}
+
/* 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);
- }
+ while (1) {
+
}
+ }
int main() {
- Thread thread_1(serial_thread); // Start Serial Thread
- Thread thread_2(lcd_thread); // Start LCD Thread
+ // 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
Thread thread_5(sonar_thread); // Start Servo Thread
while(1) {
-
- wait(1);
+ 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
+ mutex_sonar.unlock();
+ Thread::wait(25);
}
}
--- a/main.cpp.orig Fri Apr 17 10:12:20 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,92 +0,0 @@
-#include "mbed.h"
-#include "rtos.h"
-#include "Servo.h"
-#include "C12832.h"
-Servo sPan(p21);
-Servo sTilt(p22);
-Serial pc(USBTX, USBRX);
-Servo sVert(p23);
-Sonar distance(p24);
-Mutex mutexIn;
-Mutex mutexOut;
-
-AnalogIn p1(p19);
-AnalogIn p2(p20);
-
-// Global variables
-float corHoriz; // horizontal variable arrives from OpenCV
-float corVert; // vertical variable arrives from OpenCV
-float distance; // distance measured by the sonar
-float outVert; // rr
-float outTilt;
-float outHoriz;
-C12832 lcd(p5, p7, p6, p8, p11);// lcd is an object from class c12832 initialised by p5,p7....
-
-/* Thread Serial 1 - handles the output data from the control thread, and pass to the servo.
- @update s1, s2 */
-void serial_thread(void const *args) {
- while (true) {
- mutexIn.lock();
- // pc.gets(cordinates,4);
- // cordinates = pc.putc(pc.getc());
- pc.scanf("%s \n %s \n ",corHoriz,corVert);// read from serial port the data
- mutexIn.unlock();
- Thread::wait(200);
- }
-}
-
-/* Thread LCD 2 - handles the input data from the sonar sensor, and display on the LCD screen.
- @update inData */
-void lcd_thread(void const *args) {
- while (true) {
- mutexIn.lock();
- mutexOut.lock();
- // Display values on the LCD screen
- lcd.cls(); // clear the display
- lcd.locate(0,5); // the location where you want your charater to be displayed
- lcd.printf("Hor:%s",corHoriz);
- lcd.locate(0,20); // the location where you want your charater to be displayed
- lcd.printf("Ver%s", corVert);
- mutexIn.unlock();
- mutexOut.unlock();
- Thread::wait(25);
- }
-}
-
-/* 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) {
- while (true) {
- mutexIn.lock();
- float differ;
- differ = exp(corVert + sonar * outTilt) / (1 + exp(corVert + sonar * outTilt));
- if (corVert > .5) { // check if face is below the half of the camera view
- if (outTilt > .5) { // check if lamp head is facing down
- // moves lamp down by the fraction of the difference from the middle
- mutexIn.unlock();
- Thread::wait(25);
- }
-}
-
-/* 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) {
- while (true) {
- mutexOut.lock();
- sTi1t = outTilt;
- sPan = outHoriz;
- sVert = outVert;
- mutexOut.unlock();
- Thread::wait(200);
- }
-}
-
-int main() {
- Thread thread_1(serial_thread); // Start Serial Thread
- Thread thread_2(lcd_thread); // Start LCD Thread
- Thread thread_3(control_thread); // Start Control Thread
- Thread thread_4(servo_thread); // Start Servo Thread
- while(1) {
- Thread::wait(10);
- }
-}