ROS Noetic入门笔记(十)使用Python编写简单的Server与Client

11 篇文章 19 订阅
7 篇文章 1 订阅

ROS Noetic入门笔记(一)在ubuntu20.04中安装ROS Noetic并简单测试
ROS Noetic入门笔记(二)ROS Noetic创建工作空间和功能包
ROS Noetic入门笔记(三)ROS Noetic中的常用命令及工具
ROS Noetic入门笔记(四)使用rqt_console和roslaunch
ROS Noetic入门笔记(五)创建ROS话题消息(msg)和服务数据(srv
ROS Noetic入门笔记(六)使用C++编写简单的Publisher与Suscriber
ROS Noetic入门笔记(七)使用Python编写简单的Publisher与Suscriber
ROS Noetic入门笔记(八)运行Publisher与Suscriber
ROS Noetic入门笔记(九)使用C++编写简单的Server与Client
ROS Noetic入门笔记(十)使用Python编写简单的Server与Client
ROS Noetic入门笔记(十一)使用rosbag实现数据记录与回放

1.使用roscd跳转到beginner_tutorials功能包目录

先设置环境变量

$ cd ~/catkin_ws
$ source ./devel/setup.bash

然后roscd到beginner_tutorials功能包

$ roscd beginner_tutorials

roscd

2.创建Server

(1) 在scripts目录下新建add_two_ints_server.py文件并粘贴以下代码

#!/usr/bin/env python

from __future__ import print_function

from beginner_tutorials.srv import AddTwoInts,AddTwoIntsResponse
import rospy

def handle_add_two_ints(req):
    print("Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b)))
    return AddTwoIntsResponse(req.a + req.b)

def add_two_ints_server():
    rospy.init_node('add_two_ints_server')
    s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
    print("Ready to add two ints.")
    rospy.spin()

if __name__ == "__main__":
    add_two_ints_server()

(2) 给add_two_ints_server.py可执行权限

$ chmod +x add_two_ints_server.py

给.py可执行权限

3.创建Client

(1) 在scripts目录下新建add_two_ints_client.py文件并粘贴以下代码

#!/usr/bin/env python

from __future__ import print_function

import sys
import rospy
from beginner_tutorials.srv import *

def add_two_ints_client(x, y):
    rospy.wait_for_service('add_two_ints')
    try:
        add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
        resp1 = add_two_ints(x, y)
        return resp1.sum
    except rospy.ServiceException as e:
        print("Service call failed: %s"%e)

def usage():
    return "%s [x y]"%sys.argv[0]

if __name__ == "__main__":
    if len(sys.argv) == 3:
        x = int(sys.argv[1])
        y = int(sys.argv[2])
    else:
        print(usage())
        sys.exit(1)
    print("Requesting %s+%s"%(x, y))
    print("%s + %s = %s"%(x, y, add_two_ints_client(x, y)))

(2) 给add_two_ints_client.py可执行权限

$ chmod +x add_two_ints_server.py

4.运行Server与Client

运行Server与Client
上一篇:ROS Noetic入门笔记(九)使用C++编写简单的Server与Client
下一篇:ROS Noetic入门笔记(十一)使用rosbag实现数据记录与回放

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于ROS Noetic的move_base软件包,它提供了许多Python API接口用于与move_base节点进行通信和控制。以下是一些常用的Python API接口: 1. SimpleActionClient:用于与move_base的行动服务器进行通信。它提供了一组函数来发送目标位置、取消当前目标、获取当前状态等。 ```python import rospy import actionlib from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal rospy.init_node('move_base_client') client = actionlib.SimpleActionClient('move_base', MoveBaseAction) client.wait_for_server() goal = MoveBaseGoal() goal.target_pose.header.frame_id = 'map' goal.target_pose.pose.position.x = 1.0 goal.target_pose.pose.position.y = 2.0 goal.target_pose.pose.orientation.w = 1.0 client.send_goal(goal) client.wait_for_result() if client.get_state() == actionlib.GoalStatus.SUCCEEDED: rospy.loginfo("Goal reached!") else: rospy.loginfo("Failed to reach the goal.") ``` 2. SimpleActionServer:用于在自定义节点实现move_base的行动服务器。它提供了一组回调函数,用于处理来自move_base的目标请求和反馈。 ```python import rospy import actionlib from move_base_msgs.msg import MoveBaseAction, MoveBaseResult def execute_cb(goal): # 处理目标请求的回调函数 result = MoveBaseResult() # 执行导航任务... server.set_succeeded(result) rospy.init_node('move_base_server') server = actionlib.SimpleActionServer('move_base', MoveBaseAction, execute_cb, False) server.start() rospy.spin() ``` 这些是move_base软件包的一些常用的Python API接口。你可以根据自己的需求使用它们来控制和与move_base节点进行通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值