Ask Your Question
0

Why does node not run when called from LAUNCH file?

asked 2018-10-31 06:53:06 -0600

kump gravatar image

updated 2018-10-31 06:55:15 -0600

I have defined a node that publishes to a topic. When I run the node with the rosrun command in the terminal, the node launches and publishes to the topic. However if I start the node in LAUNCH file, the node never starts.

robot_gazebo/launch/simulation.launch file:

<?xml version="1.0"?>
<launch>
    <arg name="sensor" default="NOT_DEFINED"/>

    <arg name="paused" default="false"/>
    <arg name="use_sim_time" default="true"/>
    <arg name="gui" default="true"/>
    <arg name="headless" default="false"/>
    <arg name="debug" default="false"/>
    <arg name="world_name" default="worlds/empty.world"/>

    <!-- launch empty_world.launch -->
    <include file="$(find gazebo_ros)/launch/empty_world.launch" >
        <arg name="world_name" value="$(arg world_name)"/>
        <arg name="debug" value="$(arg debug)" />
        <arg name="gui" value="$(arg gui)" />
        <arg name="paused" value="$(arg paused)"/>
        <arg name="use_sim_time" value="$(arg use_sim_time)"/>
        <arg name="headless" value="$(arg headless)"/>
    </include>

    <!-- Load the URDF into the ROS Parameter Server -->
        <param name="robot_description"
            command="$(find xacro)/xacro '$(find robot_gazebo)/launch/upload_robot.xacro' sensor:=$(arg sensor)" />

    <!-- Run a python script to the send a service call to gazebo_ros to spawn a URDF robot -->
        <node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
            args="-urdf -model robot -param robot_description -x 0 -y 0 -z 0.2"/>
</launch>

robot_gazebo/launch/upload_robot.xacro file

<?xml version="1.0"?>
<robot name="robot" xmlns:xacro="http://www.ros.org/wiki/xacro">

    <xacro:property name="sensor_var" value="$(arg sensor)"/>

    <xacro:unless value="${ sensor_var == 'NOT_DEFINED' }">
        <!--  This includes the sensor -->
        <xacro:include 
        filename="$(find robot_description)/urdf/sensors/$(arg sensor).urdf.xacro" />
        <node name="$(arg sensor)_node"  pkg="robot_sensors" type="$(arg sensor)_exe" /> 

    </xacro:unless>


</robot>

When I launch the LAUNCH file and input argument sensor:=my_sensor, the sensor model is loaded into the world, so the unless block does get executed.

robot_sensors/src/my_sensor.cpp file

#include "ros/ros.h"
#include "robot_sensors/MyMsg.h"

class MySensor {
public:

  MySensor(ros::NodeHandle* nodehandle);
  void myPublisher();

private:
  ros::NodeHandle nh_;
  ros::Publisher pubMySensor_;
  std::vector<ros::Subscriber> sub_;

};

MySensor::MySensor(ros::NodeHandle* nodehandle) :
  nh_(*nodehandle) {

    pubMySensor_ = nh_.advertise<robot_sensors::MyMsg>("my_topic", 10);

}
  robot_sensors::MyMsg sensorMsg;

  sensorMsg = 0;

  pubMySensor_.publish(sensorMsg);
}

int main(int argc, char **argv) {

  ros::init(argc, argv, "my_sensor_node");

  ros::NodeHandle node;
  MySensor my_sensor(&node);

  ros::Rate loop_rate(30);

  while (ros::ok()) {

    my_sensor.myPublisher();
    ros::spinOnce();
    loop_rate.sleep();
  }

  return 0;
}

robot_sensors/CMakeLists.txt file

cmake_minimum_required(VERSION 2.8.12)
project(robot_sensors)

find_package(catkin REQUIRED COMPONENTS
  gazebo_ros
  roscpp
  rospy
  message_generation
  std_msgs
  tf2
  tf2_ros
  tf2_geometry_msgs
)

add_message_files(
  FILES
    MyMsg.msg
)

# Generates msg header files
generate_messages(
  DEPENDENCIES
    std_msgs
)

catkin_package(
  DEPENDS
     gazebo_ros
  CATKIN_DEPENDS
    message_generation
    message_runtime
    std_msgs
    LIBRARIES ${PROJECT_NAME}
)

# Depend on system install of Gazebo
find_package(gazebo REQUIRED)

link_directories(${GAZEBO_LIBRARY_DIRS})

include_directories(
  ${catkin_INCLUDE_DIRS}
  ${GAZEBO_INCLUDE_DIRS}
)

include_directories( ${catkin_INCLUDE_DIRS} ${GAZEBO_INCLUDE_DIRS} )

add_executable(my_sensor_exe src/my_sensor.cpp)
target_link_libraries(my_sensor_exe ${catkin_LIBRARIES})

### INSTALL ###
install(TARGETS my_sensor_exe
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

and I have

<run_depend>robot_sensors</run_depend>

in the robot_gazebo pkg's package.xml file.

Can anybody see any problem? Why is the node not launching?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-10-31 07:48:24 -0600

dhindhimathai gravatar image

I guess the problem is due to the fact that you are trying to run the node in upload_robot.xacro file.

Try to remove the following line from upload_robot.xacro and put it in simulation.launch.

<node name="$(arg sensor)_node"  pkg="robot_sensors" type="$(arg sensor)_exe" />

If you need to run the node only if argument sensor is defined, use an if statement in the launch.

edit flag offensive delete link more
0

answered 2018-10-31 07:50:44 -0600

gvdhoorn gravatar image

Where in your launch file do you start my_sensor_exe?

A run_depend only encodes a dependency, it does not express an executable relationship (ie: it doesn't make roslaunch start your node).

You'll need to add a node element to your launch file.

<node name="$(arg sensor)_node"  pkg="robot_sensors" type="$(arg sensor)_exe" />

node is not a valid element in a xacro file. You can only start nodes in .launch files.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-10-31 06:53:06 -0600

Seen: 54 times

Last updated: Oct 31 '18