-
Notifications
You must be signed in to change notification settings - Fork 15
/
stage_node.cpp
594 lines (514 loc) · 23.5 KB
/
stage_node.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/**
* This file is part of OpenREALM.
*
* Copyright (C) 2018 Alexander Kern <laxnpander at gmail dot com> (Braunschweig University of Technology)
* For more information see <https://github.com/laxnpander/OpenREALM>
*
* OpenREALM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenREALM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenREALM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <realm_ros/stage_node.h>
using namespace realm;
StageNode::StageNode(int argc, char **argv)
: _nrof_msgs_rcvd(0),
_is_master_stage(false),
_do_shutdown(false),
_is_tf_base_initialized(false),
_is_tf_stage_initialized(false)
{
// Read basic launch file inputs
readParams();
// Specify stage
setPaths();
readStageSettings();
// Set naming conventions
_topic_prefix = "/realm/" + _id_camera + "/" + _type_stage + "/";
_tf_base_frame_name = "realm_base";
_tf_stage_frame_name = "realm_" + _id_camera + "_" + _type_stage;
// Set ros subscriber according to launch input
_sub_input_frame = _nh.subscribe(_topic_frame_in, 5, &StageNode::subFrame, this, ros::TransportHints());
if (_is_master_stage)
{
_publisher.insert({"general/output_dir", _nh.advertise<std_msgs::String>("/realm/" + _id_camera + "/general/output_dir", 5)});
_publisher.insert({"general/gnss_base", _nh.advertise<sensor_msgs::NavSatFix>("/realm/" + _id_camera + "/general/gnss_base", 5)});
}
else
_sub_output_dir = _nh.subscribe("/realm/"+ _id_camera +"/general/output_dir", 5, &StageNode::subOutputPath, this, ros::TransportHints());
// Set ros services for stage handling
_srv_req_finish = _nh.advertiseService(_topic_prefix + "request_finish", &StageNode::srvFinish, this);
_srv_req_stop = _nh.advertiseService(_topic_prefix + "request_stop", &StageNode::srvStop, this);
_srv_req_resume = _nh.advertiseService(_topic_prefix + "request_resume", &StageNode::srvResume, this);
_srv_req_reset = _nh.advertiseService(_topic_prefix + "request_reset", &StageNode::srvReset, this);
_srv_change_param = _nh.advertiseService(_topic_prefix + "change_param", &StageNode::srvChangeParam, this);
// Provide camera information a priori to all stages
ROS_INFO("STAGE_NODE [%s]: : Loading camera from path:\n\t%s", _type_stage.c_str(),_file_settings_camera.c_str());
_settings_camera = CameraSettingsFactory::load(_file_settings_camera);
ROS_INFO("STAGE_NODE [%s]: : Detected camera model: '%s'", _type_stage.c_str(), (*_settings_camera)["type"].toString().c_str());
// Create stages
if (_type_stage == "pose_estimation")
createStagePoseEstimation();
if (_type_stage == "densification")
createStageDensification();
if (_type_stage == "surface_generation")
createStageSurfaceGeneration();
if (_type_stage == "ortho_rectification")
createStageOrthoRectification();
if (_type_stage == "mosaicing")
createStageMosaicing();
if (_type_stage == "tileing")
createStageTileing();
// set stage path if master stage
if (_is_master_stage)
_stage->initStagePath(_path_output + "/" + _dir_date_time);
// Start the thread for processing
_stage->start();
ROS_INFO("STAGE_NODE [%s]: Started stage node successfully!", _type_stage.c_str());
}
StageNode::~StageNode()
{
// In case of an unproper shutdown, at least call the finish procedure
if (!_do_shutdown)
{
_stage->requestFinish();
_stage->join();
}
}
void StageNode::spin()
{
if (_is_master_stage)
{
// Share output folder with slaves
std_msgs::String msg;
msg.data = _dir_date_time;
_publisher["general/output_dir"].publish(msg);
}
static tf::TransformBroadcaster br;
if (_is_tf_base_initialized && _is_master_stage)
{
// Master stage sends first tf as mission reference
br.sendTransform(tf::StampedTransform(_tf_base, ros::Time::now(), "utm", _tf_base_frame_name));
_publisher["general/gnss_base"].publish(_gnss_base);
}
if (_is_tf_stage_initialized)
{
// Publish of current mission reference
br.sendTransform(tf::StampedTransform(_tf_stage, ros::Time::now(), _tf_base_frame_name, _tf_stage_frame_name));
}
}
bool StageNode::isOkay()
{
std::unique_lock<std::mutex> lock(_mutex_do_shutdown);
return (!_do_shutdown && _nh.ok());
}
void StageNode::createStagePoseEstimation()
{
// Pose estimation uses external frameworks, therefore load settings for that
ROS_INFO("STAGE_NODE [%s]: : Loading vslam settings from path:\n\t%s", _type_stage.c_str(), _file_settings_method.c_str());
VisualSlamSettings::Ptr settings_vslam = VisualSlamSettingsFactory::load(_file_settings_method, _path_profile + "/" + _type_stage + "/method");
ROS_INFO("STAGE_NODE [%s]: : Detected vslam type: '%s'", _type_stage.c_str(), (*settings_vslam)["type"].toString().c_str());
ImuSettings::Ptr settings_imu = nullptr;
if ((*_settings_stage)["use_imu"].toInt() > 0)
{
settings_imu = std::make_shared<ImuSettings>();
settings_imu->loadFromFile(_file_settings_imu);
}
// Topic and stage creation
_stage = std::make_shared<stages::PoseEstimation>(_settings_stage, settings_vslam, _settings_camera, settings_imu, (*_settings_camera)["fps"].toDouble());
_publisher.insert({"output/frame", _nh.advertise<realm_msgs::Frame>(_topic_frame_out, 5)});
_publisher.insert({"output/pose/visual/utm", _nh.advertise<geometry_msgs::PoseStamped>(_topic_prefix + "pose/visual/utm", 5)});
_publisher.insert({"output/pose/visual/wgs", _nh.advertise<geometry_msgs::PoseStamped>(_topic_prefix + "pose/visual/wgs", 5)});
_publisher.insert({"output/pose/visual/traj", _nh.advertise<nav_msgs::Path>(_topic_prefix + "pose/visual/traj", 5)});
_publisher.insert({"output/pose/gnss/utm", _nh.advertise<geometry_msgs::PoseStamped>(_topic_prefix + "pose/gnss/utm", 5)});
_publisher.insert({"output/pose/gnss/wgs", _nh.advertise<geometry_msgs::PoseStamped>(_topic_prefix + "pose/gnss/wgs", 5)});
_publisher.insert({"output/pose/gnss/traj", _nh.advertise<nav_msgs::Path>(_topic_prefix + "pose/gnss/traj", 5)});
_publisher.insert({"output/pointcloud", _nh.advertise<sensor_msgs::PointCloud2>(_topic_prefix + "pointcloud", 5)});
_publisher.insert({"debug/tracked", _nh.advertise<sensor_msgs::Image>(_topic_prefix + "tracked", 5)});
linkStageTransport();
if (_topic_imu_in != "uninitialised")
{
_sub_input_imu = _nh.subscribe(_topic_imu_in, 100, &StageNode::subImu, this, ros::TransportHints());
}
}
void StageNode::createStageDensification()
{
// Densification uses external frameworks, therefore load settings for that
ROS_INFO("STAGE_NODE [%s]: : Loading densifier settings from path:\n\t%s", _type_stage.c_str(), _file_settings_method.c_str());
DensifierSettings::Ptr settings_densifier = DensifierSettingsFactory::load(_file_settings_method, _path_profile + "/" + _type_stage + "/method");
ROS_INFO("STAGE_NODE [%s]: : Detected densifier type: '%s'", _type_stage.c_str(), (*settings_densifier)["type"].toString().c_str());
// Topic and stage creation
_stage = std::make_shared<stages::Densification>(_settings_stage, settings_densifier, (*_settings_camera)["fps"].toDouble());
_publisher.insert({"output/frame", _nh.advertise<realm_msgs::Frame>(_topic_frame_out, 5)});
_publisher.insert({"output/pose/utm", _nh.advertise<geometry_msgs::PoseStamped>(_topic_prefix + "pose/utm", 5)});
_publisher.insert({"output/pose/wgs", _nh.advertise<geometry_msgs::PoseStamped>(_topic_prefix + "pose/wgs", 5)});
_publisher.insert({"output/pointcloud", _nh.advertise<sensor_msgs::PointCloud2>(_topic_prefix + "pointcloud", 5)});
_publisher.insert({"output/img_rectified", _nh.advertise<sensor_msgs::Image>(_topic_prefix + "img", 5)});
_publisher.insert({"output/depth", _nh.advertise<sensor_msgs::Image>(_topic_prefix + "depth", 5)});
_publisher.insert({"output/depth_display", _nh.advertise<sensor_msgs::Image>(_topic_prefix + "depth_display", 5)});
linkStageTransport();
}
void StageNode::createStageSurfaceGeneration()
{
_stage = std::make_shared<stages::SurfaceGeneration>(_settings_stage, (*_settings_camera)["fps"].toDouble());
_publisher.insert({"output/frame", _nh.advertise<realm_msgs::Frame>(_topic_frame_out, 5)});
_publisher.insert({"output/elevation_map", _nh.advertise<sensor_msgs::Image>(_topic_prefix + "elevation_map", 5)});
linkStageTransport();
}
void StageNode::createStageOrthoRectification()
{
_stage = std::make_shared<stages::OrthoRectification>(_settings_stage, (*_settings_camera)["fps"].toDouble());
_publisher.insert({"output/frame", _nh.advertise<realm_msgs::Frame>(_topic_frame_out, 5)});
_publisher.insert({"output/rectified", _nh.advertise<sensor_msgs::Image>(_topic_prefix + "rectified", 5)});
_publisher.insert({"output/pointcloud", _nh.advertise<sensor_msgs::PointCloud2>(_topic_prefix + "pointcloud", 5)});
linkStageTransport();
}
void StageNode::createStageMosaicing()
{
_stage = std::make_shared<stages::Mosaicing>(_settings_stage, (*_settings_camera)["fps"].toDouble());
_publisher.insert({"output/rgb", _nh.advertise<sensor_msgs::Image>(_topic_prefix + "rgb", 5)});
_publisher.insert({"output/elevation", _nh.advertise<sensor_msgs::Image>(_topic_prefix + "elevation", 5)});
_publisher.insert({"output/pointcloud", _nh.advertise<sensor_msgs::PointCloud2>(_topic_prefix + "pointcloud", 5)});
_publisher.insert({"output/mesh", _nh.advertise<visualization_msgs::Marker>(_topic_prefix + "mesh", 5)});
_publisher.insert({"output/update/ortho", _nh.advertise<realm_msgs::GroundImageCompressed>(_topic_prefix + "update/ortho", 5)});
//_publisher.insert({"output/update/elevation", _nh.advertise<realm_msgs::GroundImageCompressed>(_topic_prefix + "update/elevation", 5)});
linkStageTransport();
}
void StageNode::createStageTileing()
{
_stage = std::make_shared<stages::Tileing>(_settings_stage, (*_settings_camera)["fps"].toDouble());
//_publisher.insert({"output/rgb", _nh.advertise<sensor_msgs::Image>(_topic_prefix + "rgb", 5)});
//_publisher.insert({"output/elevation", _nh.advertise<sensor_msgs::Image>(_topic_prefix + "elevation", 5)});
//_publisher.insert({"output/pointcloud", _nh.advertise<sensor_msgs::PointCloud2>(_topic_prefix + "pointcloud", 5)});
//_publisher.insert({"output/mesh", _nh.advertise<visualization_msgs::Marker>(_topic_prefix + "mesh", 5)});
//_publisher.insert({"output/update/ortho", _nh.advertise<realm_msgs::GroundImageCompressed>(_topic_prefix + "update/ortho", 5)});
linkStageTransport();
}
void StageNode::linkStageTransport()
{
namespace ph = std::placeholders;
auto transport_frame = std::bind(&StageNode::pubFrame, this, ph::_1, ph::_2);
auto transport_pose = std::bind(&StageNode::pubPose, this, ph::_1, ph::_2, ph::_3, ph::_4);
auto transport_pointcloud = std::bind(&StageNode::pubPointCloud, this, ph::_1, ph::_2);
auto transport_img = std::bind(&StageNode::pubImage, this, ph::_1, ph::_2);
auto transport_depth = std::bind(&StageNode::pubDepthMap, this, ph::_1, ph::_2);
auto transport_mesh = std::bind(&StageNode::pubMesh, this, ph::_1, ph::_2);
auto transport_cvgridmap = std::bind(&StageNode::pubCvGridMap, this, ph::_1, ph::_2, ph::_3, ph::_4);
_stage->registerFrameTransport(transport_frame);
_stage->registerPoseTransport(transport_pose);
_stage->registerPointCloudTransport(transport_pointcloud);
_stage->registerImageTransport(transport_img);
_stage->registerDepthMapTransport(transport_img);
_stage->registerMeshTransport(transport_mesh);
_stage->registerCvGridMapTransport(transport_cvgridmap);
}
void StageNode::reset()
{
// TODO: Currently reset of stage via service seems to not suite the node reset
_nrof_msgs_rcvd = 0;
_is_tf_base_initialized = false;
_is_tf_stage_initialized = false;
}
void StageNode::subFrame(const realm_msgs::Frame &msg)
{
ROS_INFO("STAGE_NODE [%s]: Received frame.", _type_stage.c_str());
if (msg.do_reset.data)
{
_stage->requestReset();
_publisher["output/geoimg"].publish(msg);
ROS_WARN("STAGE_NODE [%s]: Mission has triggered reset. Stage resetting...", _type_stage.c_str());
return;
}
Frame::Ptr frame = to_realm::frame(msg);
if (_is_master_stage)
{
if (!_is_tf_base_initialized)
setTfBaseFrame(frame->getGnssUtm());
}
_stage->addFrame(std::move(frame));
_nrof_msgs_rcvd++;
}
void StageNode::subImu(const sensor_msgs::Imu &msg)
{
VisualSlamIF::ImuData imu;
imu.timestamp = msg.header.stamp.sec;
imu.acceleration.x = msg.linear_acceleration.x;
imu.acceleration.y = msg.linear_acceleration.y;
imu.acceleration.z = msg.linear_acceleration.z;
imu.gyroscope.x = msg.angular_velocity.x;
imu.gyroscope.y = msg.angular_velocity.y;
imu.gyroscope.z = msg.angular_velocity.z;
reinterpret_cast<stages::PoseEstimation*>(_stage.get())->queueImuData(imu);
}
void StageNode::subOutputPath(const std_msgs::String &msg)
{
// check if output directory has changed
if (_dir_date_time != msg.data)
{
// Note: master privilege is not to create folder, but to set the name of the folder
_dir_date_time = msg.data;
if (!io::dirExists(_path_output + "/" + _dir_date_time))
io::createDir(_path_output + "/" + _dir_date_time);
_stage->initStagePath(_path_output + "/" + _dir_date_time);
// Debug info
ROS_INFO("STAGE_NODE [%s]: Received output directory, set to:\n\t%s",
_type_stage.c_str(),
(_path_output + "/" + _dir_date_time).c_str());
}
}
void StageNode::pubFrame(const Frame::Ptr &frame, const std::string &topic)
{
ros::Publisher publisher = _publisher[topic];
if (publisher.getNumSubscribers() == 0)
return;
std_msgs::Header header;
header.stamp = ros::Time::now();
header.frame_id = "utm";
realm_msgs::Frame msg = to_ros::frame(header, frame);
publisher.publish(msg);
ROS_INFO("STAGE_NODE [%s]: Published frame.", _type_stage.c_str());
}
void StageNode::pubPose(const cv::Mat &pose, uint8_t zone, char band, const std::string &topic)
{
std_msgs::Header header;
header.stamp = ros::Time::now();
// utm
geometry_msgs::PoseStamped utm_msg;
utm_msg.header = header;
utm_msg.header.frame_id = "utm";
utm_msg.pose = to_ros::pose(pose);
// wgs
geometry_msgs::PoseStamped wgs_msg;
wgs_msg.header = header;
wgs_msg.header.frame_id = "wgs";
wgs_msg.pose = to_ros::poseWgs84(pose, zone, band);
_publisher[topic + "/utm"].publish(utm_msg);
_publisher[topic + "/wgs"].publish(wgs_msg);
// trajectory
std::vector<geometry_msgs::PoseStamped>* trajectory = &_trajectories[topic];
trajectory->push_back(utm_msg);
pubTrajectory(*trajectory, topic + "/traj");
// transform
_tf_stage = to_ros::tf(pose);
if (!_is_tf_stage_initialized)
_is_tf_stage_initialized = true;
}
void StageNode::pubPointCloud(const PointCloud::Ptr &sparse_cloud, const std::string &topic)
{
ros::Publisher publisher = _publisher[topic];
if (publisher.getNumSubscribers() == 0)
return;
std_msgs::Header header;
header.frame_id = "utm";
header.stamp = ros::Time::now();
sensor_msgs::PointCloud2 msg = to_ros::pointCloud(header, sparse_cloud->data());
publisher.publish(msg);
}
void StageNode::pubDepthMap(const cv::Mat &img, const std::string &topic)
{
ros::Publisher publisher = _publisher[topic];
if (publisher.getNumSubscribers() == 0)
return;
std_msgs::Header header;
header.frame_id = "utm";
header.stamp = ros::Time::now();
sensor_msgs::Image msg;
msg = *to_ros::image(header, img).toImageMsg();
publisher.publish(msg);
}
void StageNode::pubImage(const cv::Mat &img, const std::string &topic)
{
ros::Publisher publisher = _publisher[topic];
if (publisher.getNumSubscribers() == 0)
return;
std_msgs::Header header;
header.frame_id = "utm";
header.stamp = ros::Time::now();
sensor_msgs::Image msg;
msg = *to_ros::imageDisplay(header, img).toImageMsg();
publisher.publish(msg);
}
void StageNode::pubMesh(const std::vector<Face> &faces, const std::string &topic)
{
std::unique_lock<std::mutex> lock(_mutex_do_shutdown);
ros::Publisher publisher = _publisher[topic];
if (publisher.getNumSubscribers() == 0)
return;
std_msgs::Header header;
header.frame_id = _tf_base_frame_name;
header.stamp = ros::Time::now();
visualization_msgs::Marker msg = to_ros::meshMarker(header, faces, "Global Map", 0,
visualization_msgs::Marker::TRIANGLE_LIST,
visualization_msgs::Marker::ADD, _tf_base.inverse());
publisher.publish(msg);
}
void StageNode::pubCvGridMap(const CvGridMap &map, uint8_t zone, char band, const std::string &topic)
{
ros::Publisher publisher = _publisher[topic];
if (publisher.getNumSubscribers() == 0)
return;
std_msgs::Header header;
header.frame_id = "utm";
header.stamp = ros::Time::now();
cv::Rect2d roi = map.roi();
realm::UTMPose utm;
utm.easting = roi.x + roi.width/2;
utm.northing = roi.y + roi.height/2;
utm.altitude = 0.0;
utm.zone = zone;
utm.band = band;
realm_msgs::GroundImageCompressed msg;
std::vector<std::string> layer_names = map.getAllLayerNames();
if (layer_names.size() == 1)
msg = to_ros::groundImage(header, map[layer_names[0]], utm, map.resolution());
else if (layer_names.size() == 2)
msg = to_ros::groundImage(header, map[layer_names[0]], utm, map.resolution(), map[layer_names[1]]); // TODO: nobody understands that layer to is "valid"
else
throw(std::invalid_argument("Error publishing CvGridMap: More than one layer provided!"));
publisher.publish(msg);
}
void StageNode::pubTrajectory(const std::vector<geometry_msgs::PoseStamped> &traj, const std::string &topic)
{
ros::Publisher publisher = _publisher[topic];
if (publisher.getNumSubscribers() == 0)
return;
nav_msgs::Path msg;
msg.header = traj.back().header;
msg.poses = traj;
publisher.publish(msg);
}
bool StageNode::srvFinish(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
{
ROS_INFO("STAGE_NODE [%s]: Requesting stage finishCallback...!", _type_stage.c_str());
_stage->requestFinish();
_stage->join();
res.success = 1;
res.message = "Successfully finished stage!";
ROS_INFO("STAGE_NODE [%s]: Successfully finished stage!", _type_stage.c_str());
ROS_INFO("STAGE_NODE [%s]: Shutting stage node down...", _type_stage.c_str());
std::unique_lock<std::mutex> lock(_mutex_do_shutdown);
_do_shutdown = true;
return true;
}
bool StageNode::srvStop(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
{
ROS_INFO("STAGE_NODE [%s]: Requesting stage stop...!", _type_stage.c_str());
_stage->requestStop();
res.success = 1;
res.message = "Successfully stopped stage!";
ROS_INFO("STAGE_NODE [%s]: Successfully stopped stage!", _type_stage.c_str());
return true;
}
bool StageNode::srvResume(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
{
ROS_INFO("STAGE_NODE [%s]: Requesting stage resume...!", _type_stage.c_str());
_stage->resume();
res.success = 1;
res.message = "Successfully resumed stage!";
ROS_INFO("STAGE_NODE [%s]: Successfully resumed stage!", _type_stage.c_str());
return true;
}
bool StageNode::srvReset(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
{
ROS_INFO("STAGE_NODE [%s]: Requesting stage reset...!", _type_stage.c_str());
_stage->requestReset();
res.success = 1;
res.message = "Successfully reset stage!";
ROS_INFO("STAGE_NODE [%s]: Successfully reset stage!", _type_stage.c_str());
return true;
}
bool StageNode::srvChangeParam(realm_msgs::ParameterChange::Request &req, realm_msgs::ParameterChange::Response &res)
{
ROS_INFO("STAGE_NODE [%s]: Changing stage parameter %s to value %s...", _type_stage.c_str(), req.name.c_str(), req.val.c_str());
if (_stage->changeParam(req.name, req.val))
{
ROS_INFO("STAGE_NODE [%s]: Successfully changed parameter!", _type_stage.c_str());
res.success = 1;
res.message = "Successfully changed parameter!";
return true;
}
else
{
ROS_INFO("STAGE_NODE [%s]: Failed to change parameter!", _type_stage.c_str());
res.success = 0;
res.message = "Failed to change parameter!";
return false;
}
}
void StageNode::readStageSettings()
{
// Load stage settings
ROS_INFO("STAGE_NODE [%s]: Loading stage settings from path:\n\t%s", _type_stage.c_str(), _file_settings_stage.c_str());
_settings_stage = StageSettingsFactory::load(_type_stage, _file_settings_stage);
ROS_INFO("STAGE_NODE [%s]: Detected stage type: '%s'", _type_stage.c_str(), (*_settings_stage)["type"].toString().c_str());
}
void StageNode::readParams()
{
// Read parameters from launch file
ros::NodeHandle param_nh("~");
param_nh.param("stage/type", _type_stage, std::string("uninitialised"));
param_nh.param("stage/master", _is_master_stage, false);
param_nh.param("stage/output_dir", _path_output, std::string("uninitialised"));
param_nh.param("topics/input/frame", _topic_frame_in, std::string("uninitialised"));
param_nh.param("topics/input/imu", _topic_imu_in, std::string("uninitialised"));
param_nh.param("topics/output", _topic_frame_out, std::string("uninitialised"));
param_nh.param("config/id", _id_camera, std::string("uninitialised"));
param_nh.param("config/profile", _profile, std::string("uninitialised"));
param_nh.param("config/method", _method, std::string("uninitialised"));
param_nh.param("config/opt/working_directory", _path_working_directory, std::string("uninitialised"));
param_nh.param("config/opt/output_directory", _path_output, std::string("uninitialised"));
// Set specific config file paths
if (_profile == "uninitialised")
throw(std::invalid_argument("Error: Stage settings profile must be provided in launch file."));
if (_path_working_directory != "uninitialised" && !io::dirExists(_path_working_directory))
throw(std::invalid_argument("Error: Working directory does not exist!"));
}
void StageNode::setPaths()
{
if (_path_working_directory == "uninitialised")
_path_working_directory = ros::package::getPath("realm_ros");
_path_profile = _path_working_directory + "/profiles/" + _profile;
if (_path_output == "uninitialised")
_path_output = _path_working_directory + "/output";
// Set settings filepaths
_file_settings_camera = _path_profile + "/camera/calib.yaml";
_file_settings_imu = _path_profile + "/config/imu.yaml";
_file_settings_stage = _path_profile + "/" + _type_stage + "/stage_settings.yaml";
_file_settings_method = _path_profile + "/" + _type_stage + "/method/" + _method + "_settings.yaml";
if (!io::dirExists(_path_profile))
throw(std::runtime_error("Error: Profile folder '" + _path_profile + "' was not found!"));
if (!io::dirExists(_path_output))
io::createDir(_path_output);
// Master priviliges
if (_is_master_stage)
{
// Create sub directory with timestamp
_dir_date_time = io::getDateTime();
if (!io::dirExists(_path_output + "/" + _dir_date_time))
io::createDir(_path_output + "/" + _dir_date_time);
}
}
void StageNode::setTfBaseFrame(const UTMPose &utm)
{
tf::Vector3 origin(utm.easting, utm.northing, 0.0);
tf::Quaternion q(0.0, 0.0, 0.0, 1.0);
_tf_base = tf::Transform(q, origin);
_is_tf_base_initialized = true;
geographic_msgs::GeoPoint wgs = to_ros::wgs84(utm);
std_msgs::Header header;
header.stamp = ros::Time::now();
header.frame_id = _tf_base_frame_name;
_gnss_base.header = header;
_gnss_base.latitude = wgs.latitude;
_gnss_base.longitude = wgs.longitude;
_gnss_base.altitude = wgs.altitude;
ROS_INFO("STAGE_NODE [%s]: Frame reference set at: %f, %f", _type_stage.c_str(), _gnss_base.latitude, _gnss_base.longitude);
}