MATLAB Answers

0

Robotics System Toolbox: How to create a new publisher for a topic for an existing node?

Asked by James Ang on 21 Apr 2015
Latest activity Commented on by James Ang on 22 Apr 2015
Hi all,
I've some codes below to connect to gazebo (turtlebot);
>> rosinit('http://192.168.2.87:11311')
Initializing global node /matlab_global_node_77319 with NodeURI http://192.168.2.126:62581/
>> rosnode list
/gazebo
/laserscan_nodelet_manager
/matlab_global_node_77319
/mobile_base_nodelet_manager
/robot_state_publisher
/rosout
>> rostopic list
/camera/depth/camera_info
/camera/depth/image_raw
/camera/depth/points
/camera/parameter_descriptions
/camera/parameter_updates
/camera/rgb/camera_info
/camera/rgb/image_raw
/camera/rgb/image_raw/compressed
/camera/rgb/image_raw/compressed/parameter_descriptions
/camera/rgb/image_raw/compressed/parameter_updates
/clock
/gazebo/link_states
/gazebo/model_states
/gazebo/parameter_descriptions
/gazebo/parameter_updates
/gazebo/set_link_state
/gazebo/set_model_state
/joint_states
/mobile_base/commands/motor_power
/mobile_base/commands/reset_odometry
/mobile_base/commands/velocity
/mobile_base/events/bumper
/mobile_base/events/cliff
/mobile_base/sensors/imu_data
/odom
/rosout
/rosout_agg
/tf
>> rosnode info /matlab_global_node_77319
Node: [/matlab_global_node_77319]
URI: [http://192.168.2.126:62581/]
Publications (1 Active Topics):
* /rosout
Subscriptions (1 Active Topics):
* /clock
When I tried to create a publisher for a topic as below (using ‘rospublisher’), it works:
>> robot = rospublisher('/mobile_base/commands/velocity')
robot =
Publisher with properties:
TopicName: '/mobile_base/commands/velocity'
IsLatching: 1
NumSubscribers: 0
MessageType: 'geometry_msgs/Twist'
As shown below, the topic is added to the publishers list:
>> rosnode info /matlab_global_node_77319
Node: [/matlab_global_node_77319]
URI: [http://192.168.2.126:62581/]
Publications (3 Active Topics):
* /mobile_base/commands/velocity
* /odom
* /rosout
Subscriptions (1 Active Topics):
* /clock
Services (0 Active):
Now, I add a new node:
>> node_1 = robotics.ros.Node('node_1', 'http://192.168.2.87:11311');
>> rosnode info node_1
Node: [/node_1]
URI: [http://192.168.2.126:62966/]
Publications (1 Active Topics):
* /rosout
Subscriptions (1 Active Topics):
* /clock
Services (0 Active):
And add the same topic to publish but using another code (robotics.ros.publisher). It works too, as shown below:
>> robotics.ros.Publisher(node_1,'/mobile_base/commands/velocity');
>> rosnode info node_1
Node: [/node_1]
URI: [http://192.168.2.126:62966/]
Publications (2 Active Topics):
* /mobile_base/commands/velocity
* /rosout
Subscriptions (1 Active Topics):
* /clock
Services (0 Active):
However, now when I tried to use (robotics.ros.publisher) on the first node (/matlab_global_node_77319), it doesn’t work:
>> tPub = robotics.ros.Publisher(matlab_global_node_77319,'/mobile_base/commands/motor_power');
Undefined function or variable
'matlab_global_node_77319'.
>> tPub = robotics.ros.Publisher(matlab_global_node_77319,'/mobile_base/commands/motor_power','kobuki_msgs/MotorPower');
Undefined function or variable
'matlab_global_node_77319'.
>> tPub = robotics.ros.Publisher(/matlab_global_node_77319,'/mobile_base/commands/motor_power','kobuki_msgs/MotorPower');
tPub = robotics.ros.Publisher(/matlab_global_node_77319,'/mobile_base/commands/motor_power','kobuki_msgs/MotorPower');
|
Error: Unexpected MATLAB operator.
However, if I used the same code on the new node, and publish a new topic, it works:
>> robotics.ros.Publisher(node_1,'/mobile_base/commands/motor_power')
ans =
Publisher with properties:
TopicName: '/mobile_base/commands/motor...'
IsLatching: 1
NumSubscribers: 1
MessageType: 'kobuki_msgs/MotorPower'
>> rosnode info /node_1
Node: [/node_1]
URI: [http://192.168.2.126:62966/]
Publications (2 Active Topics):
* /mobile_base/commands/motor_power
* /rosout
Subscriptions (1 Active Topics):
* /clock
Services (0 Active):
How do I publish a topic for a node that already exist?

  0 Comments

Sign in to comment.

1 Answer

Answer by Sebastian Castro on 21 Apr 2015
 Accepted Answer

I don't believe that the "official" toolbox syntax is meant to play together with the "robotics.ros.Node" stuff that's hidden away in the background.
If you start a ROS master using rosinit , it doesn't create a robotics.ros.Node object in the base workspace. This is where your questions comes in, in that you can't find an object with that name.
That being said, if you use the rospublisher function, this publisher will by default be associated with the /matlab_global_node_#### node that was created.
So, it seems that if you create a node using rosinit , you must create the publisher using rospublisher . If you create a node using robotics.ros.Node , you must create the publisher using robotics.ros.Publisher .
- Sebastian

  1 Comment

I see...thanks Sebastian. This is helpful. Appreciate it.

Sign in to comment.