完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
C++
创建功能包 cd ~/catkin_workspace/src catkin_create_pkg learning_service roscpp rospy std_msgs geometry_msgs turtlesim 编写程序 nano ~/catkin_workspace/src/learning_service/src/turtle_spawn.cpp /*********************************************************************** Copyright 2020 GuYueHome (www.guyuehome.com). ***********************************************************************/ /** * 该例程将请求/spawn服务,服务数据类型turtlesim::Spawn */ #include #include int main(int argc, char** argv) { // 初始化ROS节点 ros::init(argc, argv, "turtle_spawn"); // 创建节点句柄 ros::NodeHandle node; // 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service ros::service::waitForService("/spawn"); ros::ServiceClient add_turtle = node.serviceClient // 初始化turtlesim::Spawn的请求数据 turtlesim::Spawn srv; srv.request.x = 2.0; srv.request.y = 2.0; srv.request.name = "turtle2"; // 请求服务调用 ROS_INFO("Call service to spwan turtle[x:%0.6f, y:%0.6f, name:%s]", srv.request.x, srv.request.y, srv.request.name.c_str()); add_turtle.call(srv); // 显示服务调用结果 ROS_INFO("Spwan turtle successfully [name:%s]", srv.response.name.c_str()); return 0; }; 配置CMakeLists.txt nano ~/catkin_workspace/src/learning_service/CMakeLists.txt 添加 add_executable(turtle_spawn src/turtle_spawn.cpp) target_link_libraries(turtle_spawn ${catkin_LIBRARIES}) 编译并运行 cd ~/catkin_workspace catkin_make # 新建一个终端 source ~/catkin_workspace/devel/setup.bash roscore # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun turtlesim turtlesim_node # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_service turtle_spawn python 创建并编写脚本 cd ~/catkin_workspace/src/learning_service mkdir scripts cd ~/catkin_workspace/src/learning_service/scripts nano turtle_spawn.py #!/usr/bin/env python # -*- coding: utf-8 -*- ######################################################################## #### Copyright 2020 GuYueHome (www.guyuehome.com). ### ######################################################################## # 该例程将请求/spawn服务,服务数据类型turtlesim::Spawn import sys import rospy from turtlesim.srv import Spawn def turtle_spawn(): # ROS节点初始化 rospy.init_node('turtle_spawn') # 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service rospy.wait_for_service('/spawn') try: add_turtle = rospy.ServiceProxy('/spawn', Spawn) # 请求服务调用,输入请求数据 response = add_turtle(2.0, 2.0, 0.0, "turtle2") return response.name except rospy.ServiceException, e: print "Service call failed: %s"%e if __name__ == "__main__": #服务调用并显示调用结果 print "Spwan turtle successfully [name:%s]" %(turtle_spawn()) 运行 sudo chmod +x ~/catkin_workspace/src/learning_service/scripts/turtle_spawn.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash roscore # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun turtlesim turtlesim_node # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_service turtle_spawn.py |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1906 浏览 1 评论
synopsys 的design ware:DW_fpv_div,浮点数除法器,默认32位下,想提升覆盖率(TMAX),如果用功能case去提升覆盖率呢?
2382 浏览 1 评论
RK3588 GStreamer调试四路鱼眼摄像头四宫格显示报错
5175 浏览 1 评论
【飞凌嵌入式OK3576-C开发板体验】RKNN神经网络-YOLO图像识别
254 浏览 0 评论
【飞凌嵌入式OK3576-C开发板体验】SSH远程登录网络配置及CAN通讯
1336 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 01:16 , Processed in 0.632378 second(s), Total 73, Slave 56 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号