Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Committer:
abdsha01
Date:
Sat May 23 15:05:18 2015 +0000
Revision:
4:0507835a3dce
Parent:
3:7f7a82bf59b9
Child:
5:68a2b79cf204
Improved code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abdsha01 0:15664f71b21f 1 // Code written by:
abdsha01 0:15664f71b21f 2 // Jon Baker
abdsha01 0:15664f71b21f 3 // Alessandro Grande
abdsha01 0:15664f71b21f 4 // Abdul-Rehman Sharif
abdsha01 0:15664f71b21f 5 // Lucinda Hartfield
abdsha01 0:15664f71b21f 6
abdsha01 0:15664f71b21f 7 // Circuitry made by:
abdsha01 0:15664f71b21f 8 // Yucando Navarrete
abdsha01 0:15664f71b21f 9 // Vivekanand Gupta
abdsha01 0:15664f71b21f 10
abdsha01 0:15664f71b21f 11 // The following code will control a robotic car
abdsha01 0:15664f71b21f 12 // by detetecting an object and charging towards it
abdsha01 0:15664f71b21f 13 // it uses basic functions as:
abdsha01 0:15664f71b21f 14 // charge() - used to charge on an object detected
abdsha01 0:15664f71b21f 15 // the robot will move in a straight line
abdsha01 0:15664f71b21f 16 // until it detects the arena line where
abdsha01 0:15664f71b21f 17 // it will use reverse() to move back
abdsha01 0:15664f71b21f 18 // detect_object() - used to detect an object, the robot will
abdsha01 0:15664f71b21f 19 // move in a circle to find an object and
abdsha01 0:15664f71b21f 20 // return 1 if it finds something and return
abdsha01 0:15664f71b21f 21 // 0 if it does not find anything - the search
abdsha01 0:15664f71b21f 22 // will be carried out for 10 seconds.
abdsha01 0:15664f71b21f 23 // detect_line () - used to detect a line, it returns the following
abdsha01 0:15664f71b21f 24 // an int value as follows:
abdsha01 0:15664f71b21f 25 // 0 - if no line is detected
abdsha01 0:15664f71b21f 26 // 1 - if line detected from the front
abdsha01 0:15664f71b21f 27 // -1 - if line detected from the back
abdsha01 3:7f7a82bf59b9 28 // reverse() - reverses the robot with chargespeed in same position
abdsha01 3:7f7a82bf59b9 29 // reverseandturn()- reverses whie moving in a circular direction
abdsha01 0:15664f71b21f 30
abdsha01 0:15664f71b21f 31 // Libraries for using the above functions and more ...
abdsha01 0:15664f71b21f 32 #include "mbed.h"
abdsha01 4:0507835a3dce 33 #include "Motor.h"
abdsha01 4:0507835a3dce 34 #include "hcsr04.h"
abdsha01 0:15664f71b21f 35 #include "functions.h"
abdsha01 0:15664f71b21f 36
abdsha01 0:15664f71b21f 37 // Set for debugging purpose
abdsha01 0:15664f71b21f 38 // Example: pc(TX, RX)
abdsha01 0:15664f71b21f 39 //Serial pc(USBTX, USBRX);
abdsha01 0:15664f71b21f 40
abdsha01 0:15664f71b21f 41
abdsha01 0:15664f71b21f 42 // Global parameters
abdsha01 1:bd88d4062c97 43 // Speed at which it charges an object
abdsha01 0:15664f71b21f 44 // optimum value: 0.4 to 0.8
abdsha01 0:15664f71b21f 45 float chargespeed;
abdsha01 0:15664f71b21f 46 // Speed at which it rotates to find an object
abdsha01 0:15664f71b21f 47 // optimum value: 0.3 to 0.5
abdsha01 0:15664f71b21f 48 float searchspeed;
abdsha01 0:15664f71b21f 49 // Range of detection
abdsha01 0:15664f71b21f 50 // optimum value: 30 to 50
abdsha01 0:15664f71b21f 51 int range;
abdsha01 0:15664f71b21f 52
abdsha01 0:15664f71b21f 53 void initialise()
abdsha01 0:15664f71b21f 54 {
abdsha01 0:15664f71b21f 55 chargespeed = 0.5;
abdsha01 0:15664f71b21f 56 searchspeed = 0.3;
abdsha01 0:15664f71b21f 57 range = 40;
abdsha01 0:15664f71b21f 58
abdsha01 0:15664f71b21f 59 // Wait for 5 seconds to move away from robot
abdsha01 0:15664f71b21f 60 wait(5);
abdsha01 0:15664f71b21f 61 }
abdsha01 0:15664f71b21f 62
abdsha01 0:15664f71b21f 63 // The main loop - please write your code here
abdsha01 0:15664f71b21f 64 int main()
abdsha01 0:15664f71b21f 65 {
abdsha01 0:15664f71b21f 66 // Initialise the code
abdsha01 0:15664f71b21f 67 initialise();
abdsha01 0:15664f71b21f 68
abdsha01 0:15664f71b21f 69 printf("Starting the robot...");
abdsha01 0:15664f71b21f 70
abdsha01 0:15664f71b21f 71 Timer t;
abdsha01 0:15664f71b21f 72 t.start();
abdsha01 0:15664f71b21f 73
abdsha01 2:6f15a798993d 74 while(1) {
abdsha01 4:0507835a3dce 75 // Sample code to detect and object and charge at it
abdsha01 4:0507835a3dce 76 if (detect_object(range, searchspeed)) {
abdsha01 4:0507835a3dce 77
abdsha01 4:0507835a3dce 78 charge(chargespeed);
abdsha01 4:0507835a3dce 79 wait(3);
abdsha01 4:0507835a3dce 80 stop();
abdsha01 4:0507835a3dce 81
abdsha01 4:0507835a3dce 82 } else {
abdsha01 4:0507835a3dce 83
abdsha01 4:0507835a3dce 84 }
abdsha01 0:15664f71b21f 85 }
abdsha01 0:15664f71b21f 86 }
abdsha01 0:15664f71b21f 87