
Robot code for searching an object and charging at it.
Dependencies: HCSR04 Motor mbed
Revision 19:67ea4e8be9e1, committed 2015-05-30
- Comitter:
- alex0612
- Date:
- Sat May 30 19:08:26 2015 +0000
- Parent:
- 18:7bd638e3926d
- Child:
- 20:37a89edd1cde
- Commit message:
- Added led blinking
Changed in this revision
--- a/functions.cpp Sat May 30 16:18:31 2015 +0000 +++ b/functions.cpp Sat May 30 19:08:26 2015 +0000 @@ -7,6 +7,11 @@ #include "hcsr04.h" #include "functions.h" +DigitalOut myled1(LED1); +DigitalOut myled2(LED2); +DigitalOut myled3(LED3); +DigitalOut myled4(LED4); + // Two sensors are used to detect a line, line_sens1 and line_sens2 // if there is a difference between the readings of these sensors // the robot has detected a line. @@ -23,6 +28,43 @@ // Example: usensor(Trigger, Echo) HCSR04 usensor(p25,p26); +void turn_leds_on() { + myled1 = 1; + myled2 = 1; + myled3 = 1; + myled4 = 1; + wait(0.1); + myled1 = 0; + myled2 = 0; + myled3 = 0; + myled4 = 0; +} + +void flash_leds() { + Timer t; + t.start(); + while(t.read_ms() < 1000) { + myled1 = 0; + myled2 = 0; + myled3 = 0; + myled4 = 0; + wait(0.15); + myled1 = 1; + wait(0.15); + myled2 = 1; + wait(0.15); + myled3 = 1; + wait(0.15); + myled4 = 1; + wait(0.15); + myled1 = 0; + myled2 = 0; + myled3 = 0; + myled4 = 0; + } + t.stop(); +} + // Returns value from the QRE1113 // lower numbers mean more refleacive // more than 3000 means nothing was reflected. @@ -98,7 +140,7 @@ MotorRight.speed(-(speed-0.1)); } -void charge(float speed) { +void move_forward(float speed) { MotorLeft.speed(speed); MotorRight.speed(speed); } @@ -112,20 +154,23 @@ float random_speed; printf("Moving randomly\n"); - counter = rand() % 10; + counter = rand() % 5; for (int i = 0; i < counter; i++) { random_time = rand() % 1000 + 1000; fwd_bck = rand()%2; - random_speed = (float)rand()/(float)(RAND_MAX); + //random_speed = (float)rand()/(float)(RAND_MAX); - if (random_speed < 0.6) { - random_speed = 0.5; - } else if (random_speed >= 0.9) { - random_speed = 0.9; - } + //if (random_speed < 0.6) { + //random_speed = 0.5; + //} else if (random_speed >= 0.8) { + //random_speed = 0.8; + //} + + random_speed = 0.5; + // If fwd_back == 1 move forward or backwards if (fwd_bck == 1) { fwd = rand()%2; @@ -172,6 +217,8 @@ // If an object is detected based on out set range return 1 if (dist <= range && dist >= 1) { sense = 1; + stop(); + flash_leds(); break; } else { sense = 0; @@ -200,24 +247,31 @@ t.start(); int detect = 0; + if(fwd_bck == 1) { + move_forward(speed); + wait_ms(500); + } else { + turn(speed + 0.2); + } + while (t.read_ms() < time) { - if(fwd_bck == 1) { - charge(speed); - } else { - turn(speed); - } detect = detect_line(); // If line is detected from front then reverse if(detect == 1) { - move_detect(-speed,1,1500); + stop(); + turn_leds_on(); + move_detect(-0.5,1,1000); stop(); break; // If line is detected from back just keep on moving forward } else if (detect == -1) { - move_detect(speed,1,1500); + stop(); + turn_leds_on(); + move_detect(0.5,1,1000); stop(); break; } } + stop(); t.stop(); } \ No newline at end of file
--- a/functions.h Sat May 30 16:18:31 2015 +0000 +++ b/functions.h Sat May 30 19:08:26 2015 +0000 @@ -1,5 +1,7 @@ // Header file for functions.cpp +void flash_leds(); +void turn_leds_on(); int read_line1(); int read_line2(); int detect_line(); @@ -7,7 +9,7 @@ void reverse(float speed); void turn(float speed); void reverseandturn(float speed); -void charge(float speed); +void move_forward(float speed); void stop(); int detect_object(int range, float speed); void move_detect(float speed, int fwd_bck, int time); \ No newline at end of file
--- 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; }