Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Committer:
lhartfield
Date:
Sun Jun 07 18:42:03 2015 +0000
Revision:
31:70753d91565c
Parent:
29:977f47331d85
Child:
32:775eec44ba4f
Modified comments

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
lhartfield 31:70753d91565c 11 // The following code controls a robotic car
lhartfield 31:70753d91565c 12 // by detetecting an object and charging towards it.
lhartfield 31:70753d91565c 13 // It uses the following basic functions:
abdsha01 5:68a2b79cf204 14 //
alex0612 19:67ea4e8be9e1 15 // move_forward(speed)
lhartfield 31:70753d91565c 16 // - Used to move the robot toward a detected object.
lhartfield 31:70753d91565c 17 // The robot will move forwards 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)
lhartfield 31:70753d91565c 22 // - Used to detect an object, the robot will
lhartfield 31:70753d91565c 23 // move in a circle to find an object. This function
lhartfield 31:70753d91565c 24 // returns 1 if an object is detected, otherwise it will
lhartfield 31:70753d91565c 25 // return 0. The search will be carried out for 15 seconds.
abdsha01 5:68a2b79cf204 26 //
alex0612 18:7bd638e3926d 27 // detect_line ()
lhartfield 31:70753d91565c 28 // - Used to detect a line, it returns the following values:
lhartfield 31:70753d91565c 29 // 0 - if no line is detected
lhartfield 31:70753d91565c 30 // 1 - if line detected from the front
lhartfield 31:70753d91565c 31 // -1 - if line detected from the back
abdsha01 5:68a2b79cf204 32 //
alex0612 18:7bd638e3926d 33 // reverse(speed)
lhartfield 31:70753d91565c 34 // - Reverses the robot in a straight line at given speed.
abdsha01 5:68a2b79cf204 35 //
abdsha01 20:37a89edd1cde 36 // stop()
lhartfield 31:70753d91565c 37 // - Stops the robot.
abdsha01 20:37a89edd1cde 38 //
lhartfield 24:513c88816ed8 39 // move_random(speed)
lhartfield 31:70753d91565c 40 // - Used to move the robot randomly: the robot will either
abdsha01 21:42c0db071a7f 41 // move forward, move backward, or turn around. The movement
lhartfield 31:70753d91565c 42 // will be carried out for a random time.
abdsha01 21:42c0db071a7f 43 //
abdsha01 0:15664f71b21f 44
abdsha01 0:15664f71b21f 45 // Libraries for using the above functions and more ...
abdsha01 0:15664f71b21f 46 #include "mbed.h"
abdsha01 4:0507835a3dce 47 #include "Motor.h"
abdsha01 4:0507835a3dce 48 #include "hcsr04.h"
abdsha01 0:15664f71b21f 49 #include "functions.h"
abdsha01 0:15664f71b21f 50
abdsha01 0:15664f71b21f 51 // Set for debugging purpose
abdsha01 0:15664f71b21f 52 // Example: pc(TX, RX)
abdsha01 0:15664f71b21f 53 //Serial pc(USBTX, USBRX);
abdsha01 0:15664f71b21f 54
abdsha01 0:15664f71b21f 55 // Global parameters
alex0612 19:67ea4e8be9e1 56 // Speed at which it move_forwards an object
abdsha01 0:15664f71b21f 57 // optimum value: 0.4 to 0.8
lhartfield 25:7662e93a77eb 58 float moveforwardspeed;
abdsha01 0:15664f71b21f 59 // Speed at which it rotates to find an object
abdsha01 0:15664f71b21f 60 // optimum value: 0.3 to 0.5
abdsha01 0:15664f71b21f 61 float searchspeed;
abdsha01 0:15664f71b21f 62 // Range of detection
abdsha01 0:15664f71b21f 63 // optimum value: 30 to 50
abdsha01 6:af897173cb75 64 unsigned int range;
abdsha01 0:15664f71b21f 65
alex0612 18:7bd638e3926d 66 void initialise()
alex0612 18:7bd638e3926d 67 {
lhartfield 25:7662e93a77eb 68 moveforwardspeed = 0.6;
alex0612 18:7bd638e3926d 69 searchspeed = 0.5;
abdsha01 6:af897173cb75 70 range = 30;
alex0612 18:7bd638e3926d 71
abdsha01 0:15664f71b21f 72 // Wait for 5 seconds to move away from robot
abdsha01 0:15664f71b21f 73 wait(5);
abdsha01 0:15664f71b21f 74 }
abdsha01 0:15664f71b21f 75
abdsha01 0:15664f71b21f 76 // The main loop - please write your code here
alex0612 18:7bd638e3926d 77 int main()
alex0612 18:7bd638e3926d 78 {
abdsha01 0:15664f71b21f 79 // Initialise the code
abdsha01 0:15664f71b21f 80 initialise();
abdsha01 0:15664f71b21f 81
abdsha01 0:15664f71b21f 82 printf("Starting the robot...");
abdsha01 0:15664f71b21f 83
abdsha01 0:15664f71b21f 84 Timer t;
abdsha01 0:15664f71b21f 85 t.start();
alex0612 18:7bd638e3926d 86
alex0612 16:02e533e3a91c 87 int detect_l = 0;
alex0612 16:02e533e3a91c 88 int detect_o = 0;
abdsha01 0:15664f71b21f 89
alex0612 19:67ea4e8be9e1 90 while(true) {
alex0612 19:67ea4e8be9e1 91 // Sample code to detect and object and move_forward at it
lhartfield 27:96a0ff2f474c 92
abdsha01 26:5ee2a32949e6 93 wait(1);
abdsha01 26:5ee2a32949e6 94
alex0612 18:7bd638e3926d 95 detect_o = detect_object(range, searchspeed);
alex0612 18:7bd638e3926d 96
alex0612 16:02e533e3a91c 97 if (detect_o == 1) {
alex0612 18:7bd638e3926d 98
lhartfield 25:7662e93a77eb 99 move_forward(moveforwardspeed);
abdsha01 10:cec68ef272cd 100
alex0612 16:02e533e3a91c 101 while (true) {
alex0612 18:7bd638e3926d 102
alex0612 16:02e533e3a91c 103 detect_l = detect_line();
abdsha01 10:cec68ef272cd 104 // If line is detected from front then reverse
abdsha01 28:71781431b010 105 if(detect_l == 1 || detect_l == -1) {
alex0612 19:67ea4e8be9e1 106 stop();
abdsha01 26:5ee2a32949e6 107 turn_led_right();
abdsha01 10:cec68ef272cd 108 reverse(searchspeed);
alex0612 19:67ea4e8be9e1 109 wait(1.5);
alex0612 18:7bd638e3926d 110 detect_l = 0;
abdsha01 10:cec68ef272cd 111 break;
abdsha01 10:cec68ef272cd 112 }
abdsha01 9:7770a84228c0 113 }
alex0612 18:7bd638e3926d 114
abdsha01 10:cec68ef272cd 115 } else {
alex0612 18:7bd638e3926d 116
lhartfield 22:e808fb71847d 117 move_random();
alex0612 18:7bd638e3926d 118
abdsha01 9:7770a84228c0 119 }
alex0612 18:7bd638e3926d 120
alex0612 16:02e533e3a91c 121 detect_o = 0;
abdsha01 6:af897173cb75 122 stop();
lhartfield 27:96a0ff2f474c 123 }
abdsha01 0:15664f71b21f 124 }
abdsha01 0:15664f71b21f 125