アプリとサービスのすすめ

アプリやIT系のサービスを中心に書いていきます。たまに副業やビジネス関係の情報なども気ままにつづります

Jetbotをgazeboで動かすまで その2

前回の続きでgazeboで動かすのに必要なところをまとめてくだけ。
シュミレーション環境にはturtlebot3を使った。


目次
1. gazeboの環境設定
2. JetbotのXacroファイルをgazeboに対応させる
3. gazeboのコントロール用のdiff_drive_controllerのパラメーターの設定
4. Jetbotをシュミレーションで動かす




1. gazeboの環境設定

まずgazeboで動かせるようにする。
今回はturtlebot3の環境を使うので、簡単なinstallから。

# gazeboコントローラー
$ sudo apt-get install ros-noetic-ros-control
$ sudo apt-get install ros-noetic-ros-controllers

# turtlebot3 プラグインのinstall
$ git clone https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
$ git clone https://github.com/ROBOTIS-GIT/turtlebot3.git
$ git clone https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
$ git clone https://github.com/ROBOTIS-GIT/turtlebot3_gazebo_plugin git

# ~/.bashrcに記入 
export TURTLEBOT3_MODEL=burger
# catkin_makeして再起動
cd ~/catkin_ws && cakin_make
source ~/.bashrc
sudo reboot

2. JetbotのXacroファイルをgazeboに対応させる

wheel(車輪)にtransmissionタグの記述

<transmission name="${prefix}_trans" type="SimpleTransmission">
    <type>transmission_interface/SimpleTransmission</type>
    <actuator name="${prefix}_motor">
      <mechanicalReduction>1</mechanicalReduction>
    </actuator>
    <joint name="${prefix}_joint">
      <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
    </joint>
</transmission>

重要な点は1つで指定したjointをどの指令値で操作するかだけ。jointの指令のタイプには以下の3種類があります。

・hardware_interface/EffortJointInterface : 力指令で動く
・hardware_interface/VelocityJointInterface : 速度指令で動く
・hardware_interface/PositionJointInterface : 位置指令で動く

基本的にはEffortJointInterfaceが一番gazeboと相性が良いけど、今回使う差動2輪用のドライバでは右輪と左輪の両方ともにVelocityJointInterfaceを使う必要がある。


ros_controllの追加などのgazebo pluginの追加

<!-- ros_control plugin -->
<gazebo>
  <plugin name="gazebo_ros_control" filename="libgazebo_ros_control.so">
    <robotNamespace>/jetbot</robotNamespace>
    <robotSimType>gazebo_ros_control/DefaultRobotHWSim</robotSimType>
    <legacyModeNS>true</legacyModeNS>
  </plugin>
</gazebo>


下にはOdometry(移動距離を求めるプラグイン)なので必須じゃないです。

<!-- Odometry plugin -->
<gazebo>
  <plugin name="ground_truth" filename="libgazebo_ros_p3d.so">
    <frameName>world</frameName>
    <bodyName>base_link</bodyName>
    <topicName>/tracker</topicName>
    <updateRate>10.0</updateRate>
  </plugin>
</gazebo>


link関連のgazebo要素の追加
ここでは摩擦係数とgazebo上での色を設定。

<gazebo reference="base_link">
    <mu1>0.2</mu1>
    <mu2>0.2</mu2>
    <material>Gazebo/white</material>
</gazebo>

3. gazeboのコントロール用のdiff_drive_controllerのパラメーターの設定

これはyamlファイルに書いて、起動用のlaunchファイルで読み込む。

jetbot_controller.yaml

joint_state_controller:
  type: "joint_state_controller/JointStateController"
  publish_rate: 50

diff_drive_controller:
type        : "diff_drive_controller/DiffDriveController"
left_wheel  : 'left_wheel_joint'
right_wheel : 'right_wheel_joint'
publish_rate: 50.0
pose_covariance_diagonal : [0.001, 0.001, 1000000.0, 1000000.0, 1000000.0, 1000.0]
twist_covariance_diagonal: [0.001, 0.001, 1000000.0, 1000000.0, 1000000.0, 1000.0]

# Velocity commands timeout [s], default 0.5
cmd_vel_timeout: 1.0
# Wheel separation and diameter. These are both optional.
# diff_drive_controller will attempt to read either one or both from the
# URDF if not specified as a parameter
wheel_separation : 0.43515
wheel_radius : 0.193125

# Wheel separation and radius multipliers
wheel_separation_multiplier: 1.0 # default: 1.0
wheel_radius_multiplier    : 1.0 # default: 1.0 
# tf
# enable_odom_tf: true
base_frame_id: base_link
odom_frame_id: odom

# limits
linear:
  x:
    has_velocity_limits    : true
    max_velocity           :  0.825 # m/s
    min_velocity           : -0.825 # m/s
    has_acceleration_limits: true
    max_acceleration       :  1.0 # m/s^2
    min_acceleration       : -1.0 # m/s^2
angular:
  z:
    has_velocity_limits    : true
    max_velocity           :  3.14 # rad/s
    min_velocity           : -3.14 # rad/s
    has_acceleration_limits: true
    max_acceleration       :  1.0  # rad/s^2
    min_acceleration       : -1.0 # rad/s^2
left_wheel : 'left_wheel_joint' 
right_wheel : 'right_wheel_joint'

ではそれぞれ左右輪に相当するjointを指定

wheel_separation : 0.20 # タイヤの半径
wheel_radius : 0.05   # 2つのタイヤの間の距離を設定


逆向きに走らせたいとき
URDF(Xacro)ファイルの車輪(wheel)のjointのaxisタグのy軸マイナスにすればOK。
yamlファイルでは指定できない。

<!-- 対象部分 -->
<axis xyz="0 -1 0"/>

<!-- Joint本体 -->
<joint name="${prefix}_joint" type="continuous">
      <parent link="${parent}"/>
      <child link="${prefix}_link"/>
      <origin xyz="${xyz}" rpy="0 0 0"/>
      <axis xyz="0 -1 0"/>
</joint>

起動用launchファイル


jetbot.launch

<?xml version="1.0" encoding="UTF-8"?>
<launch>
  <arg name="model" default="$(find jetbot_description)/urdf/jetbot.xacro" />
  <param name="robot_description" command="$(find xacro)/xacro $(arg model) --inorder"/>
  <include file="$(find gazebo_ros)/launch/empty_world.launch">
    <arg name="world_name" value="$(find jetbot_description)/worlds/turtlebot3_world.world"/>
    <arg name="paused" value="false"/>
    <arg name="use_sim_time" value="true"/>
    <arg name="gui" value="true"/>
    <arg name="headless" value="false"/>
    <arg name="debug" value="false"/>
  </include>

  <!-- Load the URDF into the ROS Parameter Server -->
  <param name="robot_description" command="$(find xacro)/xacro $(arg model) --inorder"/>
  <!-- Run a python script to the send a service call to gazebo_ros to spawn a URDF robot -->
  <node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" args="-param robot_description -urdf -model jetbot" />

  <!-- ros_control motoman launch file -->
  <include file="$(find jetbot_control)/launch/jetbot_control.launch"/>
</launch>

jetbot_control.launch

<launch>

  <!-- Load joint controller configurations from YAML file to parameter server -->
  <rosparam command="load" file="$(find jetbot_control)/config/jetbot_controller.yaml" ns="/jetbot" />

  <!-- load the controllers -->
  <node name="controller_spawner" pkg="controller_manager" type="spawner" respawn="false" output="screen" ns="/jetbot" 
      args="joint_state_controller diff_drive_controller" />

  <!-- running of robot_state_publisher -->
  <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" />
</launch>

4. Jetbotをシュミレーションで動かす

動かしてみる。

$ roslaunch jetbot_simulation jetbot.launch

$ roslaunch jetbot_simulation wheel_simulation.launch

wheel_simulation.launch

<launch>
  <arg name="cmd_vel" default="/jetbot/diff_drive_controller/cmd_vel"/>
  <arg name="name" default="jetbot_drive"/>
  <param name="cmd_vel_topic_name" value="$(arg cmd_vel)"/>
  <node name="jetbot_drive" pkg="jetbot_simulation" type="jetbot_drive" required="true" output="screen"/>
</launch>


f:id:trafalbad:20220314234430g:plain
動かせた。



参考

ROS講座39 車輪ロボットを作る3(diff_drive_controllerで動かす)
【ROS】 Turtlebot3 のシミュレーション環境を構築してみた
Gazebo + ROS で自分だけのロボットをつくる 7. 実際にlaunchしてみる
Gazebo Components
ROSの勉強 第31弾:移動ロボットの作成(4)