Robot code for searching an object and charging at it.
Dependencies: HCSR04 Motor mbed
main.cpp
00001 // Code written by: 00002 // Jon Baker 00003 // Alessandro Grande 00004 // Abdul-Rehman Sharif 00005 // Lucinda Hartfield 00006 00007 // Circuitry made by: 00008 // Jon Baker 00009 // Yucando Navarrete 00010 // Vivekanand Gupta 00011 00012 // The following code controls a robotic car 00013 // by detetecting an object and charging towards it. 00014 // It uses the following basic functions: 00015 // 00016 // forwards(speed) 00017 // - Sets the motor speeds in order to go forwards. 00018 // 00019 // reverse(speed) 00020 // - Sets the motor speeds in order to go backwards. 00021 // 00022 // turn_left(speed) 00023 // - Sets the motor speeds to turn the robot to the left. 00024 // 00025 // turn_right(speed) 00026 // - Sets the motor speeds to turn the robot to the right. 00027 // 00028 // stop() 00029 // - Sets the motor speeds to 0. 00030 // 00031 // turn_led_on(LED) 00032 // - Turns on the LED on the mbed specified by the 00033 // number passed as the argument 00034 // Values need to be between 1 and 4 00035 // 00036 // detect_object(range, speed) 00037 // - Used to detect an object, the robot will 00038 // move in a circle to find an object. This function 00039 // returns 1 if an object is detected, otherwise it will 00040 // return 0. The search will be carried out for 15 seconds. 00041 // 00042 // detect_line () 00043 // - Used to detect a line, it returns the following values: 00044 // 0 - if no line is detected 00045 // 1 - if line detected from the front 00046 // -1 - if line detected from the back 00047 // 00048 // move_random(speed) 00049 // - Used to move the robot randomly: the robot will either 00050 // move forward, move backward, or turn around. The movement 00051 // will be carried out for a random time. 00052 // 00053 00054 // Libraries for using the above functions and more ... 00055 #include "mbed.h" 00056 #include "Motor.h" 00057 #include "hcsr04.h" 00058 #include "functions.h" 00059 00060 // Set for debugging purpose 00061 // Example: pc(TX, RX) 00062 //Serial pc(USBTX, USBRX); 00063 00064 // Global parameters 00065 00066 float forwardspeed; 00067 float reversespeed; 00068 float searchspeed; 00069 unsigned int range; 00070 00071 void initialise() 00072 { 00073 // Each speed value can be set from 0 to 1 00074 00075 // Speed at which it moves forward 00076 // optimum value: 0.5 to 0.8 00077 forwardspeed = 0.6; 00078 // Speed at which it reverses 00079 // optimum value: 0.4 to 0.6 00080 reversespeed = 0.5; 00081 // Speed at which it rotates to find an object 00082 // optimum value: 0.4 to 0.6 00083 searchspeed = 0.5; 00084 // Range of detection 00085 // optimum value: 30 to 50 00086 range = 30; 00087 00088 // Wait for 5 seconds to move away from robot 00089 wait(5); 00090 } 00091 00092 // The main loop - please write your code here 00093 int main() 00094 { 00095 // Initialise the code 00096 initialise(); 00097 00098 printf("Starting the robot..."); 00099 00100 Timer t; 00101 t.start(); 00102 00103 int detect_l = 0; 00104 int detect_o = 0; 00105 00106 while(true) { 00107 // Sample code to detect and object and move_forward at it 00108 00109 wait(1); 00110 00111 detect_o = detect_object(range, searchspeed); 00112 00113 if (detect_o == 1) { 00114 00115 forwards(forwardspeed); 00116 00117 while (true) { 00118 00119 detect_l = detect_line(); 00120 // If line is detected from front then reverse 00121 if(detect_l == 1 || detect_l == -1) { 00122 stop(); 00123 turn_led_right(); 00124 reverse(reversespeed); 00125 wait(1); 00126 detect_l = 0; 00127 break; 00128 } 00129 } 00130 00131 } else { 00132 00133 move_random(); 00134 00135 } 00136 00137 detect_o = 0; 00138 stop(); 00139 } 00140 } 00141
Generated on Tue Jul 12 2022 21:41:27 by 1.7.2