Robot code for searching an object and charging at it.
Dependencies: HCSR04 Motor mbed
main.cpp@38:decff231d886, 2016-10-12 (annotated)
- Committer:
- alex0612
- Date:
- Wed Oct 12 14:58:55 2016 +0000
- Revision:
- 38:decff231d886
- Parent:
- 37:a08d2e37b7e6
added breaks to cases
Who changed what in which revision?
User | Revision | Line number | New 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 | 37:a08d2e37b7e6 | 16 | // forwards(speed) |
alex0612 | 37:a08d2e37b7e6 | 17 | // - Sets the motor speeds in order to go forwards. |
alex0612 | 37:a08d2e37b7e6 | 18 | // |
alex0612 | 37:a08d2e37b7e6 | 19 | // reverse(speed) |
alex0612 | 37:a08d2e37b7e6 | 20 | // - Sets the motor speeds in order to go backwards. |
alex0612 | 37:a08d2e37b7e6 | 21 | // |
alex0612 | 37:a08d2e37b7e6 | 22 | // turn_left(speed) |
alex0612 | 37:a08d2e37b7e6 | 23 | // - Sets the motor speeds to turn the robot to the left. |
alex0612 | 37:a08d2e37b7e6 | 24 | // |
alex0612 | 37:a08d2e37b7e6 | 25 | // turn_right(speed) |
alex0612 | 37:a08d2e37b7e6 | 26 | // - Sets the motor speeds to turn the robot to the right. |
alex0612 | 37:a08d2e37b7e6 | 27 | // |
alex0612 | 37:a08d2e37b7e6 | 28 | // stop() |
alex0612 | 37:a08d2e37b7e6 | 29 | // - Sets the motor speeds to 0. |
alex0612 | 37:a08d2e37b7e6 | 30 | // |
alex0612 | 37:a08d2e37b7e6 | 31 | // turn_led_on(LED) |
alex0612 | 37:a08d2e37b7e6 | 32 | // - Turns on the LED on the mbed specified by the |
alex0612 | 37:a08d2e37b7e6 | 33 | // number passed as the argument |
alex0612 | 37:a08d2e37b7e6 | 34 | // Values need to be between 1 and 4 |
abdsha01 | 5:68a2b79cf204 | 35 | // |
abdsha01 | 5:68a2b79cf204 | 36 | // detect_object(range, speed) |
lhartfield | 31:70753d91565c | 37 | // - Used to detect an object, the robot will |
lhartfield | 31:70753d91565c | 38 | // move in a circle to find an object. This function |
lhartfield | 31:70753d91565c | 39 | // returns 1 if an object is detected, otherwise it will |
lhartfield | 31:70753d91565c | 40 | // return 0. The search will be carried out for 15 seconds. |
abdsha01 | 5:68a2b79cf204 | 41 | // |
alex0612 | 18:7bd638e3926d | 42 | // detect_line () |
lhartfield | 31:70753d91565c | 43 | // - Used to detect a line, it returns the following values: |
lhartfield | 31:70753d91565c | 44 | // 0 - if no line is detected |
lhartfield | 31:70753d91565c | 45 | // 1 - if line detected from the front |
lhartfield | 31:70753d91565c | 46 | // -1 - if line detected from the back |
abdsha01 | 5:68a2b79cf204 | 47 | // |
lhartfield | 24:513c88816ed8 | 48 | // move_random(speed) |
lhartfield | 31:70753d91565c | 49 | // - Used to move the robot randomly: the robot will either |
abdsha01 | 21:42c0db071a7f | 50 | // move forward, move backward, or turn around. The movement |
lhartfield | 31:70753d91565c | 51 | // will be carried out for a random time. |
abdsha01 | 21:42c0db071a7f | 52 | // |
abdsha01 | 0:15664f71b21f | 53 | |
abdsha01 | 0:15664f71b21f | 54 | // Libraries for using the above functions and more ... |
abdsha01 | 0:15664f71b21f | 55 | #include "mbed.h" |
abdsha01 | 4:0507835a3dce | 56 | #include "Motor.h" |
abdsha01 | 4:0507835a3dce | 57 | #include "hcsr04.h" |
abdsha01 | 0:15664f71b21f | 58 | #include "functions.h" |
abdsha01 | 0:15664f71b21f | 59 | |
abdsha01 | 0:15664f71b21f | 60 | // Set for debugging purpose |
abdsha01 | 0:15664f71b21f | 61 | // Example: pc(TX, RX) |
abdsha01 | 0:15664f71b21f | 62 | //Serial pc(USBTX, USBRX); |
abdsha01 | 0:15664f71b21f | 63 | |
abdsha01 | 0:15664f71b21f | 64 | // Global parameters |
alex0612 | 30:8a5570b2de68 | 65 | |
alex0612 | 30:8a5570b2de68 | 66 | float forwardspeed; |
alex0612 | 30:8a5570b2de68 | 67 | float reversespeed; |
abdsha01 | 0:15664f71b21f | 68 | float searchspeed; |
abdsha01 | 6:af897173cb75 | 69 | unsigned int range; |
abdsha01 | 0:15664f71b21f | 70 | |
alex0612 | 18:7bd638e3926d | 71 | void initialise() |
alex0612 | 18:7bd638e3926d | 72 | { |
alex0612 | 30:8a5570b2de68 | 73 | // Each speed value can be set from 0 to 1 |
alex0612 | 30:8a5570b2de68 | 74 | |
alex0612 | 30:8a5570b2de68 | 75 | // Speed at which it moves forward |
alex0612 | 30:8a5570b2de68 | 76 | // optimum value: 0.5 to 0.8 |
alex0612 | 30:8a5570b2de68 | 77 | forwardspeed = 0.6; |
alex0612 | 30:8a5570b2de68 | 78 | // Speed at which it reverses |
alex0612 | 30:8a5570b2de68 | 79 | // optimum value: 0.4 to 0.6 |
alex0612 | 30:8a5570b2de68 | 80 | reversespeed = 0.5; |
alex0612 | 30:8a5570b2de68 | 81 | // Speed at which it rotates to find an object |
alex0612 | 30:8a5570b2de68 | 82 | // optimum value: 0.4 to 0.6 |
alex0612 | 18:7bd638e3926d | 83 | searchspeed = 0.5; |
alex0612 | 30:8a5570b2de68 | 84 | // Range of detection |
alex0612 | 30:8a5570b2de68 | 85 | // optimum value: 30 to 50 |
abdsha01 | 6:af897173cb75 | 86 | range = 30; |
alex0612 | 18:7bd638e3926d | 87 | |
abdsha01 | 0:15664f71b21f | 88 | // Wait for 5 seconds to move away from robot |
abdsha01 | 0:15664f71b21f | 89 | wait(5); |
abdsha01 | 0:15664f71b21f | 90 | } |
abdsha01 | 0:15664f71b21f | 91 | |
abdsha01 | 0:15664f71b21f | 92 | // The main loop - please write your code here |
alex0612 | 18:7bd638e3926d | 93 | int main() |
alex0612 | 18:7bd638e3926d | 94 | { |
abdsha01 | 0:15664f71b21f | 95 | // Initialise the code |
abdsha01 | 0:15664f71b21f | 96 | initialise(); |
abdsha01 | 0:15664f71b21f | 97 | |
abdsha01 | 0:15664f71b21f | 98 | printf("Starting the robot..."); |
abdsha01 | 0:15664f71b21f | 99 | |
abdsha01 | 0:15664f71b21f | 100 | Timer t; |
abdsha01 | 0:15664f71b21f | 101 | t.start(); |
alex0612 | 18:7bd638e3926d | 102 | |
alex0612 | 16:02e533e3a91c | 103 | int detect_l = 0; |
alex0612 | 16:02e533e3a91c | 104 | int detect_o = 0; |
abdsha01 | 0:15664f71b21f | 105 | |
alex0612 | 19:67ea4e8be9e1 | 106 | while(true) { |
alex0612 | 19:67ea4e8be9e1 | 107 | // Sample code to detect and object and move_forward at it |
lhartfield | 27:96a0ff2f474c | 108 | |
abdsha01 | 26:5ee2a32949e6 | 109 | wait(1); |
abdsha01 | 26:5ee2a32949e6 | 110 | |
alex0612 | 18:7bd638e3926d | 111 | detect_o = detect_object(range, searchspeed); |
alex0612 | 18:7bd638e3926d | 112 | |
alex0612 | 16:02e533e3a91c | 113 | if (detect_o == 1) { |
alex0612 | 18:7bd638e3926d | 114 | |
alex0612 | 37:a08d2e37b7e6 | 115 | forwards(forwardspeed); |
abdsha01 | 10:cec68ef272cd | 116 | |
alex0612 | 16:02e533e3a91c | 117 | while (true) { |
alex0612 | 18:7bd638e3926d | 118 | |
alex0612 | 16:02e533e3a91c | 119 | detect_l = detect_line(); |
abdsha01 | 10:cec68ef272cd | 120 | // If line is detected from front then reverse |
abdsha01 | 28:71781431b010 | 121 | if(detect_l == 1 || detect_l == -1) { |
alex0612 | 19:67ea4e8be9e1 | 122 | stop(); |
abdsha01 | 26:5ee2a32949e6 | 123 | turn_led_right(); |
alex0612 | 30:8a5570b2de68 | 124 | reverse(reversespeed); |
alex0612 | 35:89e13f72cd84 | 125 | wait(1); |
alex0612 | 18:7bd638e3926d | 126 | detect_l = 0; |
abdsha01 | 10:cec68ef272cd | 127 | break; |
abdsha01 | 10:cec68ef272cd | 128 | } |
abdsha01 | 9:7770a84228c0 | 129 | } |
alex0612 | 18:7bd638e3926d | 130 | |
abdsha01 | 10:cec68ef272cd | 131 | } else { |
alex0612 | 18:7bd638e3926d | 132 | |
lhartfield | 22:e808fb71847d | 133 | move_random(); |
alex0612 | 18:7bd638e3926d | 134 | |
abdsha01 | 9:7770a84228c0 | 135 | } |
alex0612 | 18:7bd638e3926d | 136 | |
alex0612 | 16:02e533e3a91c | 137 | detect_o = 0; |
abdsha01 | 6:af897173cb75 | 138 | stop(); |
lhartfield | 27:96a0ff2f474c | 139 | } |
abdsha01 | 0:15664f71b21f | 140 | } |
abdsha01 | 0:15664f71b21f | 141 |