Robot code for searching an object and charging at it.
Dependencies: HCSR04 Motor mbed
Diff: main.cpp
- Revision:
- 18:7bd638e3926d
- Parent:
- 16:02e533e3a91c
- Child:
- 19:67ea4e8be9e1
--- a/main.cpp Sat May 30 16:14:43 2015 +0000 +++ b/main.cpp Sat May 30 16:18:31 2015 +0000 @@ -12,30 +12,30 @@ // by detetecting an object and charging towards it // it uses basic functions as: // -// charge(speed) -// - used to charge on an object detected +// charge(speed) +// - used to charge on an object detected // the robot will move in a straight line // until it detects the arena line where // it will use reverse() to move back // // detect_object(range, speed) // - used to detect an object, the robot will -// move in a circle to find an object and +// move in a circle to find an object and // return 1 if it finds something and return // 0 if it does not find anything - the search // will be carried out for 10 seconds. // -// detect_line () +// detect_line () // - used to detect a line, it returns the following // an int value as follows: // 0 - if no line is detected // 1 - if line detected from the front // -1 - if line detected from the back // -// reverse(speed) +// reverse(speed) // - reverses the robot with chargespeed in same position -// reverseandturn(speed) +// reverseandturn(speed) // - reverses whie moving in a circular direction // @@ -61,17 +61,19 @@ // optimum value: 30 to 50 unsigned int range; -void initialise() { +void initialise() +{ chargespeed = 0.6; - searchspeed = 0.5; + searchspeed = 0.5; range = 30; - + // Wait for 5 seconds to move away from robot wait(5); } // The main loop - please write your code here -int main() { +int main() +{ // Initialise the code initialise(); @@ -79,44 +81,44 @@ Timer t; t.start(); - + int detect_l = 0; int detect_o = 0; while(1) { // Sample code to detect and object and charge at it - - detect_o = detect_object(range, searchspeed) - + + detect_o = detect_object(range, searchspeed); + if (detect_o == 1) { - + charge(chargespeed); while (true) { - + detect_l = detect_line(); // If line is detected from front then reverse if(detect_l == 1) { reverse(searchspeed); wait(2); stop(); - detect_l = 0 + detect_l = 0; break; // If line is detected from back just keep on moving forward } else if (detect_l == -1) { wait(2); stop(); - detect_l = 0 + detect_l = 0; break; } } - + } else { - + moverandom(); - + } - + detect_o = 0; stop(); }