ros获取乌龟的运动轨迹(python)

使用launch文件启动多个文件然后通过订阅乌龟的消息,实时的展示乌龟目前的运动轨迹,坐标、朝向、向速、角度,如果乌龟当前朝向是负数那么就停止收集,然后重新挂起获取参数的节点,的在获取乌龟的背景底色的数据实时展示rgb数值,

实现思路先写一个控制乌龟运动的节点,再写一个用来接收变化的节点展示目前的乌龟的参数

第一步创建一个ros的包,简单的  详细的

然后编写一个控制节点

#! /usr/bin/env python
# coding=utf8


import rospy
from geometry_msgs.msg import Twist
if __name__ == "__main__":
    rospy.init_node("my_control_p")
    # 乌龟运动的节点
    pub = rospy.Publisher("/turtle1/cmd_vel",Twist,queue_size=10)
    rata = rospy.Rate(100)
    twist = Twist()
    twist.linear.x = 0.5
    twist.linear.y = 0.5
    twist.linear.z = 0.0
    
    twist.angular.x = 0.0
    twist.angular.y = 0.0
    twist.angular.z = 1
    
    while not rospy.is_shutdown():
        print("this ok")
        pub.publish(twist)
        rata.sleep()
    
    

接收的

#! /usr/bin/env python 
# coding=utf8

import rospy
from std_msgs.msg import String
from turtlesim.msg import Pose
def Domessage(pose):
        rgb =  rospy.get_param("/turtlesim")
        print("Red:{},Green{},blue{}".format(rgb['background_r'],rgb['background_g'],rgb['background_b']))
        # dict1 = rospy.get_param("gui")
        # print(dict1)
        # print(" 目前坐标{:.2f} ,{:.2f},朝向:{:.2f},向速:{:.2f}角度:{:.2f}".format(dict1['x'],dict1['y'],dict1['theta'],dict1["linear_velocity"],dict1['angular_velocity']))
        print(" 目前坐标{:.2f} ,{:.2f},朝向:{:.2f},向速:{:.2f}角度:{:.2f}".format(pose.x,pose.y,pose.theta,pose.linear_velocity,pose.angular_velocity))
        if pose.theta < 0 :
            print("-----")
            rospy.signal_shutdown("get_set_parmas")
            # 关闭 当前 get_set_parmas 节点
            
        # pose.x,pose.y,pose.theta,pose.linear_velocity,pose.angular_velocity)
        
    
if __name__ == "__main__":
    rospy.init_node("get_set_parmas")
    sub = rospy.Subscriber("/turtle1/pose",Pose,Domessage,queue_size=100)
    rospy.spin()

编写完之后通过launch文件进行启动通过观察命令行展示出来的信息即可

<launch>
<!-- name是节点名字 pkg是ros包名(自己创建的) type 是启动的文件  output用于输出/打印 至命令行-->
    <node name="set_message" pkg="one_project" type="move_get.py" />
    <!-- <node name="get_message" pkg="one_project" type="ome.py" output="screen"/> -->
    <!-- respawn ="true" 使僵死的节点重新挂起  -->
    <node name="get_set_message" pkg="one_project" type="get_set_param.py" output="screen" respawn="true"/>

</launch>

效果:

 清楚的看到一些乌龟的运动信息当然也可以不编写自动画圆的文件起一个手动的也行

使用命令  rosrun turtlesim turtle_teleop_key  键盘录入信息然后实时展示信息如

这样就能简单的实现一个订阅乌龟运动轨迹的一个小功能

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清夢懮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值