Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Committer:
lhartfield
Date:
Sun Jun 07 15:05:00 2015 +0000
Revision:
23:07b3c12800a6
Parent:
21:42c0db071a7f
Parent:
22:e808fb71847d
Child:
24:513c88816ed8
merged

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 //
alex0612 19:67ea4e8be9e1 15 // move_forward(speed)
alex0612 19:67ea4e8be9e1 16 // - used to move_forward 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
alex0612 18:7bd638e3926d 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 21:42c0db071a7f 26 // will be carried out for 15 seconds.
abdsha01 5:68a2b79cf204 27 //
alex0612 18:7bd638e3926d 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 //
alex0612 18:7bd638e3926d 35 // reverse(speed)
alex0612 19:67ea4e8be9e1 36 // - reverses the robot with move_forwardspeed in same position
abdsha01 20:37a89edd1cde 37 //
alex0612 18:7bd638e3926d 38 // reverseandturn(speed)
abdsha01 5:68a2b79cf204 39 // - reverses whie moving in a circular direction
abdsha01 5:68a2b79cf204 40 //
abdsha01 20:37a89edd1cde 41 // stop()
abdsha01 20:37a89edd1cde 42 // - stops the robot
abdsha01 20:37a89edd1cde 43 //
abdsha01 21:42c0db071a7f 44 // moverandom(speed)
abdsha01 21:42c0db071a7f 45 // - used to move the robot randomly, the robot will either
abdsha01 21:42c0db071a7f 46 // move forward, move backward, or turn around. The movement
abdsha01 21:42c0db071a7f 47 // will be carried out for a random time
abdsha01 21:42c0db071a7f 48 //
abdsha01 0:15664f71b21f 49
abdsha01 0:15664f71b21f 50 // Libraries for using the above functions and more ...
abdsha01 0:15664f71b21f 51 #include "mbed.h"
abdsha01 4:0507835a3dce 52 #include "Motor.h"
abdsha01 4:0507835a3dce 53 #include "hcsr04.h"
abdsha01 0:15664f71b21f 54 #include "functions.h"
abdsha01 0:15664f71b21f 55
abdsha01 0:15664f71b21f 56 // Set for debugging purpose
abdsha01 0:15664f71b21f 57 // Example: pc(TX, RX)
abdsha01 0:15664f71b21f 58 //Serial pc(USBTX, USBRX);
abdsha01 0:15664f71b21f 59
abdsha01 0:15664f71b21f 60 // Global parameters
alex0612 19:67ea4e8be9e1 61 // Speed at which it move_forwards an object
abdsha01 0:15664f71b21f 62 // optimum value: 0.4 to 0.8
alex0612 19:67ea4e8be9e1 63 float move_forwardspeed;
abdsha01 0:15664f71b21f 64 // Speed at which it rotates to find an object
abdsha01 0:15664f71b21f 65 // optimum value: 0.3 to 0.5
abdsha01 0:15664f71b21f 66 float searchspeed;
abdsha01 0:15664f71b21f 67 // Range of detection
abdsha01 0:15664f71b21f 68 // optimum value: 30 to 50
abdsha01 6:af897173cb75 69 unsigned int range;
abdsha01 0:15664f71b21f 70
alex0612 18:7bd638e3926d 71 void initialise()
alex0612 18:7bd638e3926d 72 {
alex0612 19:67ea4e8be9e1 73 move_forwardspeed = 0.6;
alex0612 18:7bd638e3926d 74 searchspeed = 0.5;
abdsha01 6:af897173cb75 75 range = 30;
alex0612 18:7bd638e3926d 76
abdsha01 0:15664f71b21f 77 // Wait for 5 seconds to move away from robot
abdsha01 0:15664f71b21f 78 wait(5);
abdsha01 0:15664f71b21f 79 }
abdsha01 0:15664f71b21f 80
abdsha01 0:15664f71b21f 81 // The main loop - please write your code here
alex0612 18:7bd638e3926d 82 int main()
alex0612 18:7bd638e3926d 83 {
abdsha01 0:15664f71b21f 84 // Initialise the code
abdsha01 0:15664f71b21f 85 initialise();
abdsha01 0:15664f71b21f 86
abdsha01 0:15664f71b21f 87 printf("Starting the robot...");
abdsha01 0:15664f71b21f 88
abdsha01 0:15664f71b21f 89 Timer t;
abdsha01 0:15664f71b21f 90 t.start();
alex0612 18:7bd638e3926d 91
alex0612 16:02e533e3a91c 92 int detect_l = 0;
alex0612 16:02e533e3a91c 93 int detect_o = 0;
abdsha01 0:15664f71b21f 94
alex0612 19:67ea4e8be9e1 95 while(true) {
alex0612 19:67ea4e8be9e1 96 // Sample code to detect and object and move_forward at it
alex0612 18:7bd638e3926d 97
alex0612 18:7bd638e3926d 98 detect_o = detect_object(range, searchspeed);
alex0612 18:7bd638e3926d 99
alex0612 16:02e533e3a91c 100 if (detect_o == 1) {
alex0612 18:7bd638e3926d 101
alex0612 19:67ea4e8be9e1 102 move_forward(move_forwardspeed);
abdsha01 10:cec68ef272cd 103
alex0612 16:02e533e3a91c 104 while (true) {
alex0612 18:7bd638e3926d 105
alex0612 16:02e533e3a91c 106 detect_l = detect_line();
abdsha01 10:cec68ef272cd 107 // If line is detected from front then reverse
alex0612 16:02e533e3a91c 108 if(detect_l == 1) {
alex0612 19:67ea4e8be9e1 109 stop();
alex0612 19:67ea4e8be9e1 110 turn_leds_on();
abdsha01 10:cec68ef272cd 111 reverse(searchspeed);
alex0612 19:67ea4e8be9e1 112 wait(1.5);
alex0612 18:7bd638e3926d 113 detect_l = 0;
abdsha01 10:cec68ef272cd 114 break;
alex0612 19:67ea4e8be9e1 115 // If line is detected from back just keep on moving forward
alex0612 16:02e533e3a91c 116 } else if (detect_l == -1) {
abdsha01 10:cec68ef272cd 117 stop();
alex0612 19:67ea4e8be9e1 118 turn_leds_on();
alex0612 19:67ea4e8be9e1 119 move_forward(searchspeed);
alex0612 19:67ea4e8be9e1 120 wait(1.5);
alex0612 18:7bd638e3926d 121 detect_l = 0;
abdsha01 10:cec68ef272cd 122 break;
abdsha01 10:cec68ef272cd 123 }
abdsha01 9:7770a84228c0 124 }
alex0612 18:7bd638e3926d 125
abdsha01 10:cec68ef272cd 126 } else {
alex0612 18:7bd638e3926d 127
lhartfield 22:e808fb71847d 128 move_random();
alex0612 18:7bd638e3926d 129
abdsha01 9:7770a84228c0 130 }
alex0612 18:7bd638e3926d 131
alex0612 16:02e533e3a91c 132 detect_o = 0;
abdsha01 6:af897173cb75 133 stop();
abdsha01 0:15664f71b21f 134 }
abdsha01 0:15664f71b21f 135 }
abdsha01 0:15664f71b21f 136