cartographer跑自己的數據包-2d建圖篇(laser+Imu建圖篇)

(貓崽子生病了,這一段時間忙着給它看病,花了好多鈔票了!哎!)
相信大家裝cartographer都可以跑通官網上的示例教程,所以,博主就在這裏寫一些跑自己數據包的經驗,一呢,作爲記錄,預防自己老年健忘,二呢,跟大家分享一下經驗。
如標題所示,博主用的是激光雷達數據和IMU數據進行建圖。

硬件:北科天繪16線機械激光雷達,水平視角360度,10hz/s.慣導:諾瓦泰慣導系統,這個應該是全世界最好的慣導系統了吧.(如果不是,才疏學淺,孤陋寡聞,請見諒)
軟件:Ubuntu1604,ROS-Kinetic,cartographer框架
(建議:改動文件建議自己複製源文件進行新建文件改動,源文件作爲保留用。)
廢話不多說,開始吧!

  1. 第一步,確定硬件之間的座標轉換關係.
    根據上面所說,我用到了一個多線雷達和IMU,所以在跑數據時必須要有這兩個傳感器的TF樹變換關係.在cartographer中,用的是URDF文件進行構建該關係,當然,後來我在實車測試時屏蔽了這個文件,直接用車上的tf變換關係也是可以的,只要加入imu到激光雷達的變化即可.cartographer的urdf文件定義在~/catkin_cartographer/install_isolated/share/cartographer_ros/urdf$中,名字自己要記住,比如我的是"minibus.urdf",要說的是,在源文件夾裏的urdf其實可以不用配置.下面會詳細講到.
    如下我的urdf文件:
<!--
  Copyright 2016 The Cartographer Authors

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<robot name="mini">
  <material name="orange">
    <color rgba="1.0 0.5 0.2 1" />
  </material>
  <material name="gray">
    <color rgba="0.2 0.2 0.2 1" />
  </material>

  <link name="imu">
    <visual>
      <origin xyz="0 0 0" />
      <geometry>
        <box size="0.06 0.04 0.02" />
      </geometry>
      <material name="orange" />
    </visual>
  </link>
  <link name="lidar_mid">
    <visual>
      <origin xyz="0 0 0" />
      <geometry>
        <cylinder length="0.05" radius="0.03" />
      </geometry>
      <material name="gray" />
    </visual>
  </link>


  <joint name="imu2lidar" type="fixed">
    <parent link="lidar_mid" />
    <child link="imu" />
    <origin xyz="0 0 0" />
  </joint>

</robot>

這裏稍作解釋:裏面首先定義了兩個座標系,一個imu 的,一個雷達的,變換關係是通過joint關節信息進行添加,要根據你們自己實際情況進行添加,博主太懶,所以,變換參數懶得去填寫了,依然是跑的通的.

  1. 第二步,準備數據,這個不麻煩,自己準備2d數據包即可,實在找不到的,博主這裏附上自己的2d數據包,名字是2d.bag,自行下載.
    鏈接: https://pan.baidu.com/s/1zBTHFrH0_8b5pzh4wOSfYg 提取碼: 6375 複製這段內容後打開百度網盤手機App,操作更方便哦

裏面是我通過3d點雲轉過來的數據.

  1. 第三步,配置文件條理,這個步驟其實不復雜,只是每在配置文件夾裏改一次,就要去源文件夾裏做一下相同的修改.先貼路徑,
    配置文件夾路徑:
    在這裏插入圖片描述
    源文件夾路徑:
~/catkin_cartographer/src/cartographer_ros/cartographer_ros/

這兩個文件夾裏最主要的東西都是一樣的,configuration_files是存儲.lua的文件夾,launch是存儲啓動命令的文件夾,urdf是存儲小車模型的文件夾.
**注意:**配置文件夾裏的urdf決定了你的小車的模型,即TF樹,源文件夾裏的urdf你可以不用管.
而urdf文件在上面有說過了.

  1. 第四步,開始配置:
    1.首先,進入配置文件夾裏的configuration_files文件夾,新建my_robot.lua文件(你可以複製原本的backpack_2d.lua,然後修改內容).我的文件內容如下:
include "map_builder.lua"
include "trajectory_builder.lua"

options = {
  map_builder = MAP_BUILDER,
  trajectory_builder = TRAJECTORY_BUILDER,
  map_frame = "map",
  tracking_frame = "imu",  #這裏用到了imu,所以跟蹤的是imu,如果只用雷達,把這個寫lidar_mid
  published_frame = "lidar_mid",
  odom_frame = "odom",
  provide_odom_frame = false,#我不用里程計
  publish_frame_projected_to_2d = false,
  use_pose_extrapolator = false,
  use_odometry = false,
  use_nav_sat = false,#不用GPS
  use_landmarks = false,
  num_laser_scans = 1,#雷達個數
  num_multi_echo_laser_scans = 0,
  num_subdivisions_per_laser_scan = 1,#這個參數必須要填,>=1,博主目前也沒搞明白.
  num_point_clouds = 0,#3d的話這裏要改,2d填0
  lookup_transform_timeout_sec = 0.2,
  submap_publish_period_sec = 0.3,
  pose_publish_period_sec = 5e-3,
  trajectory_publish_period_sec = 30e-3,
  rangefinder_sampling_ratio = 1.,
  odometry_sampling_ratio = 1.,
  fixed_frame_pose_sampling_ratio = 1.,
  imu_sampling_ratio = 1.,
  landmarks_sampling_ratio = 1.,
}

MAP_BUILDER.use_trajectory_builder_2d = true
TRAJECTORY_BUILDER_2D.num_accumulated_range_data = 1

return options

保存退出,進入同級目錄的launch文件夾下,新建demo_my_robot.launch文件(可複製backpack_2d.launch,然後修改),內容如下

<launch>
  <param name="robot_description"
    textfile="$(find cartographer_ros)/urdf/minibus.urdf" />#這裏是你第一步定義的urdf文件

  <node name="robot_state_publisher" pkg="robot_state_publisher"
    type="robot_state_publisher" />

  <node name="cartographer_node" pkg="cartographer_ros"
      type="cartographer_node" args="
          -configuration_directory $(find cartographer_ros)/configuration_files
          -configuration_basename my_robot.lua"#這裏是你剛纔新建的lua文件
      output="screen">
    <remap from="scan" to="/scan" />#這裏是我的2d包裏雷達話題/Scan到carto框架Scan的映射
    <remap from="imu" to="/imu" />
  </node>

  <node name="cartographer_occupancy_grid_node" pkg="cartographer_ros"
      type="cartographer_occupancy_grid_node" args="-resolution 0.05" />
</launch>

注意:用的時候要刪除我的漢字註釋,xml文件的註釋方式不是#,博主只是懶的打那些符號了.
然後,在該文件夾裏找到demo_backpack_2d.launch文件,用gedit打開修改成:

<launch>
  <param name="/use_sim_time" value="true" />

  <include file="$(find cartographer_ros)/launch/demo_my_robot.launch" />#這裏是你剛纔建立的launch文件

  <node name="rviz" pkg="rviz" type="rviz" required="true"
      args="-d $(find cartographer_ros)/configuration_files/demo_2d.rviz" />
  <node name="playbag" pkg="rosbag" type="play"
      args="--clock $(arg bag_filename)" />
</launch>

保存退出到同級目錄中,進入urdf文件夾,檢查minibus.urdf文件是否存在,且裏面座標變換關係是否正確.至此,配置文件夾裏的改動結束.

5.第五步, 配置源文件夾裏的文件.
步驟按照配置文件夾裏的配置步驟來,懶的話,直接複製過去也是可以的.

6.第六步, 檢查兩個文件夾裏做了相同的配置之後,保存退出到工作空間路徑:

cd ~
cd ~/catkin_cartographer/
catkin_make_isolated --install --use-ninja
source devel_isolated/setup.bash

  1. 第七步,至此,所有步驟搞定,來試試吧:
cd ~
cd ~/catkin_cartographer/
source devel_isolated/setup.bash
roslaunch cartographer_ros demo_backpack_2d.launch bag_filename:=~/2d.bag

不出意外你將會看到這個(csdn好像不能插動圖,反正允許插入動圖博主也不會,才疏學淺,孤陋寡聞,各位請見諒!)

在這裏插入圖片描述

  1. 保存地圖
    這裏統一默認將地圖保存爲.pbstream的格式,當你覺得圖建的不錯 時候,另起終端,輸入如下兩條命令。
cd catkin_cartographer/
source devel_isolated/setup.bash
rosservice call /finish_trajectory 0
rosservice call /write_state "{filename: '${HOME}/Downloads/mymap.pbstream'}"

至此,完工,建好的圖也被保存在了Home/Download/路徑下,名字爲mymap.pbstream。

建圖過程中,默認開了全局的閉環,所以你能清晰看到路徑修正.效果很棒,個人感覺,不要太棒了!不過,後面測試定位的 時候發現,室內2d建圖加純定位模式成功了,但在室外,純定位用不了.下一篇,想出一篇配置參數的含義的文章.
(博主今天剛剛實車測試了3d,效果更棒,不管是室內室外都不影響,而且室外定位相當穩,哈哈哈哈,這個後面我會再出一篇來講的.)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章