-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomWalk.cpp
106 lines (84 loc) · 2.02 KB
/
RandomWalk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* RandomWalk.cpp
*
* Created on: Nov 7, 2013
* Author: dominik
*/
#include "PirateDotty.h"
#include "RandomWalk.h"
#include "Actuator.h"
unsigned long lastMove;
int DEFAULT_SPEED = 255;
int RADIUS_OFFSET = 55;
void initRandomWalk() {
randomSeed(1);
// randomSeed(analogRead(0));
}
int nextMoveTime = 0;
void randomWalk() {
if (millis() > nextMoveTime) {
LOGd(1, "randomWalk..");
int move = random(1, 8);
int duration = random(1, 4);
LOGd(3, "move: %d, duration: %d", move, duration);
switch(move) {
case 1:
moveForward(duration);
break;
case 2:
moveLeftForward(duration);
break;
case 3:
moveRightForward(duration);
break;
case 4:
moveBackward(duration);
break;
case 5:
moveLeftBackward(duration);
break;
case 6:
moveRightBackward(duration);
break;
case 7:
rotateClockwise(duration);
break;
case 8:
rotateCounterClockwise(duration);
break;
}
nextMoveTime = millis() + duration * 1000;
}
}
void moveForward(long duration) {
LOGd(1, "moveForward, %d s", duration);
drive(DEFAULT_SPEED, DEFAULT_SPEED);
}
void moveLeftForward(long duration) {
LOGd(1, "moveLeftForward, %d s", duration);
drive(DEFAULT_SPEED - RADIUS_OFFSET, DEFAULT_SPEED);
}
void moveRightForward(long duration) {
LOGd(1, "moveRightForward, %d s", duration);
drive(DEFAULT_SPEED, DEFAULT_SPEED - RADIUS_OFFSET);
}
void moveBackward(long duration) {
LOGd(1, "moveBackward, %d s", duration);
drive(-DEFAULT_SPEED, -DEFAULT_SPEED);
}
void moveLeftBackward(long duration) {
LOGd(1, "moveLeftBackward, %d s", duration);
drive(-(DEFAULT_SPEED - RADIUS_OFFSET), -DEFAULT_SPEED);
}
void moveRightBackward(long duration) {
LOGd(1, "moveRightBackward, %d s", duration);
drive(-DEFAULT_SPEED, -(DEFAULT_SPEED - RADIUS_OFFSET));
}
void rotateClockwise(long duration) {
LOGd(1, "rotateClockwise, %d s", duration);
drive(DEFAULT_SPEED, -DEFAULT_SPEED);
}
void rotateCounterClockwise(long duration) {
LOGd(1, "rotateCounterClockwise, %d s", duration);
drive(-DEFAULT_SPEED, DEFAULT_SPEED);
}