
Robot code for searching an object and charging at it.
Dependencies: HCSR04 Motor mbed
Revision 7:d94d23c55015, committed 2015-05-23
- Comitter:
- alex0612
- Date:
- Sat May 23 17:03:48 2015 +0000
- Parent:
- 6:af897173cb75
- Child:
- 8:43a650f9d4af
- Commit message:
- moverandom function added
Changed in this revision
--- a/functions.cpp Sat May 23 16:09:43 2015 +0000 +++ b/functions.cpp Sat May 23 17:03:48 2015 +0000 @@ -102,6 +102,49 @@ return; } +void moverandom() +{ + int counter; + int turn; + int direction; + float random_time; + float random_speed; + + printf("Moving randomly\n"); + counter = rand() % 10; + + for (int i = 0; i < counter; i++) + { + random_time = rand() % 1000 + 1000; + + turn = rand()/(RAND_MAX); + random_speed = (float)rand()/(float)(RAND_MAX); + if (random_speed < 0.5) { + random_speed = 0.5; + } else if (random_speed > 0.8) { + random_speed = 0.8; + } + + if (turn == 0) { + direction = rand()/(RAND_MAX); + if (direction == 1) { + MotorLeft.speed(random_speed); + MotorRight.speed(random_speed); + wait_ms(random_time); + } else { + MotorLeft.speed(-random_speed); + MotorRight.speed(-random_speed); + wait_ms(random_time); + } + } else { + MotorLeft.speed(-random_speed); + MotorRight.speed(random_speed); + wait_ms(random_time); + } + return; + } +} + void stop() { MotorLeft.speed(0.0); MotorRight.speed(0.0);
--- a/functions.h Sat May 23 16:09:43 2015 +0000 +++ b/functions.h Sat May 23 17:03:48 2015 +0000 @@ -3,6 +3,7 @@ int read_line1(); int read_line2(); int detect_line(); +void moverandom(); void reverse(float speed); void reverseandturn(float speed); void charge(float speed);
--- a/main.cpp Sat May 23 16:09:43 2015 +0000 +++ b/main.cpp Sat May 23 17:03:48 2015 +0000 @@ -89,7 +89,7 @@ stop(); } else { - + moverandom(); } stop();