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

how can i write a node to subscribe to the “joint 0”?

asked 2017-04-27 07:28:26 -0600

buaawanggg gravatar image

I used a 6-DOF Arms of a Mobile Robot
in the topic "joint_state" three are messages of joint0 joint1 joint2.。。。joint6 how can I write a node subscribe to joint 0; only “ joint 0”

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-04-27 09:44:43 -0600

updated 2017-04-27 11:06:59 -0600

You have a single topic called /joint_states that has, presumbably, sensor_msgs/JointState messages being published on it. I'm unsure if you are trying to say that a single message published on the topic contains information on all 6 joints (i.e. the name field would have 6 entries in it, which is pretty common), or if you are saying that every 6th message published on the topic is for joint0. Either way, as far as I know, it is not possible to filter topics at the subscriber level like you are interested in doing. I can think of two easy ways of working around this: (i) pulling out just the information for joint0 in your callback (if there is no joint0 info, just exit the callback), or (ii) writing an intermediate node that subscribes to /joint_states, filters out the joint0 information, and then publishes just that info on a separate topic.


Here's a simple example in Python to illustrate the concept:

#!/usr/bin/env python
import rospy
from sensor_msgs.msg import JointState
from std_msgs.msg import Float64

def joint_states_callback(message):
    # filter out joint0 position:
    for i,name in enumerate(
        if name == "joint0":
            pos = message.position[i]

if __name__ == '__main__':
    pub = rospy.Publisher("joint0_topic", Float64, queue_size=1)
    rospy.Subscriber("joint_states", JointState, joint_states_callback, queue_size=1)
edit flag offensive delete link more


sorry i am not quite understand what “every 6th message published on the topic is for joint0” mean but i think what i want to express one message contains information all 6 joints

buaawanggg gravatar image buaawanggg  ( 2017-04-27 10:05:33 -0600 )edit

how i can write in a node to filters out the joint0 information ? only joint0,i want to only publish the position message of joint0 to another topic thx!

buaawanggg gravatar image buaawanggg  ( 2017-04-27 10:06:06 -0600 )edit

I added a simple example. Hopefully that helps.

jarvisschultz gravatar image jarvisschultz  ( 2017-04-27 11:07:58 -0600 )edit


buaawanggg gravatar image buaawanggg  ( 2017-04-27 11:13:06 -0600 )edit

thank you for your help! i try to write this with C++ but there are some questions ,can you give me some advice in my latest question,thx a lot!

buaawanggg gravatar image buaawanggg  ( 2017-05-03 20:21:56 -0600 )edit

Question Tools



Asked: 2017-04-27 07:28:26 -0600

Seen: 1,228 times

Last updated: Apr 27 '17