Uses 2 HC-SR04 ultrasonic modules to steer the RenBuggy away from obstacles. Renishaw Team page fork.

Dependencies:   mbed

Fork of RenBuggy_Ultrasonic by Ren Buggy

Files at this revision

API Documentation at this revision

Comitter:
RenBuggy
Date:
Mon Jul 18 10:00:29 2016 +0000
Parent:
0:fbceffb594b6
Commit message:
First working revision, uses 2 sensors

Changed in this revision

USBDevice.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
ultrasonic_buggy.cpp Show annotated file Show diff for this revision Revisions of this file
ultrasonic_buggy.h Show annotated file Show diff for this revision Revisions of this file
diff -r fbceffb594b6 -r 80c2ef16a42f USBDevice.lib
--- a/USBDevice.lib	Thu Jul 14 12:58:48 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/USBDevice/#01321bd6ff89
diff -r fbceffb594b6 -r 80c2ef16a42f main.cpp
--- a/main.cpp	Thu Jul 14 12:58:48 2016 +0000
+++ b/main.cpp	Mon Jul 18 10:00:29 2016 +0000
@@ -1,39 +1,64 @@
+/*
+*RenBuggy_Ultrasonic
+*
+*A Program that allows the RenBuggy to avoid obstacles using two HC-SR04
+*ultrasonic modules.
+*
+*Copyright (c) 2016 Elijah Orr
+
+*Permission is hereby granted, free of charge, to any person obtaining a copy 
+*of this software and associated documentation files (the "Software"), to deal
+*in the Software without restriction, including without limitation the rights 
+*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell    
+*copies of the Software, and to permit persons to whom the Software is        
+*furnished to do so, subject to the following conditions:                     
+                                                                             
+*The above copyright notice and this permission notice shall be included in   
+*all copies or substantial portions of the Software.                          
+                                                                             
+*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR   
+*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,     
+*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
+*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER       
+*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN    
+*THE SOFTWARE.
+*/
+
+//include files - mbed library and our ultrasonic library
 #include "mbed.h"
 #include "ultrasonic_buggy.h"
-//#include "USBSerial.h"
 
-//USBSerial serial;
-
-
+//The main function is where the program will begin to execute
 int main(){
     
+    //create two floating point vairables to store distance readings in
     float left_distance;
     float right_distance;
+    //call the stop function to make sure buggy is stationary
     stop();
+    //wait for some setup time
     wait(5);
-    //serial.printf("PROGRAM START\n\r");
+    //the code in this while loop will always execute and will repeat forever
     while(1){
         
-        
-        //wait(1);
-        
+        //take a distance reading from each of the sensors
         left_distance = getDistance_l();
         right_distance = getDistance_r();
         
-        //if((left_distance > 0.3) && (right_distance > 0.3)){
-        //serial.printf("left distance = %f   right distance = %f\n\r", left_distance, right_distance);
-            /* if(left_distance > right_distance){
-                left(0.1);
-            }
-            else if(right_distance > left_distance){
-                right(0.1);
-            }
+        //if there is an object on the left, turn right
+        if(left_distance < 0.4){
+          right(0.1);
         }
-        
+        //else if there is an object on the right, turn left
+        else if(right_distance < 0.4){
+            left(0.1);
+        }
+        //else move forwards
         else{
-            right(3);
-        }*/
-        
+            forward(0.1);
+        }
+        //code loops back to start of while loop
     }
 }
 
diff -r fbceffb594b6 -r 80c2ef16a42f ultrasonic_buggy.cpp
--- a/ultrasonic_buggy.cpp	Thu Jul 14 12:58:48 2016 +0000
+++ b/ultrasonic_buggy.cpp	Mon Jul 18 10:00:29 2016 +0000
@@ -14,8 +14,8 @@
 #include "ultrasonic_buggy.h"
 
 // PwmOut is used to control the motors
-DigitalOut Lmotor(LEFT_MOTOR_PIN, 0);
-DigitalOut Rmotor(RIGHT_MOTOR_PIN, 0);
+PwmOut Lmotor(LEFT_MOTOR_PIN);
+PwmOut Rmotor(RIGHT_MOTOR_PIN);
 
 // set up digital in and out pins for the ultrasonic, as well as a timer for use in the ultrasonic functions
 DigitalOut trigger(TRIGGER_PIN);
@@ -24,10 +24,10 @@
 Timer timer;
 DigitalOut SR04_nReset(P0_7, 1);
 
-//Trim is an offset that you can adjust to help the buggy drive straight
-//Trim = -0.3 is a left trim
-//Trim =  0.3 is a right trim
-//float trim = -0.6;
+/* Trim is an offset that you can adjust to help the buggy drive straight
+Trim = -0.3 is a left trim
+Trim =  0.3 is a right trim */
+float trim = -0.3;
 
 /* Functions (listed below) contain the code that runs the buggy.
 These functions can be used in the main.cpp file */
@@ -42,8 +42,8 @@
 
 extern void forward(float time) //moves forward for (time) seconds
 {
-    Lmotor = 1; //+ trim;
-    Rmotor = 1; //- trim; //set the left and right motor to 1.0 (full speed) - the trim offset
+    Lmotor = 1.0 + trim;
+    Rmotor = 1.0 - trim; //set the left and right motor to 1.0 (full speed) - the trim offset
     hold(time); //wait for (time) seconds while the motors are on
     stop(); //stops the motors
 }
@@ -51,8 +51,8 @@
 
 extern void left(float time) //moves left for (time) seconds
 {
-    Rmotor = 1;  //set the right motor to full speed
-    Lmotor = 0; //set the left motor to off
+    Rmotor = 1.0;  //set the right motor to full speed
+    Lmotor = 0.0; //set the left motor to off
     hold(time); //waits for (time) seconds
     stop(); //stops the motors
 }
@@ -60,8 +60,8 @@
 
 extern void right(float time) //moves right for (time) seconds
 {
-    Lmotor = 1;  //set the left motor to full speed
-    Rmotor = 0; //set the right motor to off
+    Lmotor = 1.0;  //set the left motor to full speed
+    Rmotor = 0.0; //set the right motor to off
     hold(time); //waits for (time) seconds
     stop(); //stops the motors
 }
@@ -77,9 +77,10 @@
 *
 *Gets a left distance reading from the ultrasonic sensor
 *
-*Returns: float(distance in metres). Returns 0 if no object within 4m.
+*Returns: float(distance in metres). Returns 4.0 if no reflection is received.
 *
 *WARNING: SPAGHETTI
+*
 */
 extern float getDistance_l(void)
 {
@@ -87,13 +88,18 @@
     float distance_m = 0;
     
     trigger:
+    timer.stop();
+    timer.reset();
     //send trigger signal to ultrasonic
     trigger = 1;        //set trigger pin high
     wait_us(10);        //wait 10 us
     trigger = 0;        //set trigger pin low
+    timer.start();
     
     check_echo_high:
     if(echo_left == 1){          //echo pin goes high
+        timer.stop();
+        timer.reset();
         timer.start();      //start the timer
         check_echo_low:
         if(echo_left == 0){      //echo pin goes low      
@@ -104,13 +110,13 @@
         }
         else{
             if(timer.read_ms() > 30 ){   //if the timer reaches 30ms then echo is stuck high, so reset the SR04
+                
                 resetSR04();
-                if(echo_left == 0){
-                    goto trigger;   //when echo goes low again, reattempt measurement
-                }
-                else{
-                    wait_ms(1);     //otherwise wait for low state
-                }
+                                                                       
+                distance_m = 4.0;
+                goto returnvalue;
+                    
+                
             }
             else{
                 goto check_echo_low;    //go to inner loop so that RenBED keeps checking echo pin for low state
@@ -118,11 +124,20 @@
         }
     }
     else{
+        if(timer.read_ms() > 20){
+            goto trigger;                   //resend trigger if module unresponsive
+        }
         goto check_echo_high;              //go to loop so that RenBED keeps checking echo pin for high state
+        
     }
     echo_pulse_complete: 
     distance_m = (time * SPEED_OF_SOUND / 2);   //convert the time reading to a distance. Divided by 2 as the sound waves must travel and return.
     
+    if(distance_m < 0.02){
+        goto trigger;
+    }
+    
+    returnvalue:
     return distance_m;          //return the distance in metres                     
 }
 
@@ -131,7 +146,10 @@
 *
 *Gets a right distance reading from the ultrasonic sensor
 *
-*Returns: float(distance in metres). Returns 0 if no object within 4m.
+*Returns: float(distance in metres). Returns 4.0 if no reflection is received.
+*
+*WARNING: SPAGHETTI
+*
 */
 float getDistance_r(void)
 {
@@ -139,13 +157,18 @@
     float distance_m = 0;
     
     trigger:
+    timer.stop();
+    timer.reset();
     //send trigger signal to ultrasonic
     trigger = 1;        //set trigger pin high
     wait_us(10);        //wait 10 us
     trigger = 0;        //set trigger pin low
+    timer.start();      //start the timer
     
     check_echo_high:
     if(echo_right == 1){          //echo pin goes high
+        timer.stop();
+        timer.reset();
         timer.start();      //start the timer
         check_echo_low:
         if(echo_right == 0){      //echo pin goes low      
@@ -156,13 +179,11 @@
         }
         else{
             if(timer.read_ms() > 30 ){   //if the timer reaches 30ms then echo is stuck high, so reset the SR04
+                
                 resetSR04();
-                if(echo_right == 0){
-                    goto trigger;   //when echo goes low again, reattempt measurement
-                }
-                else{
-                    wait_ms(1);     //otherwise wait for low state
-                }
+                                                                           
+                distance_m = 4.0;
+                goto returnvalue;
             }
             else{
                 goto check_echo_low;    //go to inner loop so that RenBED keeps checking echo pin for low state
@@ -170,21 +191,30 @@
         }
     }
     else{
+        if(timer.read_ms() > 20){
+            goto trigger;                   //resend trigger if module unresponsive
+        }
         goto check_echo_high;              //go to loop so that RenBED keeps checking echo pin for high state
+        
     }
     echo_pulse_complete: 
     distance_m = (time * SPEED_OF_SOUND / 2);   //convert the time reading to a distance. Divided by 2 as the sound waves must travel and return.
     
+    if(distance_m < 0.02){
+        goto trigger;
+    }
+    
+    returnvalue:
     return distance_m;          //return the distance in metres                     
 }
 
 void resetSR04(void)
 {
-    SR04_nReset = 0;
-    forward(1);
+    SR04_nReset = 0;        //turn off modules
+    wait_ms(10);            //wait for a while    
     timer.stop();
     timer.reset();
-    SR04_nReset = 1;
-}
+    SR04_nReset = 1;        //turn modules back on
+}    
 
 #endif // BUGGY_FUNCTIONS_C
\ No newline at end of file
diff -r fbceffb594b6 -r 80c2ef16a42f ultrasonic_buggy.h
--- a/ultrasonic_buggy.h	Thu Jul 14 12:58:48 2016 +0000
+++ b/ultrasonic_buggy.h	Mon Jul 18 10:00:29 2016 +0000
@@ -24,18 +24,18 @@
 #define LEFT_ECHO_PIN P1_20
 #define RIGHT_ECHO_PIN P0_12
 
-#define SPEED_OF_SOUND 340      //speed of sound =~340m/s at standard conditions
+#define SPEED_OF_SOUND 340          //speed of sound =~340m/s at standard conditions
 
 /* these are function prototypes that declare all the functions
 in the library.*/
-extern void forward(float); //Move the buggy forward for (float) seconds
-extern void left(float); //Turn left for (float) seconds
-extern void right(float); //Turn right for (float) seconds
-extern void hold(float); //Hold the buggy in an idle state for (float) seconds
-extern void stop(); //Stop all motors
+extern void forward(float);         //Move the buggy forward for (float) seconds
+extern void left(float);            //Turn left for (float) seconds
+extern void right(float);           //Turn right for (float) seconds
+extern void hold(float);            //Hold the buggy in an idle state for (float) seconds
+extern void stop();                 //Stop all motors
 
-extern float getDistance_l(void);
-extern float getDistance_r(void);
-void resetSR04(void);
+extern float getDistance_l(void);   //get a distance reading in metres from the left sensor
+extern float getDistance_r(void);   //get a distance reading in metres from the right sensor
+void resetSR04(void);               //reset both ultrasonic modules
 
 #endif // BUGGY_FUNCTIONS_H
\ No newline at end of file