YDLIDAR X4とGoogle Cartographerで地図を作る（Pi4、MATE、melodic）


* Raspberry Pi 4 4GB
* ubuntu server 18.04.5 + Ubuntu MATE
* ROS melodic
* 本家EAIのYDLIDARパッケージは時間単位が適切でないため、idev_jp氏公開のパッケージを使用し、
lanch、configファイルは5montest氏公開のものを使用させていただきました。

#Ubuntu MATEのインストール
Raspberry Pi 4にubuntu server 18.04.5をインストール
sudo apt update
sudo apt upgrade
sudo apt install ubuntu-mate-desktop

<dl>
  <dt>その他、必要に応じて</dt>
  <dd>日本語入力の環境設定</dd>
  <dd>swapファイルの作成</dd>
  <dd>タイムゾーンの変更</dd>
  <dd>画面解像度の変更</dd>
</dl>

#ROS melodicのインストール
bash -c "$(curl -SsfL https://git.io/ros-melodic-desktop)"
source ~/.bashrc
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_init_workspace src
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
catkin_make && source ~/catkin_ws/devel/setup.bash

#YDLIDAR X4の設定と動作確認
出典：https://qiita.com/idev_jp/items/579933851d3cf1dfc404
cd ~/catkin_ws/
git clone https://github.com/koki-ogura/ydlidar.git src/ydlidar
sudo sh ~/catkin_ws/src/ydlidar/startup/initenv.sh 
catkin_make
source devel/setup.bash
roslaunch ydlidar lidar_view.launch

#Google Cartographerのインストール
参考：トランジスタ技術 2019年3月号
sudo apt install qt4-qmake qt4-dev-tools
sudo apt install python-wstool python-rosdep ninja-build stow

cd ~/catkin_ws/
wstool init src
wstool merge -t src https://raw.githubusercontent.com/googlecartographer/cartographer_ros/master/cartographer_ros.rosinstall
wstool update -t src

src/cartographer/scripts/install_proto3.sh 
sudo rosdep init
rosdep update
rosdep install --from-paths src --ignore-src --rosdistro=${ROS_DISTRO} -y

cd src
git clone https://github.com/GT-RAIL/robot_pose_publisher.git

cd robot_pose_publisher/src/
nano robot_pose_publisher.cpp
nh_priv.param<bool>("is_stamped", is_stamped, true);　 false -> true

cd ~/catkin_ws/
sudo apt install stow
src/cartographer/scripts/install_abseil.sh

launch、lua script 出典：https://github.com/5montest/cartographer_ydlidar

```xml:/home/ubuntu/catkin_ws/src/cartographer_ros/cartographer_ros/launch/ydlidar_2d.launch
<?xml version="1.0" ?>
<launch>
  <node
    name="horizontal_laser"
    pkg="ydlidar"
    type="ydlidar_node"
    output="screen">
  </node>

  <node
     pkg="tf"
     type="static_transform_publisher"
     name="base_link_connect"
     args="0 0 0 0 0 0 /base_link /laser_frame 100"
  />

  <node
    name="cartographer_occupancy_grid_node"
    pkg="cartographer_ros"
    type="cartographer_occupancy_grid_node"
    args="-resolution 0.05"
  />
  <node
    name="cartographer_node"
    pkg="cartographer_ros"
    type="cartographer_node"
    args="-configuration_directory $(find cartographer_ros)/configuration_files -configuration_basename ydlidar_2d.lua"
    output="screen">
  </node>

  <node
    name="rviz"
    pkg="rviz"
    type="rviz"
    required="true"
    args="-d $(find ydlidar)/launch/lidar.rviz"
  />
</launch>
```

```file:/home/ubuntu/catkin_ws/src/cartographer_ros/cartographer_ros/configuration_files/ydlidar_2d.lua
include "map_builder.lua"
include "trajectory_builder.lua"

options = {
  map_builder = MAP_BUILDER,
  trajectory_builder = TRAJECTORY_BUILDER,
  map_frame = "map",
  tracking_frame = "base_link",
  published_frame = "base_link",
  odom_frame = "odom",
  provide_odom_frame = false,
  publish_frame_projected_to_2d = false,
  use_odometry = false,
  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 = 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.
TRAJECTORY_BUILDER_2D.max_range = 10.
TRAJECTORY_BUILDER_2D.missing_data_ray_length = 5.
TRAJECTORY_BUILDER_2D.use_imu_data = false
TRAJECTORY_BUILDER_2D.use_online_correlative_scan_matching = true

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

POSE_GRAPH.optimization_problem.local_slam_pose_translation_weight = 1e5
POSE_GRAPH.optimization_problem.local_slam_pose_rotation_weight = 1e5
POSE_GRAPH.optimization_problem.odometry_translation_weight = 1e5
POSE_GRAPH.optimization_problem.odometry_rotation_weight = 1e5
POSE_GRAPH.optimization_problem.huber_scale = 1e3

TRAJECTORY_BUILDER_2D.ceres_scan_matcher.occupied_space_weight = 10
TRAJECTORY_BUILDER_2D.ceres_scan_matcher.rotation_weight = 40

TRAJECTORY_BUILDER_2D.submaps.num_range_data = 120
TRAJECTORY_BUILDER_2D.motion_filter.max_distance_meters = 0.1
TRAJECTORY_BUILDER_2D.motion_filter.max_angle_radians = math.rad(0.2)

return options

```

cd ~/catkin_ws/
catkin_make_isolated --install --use-ninja
source install_isolated/setup.bash
roslaunch cartographer_ros ydlidar_2d.launch