Why does node not run when called from LAUNCH file?
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?