本篇博文持续更新中……
本章博客是网络公开课 “[ROS tutorial for beginners] Chapter 2. ROS Basic Concepts.”, 的上课笔记,网址链接为https://www.youtube.com/watch?v=-GZP81bTuO8&index=3&list=PLK0b4e05LnzZWg_7QrIQWyvSPX2WN2ncc
roslaunch
- roslaunch的作用
roslaunch is the command used to launch a ROS program
- roslaunch与rosrun的区别
roslaunch可以多个节点,而rosrun只能一次运行一个节点
- roslaunch的具体用法
roslaunch <package_name> <file.launch>
- launch file 解读
<launch>
<arg name="world_file" default="$(env GYM_GAZEBO_WORLD_CIRCUIT2C)"/>
<arg name="base" value="$(optenv TURTLEBOT_BASE kobuki)"/> <!-- create, roomba -->
<arg name="battery" value="$(optenv TURTLEBOT_BATTERY /proc/acpi/battery/BAT0)"/> <!-- /proc/acpi/battery/BAT0 -->
<arg name="paused" value="true"/>
<arg name="gui" default="false"/>
<arg name="headless" value="true"/>
<arg name="stacks" value="$(optenv TURTLEBOT_STACKS hexagons)"/> <!-- circles, hexagons -->
<arg name="3d_sensor" value="$(optenv TURTLEBOT_3D_SENSOR kinect)"/> <!-- kinect, asus_xtion_pro -->
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="use_sim_time" value="true"/>
<arg name="debug" value="false"/>
<arg name="gui" value="$(arg gui)" />
<arg name="world_name" value="$(arg world_file)"/>
<arg name="verbose" value="false"/>
</include>
<include file="$(find turtlebot_gazebo)/launch/includes/$(arg base).launch.xml">
<arg name="base" value="$(arg base)"/>
<arg name="stacks" value="$(arg stacks)"/>
<arg name="3d_sensor" value="$(arg 3d_sensor)"/>
</include>
<node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher">
<param name="publish_frequency" type="double" value="30.0" />
</node>
</launch>
这个launch file复制的gym-gazebo的assets文的GazeboCircuit2cTurtlebotLidar_v0.launch,
这里解读一下
所有的launch文件都被包含在一个标签中。在该标签内,你可以看到一个标签,具体参数如下:
pkg = "package_name"
# Name of the package that contains the code of the ROS program to execute
type = "python_file_name.py"
# Name of the program file that we want to execute
name = "node_name"
#Name of the ROS node that will launch our Python file
output = "type_of_output"
#Through which channel will you print the output of the python file
ros package
command
rospack list
#列出所有的ros package
rospack list | grep <pacakge_name>
#找出带有package_name这一个关键词的package
ros Environment Variables
ROS uses a set of Linux system environment variables in order to work properly. You can check these variables by typing:
export | grep ROS
the results in my computer is shown as follows:
declare -x ROSLISP_PACKAGE_DIRECTORIES="/home/zhw/mybot_ws/devel/share/common-lisp"
declare -x ROS_DISTRO="kinetic"
declare -x ROS_ETC_DIR="/opt/ros/kinetic/etc/ros"
declare -x ROS_MASTER_URI="http://localhost:11311"
declare -x ROS_PACKAGE_PATH="/home/zhw/mybot_ws/src/mybot_control:/home/zhw/mybot_ws/src/mybot_description:/home/zhw/mybot_ws/src/mybot_gazebo:/opt/ros/kinetic/share"
declare -x ROS_PORT_SIM="11311"
declare -x ROS_ROOT="/opt/ros/kinetic/share/ros"
NOTE: If you want ros to find and use your package, you should add your package path into the ROS_PACKAGE_PATH
rospy
rospy常见函数
rospy.spin()
# spin() simply keeps python from exiting until this node is stopped.
# Blocks until ROS node is shutdown. Yields activity to other threads.
the difference between rospy.spin() and {rospy.rate() and rospy.rate().sleep()}
rospy.spin() resposible to handle communication events, e.g. arriving messages. If you are subscribing messages, services or actions you must call spin to process the events.
rospy.rate().sleep() Convenience class for sleeping in a loop at a specified rate. Attempt sleep at the specified rate. sleep() takes into account the time elapsed since the last successful sleep().
Rospy Messages
rospy takes msg files and generates Python source code for them. The pattern for this is:
package_name/msg/Foo.msg → package_name.msg.Foo
因此,在import messages 的时候,会出现
from std_msgs.msg import String
Rospy publisher and subscribers
- Publishing to a topic
rospy.loginfo(str), which performs triple-duty:
the messages get printed to screen;
it gets written to the Node’s log file;
it gets written to rosout.
Class Publisher
http://docs.ros.org/api/rospy/html/rospy.topics.Publisher-class.html#publish
init(self, name, data_class, subscriber_listener=None, tcp_nodelay=False, latch=False, headers=None, queue_size=None)
这儿想强调的是queue_size
queue_size (int) - The queue size used for asynchronously publishing messages from different threads. A size of zero means an infinite queue, which can be dangerous. When the keyword is not being used or when None is passed all publishing will happen synchronously and a warning message will be printed.
queue size是用来异步发布来自不同线程的消息。不同于进程,线程主要问题是资源共享问题。当使用NONE的时候,所有当发布将会同步发生,因此会引发警告。Class Subscriber
http://docs.ros.org/api/rospy/html/rospy.topics.Subscriber-class.html
init(self, name, data_class, callback=None, callback_args=None, queue_size=None, buff_size=65536, tcp_nodelay=False)
这儿我想强调几个参数:
第一个是callback
callback (fn(msg, cb_args)) - function to call ( fn(data)) when data is received. If callback_args is set, the function must accept the callback_args as a second argument, i.e. fn(data, callback_args). NOTE: Additional callbacks can be added using add_callback().
当作为subscriber当节点接收到topic中当message之后,会回调这个callback函数,这个callback函数需要我们自己去定义。
第二个是queue_size
queue_size (int) - maximum number of messages to receive at a time. This will generally be 1 or None (infinite, default). buff_size should be increased if this parameter is set as incoming data still needs to sit in the incoming buffer before being discarded. Setting queue_size buff_size to a non-default value affects all subscribers to this topic in this process.
ros没有自适应定义queue_size的功能。queue_size定义了这个node可以一次接受多少个messages。