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

Committer:
RenBuggy
Date:
Mon Jul 18 10:00:29 2016 +0000
Revision:
1:80c2ef16a42f
Parent:
0:fbceffb594b6
First working revision, uses 2 sensors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RenBuggy 1:80c2ef16a42f 1 /*
RenBuggy 1:80c2ef16a42f 2 *RenBuggy_Ultrasonic
RenBuggy 1:80c2ef16a42f 3 *
RenBuggy 1:80c2ef16a42f 4 *A Program that allows the RenBuggy to avoid obstacles using two HC-SR04
RenBuggy 1:80c2ef16a42f 5 *ultrasonic modules.
RenBuggy 1:80c2ef16a42f 6 *
RenBuggy 1:80c2ef16a42f 7 *Copyright (c) 2016 Elijah Orr
RenBuggy 1:80c2ef16a42f 8
RenBuggy 1:80c2ef16a42f 9 *Permission is hereby granted, free of charge, to any person obtaining a copy
RenBuggy 1:80c2ef16a42f 10 *of this software and associated documentation files (the "Software"), to deal
RenBuggy 1:80c2ef16a42f 11 *in the Software without restriction, including without limitation the rights
RenBuggy 1:80c2ef16a42f 12 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
RenBuggy 1:80c2ef16a42f 13 *copies of the Software, and to permit persons to whom the Software is
RenBuggy 1:80c2ef16a42f 14 *furnished to do so, subject to the following conditions:
RenBuggy 1:80c2ef16a42f 15
RenBuggy 1:80c2ef16a42f 16 *The above copyright notice and this permission notice shall be included in
RenBuggy 1:80c2ef16a42f 17 *all copies or substantial portions of the Software.
RenBuggy 1:80c2ef16a42f 18
RenBuggy 1:80c2ef16a42f 19 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
RenBuggy 1:80c2ef16a42f 20 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
RenBuggy 1:80c2ef16a42f 21 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
RenBuggy 1:80c2ef16a42f 22 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
RenBuggy 1:80c2ef16a42f 23 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
RenBuggy 1:80c2ef16a42f 24 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
RenBuggy 1:80c2ef16a42f 25 *THE SOFTWARE.
RenBuggy 1:80c2ef16a42f 26 */
RenBuggy 1:80c2ef16a42f 27
RenBuggy 1:80c2ef16a42f 28 //include files - mbed library and our ultrasonic library
RenBuggy 0:fbceffb594b6 29 #include "mbed.h"
RenBuggy 0:fbceffb594b6 30 #include "ultrasonic_buggy.h"
RenBuggy 0:fbceffb594b6 31
RenBuggy 1:80c2ef16a42f 32 //The main function is where the program will begin to execute
RenBuggy 0:fbceffb594b6 33 int main(){
RenBuggy 0:fbceffb594b6 34
RenBuggy 1:80c2ef16a42f 35 //create two floating point vairables to store distance readings in
RenBuggy 0:fbceffb594b6 36 float left_distance;
RenBuggy 0:fbceffb594b6 37 float right_distance;
RenBuggy 1:80c2ef16a42f 38 //call the stop function to make sure buggy is stationary
RenBuggy 0:fbceffb594b6 39 stop();
RenBuggy 1:80c2ef16a42f 40 //wait for some setup time
RenBuggy 0:fbceffb594b6 41 wait(5);
RenBuggy 1:80c2ef16a42f 42 //the code in this while loop will always execute and will repeat forever
RenBuggy 0:fbceffb594b6 43 while(1){
RenBuggy 0:fbceffb594b6 44
RenBuggy 1:80c2ef16a42f 45 //take a distance reading from each of the sensors
RenBuggy 0:fbceffb594b6 46 left_distance = getDistance_l();
RenBuggy 0:fbceffb594b6 47 right_distance = getDistance_r();
RenBuggy 0:fbceffb594b6 48
RenBuggy 1:80c2ef16a42f 49 //if there is an object on the left, turn right
RenBuggy 1:80c2ef16a42f 50 if(left_distance < 0.4){
RenBuggy 1:80c2ef16a42f 51 right(0.1);
RenBuggy 0:fbceffb594b6 52 }
RenBuggy 1:80c2ef16a42f 53 //else if there is an object on the right, turn left
RenBuggy 1:80c2ef16a42f 54 else if(right_distance < 0.4){
RenBuggy 1:80c2ef16a42f 55 left(0.1);
RenBuggy 1:80c2ef16a42f 56 }
RenBuggy 1:80c2ef16a42f 57 //else move forwards
RenBuggy 0:fbceffb594b6 58 else{
RenBuggy 1:80c2ef16a42f 59 forward(0.1);
RenBuggy 1:80c2ef16a42f 60 }
RenBuggy 1:80c2ef16a42f 61 //code loops back to start of while loop
RenBuggy 0:fbceffb594b6 62 }
RenBuggy 0:fbceffb594b6 63 }
RenBuggy 0:fbceffb594b6 64