【RK3399Pro学习笔记】十、ROS服务端Server的编程实现
生活随笔
收集整理的這篇文章主要介紹了
【RK3399Pro学习笔记】十、ROS服务端Server的编程实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 如何實現一個服務器
- C++
- 編寫程序
- 配置CMakeLists.txt
- 編譯并運行
- python
- 創建并編寫腳本
- 運行
平臺:華碩 Thinker Edge R 瑞芯微 RK3399Pro
固件版本:Tinker_Edge_R-Debian-Stretch-V1.0.4-20200615
記錄自【古月居】古月·ROS入門21講 | 一學就會的ROS機器人入門教程 —— 古月居GYH P14 14.服務端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 <ros/ros.h> #include <geometry_msgs/Twist.h> #include <std_srvs/Trigger.h>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,注冊回調函數commandCallbackros::ServiceServer command_service = n.advertiseService("/turtle_command", commandCallback);// 創建一個Publisher,發布名為/turtle1/cmd_vel的topic,消息類型為geometry_msgs::Twist,隊列長度10turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);// 循環等待回調函數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 #!/usr/bin/env python # -*- coding: utf-8 -*-######################################################################## #### Copyright 2020 GuYueHome (www.guyuehome.com). ### ######################################################################### 該例程將執行/turtle_command服務,服務數據類型std_srvs/Triggerimport rospy import thread,time from geometry_msgs.msg import Twist from std_srvs.srv import Trigger, TriggerResponsepubCommand = 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.5vel_msg.angular.z = 0.2turtle_vel_pub.publish(vel_msg)time.sleep(0.1)def commandCallback(req):global pubCommandpubCommand = 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,注冊回調函數commandCallbacks = 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 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【RK3399Pro学习笔记】十、ROS服务端Server的编程实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中美两本有影响力数理统计学教材的对比及其
- 下一篇: C#摄像头实现拍照功能的简单代码示例