ros turtlesim 控制小乌龟

孤街浪徒 提交于 2021-01-14 06:08:35

 

原文链接: ros turtlesim 控制小乌龟

运行小乌龟

roscore

rosrun turtlesim turtlesim_node


rosrun turtlesim turtle_teleop_key

 

可以使用方向键控制乌龟,上下代表前后移动,左右调整方向

 

 

节点详细信息

rosnode info /turtlesim


Node [/turtlesim]
Publications: 
 * /rosout [rosgraph_msgs/Log]
 * /turtle1/color_sensor [turtlesim/Color]
 * /turtle1/pose [turtlesim/Pose]

Subscriptions: 
 * /turtle1/cmd_vel [geometry_msgs/Twist]

Services: 
 * /clear
 * /kill
 * /reset
 * /spawn
 * /turtle1/set_pen
 * /turtle1/teleport_absolute
 * /turtle1/teleport_relative
 * /turtlesim/get_loggers
 * /turtlesim/set_logger_level


contacting node http://ace-VirtualBox:35435/ ...
Pid: 3624
Connections:
 * topic: /rosout
    * to: /rosout
    * direction: outbound
    * transport: TCPROS
 * topic: /turtle1/cmd_vel
    * to: /teleop_turtle (http://ace-VirtualBox:42445/)
    * direction: inbound
    * transport: TCPROS

 

图形化消息订阅情况

rosrun rqt_graph rqt_graph 

查看所有topic

rostopic list

 

查看速度变化消息,分别为前后左右

rostopic echo /turtle1/cmd_vel

 

命令行发布消息,以一秒钟十次的频率发送消息,小乌龟会自己画圆

rostopic pub -r 10  /turtle1/cmd_vel geometry_msgs/Twist "linear: 
  x: 2.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 2.0
"

 

 

服务列表

rosservice list

 

查看服务

rosservice info /spawn

 

调用服务,创建一只新乌龟,注意冒号之后有一个空格然后才是数据,否则报错!!!!!!!!!!

rosservice call /spawn "
x: 10.0
y: 20.0
theta: 0.0
name: 'ace3'
"

 

 

Python 控制小乌龟,不用在指定目录下创建文件,随意写就行

服务参数为Empty,所以直接调用

即可,不用传递一个Empty的对象

# coding:utf-8

from geometry_msgs.msg import Twist, Vector3
from std_srvs.srv import Empty

print(Empty)
import rospy


def talker():
    pub = rospy.Publisher("turtle1/cmd_vel", Twist, queue_size=10)
    rospy.init_node("move", anonymous=True)
    rate = rospy.Rate(10)
    rospy.wait_for_service("clear")
    client = rospy.ServiceProxy("clear", Empty)
    cnt = 0
    while not rospy.is_shutdown():

        speed = Vector3(2, 0, 0)
        angular = Vector3(0, 0, 2)
        twist = Twist(speed, angular)
        pub.publish(twist)
        # print(twist)

        if not cnt % 20:
            print('clear')
            resp = client.call()

        rate.sleep()
        cnt += 1


if __name__ == "__main__":
    talker()

小乌龟会画圆,过一段时间会将轨迹清空

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!