How to publish a nav_msgs::Path

asked 2022-05-28 07:00:27 -0500

iopoi97 gravatar image

Hello to everyone. I'm implementing an APF motion planner ant I would the position setpoints generated online to be saved in a nav_msgs::Path msg that I called _planned_path. At each loop i would the new setpoint in position to be added in the path msg. Every time I try to run I get a segmentation fault error. This is my code implementation:

void NAVIGATION::planner(){
   bool moved=false;
   bool rotated=false;
   ros::Rate plan_r(100);
   ros::Rate r_wait(10);
   _planned_path.header.stamp = ros::Time::now();
   _planned_path.header.frame_id = "robot_footprint";

      _planned_path.poses[_contatore_path].header.stamp = ros::Time::now();
      _planned_path.poses[_contatore_path].header.frame_id = "robot_footprint"; 
      _planned_path.poses[_contatore_path].pose.position.x=(long double)_sp_xy[0];
      _planned_path.poses[_contatore_path].pose.position.y=(long double)_sp_xy[1];
      _planned_path.poses[_contatore_path].pose.position.z = 0.0;

How may I solve this problem? _contatore_path is a class member variable I set to 0 at the start of the code.

edit retag flag offensive close merge delete


you are increasing _contatore_path indefinitely without resetting it, what is the size of _planned_path.poses, I won't recommend while loops for accessing elemnt of a list or vector.

Fetullah Atas gravatar image Fetullah Atas  ( 2022-05-28 08:53:17 -0500 )edit

hello and thanks for the comment. For what I understood the nav_msgs::Path contains a vector of geometry::msgs::PoseStamped that is Poses. If i want to push_back separately poses.position.x and then poses.position.y how can I do?

iopoi97 gravatar image iopoi97  ( 2022-05-28 12:52:51 -0500 )edit

You can create a PoseStamped msg with desired x and y values and then push back that PoseStamped into your_nav_msg.poses.pushback(stamped_pose)

Fetullah Atas gravatar image Fetullah Atas  ( 2022-05-28 21:02:17 -0500 )edit

Hello, thanks. It's like the pushback function wants the entire kind of structure that has to be added. Now I'm in it. I solved my problem thanks to your suggestions!!

iopoi97 gravatar image iopoi97  ( 2022-05-31 04:29:20 -0500 )edit