Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

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