ROS 命令以及相关内容学习(二)
在上一節中我們已經學習了package node topic message 這些的運行方式,下面我們去理解service 的運行方式。
1.sevice
1 #命令 2 rosservice list print information about active services 3 rosservice call call the service with the provided args 4 rosservice type print service type 5 rosservice find find services by service type 6 rosservice uri print service ROSRPC uri 7 #例子 8 rosservice list 9 /clear 10 /kill 11 /reset 12 /rosout/get_loggers 13 /rosout/set_logger_level 14 /spawn 15 /teleop_turtle/get_loggers 16 /teleop_turtle/set_logger_level 17 /turtle1/set_pen 18 /turtle1/teleport_absolute 19 /turtle1/teleport_relative 20 /turtlesim/get_loggers 21 /turtlesim/set_logger_level 22 #另一個例子 23 rosservice type [service] 24 25 rosservice type /clear #你會發現這個命令是沒有什么東西,?1.1 rosservice call?[service] [args]
1 當你運行著一條的 2 $ rosservice call /clear 3 就可以清空小烏龜的軌跡哦。 4 運行著一條的時候 5 rosservice call /spawn 2 2 0.2 "" 6 你會發現另一只小烏龜出現啦!2?rosparam list查看參數
1 rosparam set [param_name] #設置參數 2 rosparam get [param_name] #得到參數 3 rosparam set /background_r 150 #把參數設置成150 4 rosparam get /background_g #獲得G的參數 5 rosparam get / #獲得所有參數2.1 ?rosparam dump and rosparam load
1 rosparam dump [file_name] [namespace] #將參數寫進 。yaml文件中 2 rosparam load [file_name] [namespace] 3 4 rosparam dump params.yaml 5 6 rosparam load params.yaml copy 7 rosparam get /copy/background_b3.?Using rqt_console and roslaunch?
rqt _console ?and rqt_logger_level ?是調試的可視化界面。
roslaunch ?是一次性打開多個 節點
1 rosrun rqt_console rqt_console 2 rosrun rqt_logger_level rqt_logger_level 3 4 http://wiki.ros.org/ROS/Tutorials/UsingRqtconsoleRoslaunch#CA-21ef414cf4c910bb1286ff2aedfe349a32a099b9_1先打開這兩個界面然后就,打開node ?就可以調試啦!。不懂就去參考wik 吧。
?
創建一個roslaunch文件,你可以在 ?之前建的工作空間,src/beginner… 中建一個lanunch 文件(這個文件的名字其實是隨便取的。反正要存放的你的lanunch文件)之后我們來寫一個launch文吧。
1 cd ~/catkin_ws2 source devel/setup.bash3 roscd beginner_tutorials4 mkdir launch5 cd launch6 touch turtlemimic.launch 7 vi turtlemimic.launch 8 9 文件內容 10 <launch> #嘛 告訴你這是個lanunch 文件 11 12 <group ns="turtlesim1"> 13 <node pkg="turtlesim" name="sim" type="turtlesim_node"/> #嘛 定義以下這個節點的名字呀,sim ,類型呀。 14 </group> 15 16 <group ns="turtlesim2"> #第二個節點 17 <node pkg="turtlesim" name="sim" type="turtlesim_node"/> 18 </group> 19 20 <node pkg="turtlesim" name="mimic" type="mimic"> #第三個節點,命令文件,可以讓小烏龜跑哦。 21 <remap from="input" to="turtlesim1/turtle1"/> 22 <remap from="output" to= "turtlesim2/turtle1"/> # 運行之后,你能想想到,節點之間是怎么 傳遞 訂閱消息哦的嗎? 23 </node> 24 25 </launch>這樣你運行 ?roslaunch?beginner_tutorials turtlemimic.launch ?你就會發現兩只小烏龜出現啦!
然后你發不一條命令?rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]' ? 兩只小烏龜就可以動啦!
4.rosed?[package_name] [filename] ROS 里面的vim。
5.Creating a ROS msg and srv
$ roscd beginner_tutorials $ mkdir msg $ echo "int64 num" > msg/Num.msg #以上是創建msg 并且將int64 寫入,也可以用其他方式寫 in package.xlm make sure #以下是告訴package,xml 以及 camke 我們要創建msg <build_depend>message_generation</build_depend><run_depend>message_runtime</run_depend> in CMakeLists.txt make sure find_package(catkin REQUIRED COMPONENTSroscpprospystd_msgsmessage_generation ) catkin_package(...CATKIN_DEPENDS message_runtime ......) add_message_files(FILESNum.msg ) generate_messages(DEPENDENCIESstd_msgs )查看一個信息。。
rosmsg show XX
PS:roscp [package_name] [file_to_copy_path] [copy_path] ?ros 里買你的復制example:roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv
然后去 工作空間下catkin_make install ?就可以ˊ啦。就會生成相應的 msg srv 源碼。你可以在以下路徑下查看,py 跟cpp
~/catkin_ws/devel/lib/python2.7/dist-packages/beginner_tutorials/msg.
?~/catkin_ws/devel/share/common-lisp/ros/beginner_tutorials/msg/.?
6.Writing the Publisher Node
$ wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/talker.py $ chmod +x talker.py #將下載的東西弄成可執行 1 #!/usr/bin/env python 2 # license removed for brevity 3 import rospy 4 from std_msgs.msg import String 5 6 def talker(): 7 pub = rospy.Publisher('chatter', String, queue_size=10) #節點發送了chatter topic 以 msg type string 的形式 8 rospy.init_node('talker', anonymous=True) #?rospy.init_node(NAME) 是非常重要的,告訴了rospy 節點的名字,這樣roscore 才可以管理 9 rate = rospy.Rate(10) # 10hz 一中非常好的方法,可以循環。因為是沒有 十分之一秒的/ 10 while not rospy.is_shutdown(): 11 hello_str = "hello world %s" % rospy.get_time() 12 rospy.loginfo(hello_str) 13 pub.publish(hello_str) 14 rate.sleep() #這段loop 是為你沒有crlt+c 的時候,你可以一直保持穩定的運行。 15 16 if __name__ == '__main__': 17 try: 18 talker() 19 except rospy.ROSInterruptException: 20 pass6.Writing the?Subscriber?Node
$ roscd beginner_tutorials/scripts/ $ wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/listener.py#!/usr/bin/env python import rospy from std_msgs.msg import Stringdef callback(data):rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)def listener():# In ROS, nodes are uniquely named. If two nodes with the same# node are launched, the previous one is kicked off. The# anonymous=True flag means that rospy will choose a unique# name for our 'listener' node so that multiple listeners can# run simultaneously.rospy.init_node('listener', anonymous=True)rospy.Subscriber("chatter", String, callback)# spin() simply keeps python from exiting until this node is stopped rospy.spin()if __name__ == '__main__':listener()
?注意: 其實寫 ?訂閱以及 發送也是一個節點, 可以直接運行,你去運行試試吧。
7.Writing a Service Node
1 # Create the scripts/add_two_ints_server.py file within the beginner_tutorials package and paste the following inside it: 2 3 #!/usr/bin/env python 4 5 from beginner_tutorials.srv import * 6 import rospy 7 8 def handle_add_two_ints(req): 9 print "Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b)) 10 return AddTwoIntsResponse(req.a + req.b) #表示加法的 11 #實現發布 service 12 def add_two_ints_server(): 13 rospy.init_node('add_two_ints_server') 14 s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints) 15 print "Ready to add two ints." 16 rospy.spin() # 確保在沒有結束這段程序之前,他就一直運行。 17 18 if __name__ == "__main__": 19 add_two_ints_server()?上面的 ?發布的,我們來寫一個客戶
1 #!/usr/bin/env python 2 3 import sys 4 import rospy 5 from beginner_tutorials.srv import * 6 7 def add_two_ints_client(x, y): 8 rospy.wait_for_service('add_two_ints') #等待 服務。 9 try: 10 add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts) 11 resp1 = add_two_ints(x, y) 12 return resp1.sum 13 except rospy.ServiceException, e: 14 print "Service call failed: %s"%e 15 16 def usage(): 17 return "%s [x y]"%sys.argv[0] 18 19 if __name__ == "__main__": 20 if len(sys.argv) == 3: 21 x = int(sys.argv[1]) 22 y = int(sys.argv[2]) 23 else: 24 print usage() 25 sys.exit(1) 26 print "Requesting %s+%s"%(x, y) 27 print "%s + %s = %s"%(x, y, add_two_ints_client(x, y))?
?
??
轉載于:https://www.cnblogs.com/xialuobo/p/6082877.html
總結
以上是生活随笔為你收集整理的ROS 命令以及相关内容学习(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 3.5.2建立与DB2的连
- 下一篇: JVM基础(6)-常用参数总结