ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Subscribing to nav_msgs::Path message

asked 2018-11-19 13:28:37 -0600

kllysin gravatar image

updated 2018-11-19 13:30:47 -0600

Hi everyone,

I am trying to subscribe to a message of type nav_msgs::Path. Following is the callback function:

    nav_msgs::Path gPlan;
    void planCallback(nav_msgs::Path::ConstPtr& msg)
    {
         int i=0;
          for(std::vector<geometry_msgs::PoseStamped>::const_iterator it= msg->poses.begin(); it!= msg->poses.end();  ++it)
          {

              gPlan.poses[i] = *it;
              i++;
           }
      }

The subscriber has been defined as:

ros::Subscriber planSub = n.subscribe("global_plan", 1000, planCallback);

The error I am getting is as follows:

In file included from /opt/ros/indigo/include/ros/serialization.h:37:0,
                 from /opt/ros/indigo/include/ros/publisher.h:34,
                 from /opt/ros/indigo/include/ros/node_handle.h:32,
                 from /opt/ros/indigo/include/ros/ros.h:45,
                 from /home/lead/catkin_ws_autoware/src/PPpublisher/src/purepursuitPub.cpp:1:
/opt/ros/indigo/include/ros/message_traits.h: In instantiation of ‘static const char* ros::message_traits::MD5Sum<M>::value() [with M = boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >]’:
/opt/ros/indigo/include/ros/message_traits.h:228:103:   required from ‘const char* ros::message_traits::md5sum() [with M = boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >]’
/opt/ros/indigo/include/ros/subscribe_options.h:89:50:   required from ‘void ros::SubscribeOptions::initByFullCallbackType(const string&, uint32_t, const boost::function<void(P)>&, const boost::function<boost::shared_ptr<typename ros::ParameterAdapter<P>::Message>()>&) [with P = boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >&; std::string = std::basic_string<char>; uint32_t = unsigned int; typename ros::ParameterAdapter<P>::Message = boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >]’
/opt/ros/indigo/include/ros/node_handle.h:658:5:   required from ‘ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (*)(M), const ros::TransportHints&) [with M = boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >&; std::string = std::basic_string<char>; uint32_t = unsigned int]’
/home/lead/catkin_ws_autoware/src/PPpublisher/src/purepursuitPub.cpp:71:74:   required from here
/opt/ros/indigo/include/ros/message_traits.h:121:29: error: ‘__s_getMD5Sum’ is not a member of ‘boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >’
     return M::__s_getMD5Sum().c_str();
                             ^
/opt/ros/indigo/include/ros/message_traits.h: In instantiation of ‘static const char* ros::message_traits::DataType<M>::value() [with M = boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >]’:
/opt/ros/indigo/include/ros/message_traits.h:237:105:   required from ‘const char* ros::message_traits::datatype() [with M = boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >]’
/opt/ros/indigo/include/ros/subscribe_options.h:90:54:   required from ‘void ros::SubscribeOptions::initByFullCallbackType(const string&, uint32_t, const boost::function<void(P)>&, const boost::function<boost::shared_ptr<typename ros::ParameterAdapter<P>::Message>()>&) [with P = boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >&; std::string = std::basic_string<char>; uint32_t = unsigned int; typename ros::ParameterAdapter<P>::Message = boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >]’
/opt/ros/indigo/include/ros/node_handle.h:658:5:   required from ‘ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (*)(M), const ros::TransportHints&) [with M = boost::shared_ptr<const nav_msgs::Path_<std::allocator<void> > >&; std::string = std::basic_string<char>; uint32_t = unsigned int]’
/home/lead/catkin_ws_autoware/src/PPpublisher/src/purepursuitPub.cpp:71:74:   required from here
/opt/ros/indigo ...
(more)
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2018-11-19 13:47:40 -0600

If you just want to create a copy of the path message, then the copy constructors mean you can do this:

gPlan = nav_msgs::Path(msg);

Hope this helps.

edit flag offensive delete link more

Comments

Thanks for your answer!

kllysin gravatar image kllysin  ( 2018-11-19 14:47:01 -0600 )edit

Thanks, @PeteBlackerThe3rd, it worked like a charm!

MichelleHusbands gravatar image MichelleHusbands  ( 2020-11-12 18:36:29 -0600 )edit
0

answered 2018-11-19 14:47:47 -0600

kllysin gravatar image

Adding the solution I figured out. I changed the callback function to:

void planCallback(const nav_msgs::Path::ConstPtr& msg) { int i=0; std::vector<geometry_msgs::posestamped> data = msg->poses; for(std::vector<geometry_msgs::posestamped>::const_iterator it= data.begin(); it!= data.end(); ++it) { //gPlan.poses.header.stamp = gPlan.poses[i].header = it->header; gPlan.poses[i].pose = it->pose; i++; } }

and it works.

edit flag offensive delete link more

Comments

This may work some of the time. But you're doing something really bad here You're writing data into gPlan.poses beyond what's allocated, risking a segfault. You should use the push_back() method or pre-allocate it first.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-11-19 16:26:19 -0600 )edit

@kllysin, could you please copy your code again in a correct format? Because, now its not able to read properly. I am also trying something like you. I need to take the position info of X and Y from the nav_msgs/Path. Please help me . I have information about two waypoints in the topic named "cmd_local_waypoints" I need to take the x and y positions (two x and y s) out.

Thanks in advance

manuelmelvin gravatar image manuelmelvin  ( 2019-03-22 18:38:17 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-11-19 13:28:37 -0600

Seen: 3,232 times

Last updated: Nov 19 '18