Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Committer:
alex0612
Date:
Sun Jun 07 18:35:04 2015 +0000
Revision:
30:8a5570b2de68
Parent:
29:977f47331d85
Child:
32:775eec44ba4f
Minor changes

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