ROS 编译 Python 文件(利用 Python 解析 bag 文件)

ROS 编译 Python 文件

  • Creating a workspace for catkin
  • 添加环境变量
  • Creating a ROS Package(用于发布ROS消息)
  • 自定义话题消息(catkin包完善)
  • 创建订阅者(利用Python解析bag文件)
  • 运行可执行程序

Creating a workspace for catkin

链接: http://wiki.ros.org/catkin/Tutorials/create_a_workspace

$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/
$ catkin_make

添加环境变量

链接:

$ echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc  
$ source ~/.bashrc 

Creating a ROS Package(用于发布ROS消息)

链接: http://wiki.ros.org/ROS/Tutorials/CreatingPackage

# Creating a catkin Package
$ cd ~/catkin_ws/src
$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

# Building a catkin workspace and sourcing the setup file(上面已添加环境变量,此处无需重复添加)
$ cd ~/catkin_ws
$ catkin_make

自定义话题消息(catkin包完善)

  • 新建 msg 消息目录,定义 msg 消息
  • 在 package.xml 中添加功能包依赖
  • 在 CMakeLists.txt 添加编译选项

1)新建 msg 消息目录,定义 msg 消息

$ roscd beginner_tutorials/
$ mkdir msg
$ cd msg/
$ touch Num.msg
$ rosed beginner_tutorials Num.msg
$ cat Num.msg
int64 num

链接: vi/vim 使用(Linux)

2)在 package.xml 中添加功能包依赖

$ rosed beginner_tutorials package.xml
$ tail -n +62 ../package.xml | head -n 2
  <build_depend>message_generation</build_depend>
  <exec_depend>message_runtime</exec_depend>

3)在 CMakeLists.txt 添加编译选项

$ rosed beginner_tutorials CMakeLists.txt
  • 在 CMakeLists.txt 文件中增加对 message_generation 的依赖
$ tail -n +10 ../CMakeLists.txt | head -n 6
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
)
  • 增加消息文件
$ tail -n +50 ../CMakeLists.txt | head -n 7
## Generate messages in the 'msg' folder
add_message_files(
  FILES
  Num.msg
#   Message1.msg
#   Message2.msg
)
  • 增加消息生成包
$ tail -n +107 ../CMakeLists.txt | head -n 6
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES beginner_tutorials
  CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
#  DEPENDS system_lib
)
  • 添加包含 msgs 的软件包依赖
$ tail -n +72 ../CMakeLists.txt | head -n 5
## Generate added messages and services with any dependencies listed here
generate_messages(
  DEPENDENCIES
  std_msgs
)

4)编译代码

$ cd ~/catkin_ws
$ catkin_make

5)检查服务

$ rosmsg show beginner_tutorials/Num
int64 num

创建订阅者(利用Python解析bag文件)

$ roscd beginner_tutorials
$ mkdir scripts
$ cd scripts/

$ touch dealBag.py
$ chmod +x dealBag.py
$ rosed beginner_tutorials dealBag.py
$ cat dealBag.py
#!/usr/bin/env python
#coding=utf-8


import rospy
from nav_msgs.msg import OccupancyGrid  # map 数据


class TestMap(object):
    def __init__(self):
        # Give the node a name
        rospy.init_node('test_map', anonymous=False)

        rospy.Subscriber("/map", OccupancyGrid, self.get_map_data, queue_size=10)

    def get_map_data(self, msg):
        height = msg.info.height  # pgm 图片属性的像素值(栅格地图初始化大小——高度上的珊格个数)
        width = msg.info.width  # pgm 图片属性的像素值(栅格地图初始化大小中的宽——宽度上的珊格个数)
        origin_x = msg.info.origin.position.x  # ROS 建图的 origin
        origin_y = msg.info.origin.position.y  # ROS 建图的 origin
        resolution = msg.info.resolution  # ROS 建图的分辨率 resolution(栅格地图分辨率对应栅格地图中一小格的长和宽)
        data_size = len(msg.data)
        print(height, width, origin_x, origin_y, resolution, data_size)


if __name__ == "__main__":
    try:
        TestMap()
        rospy.spin()
    except rospy.ROSInterruptException:
        rospy.loginfo('Test Map terminated.')

运行可执行程序

# 打开终端一,启动 ros master
$ roscore

# 打开终端二,回放数据包
$ rosbag play map_test.bag 

# 打开终端二,创建订阅者
$ rosrun beginner_tutorials dealBag.py

链接:
ROS : 话题通信 (Python)
rosbag 常用命令

你可能感兴趣的:(ROS)