面向对象Python实现
小乌龟控制
1. 创建publisher
# 创建publisher
topicName = "/turtle1/cmd_vel"
self.publisher = rospy.Publisher(topicName, Twist, queue_size=1000)
2. 发布控制消息
# 创建消息
twist = Twist()
# 填充数据
twist.linear.x = linearX
twist.angular.z = angluarZ * math.pi / 180
# 发送消息
publisher.publish(twist)
小乌龟数据订阅
1. 创建subscriber
poseTopicName = "/turtle1/pose"
rospy.Subscriber(poseTopicName, Pose, self.poseCallback)
2. 添加订阅回调
def poseCallback(self, msg):
if not isinstance(msg, Pose):
return
self.labelX.setText(str(msg.x))
self.labelY.setText(str(msg.y))
self.labelLinear.setText(str(msg.linear_velocity))
self.labelAngular.setText(str(msg.angular_velocity))
self.labelDegrees.setText(str(degrees(msg.theta)))