I am trying to articulate ur5 robot by subscribing to joint_states topic. I don’t want to use articulation controller to move the robot. Is there anyway to readout the values from ROS2 joint state subscriber node using python code?
Hi @yadun.murali.kurikalveed - Yes, you can subscribe to the joint_states
topic in ROS2 using Python. Here is a basic example of how you can do this:
import rclpy
from sensor_msgs.msg import JointState
def callback(msg):
# This function will be called every time a new message is published on the joint_states topic
print('Received joint state:', msg)
def main():
rclpy.init()
node = rclpy.create_node('joint_state_listener')
subscription = node.create_subscription(
JointState,
'joint_states',
callback,
10 # Queue size
)
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
In this code, callback
is a function that will be called every time a new JointState
message is published on the joint_states
topic. The JointState
message contains information about the positions, velocities, and efforts of the joints.
Please note that you’ll need to have the sensor_msgs
package installed to use the JointState
message. You can install it with sudo apt install ros-<distro>-sensor-msgs
, replacing <distro>
with your ROS2 distribution name (e.g., foxy
).
Also, remember to source your ROS2 environment before running the script, like so: source /opt/ros/<distro>/setup.bash
.