-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
368 lines (308 loc) · 9.38 KB
/
main.cpp
File metadata and controls
368 lines (308 loc) · 9.38 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
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
/* +---------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| http://www.mrpt.org/ |
| |
| Copyright (c) 2005-2014, Individual contributors, see AUTHORS file |
| See: http://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See details in http://www.mrpt.org/License |
+---------------------------------------------------------------------------+ */
#include <mrpt/system/filesystem.h>
#include <mrpt/utils/CFileOutputStream.h>
#include <mrpt/utils/CFileGZInputStream.h>
#include <mrpt/utils/CFileGZOutputStream.h>
#include <mrpt/utils/CConfigFile.h>
#include <mrpt/slam/CMetricMapBuilderRBPF.h>
#include <mrpt/slam/CActionCollection.h>
#include <mrpt/slam/CSensoryFrame.h>
#include <mrpt/utils/CTicTac.h>
#include <mrpt/utils/CRobotSimulator.h>
#include <mrpt/slam/CRawlog.h>
#include <mrpt/slam/COccupancyGridMap2D.h>
#include <mrpt/slam/CMultiMetricMap.h>
#include <mrpt/slam/CActionRobotMovement2D.h>
#include <mrpt/slam/CObservationOdometry.h>
#include <mrpt/hwdrivers/CJoystick.h>
#include <mrpt/hwdrivers/CKinect.h>
#include <mrpt/gui/CDisplayWindow3D.h>
#include <mrpt/opengl/CSetOfObjects.h>
#include <mrpt/opengl/CPlanarLaserScan.h>
#include <mrpt/opengl/CPointCloud.h>
#include <mrpt/opengl/stock_objects.h>
#include <mrpt/opengl/CPointCloudColoured.h>
#include <mrpt/opengl/CFrustum.h>
#include <mrpt/opengl/CGridPlaneXY.h>
#include <mrpt/system/os.h>
#include <mrpt/synch/CThreadSafeVariable.h>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include "SerialPort.h"
#include "KinectGrabber.h"
#define TIMER_MS 10
#define SERIAL_PORT_ADDRESS "/dev/ttyACM0"
using namespace mrpt;
using namespace mrpt::hwdrivers;
using namespace mrpt::slam;
using namespace mrpt::opengl;
using namespace mrpt::gui;
using namespace mrpt::system;
using namespace mrpt::math;
using namespace mrpt::utils;
using namespace std;
using namespace boost;
CDisplayWindow3D window("Display Window");
CDisplayWindow3D slamWindow("SLAM OUTPUT");
CRobotSimulator the_robot(0,0);
COccupancyGridMap2D the_grid;
CMultiMetricMap* slam_map;
CJoystick joystick;
StdVector_CPose2D robot_path_GT, robot_path_ODO;
CPose2D lastOdo, pose_start;
bool we_are_closing = false;
long decimation = 1;
mrpt::opengl::CSetOfObjectsPtr gl_grid, gl_slam_grid;
mrpt::opengl::CSetOfObjectsPtr gl_robot;
mrpt::opengl::CPlanarLaserScanPtr gl_scan;
mrpt::opengl::CPointCloudPtr gl_path_GT;
mrpt::opengl::CPointCloudPtr gl_path_ODO;
int last_pressed_key = 0;
long LASER_N_RANGES = 361;//361;
double LASER_APERTURE = M_PI;
double LASER_STD_ERROR = 0.01;
double LASER_BEARING_STD_ERROR = DEG2RAD(0.05);
void update_grid_map_3d()
{
if (!gl_grid) gl_grid = CSetOfObjects::Create();
gl_grid->clear();
the_grid.getAs3DObject( gl_grid );
}
void setupDisplayWindow()
{
window.setCameraAzimuthDeg(90);
window.setCameraElevationDeg(90);
//window.setCameraZoom(8);
slamWindow.setCameraAzimuthDeg(90);
slamWindow.setCameraElevationDeg(90);
the_grid.loadFromBitmapFile("virtual_map1.png", .0275/*12./(800.*3)*/);
update_grid_map_3d();
gl_robot = mrpt::opengl::stock_objects::RobotPioneer();
gl_scan = mrpt::opengl::CPlanarLaserScan::Create();
gl_robot->insert(gl_scan);
// paths:
gl_path_GT = mrpt::opengl::CPointCloud::Create();
gl_path_GT->setColor(0,0,0, 0.7);
gl_path_GT->setLocation(0,0, 0.01);
gl_path_GT->setPointSize(3);
gl_path_ODO = mrpt::opengl::CPointCloud::Create();
gl_path_ODO->setColor(0,1,0, 0.7);
gl_path_ODO->setLocation(0,0, 0.01);
gl_path_ODO->setPointSize(2);
COpenGLScenePtr &theScene = window.get3DSceneAndLock();
theScene->insert(gl_grid);
theScene->insert(gl_robot);
theScene->insert(gl_path_GT);
theScene->insert(gl_path_ODO);
window.unlockAccess3DScene();
// slam:
if (!gl_slam_grid) gl_slam_grid = CSetOfObjects::Create();
COpenGLScenePtr &slamScene = slamWindow.get3DSceneAndLock();
slamScene->insert(gl_slam_grid);
slamWindow.unlockAccess3DScene();
}
void Run()
{
try
{
if (1) // change to joystick detection
{
float x,y,z;
vector_bool btns;
if (joystick.getJoystickPosition(0,x,y,z,btns))
{
float v = the_robot.getV();
float w = the_robot.getW();
v = -y;
w = -x;
the_robot.movementCommand(v,w);
}
}
CPose2D p;
the_robot.getRealPose(p);
}
catch(std::exception &e)
{
cout << e.what() << endl;
}
catch(...)
{
cout << "Unknown Exception" << endl;
}
}
void Start()
{
setupDisplayWindow();
the_robot.getRealPose(pose_start);
the_robot.resetOdometry(CPose2D(0,0,0));
lastOdo = CPose2D(0,0,0);
// odo errors:
double Ax_err_bias = 1e-5;//0;//.00001;
double Ax_err_std = 10e-4;
double Ay_err_bias = 1e-5;//0;//.00001;
double Ay_err_std = 10e-4;
double Aphi_err_bias = 1e-5;//0;//5e-4;
double Aphi_err_std = 10e-4;
the_robot.setOdometryErrors(true,
Ax_err_bias, Ax_err_std,
Ay_err_bias, Ay_err_std,
Aphi_err_bias, Aphi_err_std);
decimation = 1;
}
void Setup_Sim()
{}
void Setup_Bot()
{}
void RunSimulation()
{
Start();
KinectGrabber kinect_grabber;
kinect_grabber.init();
// Initialize serial stream
SerialPort serial;
if(!serial.start(SERIAL_PORT_ADDRESS, 9600)) return;
// Begin
sleep(5); // wait for arduino bootup
serial.end_of_line_char('\n');
//serial.write_some("BI010\r\n");
//serial.write_some("PE011\r\n");
//serial.write_some("RA01000\r\n");
// Simulate robot
the_robot.movementCommand(0,0);
static CTicTac tictac;
// Initiate map building
CMetricMapBuilderRBPF::TConstructionOptions rbpfMappingOptions;
rbpfMappingOptions.loadFromConfigFile(CConfigFile("/home/nick/dev/mrpt/src/mrpt/share/mrpt/config_files/rbpf-slam/gridmapping_optimal_sampling.ini"),"MappingApplication");
CMetricMapBuilderRBPF mapBuilder(rbpfMappingOptions);
mapBuilder.options.verbose = true;
mapBuilder.options.enableMapUpdating = true;
mapBuilder.options.debugForceInsertion = false;
// Store serial data
double angle_rad;
double x;
double y;
while(1)
{
double At = tictac.Tac();
tictac.Tic();
the_robot.simulateInterval(At);
// Grab angle
if(serial.angle_mutex_.try_lock()) {
x = serial.get_x();
y = serial.get_y();
angle_rad = serial.get_angle();
serial.angle_mutex_.unlock();
}
//cout << "X in: " << x << endl;
//cout << "Y in: " << y << endl;
//cout << "H in: " << angle_rad << endl << endl;
// Simulate pose
/*
CPose2D p, odo_now;
the_robot.getRealPose(p);
the_robot.getOdometry(odo_now);
*/
// Measure pose
CPose2D p(x/10, y/10, angle_rad);
CPose2D odo_now(x/10, y/10, angle_rad);
CObservation2DRangeScanPtr the_scan = CObservation2DRangeScan::Create();
// Simulate scan
/*
the_scan->sensorLabel = "LASER_SIM";
the_scan->sensorPose.setFromValues(0.0,0,0.0);
the_scan->maxRange = 80;
the_scan->aperture = LASER_APERTURE;
the_scan->stdError = LASER_STD_ERROR;
the_grid.laserScanSimulator(*the_scan, p, 0.2f, LASER_N_RANGES, LASER_STD_ERROR, 1, LASER_BEARING_STD_ERROR);
*/
// Grab scan from Kinect
kinect_grabber.grab(the_scan);
// Process Action
CActionCollectionPtr acts = CActionCollection::Create();
CActionRobotMovement2D act;
CActionRobotMovement2D::TMotionModelOptions opts;
opts.modelSelection = CActionRobotMovement2D::mmGaussian;
CPose2D Aodom = odo_now - lastOdo;
lastOdo = odo_now;
const TTimeStamp tim_now = mrpt::system::now();
act.computeFromOdometry(Aodom, opts);
act.timestamp = tim_now;
acts->insert(act);
// Process Observation:
CSensoryFramePtr sf = CSensoryFrame::Create();
the_scan->timestamp = act.timestamp;
sf->insert(CObservation2DRangeScanPtr(new CObservation2DRangeScan(*the_scan)));
// Build map
mapBuilder.processActionObservation(*acts, *sf);
CMultiMetricMap *mostLikMap = mapBuilder.mapPDF.getCurrentMostLikelyMetricMap();
// Update GL Visualization:
// :Robot
gl_robot->setPose(p);
// :Scan
gl_scan->setScan(*the_scan);
// :Path
robot_path_GT.push_back(p);
robot_path_ODO.push_back(odo_now);
if (!robot_path_GT.empty())
{
gl_path_GT->insertPoint( robot_path_GT.rbegin()->x(), robot_path_GT.rbegin()->y(), 0 );
CPose2D this_odo = pose_start + *robot_path_ODO.rbegin();
gl_path_ODO->insertPoint( this_odo.x(), this_odo.y(), 0 );
}
// :Map
gl_slam_grid->clear();
mostLikMap->getAs3DObject( gl_slam_grid );
update_grid_map_3d();
if (os::kbhit())
{
char c = os::getch();
cout << "Key Pressed: " << c << endl;
if (c == 'w')
the_robot.movementCommand(1,0);
else if (c == 'a')
the_robot.movementCommand(0,1);
else if (c == 's')
the_robot.movementCommand(-1,0);
else if (c == 'd')
the_robot.movementCommand(0,-1);
if (c == 'f')
mostLikMap->saveMetricMapRepresentationToFile("map_out.png");
if (c==27)
break;
}
window.forceRepaint();
slamWindow.forceRepaint();
usleep(3*10e3);
}
}
// ------------------------------------------------------
// MAIN
// ------------------------------------------------------
int main()
{
try
{
RunSimulation();
return 0;
} catch (std::exception &e)
{
cout << "MRPT exception caught: " << e.what() << endl;
return -1;
}
catch (...)
{
cout << "Untyped exception caught" << endl;
return -1;
}
}