multi_trajectory_visualization.cpp
Go to the documentation of this file.
1 
27 #include <tf/transform_datatypes.h>
31 
33 
34 inline void eigenToPointsMsgs(const Eigen::MatrixXd& in,std::vector<geometry_msgs::Point>& out)
35 {
36  // resizing
37  if(out.size()!= in.cols())
38  {
39  out.resize(in.cols());
40  }
41 
42  // copying points
43  for(auto t = 0u; t < in.cols(); t++)
44  {
45  out[t].x = in(0,t);
46  out[t].y = in(1,t);
47  out[t].z = in(2,t);
48  }
49 }
50 
51 inline void createToolPathMarker(const Eigen::MatrixXd& tool_line, int id, std::string frame_id,
52  const std_msgs::ColorRGBA& rgb,double line_width,
53  std::string ns,visualization_msgs::Marker& m)
54 {
55  m.ns = ns;
56  m.id = id;
57  m.header.frame_id = frame_id;
58  m.type = m.LINE_STRIP;
59  m.action = m.ADD;
60  m.color = rgb;
62  m.scale.x = line_width;
63 
64  if(tool_line.cols() == 0)
65  {
66  return;
67  }
68 
69  // copying points into marker
70  eigenToPointsMsgs(tool_line,m.points);
71 }
72 
73 inline void createSphereMarker(const Eigen::Vector3d& tool_point, int id, std::string frame_id,
74  const std_msgs::ColorRGBA& rgb,double radius,
75  std::string ns,visualization_msgs::Marker& m)
76 {
77  m.ns = ns;
78  m.id = id;
79  m.header.frame_id = frame_id;
80  m.type = m.SPHERE;
81  m.action = m.ADD;
82  m.color = rgb;
84  m.scale.x = m.scale.y = m.scale.z= 2*radius;
85  tf::pointEigenToMsg(tool_point,m.pose.position);
86 
87 }
88 
89 namespace stomp_moveit
90 {
91 namespace noisy_filters
92 {
93 
94 MultiTrajectoryVisualization::MultiTrajectoryVisualization():
95  name_("MultiTrajectoryVisualization"),
96  line_width_(0.01),
97  traj_total_(0)
98 {
99  // TODO Auto-generated constructor stub
100 
101 }
102 
103 MultiTrajectoryVisualization::~MultiTrajectoryVisualization()
104 {
105  // TODO Auto-generated destructor stub
106 }
107 
108 
109 bool MultiTrajectoryVisualization::initialize(moveit::core::RobotModelConstPtr robot_model_ptr,
110  const std::string& group_name,const XmlRpc::XmlRpcValue& config)
111 {
112  robot_model_ = robot_model_ptr;
113  group_name_ = group_name;
114 
115  if(!configure(config))
116  {
117  return false;
118  }
119 
120  // initializing publisher
121  viz_pub_ = nh_.advertise<visualization_msgs::MarkerArray>(marker_topic_,1);
122 
123  return true;
124 }
125 
126 bool MultiTrajectoryVisualization::configure(const XmlRpc::XmlRpcValue& config)
127 {
128 
129  auto toColorRgb = [](XmlRpc::XmlRpcValue& p)
130  {
131  std_msgs::ColorRGBA rgb;
132  rgb.r = (static_cast<int>(p[0]))/255.0;
133  rgb.g = static_cast<int>(p[1])/255.0;
134  rgb.b = static_cast<int>(p[2])/255.0;
135  rgb.a = 1.0;
136  return rgb;
137  };
138 
139  // check parameter presence
140  auto members = {"line_width","marker_array_topic","rgb","marker_namespace"};
141  for(auto& m : members)
142  {
143  if(!config.hasMember(m))
144  {
145  ROS_ERROR("%s failed to find one or more required parameters",getName().c_str());
146  return false;
147  }
148  }
149 
150 
151  XmlRpc::XmlRpcValue c = config;
152  try
153  {
154  line_width_ = static_cast<double>(c["line_width"]);
155  rgb_ = toColorRgb(c["rgb"]);
156  marker_topic_ = static_cast<std::string>(c["marker_array_topic"]);
157  marker_namespace_ = static_cast<std::string>(c["marker_namespace"]);
158  }
159  catch(XmlRpc::XmlRpcException& e)
160  {
161  ROS_ERROR("%s failed to read one or more required parameters, %s",getName().c_str(),e.getMessage().c_str());
162  return false;
163  }
164 
165  return true;
166 }
167 
168 bool MultiTrajectoryVisualization::setMotionPlanRequest(const planning_scene::PlanningSceneConstPtr& planning_scene,
169  const moveit_msgs::MotionPlanRequest &req,
170  const stomp_core::StompConfiguration &config,
171  moveit_msgs::MoveItErrorCodes& error_code)
172 {
173  using namespace moveit::core;
174 
175  error_code.val = error_code.SUCCESS;
176 
177 
178  // initializing points array
179  tool_traj_line_ = Eigen::MatrixXd::Zero(3,config.num_timesteps);
180  Eigen::Vector3d tool_point;
181 
182  // initializing marker array
183  traj_total_ = config.num_rollouts;
184  tool_traj_markers_.markers.resize(traj_total_);
185  tool_points_markers_.markers.resize(traj_total_);
186  for(auto r = 0u; r < config.num_rollouts; r++)
187  {
188  createToolPathMarker(tool_traj_line_,
189  r+1,robot_model_->getRootLinkName(),
190  rgb_,line_width_,
191  marker_namespace_,tool_traj_markers_.markers[r]);
192  createSphereMarker(tool_point,
193  r+1,robot_model_->getRootLinkName(),
194  rgb_,line_width_,
195  marker_namespace_ + "/goal",tool_points_markers_.markers[r]);
196  }
197 
198 
199  // updating state
200  state_.reset(new RobotState(robot_model_));
201  if(!robotStateMsgToRobotState(req.start_state,*state_,true))
202  {
203  ROS_ERROR("%s Failed to get current robot state from request",getName().c_str());
204  return false;
205  }
206 
207  //delete current markers
208  visualization_msgs::MarkerArray m;
209  viz_pub_.publish(m);
210 
211  return true;
212 }
213 
214 bool MultiTrajectoryVisualization::filter(std::size_t start_timestep,
215  std::size_t num_timesteps,
216  int iteration_number,
217  int rollout_number,
218  Eigen::MatrixXd& parameters,
219  bool& filtered)
220 {
221 
222  if(!state_)
223  {
224  ROS_ERROR("%s Robot State has not been updated",getName().c_str());
225  return false;
226  }
227 
228  if(rollout_number >= traj_total_)
229  {
230  // exceeded allocated rollouts
231  ROS_WARN("%s rollout allocation was exceeded",getName().c_str());
232  return true;
233  }
234 
235  // FK on each point
236  const moveit::core::JointModelGroup* joint_group = robot_model_->getJointModelGroup(group_name_);
237  std::string tool_link = joint_group->getLinkModelNames().back();
238  for(auto t = 0u; t < parameters.cols();t++)
239  {
240  state_->setJointGroupPositions(joint_group,parameters.col(t));
241  Eigen::Affine3d tool_pos = state_->getFrameTransform(tool_link);
242  tool_traj_line_(0,t) = tool_pos.translation()(0);
243  tool_traj_line_(1,t) = tool_pos.translation()(1);
244  tool_traj_line_(2,t) = tool_pos.translation()(2);
245  }
246 
247  // storing into marker
248  eigenToPointsMsgs(tool_traj_line_,tool_traj_markers_.markers[rollout_number].points);
249  Eigen::Vector3d goal_tool_point = tool_traj_line_.rightCols(1);
250  tf::pointEigenToMsg(goal_tool_point, tool_points_markers_.markers[rollout_number].pose.position);
251 
252  // publish after collecting all rollouts
253  if(rollout_number == traj_total_ - 1)
254  {
255  viz_pub_.publish(tool_traj_markers_);
256  viz_pub_.publish(tool_points_markers_);
257  }
258 
259  return true;
260 }
261 
262 } /* namespace filters */
263 } /* namespace stomp_moveit */
const std::string & getMessage() const
Interface class for filtering noisy trajectories.
const std::vector< std::string > & getLinkModelNames() const
Publishes rviz markers to visualize the noisy trajectories.
std::string getName(void *handle)
#define ROS_WARN(...)
PLUGINLIB_EXPORT_CLASS(test_moveit_controller_manager::TestMoveItControllerManager, moveit_controller_manager::MoveItControllerManager)
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
void pointEigenToMsg(const Eigen::Vector3d &e, geometry_msgs::Point &m)
bool hasMember(const std::string &name) const
static void poseTFToMsg(const Pose &bt, geometry_msgs::Pose &msg)
static const Transform & getIdentity()
This defines a multi trajectory visualizer for publishing the noisy trajectories. ...
r
#define ROS_ERROR(...)


stomp_moveit
Author(s): Jorge Nicho
autogenerated on Fri May 8 2020 03:35:47