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

part of the node's code is not executed

asked 2021-05-01 16:53:44 -0600

Eman.m gravatar image

updated 2021-05-01 21:45:17 -0600

Hi

I implemented a node to create a costmap2dROS from map server. Then use this costmap2dROS object to initialize NavfnROS object. I want to measure the run time of makePlan.

The code for creating costmap2dROS from map server works perfectly as I can see the costmap in Rviz. However, the code from the line: ROS_INFO("Start using willow_garage_costmap to experiment with navFn global planner"); does not work as I cannot see this ROS_INFO in the terminal or any ROS_INFO after it.

This is my node: ros_global_planners_experiment_node.cpp

#include <ros/ros.h>
#include <costmap_2d/costmap_2d_ros.h>
#include <navfn/navfn_ros.h>
#include<iostream>
#include <fstream>

using namespace std;

int main (int argc, char** argv)
{
  // Announce this program to the ROS master as a "node" called "ros_global_planners_experiment_node"
  ros::init(argc, argv,"ros_global_planners_experiment_node");


  //ros::NodeHandle n("~");

  tf::TransformListener tf_(ros::Duration(10));
  costmap_2d::Costmap2DROS willow_garage_costmap("willowgarage_costmap", tf_);

  ros::spin();

  ROS_INFO("Start using willow_garage_costmap to experiment with navFn global planner");

  ros::WallTime begin_, end_;

  double start_world_x; 
  double start_world_y; 
  double goal_world_x; 
  double goal_world_y;

  // input(read) file
  string path = "/home/evadro/Desktop/Eman/ros_workspace/src/ros_global_planners_experiment/src/";
  // each line of this text file contains two double representing startX, startY
  string startFileName = path + "ros_start_world_coordinates_random_scenarios.txt";
  ifstream startCoordinatesFile;
  startCoordinatesFile.open(startFileName,ios::in);
  // each line of this text file contains two double representing goalX, goalY
  string goalFileName = path + "ros_goal_world_coordinates_random_scenarios.txt";
  ifstream goalCoordinatesFile;
  goalCoordinatesFile.open(goalFileName,ios::in);

  navfn::NavfnROS navfn;
  navfn.initialize("my_navfn_planner", &willow_garage_costmap);

  int counter = 1;

  if(! startCoordinatesFile || ! goalCoordinatesFile)
  {
        ROS_INFO("Unable to open the files");
  }//end if
  else
  {
    do{
        ROS_INFO("Scenario #: %d",counter);
        // read double start_world_x, start_world_y, then extract and throw away white space
        startCoordinatesFile >> start_world_x >> start_world_y >> std::ws  ;
        // read double goal_world_x, goal_world_y, then extract and throw away white space
        goalCoordinatesFile >> goal_world_x >> goal_world_y >> std::ws  ;

        geometry_msgs::PoseStamped start;
        start.header.stamp = ros::Time::now();
        start.pose.position.x = start_world_x;
        start.pose.position.y = start_world_y;
        start.pose.position.z = 0.0;

        geometry_msgs::PoseStamped goal;
        goal.header.stamp = ros::Time::now();
        goal.pose.position.x = goal_world_x;
        goal.pose.position.y = goal_world_y;
        goal.pose.position.z = 0.0;

        std::vector<geometry_msgs::PoseStamped> plan;

        begin_ = ros::WallTime::now();
        navfn.makePlan(start,goal,plan);
        end_ = ros::WallTime::now();

        double execution_time = (end_ - begin_).toNSec() * 1e-6;
        ROS_INFO_STREAM("Exectution time (ms): " << execution_time);

        ++counter;
    }while(!startCoordinatesFile.eof() && !goalCoordinatesFile.eof());

    startCoordinatesFile.close();
    goalCoordinatesFile.close();
  }//end else

  return 0;
}//end main

This is the launch file:

<launch>
  <!-- 
  NOTE: You'll need to bring up something that publishes sensor data (see
  rosstage), something that publishes a map (see map_server), and something to
  visualize a costmap (see nav_view), to see things work.
  Also, on a real robot, you'd want to set the "use_sim_time" parameter to false, or just not set it.
  -->
  <param name="/use_sim_time" value="false"/>

  <!-- Map server -->
    <arg name="map_file" default="$(find rosbot_navigation)/maps/willowgarage-refined.yaml"/>
    <node name="map_server" pkg="map_server" type="map_server" args="$(arg map_file)" respawn="false" output="screen"/>

  <node pkg="tf" type="static_transform_publisher" name="map_to_base_link" args="0 0 0 0 0 0 /map base_link ...
(more)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2021-05-01 22:12:47 -0600

Eman.m gravatar image

It works!

The edits to correct the issue are:

1- add output="screen" to my node as @robustify suggested.

2- I moved ros::spin() to the end of my node just before return 0. Thanks @robustify, you gave me a hint.

3- I added the frame_id:

start.header.frame_id = "map";

goal.header.frame_id = "map";

edit flag offensive delete link more
0

answered 2021-05-01 19:23:06 -0600

robustify gravatar image

updated 2021-05-01 19:33:52 -0600

You're missing output="screen" in your <node> tag in the launch file for ros_global_planners_experiment_node. Not having that will suppress all ROS_INFO messages.

EDIT:

Another issue I just noticed is that you run ros::spin() before you run anything else. A ROS node blocks on ros::spin() until Ctrl-C is pressed to stop the node. So you are right, nothing is going to run anyway, even though not having output="screen" would also prevent the ROS_INFOs from showing up. Since it looks like you're just basically unit-testing navfn, I think if you just get rid of ros::spin() it should do what you want. In fact, you could even just not run this as a ROS node at all, since you don't need anything from the roscore like topic subscribers and publishers.

edit flag offensive delete link more

Comments

Thanks for your help. Could you please check the EDIT section at the end of my question, I have edited my code with your suggestions, but I still have a problem.

Eman.m gravatar image Eman.m  ( 2021-05-01 21:44:44 -0600 )edit

I think you fixed that problem by setting the frame ID in the start and goal headers.

robustify gravatar image robustify  ( 2021-05-01 23:09:26 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2021-05-01 16:53:44 -0600

Seen: 120 times

Last updated: May 01 '21