multi_trajectory_visualization.cpp
Go to the documentation of this file.
00001 
00026 #include <moveit/robot_state/conversions.h>
00027 #include <tf/transform_datatypes.h>
00028 #include <pluginlib/class_list_macros.h>
00029 #include <stomp_moveit/noisy_filters/multi_trajectory_visualization.h>
00030 #include <eigen_conversions/eigen_msg.h>
00031 
00032 PLUGINLIB_EXPORT_CLASS(stomp_moveit::noisy_filters::MultiTrajectoryVisualization,stomp_moveit::noisy_filters::StompNoisyFilter);
00033 
00034 inline void eigenToPointsMsgs(const Eigen::MatrixXd& in,std::vector<geometry_msgs::Point>& out)
00035 {
00036   // resizing
00037   if(out.size()!= in.cols())
00038   {
00039     out.resize(in.cols());
00040   }
00041 
00042   // copying points
00043   for(auto t = 0u; t < in.cols(); t++)
00044   {
00045     out[t].x = in(0,t);
00046     out[t].y = in(1,t);
00047     out[t].z = in(2,t);
00048   }
00049 }
00050 
00051 inline void createToolPathMarker(const Eigen::MatrixXd& tool_line, int id, std::string frame_id,
00052                           const std_msgs::ColorRGBA& rgb,double line_width,
00053                           std::string ns,visualization_msgs::Marker& m)
00054 {
00055   m.ns = ns;
00056   m.id = id;
00057   m.header.frame_id = frame_id;
00058   m.type = m.LINE_STRIP;
00059   m.action = m.ADD;
00060   m.color = rgb;
00061   tf::poseTFToMsg(tf::Transform::getIdentity(),m.pose);
00062   m.scale.x = line_width;
00063 
00064   if(tool_line.cols() == 0)
00065   {
00066     return;
00067   }
00068 
00069   // copying points into marker
00070   eigenToPointsMsgs(tool_line,m.points);
00071 }
00072 
00073 inline void createSphereMarker(const Eigen::Vector3d& tool_point, int id, std::string frame_id,
00074                           const std_msgs::ColorRGBA& rgb,double radius,
00075                           std::string ns,visualization_msgs::Marker& m)
00076 {
00077   m.ns = ns;
00078   m.id = id;
00079   m.header.frame_id = frame_id;
00080   m.type = m.SPHERE;
00081   m.action = m.ADD;
00082   m.color = rgb;
00083   tf::poseTFToMsg(tf::Transform::getIdentity(),m.pose);
00084   m.scale.x = m.scale.y = m.scale.z= 2*radius;
00085   tf::pointEigenToMsg(tool_point,m.pose.position);
00086 
00087 }
00088 
00089 namespace stomp_moveit
00090 {
00091 namespace noisy_filters
00092 {
00093 
00094 MultiTrajectoryVisualization::MultiTrajectoryVisualization():
00095     name_("MultiTrajectoryVisualization"),
00096     line_width_(0.01),
00097     traj_total_(0)
00098 {
00099   // TODO Auto-generated constructor stub
00100 
00101 }
00102 
00103 MultiTrajectoryVisualization::~MultiTrajectoryVisualization()
00104 {
00105   // TODO Auto-generated destructor stub
00106 }
00107 
00108 
00109 bool MultiTrajectoryVisualization::initialize(moveit::core::RobotModelConstPtr robot_model_ptr,
00110                         const std::string& group_name,const XmlRpc::XmlRpcValue& config)
00111 {
00112   robot_model_ = robot_model_ptr;
00113   group_name_ = group_name;
00114 
00115   if(!configure(config))
00116   {
00117     return false;
00118   }
00119 
00120   // initializing publisher
00121   viz_pub_ = nh_.advertise<visualization_msgs::MarkerArray>(marker_topic_,1);
00122 
00123   return true;
00124 }
00125 
00126 bool MultiTrajectoryVisualization::configure(const XmlRpc::XmlRpcValue& config)
00127 {
00128 
00129   auto toColorRgb = [](XmlRpc::XmlRpcValue& p)
00130   {
00131     std_msgs::ColorRGBA rgb;
00132     rgb.r = (static_cast<int>(p[0]))/255.0;
00133     rgb.g = static_cast<int>(p[1])/255.0;
00134     rgb.b = static_cast<int>(p[2])/255.0;
00135     rgb.a = 1.0;
00136     return rgb;
00137   };
00138 
00139   // check parameter presence
00140   auto members = {"line_width","marker_array_topic","rgb","marker_namespace"};
00141   for(auto& m : members)
00142   {
00143     if(!config.hasMember(m))
00144     {
00145       ROS_ERROR("%s failed to find one or more required parameters",getName().c_str());
00146       return false;
00147     }
00148   }
00149 
00150 
00151   XmlRpc::XmlRpcValue c = config;
00152   try
00153   {
00154     line_width_ = static_cast<double>(c["line_width"]);
00155     rgb_ = toColorRgb(c["rgb"]);
00156     marker_topic_ = static_cast<std::string>(c["marker_array_topic"]);
00157     marker_namespace_ = static_cast<std::string>(c["marker_namespace"]);
00158   }
00159   catch(XmlRpc::XmlRpcException& e)
00160   {
00161     ROS_ERROR("%s failed to read one or more required parameters, %s",getName().c_str(),e.getMessage().c_str());
00162     return false;
00163   }
00164 
00165   return true;
00166 }
00167 
00168 bool MultiTrajectoryVisualization::setMotionPlanRequest(const planning_scene::PlanningSceneConstPtr& planning_scene,
00169                  const moveit_msgs::MotionPlanRequest &req,
00170                  const stomp_core::StompConfiguration &config,
00171                  moveit_msgs::MoveItErrorCodes& error_code)
00172 {
00173   using namespace moveit::core;
00174 
00175   error_code.val = error_code.SUCCESS;
00176 
00177 
00178   // initializing points array
00179   tool_traj_line_ = Eigen::MatrixXd::Zero(3,config.num_timesteps);
00180   Eigen::Vector3d tool_point;
00181 
00182   // initializing marker array
00183   traj_total_ = config.num_rollouts;
00184   tool_traj_markers_.markers.resize(traj_total_);
00185   tool_points_markers_.markers.resize(traj_total_);
00186   for(auto r = 0u; r < config.num_rollouts; r++)
00187   {
00188     createToolPathMarker(tool_traj_line_,
00189                          r+1,robot_model_->getRootLinkName(),
00190                          rgb_,line_width_,
00191                          marker_namespace_,tool_traj_markers_.markers[r]);
00192     createSphereMarker(tool_point,
00193                          r+1,robot_model_->getRootLinkName(),
00194                          rgb_,line_width_,
00195                          marker_namespace_ + "/goal",tool_points_markers_.markers[r]);
00196   }
00197 
00198 
00199   // updating state
00200   state_.reset(new RobotState(robot_model_));
00201   if(!robotStateMsgToRobotState(req.start_state,*state_,true))
00202   {
00203     ROS_ERROR("%s Failed to get current robot state from request",getName().c_str());
00204     return false;
00205   }
00206 
00207   //delete current markers
00208   visualization_msgs::MarkerArray m;
00209   viz_pub_.publish(m);
00210 
00211   return true;
00212 }
00213 
00214 bool MultiTrajectoryVisualization::filter(std::size_t start_timestep,
00215                     std::size_t num_timesteps,
00216                     int iteration_number,
00217                     int rollout_number,
00218                     Eigen::MatrixXd& parameters,
00219                     bool& filtered)
00220 {
00221 
00222   if(!state_)
00223   {
00224     ROS_ERROR("%s Robot State has not been updated",getName().c_str());
00225     return false;
00226   }
00227 
00228   if(rollout_number >= traj_total_)
00229   {
00230     // exceeded allocated rollouts
00231     ROS_WARN("%s rollout allocation was exceeded",getName().c_str());
00232     return true;
00233   }
00234 
00235   // FK on each point
00236   const moveit::core::JointModelGroup* joint_group = robot_model_->getJointModelGroup(group_name_);
00237   std::string tool_link = joint_group->getLinkModelNames().back();
00238   for(auto t = 0u; t < parameters.cols();t++)
00239   {
00240     state_->setJointGroupPositions(joint_group,parameters.col(t));
00241     Eigen::Affine3d tool_pos = state_->getFrameTransform(tool_link);
00242     tool_traj_line_(0,t) = tool_pos.translation()(0);
00243     tool_traj_line_(1,t) = tool_pos.translation()(1);
00244     tool_traj_line_(2,t) = tool_pos.translation()(2);
00245   }
00246 
00247   // storing into marker
00248   eigenToPointsMsgs(tool_traj_line_,tool_traj_markers_.markers[rollout_number].points);
00249   Eigen::Vector3d goal_tool_point = tool_traj_line_.rightCols(1);
00250   tf::pointEigenToMsg(goal_tool_point, tool_points_markers_.markers[rollout_number].pose.position);
00251 
00252   // publish after collecting all rollouts
00253   if(rollout_number == traj_total_ - 1)
00254   {
00255     viz_pub_.publish(tool_traj_markers_);
00256     viz_pub_.publish(tool_points_markers_);
00257   }
00258 
00259   return true;
00260 }
00261 
00262 } /* namespace filters */
00263 } /* namespace stomp_moveit */


stomp_moveit
Author(s): Jorge Nicho
autogenerated on Sat Jun 8 2019 19:24:01