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

Publish Joint_State with Python to RVIZ

asked 2015-07-07 15:18:10 -0600

kevlarkevlar gravatar image

I have a robot that I built in URDF that I am visualizing in RVIZ. I can control the joints using the gui tool or a rostopic pub command, but I am unable to build a publisher node to interface with the rviz visualization.

The rostopic pub works fine so I tried to build a ros package that would publish the joint information to the rviz visualization. "Rostopic echo /joint_states" lists the newly published command, but the rviz model does not move.

I am able to use the following command to move the robot using a terminal:

rostopic pub -1 /joint_states sensor_msgs/JointState '{header: auto, name: ['joint0', 'joint1', 'joint2', 'joint3'], position: [1, 0.5418, -1.7297, -3.1017], velocity: [], effort: []}'

Here is the code I am trying to build to publish the same information. I built it into a package that I am running:

#!/usr/bin/env python

import rospy
from sensor_msgs.msg import JointState
from std_msgs.msg import Header

def talker():
    pub = rospy.Publisher('joint_states', JointState, queue_size=10)
    rospy.init_node('joint_state_publisher')
    rate = rospy.Rate(10) # 10hz
    hello_str = JointState()
    hello_str.header = Header()
    hello_str.header.stamp = rospy.Time.now()
    hello_str.name = ['joint0', 'joint1', 'joint2', 'joint3']
    hello_str.position = [3, 0.5418, -1.7297, -3.1017]
    hello_str.velocity = []
    hello_str.effort = []
    pub.publish(hello_str)
    rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

After I run the package to move the robot, rostopic list shows the new state, but the robot has not moved in RVIZ.

edit retag flag offensive close merge delete

Comments

I can control the joints using the gui tool [..]

Which tool are you referring to here? rqt_publisher, joint_state_publisher or something else?

gvdhoorn gravatar image gvdhoorn  ( 2015-07-08 04:01:53 -0600 )edit

he is referring to joint_state_publisher_gui, http://wiki.ros.org/joint_state_publi...

umar_anjum gravatar image umar_anjum  ( 2022-01-27 12:10:58 -0600 )edit

That package didn't exist in 2015.

gvdhoorn gravatar image gvdhoorn  ( 2022-01-28 05:04:58 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
3

answered 2015-07-08 04:07:56 -0600

gvdhoorn gravatar image

updated 2015-07-08 04:09:36 -0600

def talker():
    pub = rospy.Publisher('joint_states', JointState, queue_size=10)
    rospy.init_node('joint_state_publisher')
    rate = rospy.Rate(10) # 10hz
    hello_str = JointState()
    [..]
    pub.publish(hello_str)
    rate.sleep()

What do expect this does? You have no loop construct in there, and the rate is set to sleep for 0.1 seconds only. I can imagine that is not enough time for ROS to be able to successfully distribute your message to the rest of the system (especially not the first message). The talker() function will exit (and take all things in its scope with it) after a very short time.

Even for testing (so with static data), I'd recommend something like:

def talker():
    pub = rospy.Publisher('joint_states', JointState, queue_size=10)
    rospy.init_node('joint_state_publisher')
    rate = rospy.Rate(10) # 10hz
    hello_str = JointState()
    [..]

    while not rospy.is_shutdown():
      hello_str.header.stamp = rospy.Time.now()
      pub.publish(hello_str)
      rate.sleep()
edit flag offensive delete link more

Comments

Thanks, that worked! I had never worked with static data publishing.

kevlarkevlar gravatar image kevlarkevlar  ( 2015-07-08 11:16:30 -0600 )edit
0

answered 2015-07-08 07:40:28 -0600

anonymous user

Anonymous

I always go back to the tutorials.. http://wiki.ros.org/ROS/Tutorials/Wri...

edit flag offensive delete link more

Comments

Thanks! That's where I actually got the original code that I was using.

kevlarkevlar gravatar image kevlarkevlar  ( 2015-07-08 11:17:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-07-07 15:18:10 -0600

Seen: 11,963 times

Last updated: Jul 08 '15