-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandomwalk.cpp
More file actions
160 lines (158 loc) · 3.13 KB
/
randomwalk.cpp
File metadata and controls
160 lines (158 loc) · 3.13 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "motion.cpp"
#include <iostream>
#include "shortIR.cpp"
const float slp = 0.5;
const float reflect_distance = 6.0;
const float rotate_speed = 1.0;
const float forward_speed = 1.0;
class Randomwalk { //this is the normal random walk
Motor* left;
Motor* right;
IR* irlf;
IR* irlb;
IR* irf;
IR* irr;
mraa::Gpio* uirb;
Location* start;
Location* current;
Odometry* odo;
float current_angle;
int channel;
bool interuption;
bool initialized;
float in_angle; float mid_angle; float out_angle; bool wait; int cnt; int cntinc; bool dec;
public:
Randomwalk(Motor* _left, Motor* _right, IR* _irlf, IR* _irlb, IR* _irf, IR* _irr, mraa::Gpio* _uirb, Location* _start) {
left = _left;
right = _right;
irlf = _irlf;
irlb = _irlb;
irr = _irr;
irf = _irf;
uirb = _uirb;
start = _start;
current = start;
current_angle = start->theta();
odo = new Odometry(left,right,start->x(),start->y(),start->theta());
}
void channel_stop() {
left->stop();
right->stop();
initialized = false;
}
void forward_setup() {
left->forward();
right->forward();
left->setSpeed(forward_speed);
right->setSpeed(forward_speed);
}
void forward_run() {
odo->run();
left->run();
right->run();
}
void reflect_setup() {
odo->run();
left->forward();
right->backward();
left->setSpeed(rotate_speed);
right->setSpeed(rotate_speed);
current_angle = odo->getAngle();
in_angle = current_angle;
wait = false;
cnt = 0;
cntinc = 0;
dec = false;
}
void reflect_run() {
odo->run();
left->run();
right->run();
float lfd = (irlf->getDistance());
float lbd = (irlb->getDistance());
if (!wait) {
if (lfd>10 || lbd>10) {
cnt = 0;
}
else {
if (lbd>lfd) {
if(cnt<1) cnt++;
else dec = true;
cntinc = 0;
}
else {
if (dec = true) {
if (cntinc == 0) {
mid_angle = odo->getAngle();
out_angle = 2*mid_angle-in_angle;
cntinc++;
}
else {
wait = true;
}
}
else cnt = 0;
}
}
}
}
bool forward_next() {
bool f = ((irf->getDistance())<reflect_distance);
bool lf = ((irlf->getDistance())<reflect_distance);
bool lb = ((irlb->getDistance())<reflect_distance);
bool r = ((irr->getDistance())<reflect_distance);
bool b = !uirb->read();
return (f || lf || lb || r || b);
}
bool reflect_next() {
if (!wait) return false;
else {
if ((odo->getAngle())>out_angle) {
return true;
}
}
return false;
}
void interupt(){
interuption = true;
}
int run(int _channel) {
channel = _channel;
if (interuption) { //interruption handler
interuption = false;
return 1;
}
else {
if (channel == 1) { //move forward
if (!initialized) {
forward_setup();
initialized = true;
return 1;
}
else {
forward_run();
if (forward_next()) {
channel_stop();
return 2;
}
else return 1;
}
}
if (channel == 2) { //reflect
if (!initialized) {
reflect_setup();
initialized = true;
return 2;
}
else {
reflect_run();
if (reflect_next()) {
channel_stop();
return 1;
}
else return 2;
}
}
}
}
};