-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
423 lines (343 loc) · 11.5 KB
/
main.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* n-dimensional Fast Marching example with the main functions used */
#include <iostream>
#include <cmath>
#include <chrono>
#include <array>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <sstream>
#include <time.h>
#include "fmm/fmdata/fmcell.h"
#include "ndgridmap/ndgridmap.hpp"
#include "console/console.h"
#include "fmm/fmm.hpp"
#include "fm2/fm2.hpp"
#include "fm2/fm2star.hpp"
#include "fmm/fmdata/fmfibheap.hpp"
#include "fmm/fmdata/fmpriorityqueue.hpp"
#include "fmm/fmdata/fmdaryheap.hpp"
#include "fmm/fmdata/fmdaryheap.hpp"
#include "io/maploader.hpp"
#include "io/gridplotter.hpp"
#include "io/gridwriter.hpp"
#include "io/gridpoints.hpp"
#include "gradientdescent/gradientdescent.hpp"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <mqueue.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include "/home/pi/repos/UUGear/RaspberryPi/src/UUGear.h"
using namespace std;
using namespace std::chrono;
using namespace cv;
void global_to_relative(double x, double y, double dx, double dy, double alfa, double &rx, double &yr);
void compareR(double &w, int &v);
void compareL(double &w, int &v);
int R_Servo=11;
int L_Servo=12;
int main(int argc, const char ** argv)
{
constexpr unsigned int ndims = 2; // Setting two dimensions.
constexpr unsigned int ndims3 = 3; // Setting three dimensions.
setupUUGear();
setShowLogs(1);
/* replace the device id with yours (listed by lsuu) */
UUGearDevice dev = attachUUGearDevice ("UUGear-Arduino-6746-1908");
cout << dev.fd << endl;
if (dev.fd == -1)
{
cout << "UUGEAR Fail" << endl;
return 2;
}
attachServo(&dev, R_Servo);
attachServo(&dev, L_Servo);
time_point<std::chrono::steady_clock> start, end; // Time measuring.
double time_elapsed;
console::info("Parsing input arguments.");
string filename1, filename2, filename_vels;
console::parseArguments(argc,argv, "-map1", filename1);
console::parseArguments(argc,argv, "-map2", filename2);
console::parseArguments(argc,argv, "-vel", filename_vels);
console::info("OpenCV Transform...");
// Load the image
Mat src = imread(filename2);
// Check if everything was fine
if (!src.data)
return -1;
//imshow("Source Image", src);
//Binary threshold
Mat bw;
cvtColor(src, bw, CV_BGR2GRAY);
threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
//imshow("Binary Image", bw);
// Dilate a bit the dist image
Mat kernel1 = Mat::ones(3, 3, CV_8UC1);
for(int i=0;i<5;i++)
erode(bw, bw, kernel1);
//imshow("dilate", bw);
// Perform the distance transform algorithm
Mat dist;
distanceTransform(bw, dist, CV_DIST_C, 5);
// Normalize the distance image for range = {0.0, 1.0}
// so we can visualize and threshold it
normalize(dist, dist, 0, 255, NORM_MINMAX);
//imshow("Distance Transform Image", dist);
imwrite("mapdis.jpg",dist);
//LUT
Mat save(dist.size(),CV_8UC1);
dist.convertTo(dist,CV_8UC1);
convertScaleAbs(dist,dist);
Mat lookUpTable(1, 256, CV_8UC1);
uchar* p = lookUpTable.data;
for( int i = 0; i < 256; ++i){
// p[i] = 255*log(i+1)/log(256);
// p[i] = sqrt(255*i);
p[i] = cbrt(255*255*i);
}
LUT(dist,lookUpTable,save);
//imshow("save",save);
//save file
imwrite( "map.jpg", save);
//waitKey(0);
// waitKey(100);
console::info("Creating grid from image and testing Fast Marching Method..");
nDGridMap<FMCell, ndims> grid;
MapLoader::loadMapFromImg("map.jpg", grid);
std::array<unsigned int, ndims> coords_init, coords_goal;
coords_init[0] = 15;
coords_init[1] = 100;
coords_goal[0] = 235;
coords_goal[1] =100;
vector<unsigned int> init_points;
unsigned int idx, goal;
grid.coord2idx(coords_init, idx);
init_points.push_back(idx);
grid.coord2idx(coords_goal, goal);
FMM< nDGridMap<FMCell, ndims> > fmm;
fmm.setEnvironment(&grid);
fmm.setInitialAndGoalPoints(init_points, goal);
fmm.compute();
cout << "\tElapsed FM time: " << fmm.getTime() << " ms" << endl;
console::info("Plotting the results and saving into test_fm.txt");
// GridPlotter::plotArrivalTimes(grid);
GridWriter::saveGridValues("test_fm.txt", grid);
console::info("Computing gradient descent ");
typedef typename std::vector< std::array<double, ndims> > Path; // A bit of short-hand.
Path path;
std::vector <double> path_velocity; // Velocities profile
start = steady_clock::now();
GradientDescent< nDGridMap<FMCell, ndims> > grad;
grad.apply(grid,goal,path,path_velocity);
end = steady_clock::now();
time_elapsed = duration_cast<milliseconds>(end-start).count();
cout << "\tElapsed gradient descent time: " << time_elapsed << " ms" << endl;
GridWriter::savePath("test_path.txt", grid, path);
GridWriter::savePathVelocity("path_velocity.txt", grid, path, path_velocity);
// GridPlotter::plotMapPath(grid,path);
//open path file
ifstream file("test_path.txt");
string str;
float x,y;
vector<pair<float,float>> path_vector;
//remove 4 first lines
for(int i =0 ;i < 4; i++)
getline(file, str);
//store in a vector
while(file >> x >> y){
pair<float,float> pos;
pos.first= x;
pos.second = y;
//cout << pos.first << ", " << pos.second << endl;
path_vector.push_back(pos);
}
double xpath, ypath, xrobot, yrobot, xrelative, yrelative;
int speed = 6; // velocidad total lineal del robot
double distance; //distancia entre el robot y el punto del path
double distance_threshold = 2.0;
float distance_wheel = 8.5; //separación entre las ruedas en cm
float wheel_radius = 3; //radio de la rueda en cm
double teta;
float basetime = 500.0; //ms
cout << "------" << endl;
//inicialize robot position
xrobot =path_vector.back().first;
yrobot =path_vector.back().second;
double alfa = 0;
int vel_r, vel_l;
//read the path from the vector
for (vector<pair<float, float> >::iterator i = path_vector.end(); i != path_vector.begin(); i--) {
pair <float,float> actual_point;
actual_point = make_pair(i->first,i->second);
xpath=actual_point.first;
ypath=actual_point.second;
cout << "----" << endl;
cout << "Coord Path x:" << xpath << " Coord Path y:"<< ypath << endl;
global_to_relative(xpath, ypath, xrobot, yrobot, alfa, xrelative, yrelative);//paso a coordenadas globales a relativas al robot
cout << "Coord Path x:" << xrelative << " Coord Path y:"<< yrelative << endl;
distance = sqrt(xrelative*xrelative+yrelative*yrelative);//distancia entre el robot y el punto del path
cout << "Distance:" <<distance << endl;
if(distance <= distance_threshold){//elegir el proximo punto de la trayectoria
continue;
}
teta = atan2(yrelative, xrelative);//cálculo del ángulo del robot con el punto del a trayectoria
cout <<"Angulo: "<< teta * 180 / 3.1415 << endl;
double turning_speed = 0.25 * teta;//en radianes parámetro configurable del controlador P
double R_Wheel_Speed = (speed + distance_wheel*turning_speed );//velocidad lineal de las ruedas
double L_Wheel_Speed = (speed - distance_wheel*turning_speed );
cout << "Rueda derecha: " << R_Wheel_Speed << " Rueda izquierda: " << L_Wheel_Speed << endl;
double Omega_R = R_Wheel_Speed / wheel_radius;
double Omega_L = L_Wheel_Speed / wheel_radius;
cout << "Rueda derecha: " << Omega_R << " Rueda izquierda: " << Omega_L << endl;
// Adecuar velocidad
compareR(Omega_R,vel_r);
compareL(Omega_L,vel_l);
//Mover los servos
writeServo(&dev, R_Servo, vel_r);
writeServo(&dev, L_Servo, vel_l );
cout << "pulse:" << vel_r << ", " << vel_l << endl;
R_Wheel_Speed = Omega_R*wheel_radius;
L_Wheel_Speed = Omega_L*wheel_radius;
cout << "w: "<< Omega_R << ", " << Omega_L << endl;
cout << "v: " << R_Wheel_Speed << ", " << L_Wheel_Speed << endl;
//calculo de posición mediante odometria, desde el eje de referencia global
xrobot = xrobot + (((R_Wheel_Speed + L_Wheel_Speed)/2.0)*cos(teta))*basetime/1000.0;// e = v*t
yrobot = yrobot + (((R_Wheel_Speed + L_Wheel_Speed)/2.0)*sin(teta))*basetime/1000.0;
double alfa = alfa + ((R_Wheel_Speed + L_Wheel_Speed)/distance_wheel)*basetime/1000.0;
cout << "Coordenadas movidas real x: " << xrobot << " y:" << yrobot << endl;
cout << "Teta: "<< teta * (180 / 3.1415) << endl;// proximo angulo a desplazar el eje de referencia
usleep(basetime*1000);
}
detachServo(&dev, R_Servo);
detachServo(&dev, L_Servo);
detachUUGearDevice (&dev);
// usleep(100000);
cleanupUUGear();
// usleep(100000);
cout << "FIN" << endl;
return 0;
}
void global_to_relative(double x, double y, double dx, double dy, double alfa, double &xr, double &yr){
xr = cos(alfa)*x - sin(alfa)*y - dx; //dx y dy es el desplazamiento del robot sobre el eje de coordenadas global
yr = sin(alfa)*x + cos(alfa)*y - dy;
/*
coordsR_Path[0] = x - dx;
coordsR_Path[1] = y - dy;
*/
/*
coordsR_Path[0] = cos(alfa)*x - sin(alfa)*y + dy; //dx y dy es el desplazamiento del robot sobre el global
coordsR_Path[1] = sin(alfa)*x + cos(alfa)*y - dx;
*/
}
void compareL(double &w, int &v){
float vector_w[]={
0,
0.5351946599,
1.064946662,
1.266771231,
1.72614981,
2.327105669,
2.963766654,
3.452299619,
3.878509449,
4.30355158,
4.553032831,
4.908738521,
5.150151891,
5.416539058,
5.711986643,
5.711986643,
5.817764173,
5.927533309,
6.04152433};
int vector_v[]={
82,
83,
84,
85,
86,
87,
88,
89,
90,
91,
92,
93,
94,
95,
96,
97,
98,
110,
120
};
float error=1000000;
int iter;
for(int i=0;i<(sizeof(vector_w)/4);i++){
if(error > ((vector_w[i]-w)*(vector_w[i]-w))){
iter = i;
error = ((vector_w[i]-w)*(vector_w[i]-w));
}
}
w = vector_w[iter];
v = vector_v[iter];
}
void compareR(double &w, int &v){
float vector_w[]={
0,
0.9941748904,
1.56298142,
1.795195802,
2.309994598,
2.908882087,
3.452299619,
3.926990817,
4.487989505,
4.759988869,
5.067084925,
5.324733311,
5.711986643,
5.817764173,
5.817764173,
5.927533309,
6.159985595
};
int vector_v[]={
90,
89,
88,
87,
86,
85,
84,
83,
82,
81,
80,
79,
78,
77,
76,
75,
64
};
float error=1000000;
int iter;
for(int i=0;i<(sizeof(vector_w)/sizeof(*vector_w));i++){
if(error > ((vector_w[i]-w)*(vector_w[i]-w)) ){
iter = i;
error = ((vector_w[i]-w)*(vector_w[i]-w));
}
}
w = vector_w[iter];
v = vector_v[iter];
}