1. 程式人生 > >cartographer+turtlebot+hokuyo|安裝配置

cartographer+turtlebot+hokuyo|安裝配置

cartographer+turtlebot+hokuyo|安裝配置


系統:ubuntu 14.04+ros indigo 或 ubuntu 16.04+ros kinetic
平臺:turtlebot2
感測器:hokuyo UTM-30LX laser

本文假設已經成功安裝ubuntu,ros,以及各種與turtlebot相關的ros package等。本文主要介紹,怎麼安裝配cartographer+turtlebot+hokuyo,具體的包括安裝cartographer, cartographer_ros, cartographer_turtlebot,以及在gazebo模擬環境中使用cartographer等。後續相關的工作會不定期往後續。


文章目錄

1.安裝cartographer,cartographer_ros,cartographer_turtlebot

1.1 cartographer+cartographer_ros

從師弟那見識了cartographer在實際機器人上進行slam的效果後,馬上回來學習學習。由於開始並不知道谷歌研究人員為了方便cartographer在turtlebot機器人上的應用,已經出了cartographer_turtlebot。所以開始我按照

谷歌cartographer_ros官網的方式安裝,一次性就成功了(打開了科學上網工具)。後來發現還有另一種流行的安裝方式,我稱之為民間方法,經過親自測試,也是有效的。所以,本人測試,兩種安裝方式都是有效的。各自的優缺點列出如下:

  • 谷歌官網安裝
    優點:類似於一鍵安裝,所有的安裝包都在一個工作空間,便於管理。
    缺點:有些步驟需要科學上網(翻牆)才能成功。

  • 民間安裝方法
    優點:每一步是分離開的,可控性更大,可以一個庫一個庫的安裝。
    缺點:基於的github源可能有點老,不知道作者更新了沒。

大家可以任意選擇一種嘗試,不行換另一個就行。在安裝過程中,會出現有些庫很難安裝成功,甚至找不到該庫,這是因為ros的國內映象源的問題,更換源就行了。本次安裝,我換了好幾次源(易科的、USTC、以及清華大學)。有些庫在易科的源下面卡半天,但是換成在USTC源下面很順利的就安裝成功。這是我想強調給大家的,不要盲目的試,不是什麼神祕的現象,是你的ros映象源的問題。重點!!!!

1.2 cartographer_turtlebot

本人償試了官網的安裝方式,安裝不成功。也試了git clone到本地再編譯的方式也不成功。下面介紹自己在無數次失敗後成功的安裝方法。

官網的安裝方式的特點是,一鍵搞定。例如,在安裝cartographer_ros時,會同時安裝cartographer以及其它依賴包。而cartographer_turtlebot是同時依賴cartographer與cartographer_ros的,官網在安裝cartographer_turtlebot時會再次把cartographer,cartographer_ros,以及其它依賴包再次安裝一遍,這是在用谷歌官網教程安裝時要注意的。他是向前包容的,也就是說,理想情況下,我只需要按裝官網安裝cartographer_turtlebot的方法,執行所有命令就可以了,並不需要提前單獨裝cartographer_ros,cartographer等。但是,我將安裝好的cartographer與cartographer_ros工作空間刪掉後,嚴格按照cartographer_turtlebot官網安裝來了一遍,結果沒有成功(還好,在刪之前,我把工作空間都備份了,所以說備份很重要!!!)。我本來已經放棄使用cartographer_turtlebot了,而是選擇研究cartographer_turtlebot裡面的launch檔案、configuration_files裡的檔案來模仿它自己寫配置檔案。後來發現launch檔案裡只依賴一個cartographer_turtlebot包裡生成的node,也就是說該包裡只有一個node。於是,我自己建立工作空間,將這個節點.cc檔案進行編譯。後面還真成功了,下面給出詳細的過程。

下面是我在已經安裝好cartographer_ros的前提下,安裝cartographer_turtlebot的方法。

  • 1.構建ros工作空間
mkdir -p ~/cartographer_turtlebot_ws/src
cd ~/cartographer_turtlebot_ws/src
catkin_init_workspace
cd ..
catkin_make

注意:建立新的工作空間後,記得將source ~/cartographer_turtlebot_ws/devel/setup.bash新增到~/.bashrc檔案中

  • 2.建立新的pkg
cd ~/cartographer_turtlebot_ws/src
catkin_create_pkg cartographer_turtlebot
cd ..
catkin_make
  • 3.將cartographer_turtlebot包 git clone到本地
cd ~/Download
git clone https://github.com/googlecartographer/cartographer_turtlebot
  • 4.將~/Download/cartographer_turtlebot/cartographer_turtlebot資料夾下的三個子資料夾cartographer_turtlebot(當複製到目標資料夾後,改名為src),configuration_files,launch全部複製到~/cartographer_turtlebot_ws/src/cartographer_turtlebot。並將複製過來的子資料夾cartographer_turtlebot更名為src。此時的檔案目錄如下圖所示
    在這裡插入圖片描述

  • 5.對~/cartographer_turtlebot_ws/src/cartographer_turtlebot/CMakeLists.txt檔案進行修改,下面直接帖出修改後的檔案內容,只需複製該內容替換原來的CMakeLists.txt檔案即可。

cmake_minimum_required(VERSION 2.8.3)
project(cartographer_turtlebot)

# Compile as C++11
add_compile_options(-std=c++11)

set(PACKAGE_DEPENDENCIES
  roscpp
  roslib
  sensor_msgs
)

find_package(catkin REQUIRED COMPONENTS ${PACKAGE_DEPENDENCIES})
catkin_package(
)

add_executable(cartographer_flat_world_imu_node "src/flat_world_imu_node_main.cc")
target_link_libraries(cartographer_flat_world_imu_node ${catkin_LIBRARIES})
add_dependencies(cartographer_flat_world_imu_node ${catkin_EXPORTED_TARGETS})

~/cartographer_turtlebot_ws/src/cartographer_turtlebot/package.xml先不做修改。

如果後面出了錯誤,可以償試在該檔案內容的<buildtool_depend>catkin</buildtool_depend>後面新增下面幾行內容。

  <build_depend>sensor_msgs</build_depend>
  <build_export_depend>sensor_msgs</build_export_depend>
  <exec_depend>sensor_msgs</exec_depend>
  • 6.編譯工作空間
cd ~/cartographer_turtlebot_ws
catkin_make

不出意外,會編譯完成。

2.測試資料集

2.1 cartographer_ros資料集

  • 1.下載資料集
# Download the 2D backpack example bag.
wget -P ~/Downloads https://storage.googleapis.com/cartographer-public-data/bags/backpack_2d/cartographer_paper_deutsches_museum.bag
# Download the 3D backpack example bag.
wget -P ~/Downloads https://storage.googleapis.com/cartographer-public-data/bags/backpack_3d/with_intensities/b3-2016-04-05-14-14-00.bag
  • 2.測試資料集
# Launch the 2D backpack demo.
roslaunch cartographer_ros demo_backpack_2d.launch bag_filename:=${HOME}/Downloads/cartographer_paper_deutsches_museum.bag

部分效果如下圖所示:
在這裡插入圖片描述

# Launch the 3D backpack demo.
roslaunch cartographer_ros demo_backpack_3d.launch bag_filename:=${HOME}/Downloads/b3-2016-04-05-14-14-00.bag

資料太大9個G,就不跑了。

2.2 cartographer_turtlebot

  • 1.下載測試資料集
# Download the example bag.
wget -P ~/Downloads https://storage.googleapis.com/cartographer-public-data/bags/turtlebot/cartographer_turtlebot_demo.bag
  • 2.測試結果
# Launch the 2D LIDAR demo.
roslaunch cartographer_turtlebot demo_lidar_2d.launch bag_filename:=${HOME}/Downloads/cartographer_turtlebot_demo.bag

部分結果如下圖所示:
在這裡插入圖片描述

# Launch the 2D depth camera demo.
roslaunch cartographer_turtlebot demo_depth_camera_2d.launch bag_filename:=${HOME}/Downloads/cartographer_turtlebot_demo.bag

部分結果如下圖所示:
在這裡插入圖片描述

# Launch the 3D depth camera demo.
roslaunch cartographer_turtlebot demo_depth_camera_3d.launch bag_filename:=${HOME}/Downloads/cartographer_turtlebot_demo.bag

部分結果如下圖所示:
在這裡插入圖片描述
這個應該是有點問題,不過cartographer_turtlebot肯定是安裝成功了。

3. turtlebot+hokuyo+cartographer in gazebo

當實體機器人成本很高,而且不是很方便隨處攜帶時,在模擬軟體中進行機器人的研究不失一種很好的替代。在機器人模擬平臺中,gazebo更是以其出色的物理引擎,以及與ros完全的介面,被廣泛應用。本節主要介紹利用gazebo搭建立turtlebot+hokuyo的模擬環境,並且基於模擬資料實現cartographer的SLAM目的。

3.1 turtlebot_gazebo模擬環境新增hokuyo

由於turtlebot_gazebo包裡的turtlebot預設的感測器是kinect,所以核心問題是怎麼將kinect更換成hokuyo。該部分可以參照MaxChanger|CSDN部落格,本人就是按照他一步一步配置,最後成功在gazebo中執行turtlebot+hokuyo,並能成功釋出鐳射資料。所以,本篇就不重述整個配置過程,只對其中未講明白的地方進行闡述。主要有以下三點:

  1. CSDN博主RGiantMaxChanger|CSDN部落格的未尾評論的:“步驟3實際修改了3處,不要漏掉:
<sensor_hokuyo parent="base_link" />
  1. MaxChanger|CSDN部落格實際上配置的是turtlebot+kinect+hokuyo的過程。如果只是想用hokuyo替換掉kinect的話,可以將步驟中有關kinect的指令碼從檔案中刪掉即可。具體地:步驟3中將kobuki_hexagons_hokuyo.urdf.xacro檔案中第12,17註釋掉;步驟6中將turtlebot_gazebo.urdf.xacro檔案中4-41行註釋掉。

  2. MaxChanger|CSDN部落格配置完的hokuyo鐳射視角FOV為 from 1 2 π -\frac{1}{2}\pi to 1 2 π \frac{1}{2}\pi ,而我的hokuyo型號為UTM-30LX,FOV為 from 3 4 π -\frac{3}{4}\pi to 3 4 π \frac{3}{4}\pi 。而該設定與步驟6turtlebot_gazebo.urdf.xacro檔案中第89、90行。

<min_angle>-1.570796</min_angle>
<max_angle>1.570796</max_angle>

更改為:

<!-- FOV from -3*pi/4 to 3*pi/4 -->
<min_angle>-2.356194</min_angle> 
<max_angle>2.356194</max_angle>

配置中預設hokuyo安裝在turtlebot的前額,如果想安裝在正中間,可以將步驟5的第10行由:

<origin xyz="0.10 0 0.435" rpy="0 0.0 0.0" />

改為

<origin xyz="0.0 0 0.435" rpy="0 0.0 0.0" />

注意以上幾點後,配置完成後,就可以修改環境變數:

gedit .bashrc
#在最後新增以下內容
#For gazebo 
export  TURTLEBOT_BASE=kobuki
export  TURTLEBOT_STACKS=hexagons
export  TURTLEBOT_3D_SENSOR=hokuyo

之後執行,正常,鐳射有資料,話題/scan正常

source /opt/ros/indigo/setup.bash
roslaunch turtlebot_gazebo explorer.launch
roslaunch turtlebot_rviz_launchers view_navigation.launch

更改後的機器人turtlebot+鐳射hokuyo,注意此時Kinect已經不在機器人上了。
在這裡插入圖片描述
在這裡插入圖片描述
turtlebot+hokuyo在rviz中顯示的:

在這裡插入圖片描述

turtlebot+hokuyo在gazebo中:
在這裡插入圖片描述

3.2 模擬環境+cartographer

~/cartographer_turtlebot_ws/src/cartographer_turtlebot/launch資料夾下新建檔案turltebot_hokuyo.launch

<!-- turtlebot_hokuyo.launch -->
<launch>
  <param name="/use_sim_time" value="true" />

  <node name="cartographer_node" pkg="cartographer_ros"
      type="cartographer_node" args="
          -configuration_directory
              $(find cartographer_turtlebot)/configuration_files
          -configuration_basename turtlebot_hokuyo.lua"
      output="screen">
    <!--remap from="base_footprint" to="/base_link" /-->
    <!--remap from="scan" to="/laser_scan" /-->
  </node>

  <node name="flat_world_imu_node" pkg="cartographer_turtlebot"
      type="cartographer_flat_world_imu_node" output="screen">
    <!--remap from="imu_in" to="/mobile_base/sensors/imu_data_raw" /-->
    <remap from="imu_in" to="/mobile_base/sensors/imu_data" />
    <remap from="imu_out" to="/imu" />
  </node>

  <node name="rviz" pkg="rviz" type="rviz" required="true"
      args="-d $(find cartographer_turtlebot
          )/configuration_files/demo_turtlebot.rviz" />


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

~/cartographer_turtlebot_ws/src/cartographer_turtlebot/configuration_files資料夾下新建檔案turltebot_hokuyo.lua


<!-- turtlebot_hokuyo.lua -->
include "map_builder.lua"
include "trajectory_builder.lua"

options = {
  map_builder = MAP_BUILDER,
  trajectory_builder = TRAJECTORY_BUILDER,
  map_frame = "map",
  tracking_frame = "gyro_link",
  published_frame = "odom",
  odom_frame = "odom",
  provide_odom_frame = false,
  publish_frame_projected_to_2d = false,
  use_odometry = true,
  use_nav_sat = false,
  use_landmarks = false,
  num_laser_scans = 1,
  num_multi_echo_laser_scans = 0,
  num_subdivisions_per_laser_scan = 1,
  num_point_clouds = 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 = 0.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.min_range = 0.1
TRAJECTORY_BUILDER_2D.max_range = 8.
TRAJECTORY_BUILDER_2D.missing_data_ray_length = 5.
TRAJECTORY_BUILDER_2D.use_imu_data = false
TRAJECTORY_BUILDER_2D.use_online_correlative_scan_matching = true
TRAJECTORY_BUILDER_2D.motion_filter.max_angle_radians = math.rad(0.1)

POSE_GRAPH.constraint_builder.min_score = 0.65
POSE_GRAPH.constraint_builder.global_localization_min_score = 0.7

return options

按照以上配置完成後,執行

source /opt/ros/indigo/setup.bash
roslaunch turtlebot_gazebo explorer.launch
roslaunch cartographer_turtlebot turtlebot_hokuyo.launch

gazebo
在這裡插入圖片描述
rviz
在這裡插入圖片描述

debug…

大家只需要按照以上過程配置即可,此處記錄本人摸索過程遇到的問題,只為記錄,大家可以忽略。

當我直接利用原始的turtlebot_urg_lidar_2d.lua檔案時,出現錯誤map_by_time.h:43] Check failed: data.time > std::prev(trajectory.end())->first #831,相關問題及解答網址貼在此處以備查閱map_by_time.h:43,Check failed: data.time > std::prev(trajectory.end())->first #831

我採用這個人的方法,對.lua檔案做對應改正後,執行就沒問題了。
在這裡插入圖片描述

到目前,我們已經可以在gazebo模擬利用turtlebot+hokuyo與cartographer進行slam構建地圖與定位。

4. turtlebot+hokuyo+cartographer in real world

如果在gazebo中跑通了turtlebot+hokuyo+cartographer,就可以經過一些修改應用於真實的turtlebot機器人。

  • 1.安裝配置hokuyo_node
    如果ros是indigo版本,直接執行sudo apt-get install ros-indigo-hokuyo-node安裝。如果是kinetic版本,需要從源安裝。請先編譯完driver_common後再git clone hokuyo_node,編譯
cd ~/cartographer_turtlebot_ws/src
git clone https://github.com/ros-drivers/driver_common.git
cd ..
catkin_make
cd ~/cartographer_turtlebot_ws/src
git clone git clone https://github.com/ros-drivers/hokuyo_node.git 
cd ..
catkin_make

執行ls -l /dev/ttyACM0,如果報錯,沒有目標檔案,則說明計算機沒有配置ACM串列埠模組。對一般的PC應該不會出這種情況,我在英偉達的TX2上遇到過這種情況。解決方案請參照本人另一篇部落格TX2 安裝ttyACM串列埠驅動

如果顯示crw-rw-XX- 1 root dialout 166, 0 2009-10-27 14:18 /dev/ttyACM0,則正常,然後執行sudo chmod a+rw /dev/ttyACM0

然後turtlebot連線hokuyo,執行

roscore
rosrun hokuyo_node hokuyo_node

檢視是否釋出鐳射資料

rostopic echo /scan
  • 2.編定啟動launch檔案.
cd ~/cartograher_turtlebot_ws/src/cartographer_turtlebot/launch
gedit turtlebot_hokuyo_realworld.launch
<launch>
  <arg name="configuration_basename" />

  <include file="$(find turtlebot_bringup)/launch/minimal.launch" />
  <node name="hokuyo_node" pkg="hokuyo_node" type="hokuyo_node">
  	<param name="frame_id" value="hokuyo_link"/>
  </node>

  <node name="cartographer_node" pkg="cartographer_ros"
      type="cartographer_node" args="
          -configuration_directory
              $(find cartographer_turtlebot)/configuration_files
          -configuration_basename $(arg configuration_basename)"
      output="screen">
    <!--remap from="points2" to="/camera/depth/points" /-->
  </node>

  <node name="flat_world_imu_node" pkg="cartographer_turtlebot"
      type="cartographer_flat_world_imu_node" output="screen">
    <remap from="imu_in" to="/mobile_base/sensors/imu_data" />
    <remap from="imu_out" to="/imu" />
  </node>
  
  <node name="rviz" pkg="rviz" type="rviz" required="true"
      args="-d $(find cartographer_turtlebot
          )/configuration_files/demo_turtlebot.rviz" />

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

執行roslaunch cartographer_turtlebot turtlebot_hokuyo_realworld.launch


未完待續…