-
Notifications
You must be signed in to change notification settings - Fork 2
/
rovio_example.cpp
374 lines (269 loc) · 10.1 KB
/
rovio_example.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// *****************************************************************************
// Example: controlling the rovio robot using the RovioLib C++ API
// usage: rovio_example [<hostname> <username> <pass>]
// Dependancies : OpenCV 2.1 (or later), libCurl 7.18 (or later),
// RovioLib C++ API 1.3 or later
// Author : Toby Breckon, [email protected]
// Copyright (c) 2011-2013 Toby Breckon, School of Engineering, Cranfield University
// License : GPL - http://www.gnu.org/copyleft/gpl.html
// *****************************************************************************
// standard includes
#include <iostream>
#include <ctime>
using namespace std;
// OpenCV includes
#include <cv.h>
#include <highgui.h>
using namespace cv;
// RovioLib API includes
#include "rovio_cc_lib.h"
// *****************************************************************************
// define robot network access parameters
// #define TESTHARNESS_ROVIO_HOST "host.domain.tld" // or IP address
// #define TESTHARNESS_ROVIO_USER "username"
// #define TESTHARNESS_ROVIO_PASS "password"
#define TESTHARNESS_ROVIO_HOST "192.168.10.18" // or IP address
#define TESTHARNESS_ROVIO_USER ""
#define TESTHARNESS_ROVIO_PASS ""
// *****************************************************************************
// do basic display of robot position and path
void disp_kinematics(Rovio *robot)
{
// static values (preserved across calls)
static string windowName = (string) "Robot Kinematics View :"
+ (string) robot->name; // window name
static bool firstCall = false;
static Point lastPos = Point(125, 125);
static Mat map_img = Mat::zeros(250, 250, CV_8UC3);
static double omega_cumulative = 0; // accumulated rotation
static int lastR = 0;
static int lastL = 0;
static int lastB = 0;
// current values (used in this call)
double x, y, omega; // kinematics values
int wheelR, wheelL, wheelB; // wheel encoder values
bool dirR, dirL, dirB; // wheel encoder directions
Point next; // next position of robot on map
// first time called - create window
if (!firstCall)
{
namedWindow(windowName, CV_WINDOW_AUTOSIZE);
firstCall = true;
}
// get wheel encoder values
robot->getWheelEncoders(dirR, wheelR, dirL, wheelL, dirB, wheelB);
// see if we have moved at all
if ((lastR != wheelR) || (lastL != wheelL) || (lastB != wheelB))
{
// **only** if we have moved then update kinematics display
// check we are still on map (!)
if ((lastPos.x < 0) || (lastPos.y < 0) || (lastPos.x > 250 ) || (lastPos.y > 250))
{
// if not reset us to the origin (and redraw map)
lastPos = Point(125, 125);
map_img = Mat::zeros(250, 250, CV_8UC3);
}
// get kinematic from last robot robot
robot->getForwardKinematics(x,y,omega);
omega_cumulative += omega;
// rotate offset by omega then add to last position
next.x = cvCeil((x*cos(omega_cumulative) - y*sin(omega_cumulative)) + lastPos.x);
next.y = cvCeil((x*sin(omega_cumulative) + y*cos(omega_cumulative)) + lastPos.y);
// draw robot movement
line(map_img, lastPos, next, Scalar(255,0,0), 2);
// update position
lastPos = next; // where we are now is where we start next time
}
// in all cases, update record of last move and display map
lastR = wheelR;
lastL = wheelL;
lastB = wheelB;
imshow(windowName, map_img);
}
// *****************************************************************************
// do basic display image
void disp_cam_image(Rovio *robot)
{
static string windowName = (string) "Camera View :"
+ (string) robot->name; // window name
static bool first = false;
string dir[] = {(string) "backward", (string) "forward"}; // forward == 1
bool rightDir, leftDir, rearDir;
int rightTicks, leftTicks, rearTicks, x, y, room, sigst;
float theta;
if (!first)
{
namedWindow(windowName, CV_WINDOW_AUTOSIZE);
first = true;
}
Mat img = robot->getImage(CV_LOAD_IMAGE_UNCHANGED);
if (!img.empty())
{
// report wheel encoders
robot->getWheelEncoders(leftDir, leftTicks,
rightDir, rightTicks,
rearDir, rearTicks);
stringstream ss;
ss << "right: " << dir[rightDir] << " @ " << rightTicks << " left: " << dir[leftDir]
<< " @ " << leftTicks << " rear: " << dir[rearDir] << " @ " << rearTicks;
putText(img, ss.str(),
Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,255,0), 2, 8, false );
ss.str(std::string());
// report base station info
robot->getBaseStationSignalInfo(x,y,theta, room, sigst);
ss << "x: " << x << " y: " << y << " theta: " << theta << " room: " << room << " signal: " << sigst;
putText(img, ss.str(),
Point(10, 40), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,255,0), 2, 8, false );
imshow(windowName, img);
}
}
// *****************************************************************************
// very basic obstacle avoidance
void do_obstcale_avoid(Rovio* robot, RNG &random)
{
// get random choice and angle from system time
int choice = random.uniform(ROVIO_LEFT, ROVIO_RIGHT + 1);
int angle = random.uniform(5, 60);
int repeat = random.uniform(1, 4);
for (int i = 0; i < repeat; i++){
disp_cam_image(robot);
robot->manualDrive(choice, 3);
robot->waitUntilComplete(-1);
disp_kinematics(robot);
}
disp_cam_image(robot);
robot->rotateRight(3, (int) angle);
robot->waitUntilComplete(-1);
disp_kinematics(robot);
// if still blocked recursively solve problem
// (as we are probably not blocked on all angles)
if (robot->getIRObstacle()) {
do_obstcale_avoid(robot, random);
}
}
// *****************************************************************************
// do basic plot wifi signal strength
void plot_wifi(Mat &img, int strength)
{
static int count = 0; // static variable preserved across calls
if (img.empty() || (count == 200))
{
img = Mat(255, 200, CV_8U);
img.setTo(Scalar(255));
count = 0;
}
line(img, Point(count, img.rows), Point(count, 255 - strength),
Scalar::all(0), 1, 8, 0 );
count++;
}
// *****************************************************************************
int main(int argc, char** argv ) {
Mat img, wifi; // image objects
string windowName2 = (string) "Wifi Strength"; // window name
char key; // user input
bool keepExploring = true; // control state of robot
RNG random ((uint64) time(NULL));
int choice;
int backOff = 0;
// create named window
namedWindow(windowName2, CV_WINDOW_AUTOSIZE);
// if specified then take from command line in order
char *host, *user, *pass;
if (argc > 1)
{
host = argv[1];
user = argv[2];
pass = argv[3];
} else {
host = (char *) TESTHARNESS_ROVIO_HOST;
user = (char *) TESTHARNESS_ROVIO_USER;
pass = (char *) TESTHARNESS_ROVIO_PASS;
}
// connect to robot and check connection
Rovio* robot = new Rovio((char *) host,
(char *) user,
(char *) pass);
if (!robot->isConnected()){
cout << "ERROR: not connected to robot " << TESTHARNESS_ROVIO_HOST
<< " with user/pass = " << TESTHARNESS_ROVIO_USER "/"
<< TESTHARNESS_ROVIO_PASS << endl << endl;
return 1;
}
// turn off blue lights
robot->setLightState(false);
// uncomment following line to give verbose output
// from API of robot <-> PC communications
// robot->setAPIVerbose(true);
// setup camera
robot->setBrightness(3);
robot->setAGC(true);
robot->setContrast(60);
robot->setFrameRate(25);
robot->setResolution(3);
robot->setSensorFrequencyCompensation(50);
// turn of IR sensor
if (!(robot->setIRPowerState(true)))
{
cout << "ERROR: robot IR sensor failed to activate" << endl;
return 1;
}
// explore environment
while (keepExploring)
{
// check for obstacles and avoid
if (robot->getIRObstacle())
{
do_obstcale_avoid(robot, random);
}
// check battery level (go to dock if low)
if (robot->getBatteryLevel() < 106)
{
cout << "WARNING: robot battery low returning to dock" << endl;
robot->returnHome();
robot->waitUntilComplete(-1);
robot->returnHomeDock();
// until we get home keep relaying images
while (!robot->getChargingStatus())
{
disp_cam_image(robot);
}
return 0;
}
// make exploration moves
robot->manualDrive(ROVIO_FORWARD, 5);
robot->waitUntilComplete(-1);
backOff++;
disp_cam_image(robot);
disp_kinematics(robot);
// periodically do back to avoid getting stuck
if (backOff == 25) {
choice = random.uniform(ROVIO_BACKWARDLEFT, ROVIO_BACKWARDRIGHT + 1);
for (int i = 0; i < 5; i++)
{
robot->manualDrive(choice, 2);
robot->waitUntilComplete(-1);
disp_cam_image(robot);
disp_kinematics(robot);
}
backOff = 0;
}
// plot wifi
plot_wifi(wifi, robot->getWifiSS());
imshow(windowName2, wifi);
key = waitKey(10);
if (key == 'x')
{
keepExploring = false;
} /* else if (key == 'f') {
robot->manualDrive(ROVIO_FORWARD, 5);
} else if (key == 'b') {
robot->manualDrive(ROVIO_BACKWARD, 5);
} else if (key == 'r') {
robot->manualDrive(ROVIO_TURNRIGHT, 5);
} else if (key == 'l') {
robot->manualDrive(ROVIO_TURNLEFT, 5);
} */
}
return 0;
}
// *****************************************************************************