跳到正文

Wiki

Harmonic差速底盤仿真

約 2 分鐘閱讀

本文由簡體中文內容確定性轉換,並受版本化術語表保護。

自己建立一個差速底盤模型。控制器使用輪子反饋計算里程計,並與世界位姿對照。

1.示例 1:完整模型

保存 urdf/diff_gz.urdf.xacro:

<?xml version="1.0"?>
<robot name="learning_diffbot" xmlns:xacro="http://www.ros.org/wiki/xacro">
  <link name="base_link">
    <inertial><mass value="2"/><inertia ixx="0.02" ixy="0" ixz="0" iyy="0.03" iyz="0" izz="0.04"/></inertial>
    <visual><geometry><box size="0.3 0.2 0.08"/></geometry></visual>
    <collision><geometry><box size="0.3 0.2 0.08"/></geometry></collision>
  </link>

  <link name="left_wheel">
    <inertial><mass value="0.2"/><inertia ixx="0.00014" ixy="0" ixz="0" iyy="0.00025" iyz="0" izz="0.00014"/></inertial>
    <visual><origin rpy="1.57079632679 0 0"/><geometry><cylinder radius="0.05" length="0.03"/></geometry></visual>
    <collision><origin rpy="1.57079632679 0 0"/><geometry><cylinder radius="0.05" length="0.03"/></geometry></collision>
  </link>
  <joint name="left_wheel_joint" type="continuous">
    <parent link="base_link"/><child link="left_wheel"/>
    <origin xyz="0 0.15 0"/><axis xyz="0 1 0"/>
    <limit effort="5" velocity="20"/>
  </joint>

  <link name="right_wheel">
    <inertial><mass value="0.2"/><inertia ixx="0.00014" ixy="0" ixz="0" iyy="0.00025" iyz="0" izz="0.00014"/></inertial>
    <visual><origin rpy="1.57079632679 0 0"/><geometry><cylinder radius="0.05" length="0.03"/></geometry></visual>
    <collision><origin rpy="1.57079632679 0 0"/><geometry><cylinder radius="0.05" length="0.03"/></geometry></collision>
  </link>
  <joint name="right_wheel_joint" type="continuous">
    <parent link="base_link"/><child link="right_wheel"/>
    <origin xyz="0 -0.15 0"/><axis xyz="0 1 0"/>
    <limit effort="5" velocity="20"/>
  </joint>

  <link name="front_caster">
    <inertial><mass value="0.02"/><inertia ixx="0.0000032" ixy="0" ixz="0" iyy="0.0000032" iyz="0" izz="0.0000032"/></inertial>
    <visual><geometry><sphere radius="0.02"/></geometry></visual>
    <collision><geometry><sphere radius="0.02"/></geometry></collision>
  </link>
  <joint name="front_caster_fixed" type="fixed"><parent link="base_link"/><child link="front_caster"/><origin xyz="0.12 0 -0.03"/></joint>
  <gazebo reference="front_caster"><mu1>0.0</mu1><mu2>0.0</mu2></gazebo>

  <link name="rear_caster">
    <inertial><mass value="0.02"/><inertia ixx="0.0000032" ixy="0" ixz="0" iyy="0.0000032" iyz="0" izz="0.0000032"/></inertial>
    <visual><geometry><sphere radius="0.02"/></geometry></visual>
    <collision><geometry><sphere radius="0.02"/></geometry></collision>
  </link>
  <joint name="rear_caster_fixed" type="fixed"><parent link="base_link"/><child link="rear_caster"/><origin xyz="-0.12 0 -0.03"/></joint>
  <gazebo reference="rear_caster"><mu1>0.0</mu1><mu2>0.0</mu2></gazebo>

  <ros2_control name="DiffSystem" type="system">
    <hardware><plugin>gz_ros2_control/GazeboSimSystem</plugin></hardware>
    <joint name="left_wheel_joint"><command_interface name="velocity"/>
      <state_interface name="position"/><state_interface name="velocity"/>
    </joint>
    <joint name="right_wheel_joint"><command_interface name="velocity"/>
      <state_interface name="position"/><state_interface name="velocity"/>
    </joint>
  </ros2_control>

  <gazebo>
    <plugin filename="libgz_ros2_control-system.so" name="gz_ros2_control::GazeboSimROS2ControlPlugin">
      <parameters>$(find jazzy_control_hardware)/config/diff_controllers.yaml</parameters>
    </plugin>
  </gazebo>
</robot>

車體 x 朝前、y 朝左,兩輪軸沿 +y;輪距 0.30 m、半徑 0.05 m。前後支撐球降低轉彎阻力。參數用於教學,不代表某台真實機械人的辨識結果。

2.示例 2:完整配置

保存 config/diff_controllers.yaml,並在 package.xml 增加 diff_drive_controller 運行依賴:

<exec_depend>diff_drive_controller</exec_depend>
controller_manager:
  ros__parameters:
    update_rate: 100
    joint_state_broadcaster:
      type: joint_state_broadcaster/JointStateBroadcaster
    diff_controller:
      type: diff_drive_controller/DiffDriveController
diff_controller:
  ros__parameters:
    left_wheel_names: [left_wheel_joint]
    right_wheel_names: [right_wheel_joint]
    wheel_separation: 0.30
    wheel_radius: 0.05
    position_feedback: true
    open_loop: false
    cmd_vel_timeout: 0.5
    publish_rate: 50.0
    odom_frame_id: odom
    base_frame_id: base_link
    enable_odom_tf: true

本例 open_loop=false,輸入為 /diff_controller/cmd_vel,類型 TwistStamped。不要使用無時間戳 Twist,也不要從其它版本配置中搬入 use_stamped_vel。

3.編譯運行

cd ~/ros2_control_jazzy_ws
colcon build --symlink-install --packages-select jazzy_control_hardware
source install/setup.bash
ros2 launch jazzy_control_hardware sim.launch.py gui:=false \
  model:=$(ros2 pkg prefix jazzy_control_hardware)/share/jazzy_control_hardware/urdf/diff_gz.urdf.xacro \
  config:=$(ros2 pkg prefix jazzy_control_hardware)/share/jazzy_control_hardware/config/diff_controllers.yaml \
  controller:=diff_controller

另一終端 source 學習工作區後:

ros2 topic pub --rate 10 --use-sim-time /diff_controller/cmd_vel \
  geometry_msgs/msg/TwistStamped \
  "{header: auto, twist: {linear: {x: 0.08}, angular: {z: 0.0}}}"

這裏必須讓時間戳來自 /clock。等待仿真時鐘開始前進後,再檢查命令是否被接收。

4.示例 3:確認車體確實運動

ros2 topic echo /diff_controller/odom
ros2 topic echo /joint_states
gz topic -e -n 1 -t /world/empty/pose/info

按模型名找到 learning_robot,比較不同時刻的 position。輪子里程計與世界位姿可能因接觸和打滑而不同;只看到輪子轉動不能證明車體移動。

停止直行發佈後,可將 angular.z 改為 0.3、linear.x 改為 0 來觀察轉彎。停止發佈後檢查速度目標歸零和實際停車過程。

4.1.排查順序

控制器不 active 先查接口和參數;輪子不轉先查目標與時鐘;輪子轉而車不走再查碰撞、摩擦、軸向和接觸。不要用 open_loop=true 掩蓋物理模型問題。


返回 Jazzy 總覽 · 上一篇:DiffBot與差速底盤控制 · 下一篇:多接口與硬件類型