trajectory_msgs/JointTrajectory Exception :AttributeError: 'str' object has no attribute 'positions'
I wrote a python script to publish a JointTrajectory message. It results in an Exception: AttributeError: 'str' object has no attribute 'positions'.
The python script:
#!/usr/bin/env python3
import rospy
import numpy
from trajectory_msgs.msg import JointTrajectory
rospy.init_node('topic_publisher')
pub= rospy.Publisher('Command',JointTrajectory,queue_size=2)
rate = rospy.Rate(2)
msg=JointTrajectory()
#msg.points=dict()
pos=[1.0,1.1,1.2,1.3]
velo=[0.0,0.0,0.0,0.0]
time=[2.0,0.0]
msg.joint_names=["hip","shoulder","elbow","wrist"]
msg.points=dict([('positions', pos), ('velocities', velo), ('time_from_start', time)])
#msg.points={'position':pos,'velocities':velo,'time_from_start':time}
print (msg)
while not rospy.is_shutdown():
pub.publish(msg)
rate.sleep()
I added a print to show the message.
Result:
header:
seq: 0
stamp:
secs: 0
nsecs: 0
frame_id: ''
joint_names:
- hip
- shoulder
- elbow
- wrist
points: {'positions': [1.0, 1.1, 1.2, 1.3], 'velocities': [0.0, 0.0, 0.0, 0.0], 'time_from_start': [2.0, 0.0]}
Traceback (most recent call last):
File "/KubuntuDaten/myrobot_ws/src/myrobot/topic_publisher.py", line 23, in <module>
pub.publish(msg)
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 882, in publish
self.impl.publish(data)
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 1066, in publish
serialize_message(b, self.seq, message)
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/msg.py", line 152, in serialize_message
msg.serialize(b)
File "/opt/ros/noetic/lib/python3/dist-packages/trajectory_msgs/msg/_JointTrajectory.py", line 110, in serialize
length = len(val1.positions)
AttributeError: 'str' object has no attribute 'positions'
Now I am a bit confused of which type the data element points should be. Is it a list, a dict or a string? I tried several variations of the message format, always with the same result.
ROS noetic runs on Ubuntu focal.
This is my testcoding:
import rospy import numpy from trajectory_msgs.msg import JointTrajectory from trajectory_msgs.msg import JointTrajectoryPoint
rospy.init_node('topic_publisher')
pub= rospy.Publisher('arm_controller/command',JointTrajectory,queue_size=2) rate = rospy.Rate(30)
r0=0.0 r1=0.5 r3=0.0 r4=0.0 t=20.0 msg=JointTrajectory() point=JointTrajectoryPoint() point.positions=[r0,r1,r3,r4] point.velocities=[0.0,0.0,0.0,0.0] point.time_from_start=rospy.Duration(t) msg.joint_names=["hip","shoulder","elbow","wrist"] msg.points.append(point) while not rospy.is_shutdown():
pub.publish(msg) if point.positions[0] >= 2numpy.pi : point.positions[0] = 0.0 point.positions[0]+=0.78542
rate.sleep()