Python中roslibpy 获取小车坐标并打印出来
时间: 2023-05-20 16:07:20 浏览: 52
可以使用如下代码:
```python
import rospy
from roslibpy import Topic
# create a ROS node
rospy.init_node('get_car_position')
# create a topic for subscribing the car's position
car_position_topic = Topic(rospy, '/car_position', 'geometry_msgs/PointStamped')
# define a callback function to print the car's position
def print_car_position(message):
x = message['point']['x']
y = message['point']['y']
z = message['point']['z']
print('Car position: x={}, y={}, z={}'.format(x, y, z))
# subscribe to the car's position topic
car_position_topic.subscribe(print_car_position)
# spin the ROS node to keep it alive
rospy.spin()
```
此代码使用roslibpy库中的Topic类来订阅小车位置主题。订阅后,每当有位置信息时,将调用print_car_position函数打印出位置信息。