-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcraft.cpp
295 lines (242 loc) · 7 KB
/
craft.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
//
// Assignment on Solving a Maze.
//
// Author: Ugo Pattacini - <[email protected]>
#include <cstdlib>
#include <vector>
#include <deque>
#include <algorithm>
#include <cmath>
#include <limits>
#include <yarp/os/Network.h>
#include <yarp/os/LogStream.h>
#include <yarp/os/Time.h>
#include <yarp/os/Bottle.h>
#include <yarp/os/Property.h>
#include <yarp/os/ResourceFinder.h>
#include <yarp/os/RFModule.h>
#include <yarp/os/BufferedPort.h>
#include <yarp/sig/Vector.h>
#include <yarp/math/Math.h>
#include <yarp/math/Rand.h>
#define DEG2RAD (M_PI/180.0)
#define RAD2DEG (180.0/M_PI)
using namespace std;
using namespace yarp::os;
using namespace yarp::sig;
using namespace yarp::math;
enum class State {
idle,
pickup_direction,
rotating,
moving,
on_target
};
class SteadyStateChecker {
deque<double> fifo;
unsigned int length;
public:
SteadyStateChecker() : length(5) { }
void push(const double item) {
fifo.push_back(item);
if (fifo.size() > length)
fifo.pop_front();
}
void clear() {
fifo.clear();
}
bool check(const double threshold) const {
if (fifo.size() < length)
return false;
for (auto& item:fifo)
if (abs(item) > threshold)
return false;
return true;
}
};
class CraftModule : public RFModule {
BufferedPort<Bottle> portMotor;
BufferedPort<Property> portRadar;
Vector craft;
Vector target;
Vector wayPoint;
vector<Vector> obstacles;
vector<Vector> walls;
double direction;
double safety_margin;
SteadyStateChecker steady;
State state;
void getRadar() {
if (Property* radar = portRadar.read(false)) {
if (Bottle* b = radar->find("craft").asList())
for (int i = 0; i < b->size(); i++)
craft[i] = b->get(i).asFloat64();
if (Bottle* b = radar->find("target").asList())
for (int i = 0; i < b->size(); i++)
target[i] = b->get(i).asFloat64();
target[0] -= craft[0];
target[1] -= craft[1];
if (Bottle* b = radar->find("obstacles").asList()) {
obstacles.clear();
for (int i = 0; i < b->size(); i++) {
if (Bottle* o = b->get(i).asList()) {
Vector obstacle(o->size());
for (int j = 0; j < o->size(); j++)
obstacle[j] = o->get(j).asFloat64();
obstacle[0] -= craft[0];
obstacle[1] -= craft[1];
obstacles.push_back(obstacle);
}
}
}
double length = radar->find("length").asFloat64();
walls[0][0] = length - craft[0];
walls[0][1] = 0.0;
walls[1][0] = 0.0;
walls[1][1] = length - craft[1];
walls[2][0] = -craft[0];
walls[2][1] = 0.0;
walls[3][0] = 0.0;
walls[3][1] = -craft[1];
if (state == State::idle)
state = State::pickup_direction;
}
}
Vector computeCrashPoint(const double direction) const {
Vector dir(2);
dir[0] = cos(direction);
dir[1] = sin(direction);
Vector crashPoint(2, numeric_limits<double>::infinity());
for (auto& obstacle:obstacles) {
Vector v = obstacle.subVector(0, 1);
double d = norm(v);
double r = obstacle[2];
double cos_theta = dot(dir, v) / d;
if (acos(cos_theta) <= asin(r / d)) {
double dist = d * cos_theta - sqrt(r * r - d * d * (1.0 - cos_theta * cos_theta));
if (norm(crashPoint) > dist)
crashPoint = dist * dir;
}
}
for (auto& wall:walls) {
double d = norm(wall);
double cos_theta = dot(dir, wall) / d;
if (cos_theta > 0.0) {
double theta = acos(cos_theta);
double tan_theta = tan(theta);
double dist = d * sqrt(1.0 + tan_theta * tan_theta);
if (norm(crashPoint) > dist)
crashPoint = dist * dir;
}
}
return crashPoint;
}
bool rotate() {
Bottle& motor = portMotor.prepare();
motor.clear();
double error = RAD2DEG * direction - craft[2];
double vel = std::min(std::max(error, -100.0), 100.0);
motor.addFloat64(0.0);
motor.addFloat64(vel);
portMotor.writeStrict();
steady.push(error);
return steady.check(0.1);
}
bool move() {
Bottle& motor = portMotor.prepare();
motor.clear();
Vector dir(2);
dir[0] = cos(direction);
dir[1] = sin(direction);
Vector dist = wayPoint - craft.subVector(0, 1);
double error = sign(dot(dir, dist)) * norm(dist);
double vel = std::min(std::max(error, -100.0), 100.0);
motor.addFloat64(vel);
motor.addFloat64(0.0);
portMotor.writeStrict();
steady.push(error);
return steady.check(target[2]);
}
void stop() {
Bottle& motor = portMotor.prepare();
motor.clear();
motor.addFloat64(0.0);
motor.addFloat64(0.0);
portMotor.writeStrict();
Time::delay(1.0);
}
public:
bool configure(ResourceFinder& rf)override {
Rand::init();
craft.resize(3, 0.0);
target.resize(3, 0.0);
wayPoint.resize(2, 0.0);
walls.assign(4, Vector(2, 0.0));
direction = 0.0;
safety_margin = 20.0;
state = State::idle;
portMotor.open("/assignment_solve-maze-craft/motor:o");
portRadar.open("/assignment_solve-maze-craft/radar:i");
return true;
}
bool close()override {
portMotor.close();
portRadar.close();
return true;
}
double getPeriod()override {
return 0.02;
}
bool updateModule()override {
getRadar();
if (state == State::pickup_direction) {
double direction_candidate = atan2(target[1], target[0]);
if (direction_candidate < 0.0)
direction_candidate += 2.0 * M_PI;
Vector crashPoint = computeCrashPoint(direction_candidate);
yInfo() << "test direction to target [deg] =" << RAD2DEG * direction_candidate;
double d = norm(crashPoint);
if (d > safety_margin)
state = State::rotating;
else {
direction_candidate = Rand::scalar(0.0, 2.0 * M_PI);
crashPoint = computeCrashPoint(direction_candidate);
yInfo() << "test random direction [deg] =" << RAD2DEG * direction_candidate;
d = norm(crashPoint);
if (d > safety_margin)
state = State::rotating;
}
if (state == State::rotating) {
direction = direction_candidate;
wayPoint = craft.subVector(0, 1) + ((d - 0.5 * safety_margin) / d) * crashPoint;
steady.clear();
yInfo() << "set way-point = (" << wayPoint.toString(3, 3) << ")";
}
} else if (state == State::rotating) {
if (rotate()) {
stop();
steady.clear();
state = State::moving;
}
} else if (state == State::moving) {
if (move()) {
stop();
state = (norm(target.subVector(0, 1)) < target[2]) ?
State::on_target : State::pickup_direction;
}
}
return true;
}
};
int main(int argc, char* argv[]) {
Network yarp;
if (!yarp.checkNetwork()) {
yError() << "YARP doesn't seem to be available";
return EXIT_FAILURE;
}
ResourceFinder rf;
rf.configure(argc, argv);
CraftModule mod;
return mod.runModule(rf);
}