ROS 编写服务请求(client)程序

2 第一次发文章,有问题希望大家友善指出,谢谢大家:)!!!!! 

  1. 创建一个Python的文件(name:Turtle_client_method.py)路径:功能包名/scripts/Method
    #1.导包
    import rospy
    import math
    from turtlesim.srv import *
    from std_srvs.srv import *
    from geometry_msgs.msg import Twist
    
    # 1.创造小乌龟函数
    def create_Turtle(x,y,z,name,client):
        client = rospy.ServiceProxy("/spawn",Spawn)
        # 等待服务启动
        client.wait_for_service()
        # 定义服务响应
        response = SpawnResponse()
        # 定义服务请求
        req = SpawnRequest()
        # 设置服务参数
        req.x = x
        req.y = y
        req.theta = (z/180)*math.pi # req.theta = z
        req.name = name
    
        try:
            response = client.call(req) # 调用服务
            rospy.loginfo("创建成功: x=%.2f y=%.2f z=%.2f %s",x,y,(z/math.pi)*180,response)
        except Exception as e:
            # 调用失败,抛出异常
            rospy.loginfo("服务调用失败....")
            # rospy.loginfo("%s",response)
    
    # 2.清除小乌龟函数 
    def clear_Turtle():
        clear_client = rospy.ServiceProxy("/clear",Empty)
        clear_client.wait_for_service()
        try:
            clear_client.call()
            rospy.loginfo("清除轨迹服务调用成功")
        except Exception as e:
            rospy.loginfo("服务调用失败")
    # 3.重置小乌龟函数 
    def reset_Turtle():
        reset_client = rospy.ServiceProxy("/reset",Empty)
        reset_client.wait_for_service()
        try:
            reset_client.call()
            rospy.loginfo("重置服务调用成功")
        except Exception as e:
            rospy.loginfo("重置服务调用失败")
    
    # 4.杀死小乌龟函数 
    def kill_Turtle(name,client):
        # 创建服务客户端
        client=rospy.ServiceProxy("/kill",Kill)
        # 等待服务启动
        client.wait_for_service()
        response = KillRequest()
        # 定义服务请求
        req = KillRequest()
        req.name=name
        
        try:
            client.call(req)
            rospy.loginfo("杀死成功: name:%s",name)
        except Exception as e:
            rospy.loginfo("7.杀死服务调用失败....")
    # 5.移动参数函数
    def doMove(x,z,name):
    	str="/"+name+"/cmd_vel"
    	pub=rospy.Publisher(str,Twist,queue_size=10)
    	rate=rospy.Rate(1)
    	msg=Twist()
    	rate.sleep()
    	msg.linear.x =x
    	msg.angular.z =math.pi*(z/180)
    	pub.publish(msg)
    
    # 6.改变海龟运行轨迹的颜色和宽度
    def set_pen_Turtle(r,g,b,width,off,name):
        str ="/"+name+"/set_pen"
        client = rospy.ServiceProxy(str,SetPen)
        # 等待服务启动
        client.wait_for_service()
        # 定义服务响应
        response = SetPenResponse()
        # 定义服务请求
        req = SetPenRequest()
        # 设置服务参数
        req.r = r
        req.g = g
        req.b = b
        req.width = width
        req.off =off
    
        try:
            response = client.call(req) # 调用服务
            rospy.loginfo("修改成功: r=%d g=%d b=%d width=%d off=%d",r,g,b,width,off)
            # rospy.loginfo("%s",response)
        except Exception as e:
            # 调用失败,抛出异常
            rospy.loginfo("修改服务调用失败")
    
    # 7.改变海龟位置
    def Absolute_Turtle(x,y,theta,name):
        str ="/"+name+"/teleport_absolute"
        # client = rospy.ServiceProxy("/turtle1/teleport_absolute",TeleportAbsolute)
        client = rospy.ServiceProxy(str,TeleportAbsolute)
        # 等待服务启动
        client.wait_for_service()
        # 定义服务响应
        response = TeleportAbsoluteResponse()
        # 定义服务请求
        req = TeleportAbsoluteRequest()
        # 设置服务参数
        req.x = x
        req.y = y
        req.theta = (theta/180)*math.pi # req.theta = theta
    
        try:
            response = client.call(req) # 调用服务
            rospy.loginfo("%s 位置修改成功: x=%.2f y=%.2f theta=%.2f度",name,x,y,theta)
            # rospy.loginfo("%d",response)
        except Exception as e:
            # 调用失败,抛出异常
            rospy.loginfo("修改服务调用失败")
    
    # 8.移动海龟到指定坐标
    def Request_Turtle(linear,angular,name):
        str ="/"+name+"/teleport_relative"
        client = rospy.ServiceProxy(str,TeleportRelative)
        # 等待服务启动
        client.wait_for_service()
        # 定义服务响应
        response = TeleportRelativeResponse()
        # 定义服务请求
        req = TeleportRelativeRequest()
        # 设置服务参数
        req.linear = linear
        req.angular = angular
       
    
        try:
            response = client.call(req) # 调用服务
            rospy.loginfo("%s 速度成功: rlinear=%.2f angular=%.2f ",name,linear,angular)
            # rospy.loginfo("%d",response)
        except Exception as e:
            # 调用失败,抛出异常
            rospy.loginfo("修改服务调用失败")
  2. 创建第二个Python文件(name:Draw_face.py)路径:功能包名/scripts/Method
    import ntpath
    from numpy import rate
    import rospy
    import math
    from turtlesim.srv import *
    from std_srvs.srv import *
    from geometry_msgs.msg import Twist
    
    import Method.Turtle_client_method as Turtle_client_method
    
    def draw_face_smile():
        Turtle_client_method.reset_Turtle()
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(6,2,0,"turtle1")
        Turtle_client_method.set_pen_Turtle(255,0,0,10,"turtle1",0)
        Turtle_client_method.doMove(6*math.pi,360,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(4,6,90,"turtle1")
        Turtle_client_method.set_pen_Turtle(255,255,0,10,"turtle1",0)
        Turtle_client_method.doMove(1*math.pi,-360,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(7.75,6,90,"turtle1")
        Turtle_client_method.set_pen_Turtle(255,255,0,10,"turtle1",0)
        Turtle_client_method.doMove(1*math.pi,360,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(4,3.5,-45,"turtle1")
        Turtle_client_method.set_pen_Turtle(0,255,255,15,"turtle1",0)
        Turtle_client_method.doMove(4,90,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
    def draw_face_cry():
        Turtle_client_method.reset_Turtle()
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(6,2,0,"turtle1")
        Turtle_client_method.set_pen_Turtle(255,0,0,10,"turtle1",0)
        Turtle_client_method.doMove(6*math.pi,360,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(4,6,90,"turtle1")
        Turtle_client_method.set_pen_Turtle(255,255,0,10,"turtle1",0)
        Turtle_client_method.doMove(1*math.pi,-360,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(7.75,6,90,"turtle1")
        Turtle_client_method.set_pen_Turtle(255,255,0,10,"turtle1",0)
        Turtle_client_method.doMove(1*math.pi,360,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(4.5,3.5,45,"turtle1")
        Turtle_client_method.set_pen_Turtle(0,255,255,15,"turtle1",0)
        Turtle_client_method.doMove(3,-90,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
    def draw_face_zdy():
        Turtle_client_method.reset_Turtle()
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(6,2,0,"turtle1")
        Turtle_client_method.set_pen_Turtle(255,0,0,10,"turtle1",0)
        Turtle_client_method.doMove(6*math.pi,360,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(4,6,90,"turtle1")
        Turtle_client_method.set_pen_Turtle(255,255,0,10,"turtle1",0)
        Turtle_client_method.doMove(1*math.pi,-360,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(7.75,6,90,"turtle1")
        Turtle_client_method.set_pen_Turtle(255,255,0,10,"turtle1",0)
        Turtle_client_method.doMove(1*math.pi,360,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(4.5,3.5,0,"turtle1")
        Turtle_client_method.set_pen_Turtle(0,255,255,15,"turtle1",0)
        Turtle_client_method.doMove(2.5,0,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
    
    def draw_face_taiji():
        Turtle_client_method.reset_Turtle()
        Turtle_client_method.kill_Turtle("turtle1")
    
    
        # Turtle_client_method.create_Turtle(5,5,90,"turtle1")
        # Turtle_client_method.set_pen_Turtle(255,255,255,100,"turtle1",0)
        # Turtle_client_method.doMove(1*math.pi,-180,"turtle1")
        # Turtle_client_method.doMove(1*math.pi,-180,"turtle1")
        # Turtle_client_method.kill_Turtle("turtle1")
    
        Turtle_client_method.create_Turtle(4,5,90,"turtle1")
        Turtle_client_method.set_pen_Turtle(0,0,0,20,"turtle1",0)
        Turtle_client_method.doMove(0.5*math.pi,-150,"turtle1")
        # Turtle_client_method.doMove(1*math.pi,-180,"turtle1")
        Turtle_client_method.kill_Turtle("turtle1")
  3. 创建第三个Python文件(name:create_test01_p.py)路径:功能包名/scripts
    #1.导包
    import ntpath
    from numpy import rate
    import rospy
    import math
    
    from turtlesim.srv import *
    from std_srvs.srv import *
    from geometry_msgs.msg import Twist
    
    import Method.Turtle_client_method as Turtle_client_method
    
    import Method.Draw_face as Draw_face
    
    
    if __name__ == "__main__":
        # 2.初始化 ros 节点
        rospy.init_node("create_face_p")
        # Turtle_client_method.kill_Turtle("turtle1")
    
        pub=rospy.Publisher("/turtle1/cmd_vel",Twist,queue_size=10)
        rate =rospy.Rate(1)
        
        # Draw_face.draw_face_smile()
        # Draw_face.draw_face_zdy()
        Draw_face.draw_face_cry()
        # Draw_face.draw_face_taiji()
    
  4. 配置CMakeLists
    catkin_install_python(PROGRAMS
      scripts/draw01_test_ser_p.py
      scripts/create_face01_p.py
      scripts/create_face02_cry.py
      
      DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
    )
  5. 运行效果
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值