Robot code for searching an object and charging at it.
Dependencies: HCSR04 Motor mbed
Diff: main.cpp
- Revision:
- 19:67ea4e8be9e1
- Parent:
- 18:7bd638e3926d
- Child:
- 20:37a89edd1cde
--- a/main.cpp Sat May 30 16:18:31 2015 +0000 +++ b/main.cpp Sat May 30 19:08:26 2015 +0000 @@ -12,8 +12,8 @@ // by detetecting an object and charging towards it // it uses basic functions as: // -// charge(speed) -// - used to charge on an object detected +// move_forward(speed) +// - used to move_forward 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 @@ -33,7 +33,7 @@ // -1 - if line detected from the back // // reverse(speed) -// - reverses the robot with chargespeed in same position +// - reverses the robot with move_forwardspeed in same position // reverseandturn(speed) // - reverses whie moving in a circular direction @@ -51,9 +51,9 @@ // Global parameters -// Speed at which it charges an object +// Speed at which it move_forwards an object // optimum value: 0.4 to 0.8 -float chargespeed; +float move_forwardspeed; // Speed at which it rotates to find an object // optimum value: 0.3 to 0.5 float searchspeed; @@ -63,7 +63,7 @@ void initialise() { - chargespeed = 0.6; + move_forwardspeed = 0.6; searchspeed = 0.5; range = 30; @@ -85,29 +85,32 @@ int detect_l = 0; int detect_o = 0; - while(1) { - // Sample code to detect and object and charge at it + while(true) { + // Sample code to detect and object and move_forward at it detect_o = detect_object(range, searchspeed); if (detect_o == 1) { - charge(chargespeed); + move_forward(move_forwardspeed); while (true) { detect_l = detect_line(); // If line is detected from front then reverse if(detect_l == 1) { + stop(); + turn_leds_on(); reverse(searchspeed); - wait(2); - stop(); + wait(1.5); detect_l = 0; break; - // If line is detected from back just keep on moving forward + // If line is detected from back just keep on moving forward } else if (detect_l == -1) { - wait(2); stop(); + turn_leds_on(); + move_forward(searchspeed); + wait(1.5); detect_l = 0; break; }