Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Committer:
abdsha01
Date:
Sat May 23 17:48:01 2015 +0000
Revision:
10:cec68ef272cd
Parent:
8:43a650f9d4af
Parent:
9:7770a84228c0
Child:
12:703f75ae2453
Updated stuff

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 5:68a2b79cf204 14 //
abdsha01 5:68a2b79cf204 15 // charge(speed)
abdsha01 5:68a2b79cf204 16 // - used to charge on an object detected
abdsha01 5:68a2b79cf204 17 // the robot will move in a straight line
abdsha01 5:68a2b79cf204 18 // until it detects the arena line where
abdsha01 5:68a2b79cf204 19 // it will use reverse() to move back
abdsha01 5:68a2b79cf204 20 //
abdsha01 5:68a2b79cf204 21 // detect_object(range, speed)
abdsha01 5:68a2b79cf204 22 // - used to detect an object, the robot will
abdsha01 5:68a2b79cf204 23 // move in a circle to find an object and
abdsha01 5:68a2b79cf204 24 // return 1 if it finds something and return
abdsha01 5:68a2b79cf204 25 // 0 if it does not find anything - the search
abdsha01 5:68a2b79cf204 26 // will be carried out for 10 seconds.
abdsha01 5:68a2b79cf204 27 //
abdsha01 5:68a2b79cf204 28 // detect_line ()
abdsha01 5:68a2b79cf204 29 // - used to detect a line, it returns the following
abdsha01 5:68a2b79cf204 30 // an int value as follows:
abdsha01 5:68a2b79cf204 31 // 0 - if no line is detected
abdsha01 5:68a2b79cf204 32 // 1 - if line detected from the front
abdsha01 5:68a2b79cf204 33 // -1 - if line detected from the back
abdsha01 5:68a2b79cf204 34 //
abdsha01 5:68a2b79cf204 35 // reverse(speed)
abdsha01 5:68a2b79cf204 36 // - reverses the robot with chargespeed in same position
abdsha01 5:68a2b79cf204 37
abdsha01 5:68a2b79cf204 38 // reverseandturn(speed)
abdsha01 5:68a2b79cf204 39 // - reverses whie moving in a circular direction
abdsha01 5:68a2b79cf204 40 //
abdsha01 0:15664f71b21f 41
abdsha01 0:15664f71b21f 42 // Libraries for using the above functions and more ...
abdsha01 0:15664f71b21f 43 #include "mbed.h"
abdsha01 4:0507835a3dce 44 #include "Motor.h"
abdsha01 4:0507835a3dce 45 #include "hcsr04.h"
abdsha01 0:15664f71b21f 46 #include "functions.h"
abdsha01 0:15664f71b21f 47
abdsha01 0:15664f71b21f 48 // Set for debugging purpose
abdsha01 0:15664f71b21f 49 // Example: pc(TX, RX)
abdsha01 0:15664f71b21f 50 //Serial pc(USBTX, USBRX);
abdsha01 0:15664f71b21f 51
abdsha01 0:15664f71b21f 52
abdsha01 0:15664f71b21f 53 // Global parameters
abdsha01 1:bd88d4062c97 54 // Speed at which it charges an object
abdsha01 0:15664f71b21f 55 // optimum value: 0.4 to 0.8
abdsha01 0:15664f71b21f 56 float chargespeed;
abdsha01 0:15664f71b21f 57 // Speed at which it rotates to find an object
abdsha01 0:15664f71b21f 58 // optimum value: 0.3 to 0.5
abdsha01 0:15664f71b21f 59 float searchspeed;
abdsha01 0:15664f71b21f 60 // Range of detection
abdsha01 0:15664f71b21f 61 // optimum value: 30 to 50
abdsha01 6:af897173cb75 62 unsigned int range;
abdsha01 0:15664f71b21f 63
abdsha01 6:af897173cb75 64 void initialise() {
abdsha01 6:af897173cb75 65 chargespeed = 0.6;
abdsha01 6:af897173cb75 66 searchspeed = 0.5;
abdsha01 6:af897173cb75 67 range = 30;
abdsha01 0:15664f71b21f 68
abdsha01 0:15664f71b21f 69 // Wait for 5 seconds to move away from robot
abdsha01 0:15664f71b21f 70 wait(5);
abdsha01 0:15664f71b21f 71 }
abdsha01 0:15664f71b21f 72
abdsha01 0:15664f71b21f 73 // The main loop - please write your code here
abdsha01 6:af897173cb75 74 int main() {
abdsha01 0:15664f71b21f 75 // Initialise the code
abdsha01 0:15664f71b21f 76 initialise();
abdsha01 0:15664f71b21f 77
abdsha01 0:15664f71b21f 78 printf("Starting the robot...");
abdsha01 0:15664f71b21f 79
abdsha01 0:15664f71b21f 80 Timer t;
abdsha01 0:15664f71b21f 81 t.start();
abdsha01 9:7770a84228c0 82
abdsha01 9:7770a84228c0 83 int detect = 0;
abdsha01 0:15664f71b21f 84
alex0612 8:43a650f9d4af 85 while(1) {
abdsha01 10:cec68ef272cd 86 // Sample code to detect and object and charge at it
abdsha01 10:cec68ef272cd 87 if (detect_object(range, searchspeed)) {
abdsha01 10:cec68ef272cd 88 while (true) {
abdsha01 10:cec68ef272cd 89 charge(chargespeed);
abdsha01 10:cec68ef272cd 90
abdsha01 10:cec68ef272cd 91 detect = detect_line();
abdsha01 10:cec68ef272cd 92 // If line is detected from front then reverse
abdsha01 10:cec68ef272cd 93 if(detect == 1) {
abdsha01 10:cec68ef272cd 94 reverse(searchspeed);
abdsha01 10:cec68ef272cd 95 wait(2);
abdsha01 10:cec68ef272cd 96 stop();
abdsha01 10:cec68ef272cd 97 break;
abdsha01 10:cec68ef272cd 98 // If line is detected from back just keep on moving forward
abdsha01 10:cec68ef272cd 99 } else if (detect == -1) {
abdsha01 10:cec68ef272cd 100 wait(2);
abdsha01 10:cec68ef272cd 101 stop();
abdsha01 10:cec68ef272cd 102 break;
abdsha01 10:cec68ef272cd 103 }
abdsha01 9:7770a84228c0 104 }
abdsha01 10:cec68ef272cd 105 } else {
abdsha01 10:cec68ef272cd 106 moverandom();
abdsha01 9:7770a84228c0 107 }
abdsha01 6:af897173cb75 108 stop();
abdsha01 0:15664f71b21f 109 }
abdsha01 0:15664f71b21f 110 }
abdsha01 0:15664f71b21f 111