1 指令ros2 topic pub的潜规则
在yaml格式输入的指令中,其格式中有某些细节需要注意;比如,一个小小的空格就能影响,本文是作者在执行此类命令时,发现的yaml格式中的“潜规则”。
在测试小海龟运动时,用指令:
ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear:{x=5,y=5,z:0},angular:{x=0,y=0,z=3.14}}"
出现下面提示:
Failed to populate field: 'Vector3' object has no attribute 'x:5'
这是什么原因呢?是因为
2 正确的输入格式如下:
ros2 topic pub --once /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
3 对应的Python程序实现
如:我正在尝试使用 RosAria 向 Pioneer P3-AT(编辑:3-AT 或 P3-AT,两个名称都使用)发送命令。我知道 RosAria 可以工作,因为当我在终端时,我可以使用以下命令移动机器人:
import rclpy
from geometry_msgs.msg import Twist
topic = rospy.Publisher('/RosAria/cmd_vel', Twist, queue_size=10)
linear = [ 0, 0, 0 ]
angular = [ 0, 0, 0 ]
rospy.sleep(1) # waiting for subscribers
test = topic.publish(linear, angular)
又发生类似的错误提示,
Traceback (most recent call last):
File "robotControl.py", line 32, in startServer
test = topic.publish(linear, angular)
packages/geometry_msgs/msg/_Twist.py", line 71, in serialize
buff.write(_get_struct_6d().pack(_x.linear.x, _x.linear.y, _x.linear.z, _x.angular.x, _x.angular.y, _x.angular.z))
AttributeError: 'list' object has no attribute 'x'
当我使用集合或 numpy 数组时,也会出现同样的错误。如果我尝试以另一种格式(不是两个列表)发布值,则会给出不期望格式的错误。从任一列表中添加或删除条目也不能解决此错误。
以上代码请大家自己试试,看能否调通。