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_b
3. 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_ws 2 source devel/setup.bash 3 roscd beginner_tutorials 4 mkdir launch 5 cd launch 6 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 COMPONENTS roscpp rospy std_msgs message_generation ) catkin_package( ... CATKIN_DEPENDS message_runtime ... ...) add_message_files( FILES Num.msg ) generate_messages( DEPENDENCIES std_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 pass
6.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 String def 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))

浙公网安备 33010602011771号