Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Committer:
alex0612
Date:
Wed Oct 12 14:58:55 2016 +0000
Revision:
38:decff231d886
Parent:
32:775eec44ba4f
added breaks to cases

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lhartfield 32:775eec44ba4f 1 // Code written by:
lhartfield 32:775eec44ba4f 2 // Jon Baker
lhartfield 32:775eec44ba4f 3 // Alessandro Grande
lhartfield 32:775eec44ba4f 4 // Abdul-Rehman Sharif
lhartfield 32:775eec44ba4f 5 // Lucinda Hartfield
lhartfield 32:775eec44ba4f 6
lhartfield 32:775eec44ba4f 7 // Circuitry made by:
lhartfield 32:775eec44ba4f 8 // Yucando Navarrete
lhartfield 32:775eec44ba4f 9 // Vivekanand Gupta
lhartfield 32:775eec44ba4f 10
lhartfield 32:775eec44ba4f 11 // The following code will control a robotic car
lhartfield 32:775eec44ba4f 12 // by detetecting an object and charging towards it
lhartfield 32:775eec44ba4f 13 // it uses basic functions as:
lhartfield 32:775eec44ba4f 14 //
lhartfield 32:775eec44ba4f 15 // move_forward(speed)
lhartfield 32:775eec44ba4f 16 // - used to move_forward on an object detected
lhartfield 32:775eec44ba4f 17 // the robot will move in a straight line
lhartfield 32:775eec44ba4f 18 // until it detects the arena line where
lhartfield 32:775eec44ba4f 19 // it will use reverse() to move back
lhartfield 32:775eec44ba4f 20 //
lhartfield 32:775eec44ba4f 21 // detect_object(range, speed)
lhartfield 32:775eec44ba4f 22 // - used to detect an object, the robot will
lhartfield 32:775eec44ba4f 23 // move in a circle to find an object and
lhartfield 32:775eec44ba4f 24 // return 1 if it finds something and return
lhartfield 32:775eec44ba4f 25 // 0 if it does not find anything - the search
lhartfield 32:775eec44ba4f 26 // will be carried out for 15 seconds.
lhartfield 32:775eec44ba4f 27 //
lhartfield 32:775eec44ba4f 28 // detect_line ()
lhartfield 32:775eec44ba4f 29 // - used to detect a line, it returns the following
lhartfield 32:775eec44ba4f 30 // an int value as follows:
lhartfield 32:775eec44ba4f 31 // 0 - if no line is detected
lhartfield 32:775eec44ba4f 32 // 1 - if line detected from the front
lhartfield 32:775eec44ba4f 33 // -1 - if line detected from the back
lhartfield 32:775eec44ba4f 34 //
lhartfield 32:775eec44ba4f 35 // reverse(speed)
lhartfield 32:775eec44ba4f 36 // - reverses the robot with forwardspeed in same position
lhartfield 32:775eec44ba4f 37 //
lhartfield 32:775eec44ba4f 38 // stop()
lhartfield 32:775eec44ba4f 39 // - stops the robot
lhartfield 32:775eec44ba4f 40 //
lhartfield 32:775eec44ba4f 41 // move_random(speed)
lhartfield 32:775eec44ba4f 42 // - used to move the robot randomly, the robot will either
lhartfield 32:775eec44ba4f 43 // move forward, move backward, or turn around. The movement
lhartfield 32:775eec44ba4f 44 // will be carried out for a random time
lhartfield 32:775eec44ba4f 45 //
lhartfield 32:775eec44ba4f 46
lhartfield 32:775eec44ba4f 47 // Libraries for using the above functions and more ...
lhartfield 32:775eec44ba4f 48 #include "mbed.h"
lhartfield 32:775eec44ba4f 49 #include "Motor.h"
lhartfield 32:775eec44ba4f 50 #include "hcsr04.h"
lhartfield 32:775eec44ba4f 51 #include "functions.h"
lhartfield 32:775eec44ba4f 52
lhartfield 32:775eec44ba4f 53 // Set for debugging purpose
lhartfield 32:775eec44ba4f 54 // Example: pc(TX, RX)
lhartfield 32:775eec44ba4f 55 //Serial pc(USBTX, USBRX);
lhartfield 32:775eec44ba4f 56
lhartfield 32:775eec44ba4f 57 // Global parameters
lhartfield 32:775eec44ba4f 58
lhartfield 32:775eec44ba4f 59 float forwardspeed;
lhartfield 32:775eec44ba4f 60 float reversespeed;
lhartfield 32:775eec44ba4f 61 float searchspeed;
lhartfield 32:775eec44ba4f 62 unsigned int range;
lhartfield 32:775eec44ba4f 63
lhartfield 32:775eec44ba4f 64 void initialise()
lhartfield 32:775eec44ba4f 65 {
lhartfield 32:775eec44ba4f 66 // Each speed value can be set from 0 to 1
lhartfield 32:775eec44ba4f 67
lhartfield 32:775eec44ba4f 68 // Speed at which it moves forward
lhartfield 32:775eec44ba4f 69 // optimum value: 0.5 to 0.8
lhartfield 32:775eec44ba4f 70 forwardspeed = 0.6;
lhartfield 32:775eec44ba4f 71 // Speed at which it reverses
lhartfield 32:775eec44ba4f 72 // optimum value: 0.4 to 0.6
lhartfield 32:775eec44ba4f 73 reversespeed = 0.5;
lhartfield 32:775eec44ba4f 74 // Speed at which it rotates to find an object
lhartfield 32:775eec44ba4f 75 // optimum value: 0.4 to 0.6
lhartfield 32:775eec44ba4f 76 searchspeed = 0.5;
lhartfield 32:775eec44ba4f 77 // Range of detection
lhartfield 32:775eec44ba4f 78 // optimum value: 30 to 50
lhartfield 32:775eec44ba4f 79 range = 30;
lhartfield 32:775eec44ba4f 80
lhartfield 32:775eec44ba4f 81 // Wait for 5 seconds to move away from robot
lhartfield 32:775eec44ba4f 82 wait(5);
lhartfield 32:775eec44ba4f 83 }
lhartfield 32:775eec44ba4f 84
lhartfield 32:775eec44ba4f 85 // The main loop - please write your code here
lhartfield 32:775eec44ba4f 86 int main()
lhartfield 32:775eec44ba4f 87 {
lhartfield 32:775eec44ba4f 88 // Initialise the code
lhartfield 32:775eec44ba4f 89 initialise();
lhartfield 32:775eec44ba4f 90
lhartfield 32:775eec44ba4f 91 printf("Starting the robot...");
lhartfield 32:775eec44ba4f 92
lhartfield 32:775eec44ba4f 93 Timer t;
lhartfield 32:775eec44ba4f 94 t.start();
lhartfield 32:775eec44ba4f 95
lhartfield 32:775eec44ba4f 96 int detect_l = 0;
lhartfield 32:775eec44ba4f 97 int detect_o = 0;
lhartfield 32:775eec44ba4f 98
lhartfield 32:775eec44ba4f 99 while(true) {
lhartfield 32:775eec44ba4f 100 // Sample code to detect and object and move_forward at it
lhartfield 32:775eec44ba4f 101
lhartfield 32:775eec44ba4f 102 wait(1);
lhartfield 32:775eec44ba4f 103
lhartfield 32:775eec44ba4f 104 detect_o = detect_object(range, searchspeed);
lhartfield 32:775eec44ba4f 105
lhartfield 32:775eec44ba4f 106 if (detect_o == 1) {
lhartfield 32:775eec44ba4f 107
lhartfield 32:775eec44ba4f 108 move_forward(forwardspeed);
lhartfield 32:775eec44ba4f 109
lhartfield 32:775eec44ba4f 110 while (true) {
lhartfield 32:775eec44ba4f 111
lhartfield 32:775eec44ba4f 112 detect_l = detect_line();
lhartfield 32:775eec44ba4f 113 // If line is detected from front then reverse
lhartfield 32:775eec44ba4f 114 if(detect_l == 1 || detect_l == -1) {
lhartfield 32:775eec44ba4f 115 stop();
lhartfield 32:775eec44ba4f 116 turn_led_right();
lhartfield 32:775eec44ba4f 117 reverse(reversespeed);
lhartfield 32:775eec44ba4f 118 wait(1.5);
lhartfield 32:775eec44ba4f 119 detect_l = 0;
lhartfield 32:775eec44ba4f 120 break;
lhartfield 32:775eec44ba4f 121 }
lhartfield 32:775eec44ba4f 122 }
lhartfield 32:775eec44ba4f 123
lhartfield 32:775eec44ba4f 124 } else {
lhartfield 32:775eec44ba4f 125
lhartfield 32:775eec44ba4f 126 move_random();
lhartfield 32:775eec44ba4f 127
lhartfield 32:775eec44ba4f 128 }
lhartfield 32:775eec44ba4f 129
lhartfield 32:775eec44ba4f 130 detect_o = 0;
lhartfield 32:775eec44ba4f 131 stop();
lhartfield 32:775eec44ba4f 132 }
lhartfield 32:775eec44ba4f 133 }
lhartfield 32:775eec44ba4f 134