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


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