Skip to content

Commit 805ed50

Browse files
committed
Added auto initialization if cached poses are used.
Deactivated marker server clearing due to RVIZ bug passing invalid quaternions to OGRE 1.9 causing it to crash (Opened issue ticket ros-visualization/rviz#1185)
1 parent 4eb9337 commit 805ed50

File tree

2 files changed

+94
-25
lines changed

2 files changed

+94
-25
lines changed

source/dbot_ros/util/interactive_marker_initializer.cpp

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,39 @@ InteractiveMarkerInitializer::InteractiveMarkerInitializer(
3232
const std::string& object_directory,
3333
const std::vector<std::string>& object_names,
3434
const std::vector<geometry_msgs::Pose>& poses,
35-
bool load_from_cache)
36-
: server_("interactive_marker_initializer"),
35+
bool load_from_cache,
36+
bool accept_cached_poses)
37+
: load_from_cache_(load_from_cache),
38+
accept_cached_poses_(accept_cached_poses),
39+
server_(std::make_shared<interactive_markers::InteractiveMarkerServer>(
40+
"interactive_marker_initializer")),
3741
camera_frame_id_(camera_frame_id)
3842
{
39-
set_objects(
40-
object_package, object_directory, object_names, poses, load_from_cache);
43+
if (load_from_cache_ && accept_cached_poses_)
44+
{
45+
ROS_INFO(
46+
"Skipping interactive marker initializer. Using cached poses.");
47+
poses_.resize(object_names.size());
48+
for (size_t i = 0; i < object_names.size(); ++i)
49+
{
50+
std::string name = std::to_string(i);
51+
load_pose_from_cache(name, poses_[i]);
52+
}
53+
}
54+
else
55+
{
56+
set_objects(object_package,
57+
object_directory,
58+
object_names,
59+
poses,
60+
load_from_cache);
61+
}
4162
}
4263

4364
InteractiveMarkerInitializer::InteractiveMarkerInitializer(
4465
const std::string& camera_frame_id)
45-
: server_("interactive_marker_initializer"),
66+
: server_(std::make_shared<interactive_markers::InteractiveMarkerServer>(
67+
"interactive_marker_initializer")),
4668
camera_frame_id_(camera_frame_id)
4769
{
4870
}
@@ -71,10 +93,9 @@ void InteractiveMarkerInitializer::set_objects(
7193
bool load_from_cache,
7294
bool make_active)
7395
{
74-
poses_ = poses;
75-
load_from_cache_ = load_from_cache;
96+
poses_ = poses;
7697

77-
server_.clear();
98+
server_->clear();
7899
poses_.resize(object_names.size());
79100

80101
// activate all markers
@@ -84,13 +105,14 @@ void InteractiveMarkerInitializer::set_objects(
84105
active_[i] = make_active;
85106
}
86107

87-
// of poses provided they have to match the number of objects
108+
// If poses provided they have to match the number of objects
88109
if (poses.size() > 0 && object_names.size() != poses.size())
89110
{
90111
ROS_ERROR("#object names != #poses");
91112
exit(1);
92113
}
93114

115+
94116
for (size_t i = 0; i < object_names.size(); i++)
95117
{
96118
// create an interactive marker for our server
@@ -116,16 +138,17 @@ void InteractiveMarkerInitializer::set_objects(
116138
switch_marker(int_marker, make_active);
117139

118140
// add the interactive marker to our collection &
119-
// tell the server to call process_feedback() when feedback arrives for
141+
// tell the server to call process_feedback() when feedback arrives
142+
// for
120143
// it
121-
server_.insert(
144+
server_->insert(
122145
int_marker,
123146
boost::bind(
124147
&InteractiveMarkerInitializer::process_feedback, this, _1));
125148
}
126149

127150
// 'commit' changes and send to all clients
128-
server_.applyChanges();
151+
server_->applyChanges();
129152
}
130153

131154
void InteractiveMarkerInitializer::poses_update_callback(Callback callback)
@@ -138,8 +161,14 @@ void InteractiveMarkerInitializer::delete_poses_update_callback()
138161
poses_update_callback_ = Callback();
139162
}
140163

141-
bool InteractiveMarkerInitializer::are_all_object_poses_set()
164+
bool InteractiveMarkerInitializer::are_all_object_poses_set(
165+
bool accept_cached_poses)
142166
{
167+
if (accept_cached_poses && load_from_cache_)
168+
{
169+
return true;
170+
}
171+
143172
for (bool still_active : active_)
144173
{
145174
if (still_active) return false;
@@ -151,10 +180,11 @@ bool InteractiveMarkerInitializer::are_all_object_poses_set()
151180
bool InteractiveMarkerInitializer::wait_for_object_poses()
152181
{
153182
ROS_INFO(
154-
"Please use rviz to align and initialize the object poses under the "
183+
"Please use rviz to align and initialize the object poses under "
184+
"the "
155185
"topic.");
156186
ROS_INFO("Waiting for all interactive object poses to be set ...");
157-
while (!are_all_object_poses_set())
187+
while (!are_all_object_poses_set(accept_cached_poses_))
158188
{
159189
if (!ros::ok()) return false;
160190

@@ -193,9 +223,13 @@ void InteractiveMarkerInitializer::create_interactive_marker(
193223
int_marker.scale = 0.2;
194224
// position interactive marker by default 1 m in font
195225
// of camera in viewing condition
196-
int_marker.pose.position.x = 0.0;
197-
int_marker.pose.position.y = 0.0;
198-
int_marker.pose.position.z = 1.0;
226+
int_marker.pose.position.x = 0.0;
227+
int_marker.pose.position.y = 0.0;
228+
int_marker.pose.position.z = 1.0;
229+
int_marker.pose.orientation.w = 1;
230+
int_marker.pose.orientation.x = 0;
231+
int_marker.pose.orientation.y = 0;
232+
int_marker.pose.orientation.z = 0;
199233

200234
// load pose from cache if available
201235
if (load_from_cache_)
@@ -228,13 +262,18 @@ void InteractiveMarkerInitializer::add_object_controller(
228262
object_marker.color.g = 0.5;
229263
object_marker.color.b = 0.5;
230264
object_marker.color.a = 1.0;
265+
object_marker.pose.orientation.w = 1;
266+
object_marker.pose.orientation.x = 0;
267+
object_marker.pose.orientation.y = 0;
268+
object_marker.pose.orientation.z = 0;
231269

232270
// create a non-interactive control which contains the object
233271
visualization_msgs::InteractiveMarkerControl object_control;
234272
object_control.interaction_mode =
235273
visualization_msgs::InteractiveMarkerControl::BUTTON;
236274
object_control.name = "button_control";
237275
object_control.always_visible = true;
276+
object_control.orientation.w = 1;
238277
object_control.markers.push_back(object_marker);
239278

240279
// add the control to the interactive marker
@@ -295,7 +334,8 @@ void InteractiveMarkerInitializer::process_feedback(
295334

296335
ROS_INFO("Marker Frame %s", feedback->header.frame_id.c_str());
297336

298-
// if the feedback from the marker is in a different than the desired frame
337+
// if the feedback from the marker is in a different than the desired
338+
// frame
299339
// convert it using a transform listener
300340
if (feedback->header.frame_id.compare(camera_frame_id_) != 0)
301341
{
@@ -321,7 +361,7 @@ void InteractiveMarkerInitializer::process_feedback(
321361
}
322362

323363
visualization_msgs::InteractiveMarker int_marker;
324-
server_.get(feedback->marker_name, int_marker);
364+
server_->get(feedback->marker_name, int_marker);
325365
if (feedback->event_type ==
326366
visualization_msgs::InteractiveMarkerFeedback::BUTTON_CLICK)
327367
{
@@ -343,9 +383,20 @@ void InteractiveMarkerInitializer::process_feedback(
343383
}
344384

345385
// update marker
386+
346387
int_marker.header.frame_id = feedback->header.frame_id;
347-
server_.insert(int_marker);
348-
server_.applyChanges();
388+
389+
int_marker.controls.front().orientation.w = 1;
390+
int_marker.pose.orientation.w = 1;
391+
int_marker.pose.orientation.x = 0;
392+
int_marker.pose.orientation.y = 0;
393+
int_marker.pose.orientation.z = 0;
394+
395+
// apply no changes this will cause rviz to crash (with ogre 1.9)
396+
// server_->insert(int_marker);
397+
// server_->applyChanges();
398+
// same happens when destroying the marker server
399+
// server_.reset();
349400
}
350401
}
351402

@@ -380,6 +431,7 @@ void InteractiveMarkerInitializer::load_pose_from_cache(
380431
ROS_INFO("Loading interactive marker pose %s from ",
381432
cache_file.c_str());
382433

434+
383435
pose_tmp_file >> pose.position.x;
384436
pose_tmp_file >> pose.position.y;
385437
pose_tmp_file >> pose.position.z;
@@ -389,6 +441,18 @@ void InteractiveMarkerInitializer::load_pose_from_cache(
389441
pose_tmp_file >> pose.orientation.z;
390442
pose_tmp_file.close();
391443
}
444+
else
445+
{
446+
ROS_WARN_STREAM("No cached pose for object " << object_name);
447+
}
448+
449+
double length = std::sqrt(
450+
std::pow(pose.orientation.w, 2) + std::pow(pose.orientation.x, 2) +
451+
std::pow(pose.orientation.y, 2) + std::pow(pose.orientation.z, 2));
452+
pose.orientation.w /= length;
453+
pose.orientation.x /= length;
454+
pose.orientation.y /= length;
455+
pose.orientation.z /= length;
392456
}
393457

394458
void InteractiveMarkerInitializer::cache_pose(

source/dbot_ros/util/interactive_marker_initializer.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <geometry_msgs/Pose.h>
2525
#include <geometry_msgs/PoseArray.h>
2626
#include <interactive_markers/interactive_marker_server.h>
27+
#include <memory>
2728
#include <mutex>
2829
#include <ros/ros.h>
2930
#include <tf/transform_listener.h>
@@ -44,7 +45,8 @@ class InteractiveMarkerInitializer
4445
const std::string& object_directory,
4546
const std::vector<std::string>& object_names,
4647
const std::vector<geometry_msgs::Pose>& poses,
47-
bool load_from_cache);
48+
bool load_from_cache,
49+
bool accept_cached_poses = false);
4850

4951
/**
5052
*
@@ -90,8 +92,10 @@ class InteractiveMarkerInitializer
9092

9193
/**
9294
* \brief Checks wheather all object poses have been aligned
95+
* \param accept_cached_poses If set to true and cached poses where loaded,
96+
* this will return true.
9397
*/
94-
bool are_all_object_poses_set();
98+
bool are_all_object_poses_set(bool accept_cached_poses = false);
9599

96100
/**
97101
* \brief A call to this fucntion will block until all object poses have
@@ -174,6 +178,7 @@ class InteractiveMarkerInitializer
174178
std::vector<bool> active_;
175179

176180
bool load_from_cache_;
181+
bool accept_cached_poses_;
177182

178183
/**
179184
* \brief Poses container of all interactive markers
@@ -195,7 +200,7 @@ class InteractiveMarkerInitializer
195200
/**
196201
* \brief Interactive marker backend
197202
*/
198-
interactive_markers::InteractiveMarkerServer server_;
203+
std::shared_ptr<interactive_markers::InteractiveMarkerServer> server_;
199204

200205
/**
201206
* \brief Camera reference frame for the markers

0 commit comments

Comments
 (0)