ROS 系列教程 (二)

理解ROS Topics

  • turtlesim_node和turtle_teleop_key 节点之间是通过一个ROS Topic进行交流的。turtle_teleop_key将键盘敲击发送到一个Topic上,turtlesim订阅相同的Topic来接收键盘敲击。
  • 让我们使用rqt_graph,它展示了当前正在运行着的node和Topic。

使用rqt_graph

  • r q t _ g r a p h rqt\_graph rqt_graph creates a dynamic graph of what’s going on in the system. r q t g r a p h rqt_graph rqtgraph is part of the rqt package. Unless you already have it installed, run:
sudo apt-get install ros-<distro>-rqt
sudo apt-get install ros-<distro>-rqt-common-plugins

# run
rosrun rqt_graph rqt_graph

介绍rostopic

  • rostopic 允许你获取ROS主题的信息
rostopic -h

    rostopic bw     display bandwidth used by topic  显示主题的带宽
    rostopic echo   print messages to screen   打印消息到屏幕
    rostopic hz     display publishing rate of topic    显示主题的发布频率
    rostopic list   print information about active topics   打印动态主题的信息
    rostopic pub    publish data to topic   将数据发布到主题
    rostopic type   print topic type   打印主题类型
  • 使用rostopic echo
    • r o s t o p i c e c h o rostopic echo rostopicecho shows the data published on a topic
    • usage: r o s t o p i c   e c h o   [ t o p i c ] rostopic\ echo\ [topic] rostopic echo [topic]
  • 使用rostopic list
    • 返回一列当前被订阅和发布了的topic
  • ROS Messages
    • Topics之间的交流通过节点之间发送ROS Messages, 对于publisher和subscriber之间的交流,publisher和subscriber必须发送和接受相同类型的messages。这意味着一个Topic type是由发布的消息类型定义的。发布到一个topic的消息的类型可以通过使用rostopic type来决定。
    • 使用rostopic type:返回发布的任何topic的消息类型
    Usage:
    rostopic type [topic]
    
    • 我们可以通过使用rosmsg看下消息的详细信息
    rosmsg show [消息类型名称]
    
    • 使用rostopic pub:发布数据到一个当前发布的主题上
    Usage:
    rostopic pub [topic] [msg_type] [args]
    
    • 你可能发现乌龟停止移动了;这是因为乌龟需要一个稳定的命令流以1Hz保持运动。我们可以使用rostopic pub -r命令发布一个稳定命令流:
    rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'
    
    • 使用rostopic hz:报告数据发布的速率
    Usage:
    rostopic hz [topic]
    
    • 使用rqt_plot: displays a scrolling time plot of the data published on topics.
    Usage:
    rosrun rqt_plot rqt_plot
    

Understanding ROS Services and Parameters

  • ROS Services: Services are another way that nodes can communicate with each other. Services allow nodes to send a request and receive a response.
  • 使用 rosservice
rosservice list         print information about active services 打印关于活动的服务的信息
rosservice call         call the service with the provided args 呼叫指定参数的服务
rosservice type         print service type 打印服务类型
rosservice find         find services by service type 查找服务类型的服务
rosservice uri          print service ROSRPC uri 打印服务RSRPC统一资源标识符

# usage
rosservice type /spawn | rossrv show 展示spawn使用的消息类型的详细信息
  • 使用 rosparam: rosparam allows you to store and manipulate data on the ROS Parameter Server. The Parameter Server can store integers, floats, boolean, dictionaries, and lists. rosparam uses the YAML markup language for syntax. In simple cases, YAML looks very natural: 1 is an integer, 1.0 is a float, one is a string, true is a boolean, [1, 2, 3] is a list of integers, and {a: b, c: d} is a dictionary. rosparam has many commands that can be used on parameters, as shown below:
Usage:

rosparam set            set parameter  设置参数
rosparam get            get parameter  获取参数
rosparam load           load parameters from file  从文件加载参数
rosparam dump           dump parameters to file  将参数扔到文件中
rosparam delete         delete parameter  删除参数
rosparam list           list parameter names  列出参数名字
  • 你可能希望将这个存储到一个文件中从而你能够在以后某个时间重载它。这通过使用rosparam可以很容易实现:rosparam dump和rosparam load

使用 rqt console和roslaunch

  • 使用rqt_console和rqt_logger_level
    • rqt_console attaches to ROS’s logging framework to display output from nodes.
    • rqt_logger_level allow us to change the verbosity level (DEBUG, WARN, INFO, and ERROR) of nodes as they run.
    • 在我们启动turtlesim之前,在两个新的终端启动rqt_console和rqt_logger_level:
    rosrun rqt_console rqt_console
    rosrun rqt_logger_level rqt_logger_level
    
  • Logging levels级别优先顺序
    • Fatal
    • Error
    • Warn
    • Info
    • Debug
  • Fatal是最高级别的,Debug是最低级别的。通过设置logger level级别,你将会得到那个优先级或更高级别的所有信息。例如,将级别设置成Warn,你将会得到所有Warn,Error以及Fatal的记录信息。
  • 使用roslaunch:roslaunch开启一个launch文件中定义的节点
roslaunch [package] [filename.launch]
  • launch 文件: turtlesim2会模仿turtlesim1的动作
<launch>
    <group ns="turtlesim1">
        <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
    </group>

    <group ns="turtlesim2">
        <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
    </group>

    <node pkg="turtlesim" name="mimic" type="mimic">
        <remap from="input" to="turtlesim1/turtle1"/>
        <remap from="output" to="turtlesim2/turtle1"/>
    </node>
</launch>

使用rosed在ROS中编辑文件

  • rosed是rosbash套件的一部分。它允许你使用包名直接编辑一个包中的文件,而不用输入包的整个path。
rosed [package_name] [filename]

创建一个ROS消息和服务

  • 介绍msg和srv
    • msg: msg files are simple text file that describe the fields of a ROS message. They are used to generate source code for messages in different language.

    • srv: an srv file describes a service. It is composed of two parts: a request and a response.

    • msg files are stored in the msg directory of a package, and srv files are stored in the srv directory.

    • msgs只是简单的每行带有域类型和域名的text文件。你能用的域类型有:

      • int8, int16, int32, int64 (plus uint*)
      • float32, float64
      • string
      • time, duration
      • other msg files
      • variable-length array[] and fixed-length array[C]
    • 在ROS中还有一个特殊的类型:Header,header包含一个能够广泛用于ROS的时间戳和坐标轴信息。你将会频繁地看到msg文件的第一行有Header header。

    • Here is an example of a msg that uses a Header, a string primitive, and two other msgs :

    Header header
    string child_frame_id
    geometry_msgs/PoseWithCovariance pose
    geometry_msgs/TwistWithCovariance twist
    
    • srv文件就像msg文件,除了它们包含两部分:一个请求和一个回复。这两部分是由一条“—”线分割开的。这里是一个srv文件的例子:
    int64 A
    int64 B
    ---
    int64 Sum
    # In the above example, A and B are the request, and Sum is the response. 
    
  • 使用msg消息
    • 创建一个msg消息
      • 在自己定义的package内新建一个文件夹msg,并且在msg内新建.msg文件
      • 在.msg文件中写入你想要的内容
      • 打开paclage.xml并且保证包含这两行
      	<build_depend>message_generation</build_depend>
      	<exec_depend>message_runtime</exec_depend>
      
      注意到:在build时期,我们需要“message_generation”; 在runtime时期,我们需要“message_runtime”
      • 打开CMakeLists.txt,在find_package中增加message_generation
      find_package(catkin REQUIRED COMPONENTS
        roscpp
        rospy
        std_msgs
        message_generation
      )
      
      • 也要保证你export the message runtime dependency
      catkin_package(
       ...
       CATKIN_DEPENDS message_runtime ...
       ...)
      
      • 找到下面的代码
      	# add_message_files(
         #   FILES
         #   Message1.msg
         #   Message2.msg
         # )
      
      Uncomment it by removing the # symbols and then replace the stand in Message*.msg files with your .msg file, such that it looks like this:
      add_message_files(
        FILES
        your.msg
      )
      
      • 确保generate_message()函数被调用,uncomment下面代码
      	# generate_messages(
      	#   DEPENDENCIES
      	#   std_msgs
      	# )
      
  • 使用srv
    • 在你创建的package下新建srv文件夹,并且在srv文件夹下新建.srv文件
    • 确保 创建一个msg消息中的第三第四步已经被完成
    • 加入如下内容:
    add_service_files(
      FILES
      your.srv
    )
    
  • 需要再次make package
# In your catkin workspace
$ roscd beginner_tutorials
$ cd ../..
$ catkin_make
$ cd -

Any .msg file in the msg directory will generate code for use in all supported languages. The C++ message header file will be generated in ~/catkin_ws/devel/include/beginner_tutorials/. The Python script will be created in ~/catkin_ws/devel/lib/python2.7/dist-packages/beginner_tutorials/msg. The lisp file appears in ~/catkin_ws/devel/share/common-lisp/ros/beginner_tutorials/msg/.

  • Review
    • rospack = ros+pack(age) : provides information related to ROS packages
    • roscd = ros+cd : changes directory to a ROS package or stack
    • rosls = ros+ls : lists files in a ROS package
    • roscp = ros+cp : copies files from/to a ROS package
    • rosmsg = ros+msg : provides information related to ROS message definitions
    • rossrv = ros+srv : provides information related to ROS service definitions
    • catkin_make : makes (compiles) a ROS package
      • rosmake = ros+make : makes (compiles) a ROS package (if you’re not using a catkin workspace)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值