完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
如何实现一个服务器
初始化ROS节点; 创建Server实例; 循环等待服务请求,进入回调函数; 在回调函数中完成服务功能的处理,并反馈应答数据。 C++ 编写程序 nano ~/catkin_workspace/src/learning_service/src/turtle_command_server.cpp /*********************************************************************** Copyright 2020 GuYueHome (www.guyuehome.com). ***********************************************************************/ /** * 该例程将执行/turtle_command服务,服务数据类型std_srvs/Trigger */ #include #include #include ros::Publisher turtle_vel_pub; bool pubCommand = false; // service回调函数,输入参数req,输出参数res bool commandCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res) { pubCommand = !pubCommand; // 显示请求数据 ROS_INFO("Publish turtle velocity command [%s]", pubCommand==true?"Yes":"No"); // 设置反馈数据 res.success = true; res.message = "Change turtle command state!"; return true; } int main(int argc, char **argv) { // ROS节点初始化 ros::init(argc, argv, "turtle_command_server"); // 创建节点句柄 ros::NodeHandle n; // 创建一个名为/turtle_command的server,注册回调函数commandCallback ros::ServiceServer command_service = n.advertiseService("/turtle_command", commandCallback); // 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10 turtle_vel_pub = n.advertise // 循环等待回调函数 ROS_INFO("Ready to receive turtle command."); // 设置循环的频率 ros::Rate loop_rate(10); while(ros::ok()) { // 查看一次回调函数队列 ros::spinOnce(); // 如果标志为true,则发布速度指令 if(pubCommand) { geometry_msgs::Twist vel_msg; vel_msg.linear.x = 0.5; vel_msg.angular.z = 0.2; turtle_vel_pub.publish(vel_msg); } //按照循环频率延时 loop_rate.sleep(); } return 0; } 配置CMakeLists.txt nano ~/catkin_workspace/src/learning_service/CMakeLists.txt 添加 add_executable(turtle_command_server src/turtle_command_server.cpp) target_link_libraries(turtle_command_server ${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_command_server # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosservice call /turtle_command "{}" rqt_graph python 创建并编写脚本 nano ~/catkin_workspace/src/learning_service/scripts/turtle_command_server.py 1 #!/usr/bin/env python # -*- coding: utf-8 -*- ######################################################################## #### Copyright 2020 GuYueHome (www.guyuehome.com). ### ######################################################################## # 该例程将执行/turtle_command服务,服务数据类型std_srvs/Trigger import rospy import thread,time from geometry_msgs.msg import Twist from std_srvs.srv import Trigger, TriggerResponse pubCommand = False; turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) def command_thread(): while True: if pubCommand: vel_msg = Twist() vel_msg.linear.x = 0.5 vel_msg.angular.z = 0.2 turtle_vel_pub.publish(vel_msg) time.sleep(0.1) def commandCallback(req): global pubCommand pubCommand = bool(1-pubCommand) # 显示请求数据 rospy.loginfo("Publish turtle velocity command![%d]", pubCommand) # 反馈数据 return TriggerResponse(1, "Change turtle command state!") def turtle_command_server(): # ROS节点初始化 rospy.init_node('turtle_command_server') # 创建一个名为/turtle_command的server,注册回调函数commandCallback s = rospy.Service('/turtle_command', Trigger, commandCallback) # 循环等待回调函数 print "Ready to receive turtle command." thread.start_new_thread(command_thread, ()) rospy.spin() if __name__ == "__main__": turtle_command_server() 运行 sudo chmod +x ~/catkin_workspace/src/learning_service/scripts/turtle_command_server.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_command_server.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosservice call /turtle_command "{}" rqt_graph |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
基于米尔瑞芯微RK3576核心板/开发板的人脸疲劳检测应用方案
1043 浏览 0 评论
1220 浏览 1 评论
968 浏览 1 评论
2238 浏览 1 评论
3561 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-28 21:59 , Processed in 0.709436 second(s), Total 70, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号