【ROS】服务端&客户端

C++

客户端

#include "ros/ros.h"
#include "beginner_tutorials/AddTwoInts.h"
bool add(beginner_tutorials::AddTwoInts::Request  &req,
         beginner_tutorials::AddTwoInts::Response &res) // 接受了请求和响应,返回布尔值
{
    res.sum = req.a + req.b;
    ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
    ROS_INFO("sending: [%ld]", (long int)res.sum);
    return true;
}
int main(int argc, char **argv)
{
    ros::init(argc, argv, "Server");
    ros::NodeHandle n;
    ros::ServiceServer service = n.advertiseService("sum", add);
    ROS_INFO("Ready");
    ros::spin();
    return 0;
}

服务端

#include "ros/ros.h"
#include "beginner_tutorials/AddTwoInts.h"
#include <cstdlib>
int main(int argc, char **argv)
{
    ros::init(argc, argv, "client");
    if (argc != 3)
    {
        ROS_INFO("usage: add a b");
        return 1;
    }
    ros::NodeHandle n;
    // 创建一个客户端请求add服务
    ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("sum");
    beginner_tutorials::AddTwoInts srv; // 定义类,传入参数
    srv.request.a = atoll(argv[1]);
    srv.request.b = atoll(argv[2]);
    if(client.call(srv))    //  若成功,返回true,失败返回false
    {
        ROS_INFO("sum = %ld", (long int)srv.response.sum);
    }
    else
    {
        ROS_ERROR("Fail to call");
        return 1;
    }
    return 0;
}

补充

add_executable(server src/server.cpp)
target_link_libraries(server ${catkin_LIBRARIES})
add_dependencies(server beginner_tutorials_gencpp)

add_executable(client src/client.cpp)
target_link_libraries(client ${catkin_LIBRARIES})
add_dependencies(client beginner_tutorials_gencpp)

python

客服端

#!/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('sum', AddTwoInts, handle_add_two_ints)
    print("Ready to add two ints.")
    rospy.spin()

if __name__ == "__main__":
    add_two_ints_server()

服务端

返回/opt/ros/melodic/bin/rosrun: 行 116: /home/udi/catkin_ws/src/beginner_tutorials/scripts/client.py: 成功暂不清楚原因

#!/usr/bin/env python

from __future__ import print_function

import sys
import rospy
# from beginner_tutorials.srv import *
from beginner_tutorials.srv import AddTwoInts,AddTwoIntsResponse
print("step1")

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

def usage():
    print("step3")
    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])
        print("step4")
    else:
        print(usage())
        print("step5")
        sys.exit(1)
    print("Requesting %s+%s"%(x, y))
    print("%s + %s = %s"%(x, y, add_two_ints_client(x, y)))

补充

catkin_install_python(PROGRAMS
  scripts/talker.py
  scripts/listener.py
  scripts/server.py
  scripts/client.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值