2025年8月27日 星期三

Rviz2 for Twowheel

 要在 RViz2 裡觀察你兩輪車(twowheel_amr)的狀態與感測器資料,可以走這個最小流程:


1️⃣ 準備工作

先確定三個東西都在跑:

  1. gzserver(Gazebo Classic 伺服器,載入了 libgazebo_ros_factory.so

  2. spawn_entity.py(已把 twowheel_amr 放進 Gazebo)

  3. robot_state_publisher(用 robot_description 廣播 TF,讓 RViz2 看得到機器結構)

你可以用下面這個最小 Launch File(display.launch.py)幫你啟 robot_state_publisher:

from launch import LaunchDescription from launch.substitutions import Command, LaunchConfiguration from launch.actions import DeclareLaunchArgument from launch_ros.actions import Node def generate_launch_description(): urdf_file = LaunchConfiguration('urdf_file') return LaunchDescription([ DeclareLaunchArgument( 'urdf_file', default_value='/absolute/path/to/twowheel.fixed.urdf.xacro', description='URDF Xacro file' ), Node( package='robot_state_publisher', executable='robot_state_publisher', parameters=[{'robot_description': Command(['xacro ', urdf_file])}], output='screen' ), Node( package='joint_state_publisher_gui', executable='joint_state_publisher_gui' ) ])

啟動:

ros2 launch twowheel_bot display.launch.py urdf_file:=/absolute/path/to/twowheel.fixed.urdf.xacro




================


2️⃣ 開啟 RViz2

在另一個終端執行:

rviz2

3️⃣ RViz2 裡的設定

  1. 左下角 Add → 選擇 RobotModel → 你應該會看到 twowheel_amr 的 URDF。

    • Description Source 設為 robot_description

    • 參考座標框(Fixed Frame)設為 odombase_footprint

  2. Add → 選擇 LaserScan

    • Topic 設為 /twowheel_amr/scan(如果你用我幫你改的 <output_type>LaserScan</output_type>

    • Style 建議設為 PointsFlat Squares

  3. Add → 選擇 Imu

    • Topic 設為 /twowheel_amr/imu/data

    • 可以勾選「Axes」或「Imu」顯示方向。

  4. 如果需要 odometry,AddOdometry,topic 設為 /odom


4️⃣ 驗證

  • 在 RViz2 應該可以看到你的兩輪車模型。

  • LaserScan 會顯示一圈雷射點。

  • IMU 可以看到方向箭頭隨 Gazebo 車體晃動而改變。

  • 你也可以用指令確認:

    ros2 topic list | egrep "scan|imu|odom" ros2 topic echo /twowheel_amr/scan --once ros2 topic echo /twowheel_amr/imu/data --once

要不要我幫你直接寫一個 整合版 twowheel_rviz.launch.py

(同時跑 robot_state_publisher + rviz2,

並且自動載入一個配置好的 RViz2 設定檔 .rviz,直接打開就能看到車體+雷射+IMU)?


沒有留言:

張貼留言