welcom ! Handel home

2025年2月18日 星期二

(Robot_arm)Gazebo 降低 Timer 時序速度

 

 如何降低 Gazebo Timer 時序速度

Gazebo 模擬中,有時你可能需要降低模擬時間速度,讓模擬過程更慢,以便分析機器人行為、偵錯控制演算法或進行低速測試。Gazebo 提供 幾種方法 來調整模擬速度,包括 降低 real-time factor、修改 physics update rate、調整 sensor 頻率 等。


1️⃣ 方法 1:降低 real_time_factor

🔹 什麼是 real_time_factor

real_time_factor(即時係數)表示 Gazebo 模擬時間與真實時間的比率:

  • real_time_factor = 1.0預設) → 模擬時間 = 真實時間
  • real_time_factor = 0.5 → 模擬時間速度減半(變慢)
  • real_time_factor = 2.0 → 模擬時間加快 2 倍

✅ 調整 real_time_factor 方法

方式 1:在 Gazebo GUI 內手動調整

  1. 啟動 Gazebo
    bash
    roslaunch gazebo_ros empty_world.launch
  2. 開啟 Gazebo GUI
    • 點擊 World > Physics 設定
    • 調整 Real Time Factor,例如改為 0.5

方式 2:透過 ROS 指令修改

在運行 Gazebo 時,可以透過 rosservice call 來修改 real_time_factor

bash
rosservice call /gazebo/set_physics_properties "time_step: 0.001 max_update_rate: 1000 gravity: {x: 0, y: 0, z: -9.81} ode_config: {solver_type: 1, sor_pgs_precon_iters: 0, sor_pgs_iters: 50, sor_pgs_w: 1.3, sor_pgs_rms_error_tol: 0.0, contact_surface_layer: 0.001, contact_max_correcting_vel: 100.0, cfm: 0.0, erp: 0.2, max_contacts: 20} real_time_factor: 0.5"

這將 real_time_factor 設定為 0.5(速度變慢一半)。


2️⃣ 方法 2:降低 max_update_rate

🔹 什麼是 max_update_rate

max_update_rate 控制 Gazebo 模擬每秒更新次數,降低此數值可減慢整個模擬速度:

  • max_update_rate = 1000預設) → Gazebo 每秒更新 1000 次
  • max_update_rate = 500 → 更新頻率降低,模擬變慢
  • max_update_rate = 100 → 進一步降低模擬速度

✅ 修改 max_update_rate 方法

world 檔案(.world)中,找到 physics 區塊:

xml
<physics type="ode"> <real_time_update_rate>500</real_time_update_rate> <!-- 降低更新率 --> <max_step_size>0.001</max_step_size> </physics>

這將 降低模擬速度,使 Gazebo 以較慢的頻率更新機器狀態。


3️⃣ 方法 3:降低 sensor update_rate

🔹 什麼是 update_rate

Gazebo 感測器(如相機、LiDAR、IMU)有自己的 更新頻率,過高的更新頻率會導致 Gazebo 運行過快。

✅ 降低感測器更新頻率

如果你的機器人使用 LiDAR 或 Camera,可以在 URDF / SDF 設定 update_rate

xml
<sensor name="camera" type="camera"> <update_rate>10</update_rate> <!-- 10Hz,降低頻率 --> </sensor> <sensor name="lidar" type="ray"> <update_rate>5</update_rate> <!-- 5Hz,讓 LiDAR 更新變慢 --> </sensor>

update_rate 設為較小值(如 5Hz 或 10Hz),可以減少感測器的計算負擔,降低模擬速度。


4️⃣ 方法 4:降低 max_step_size

🔹 什麼是 max_step_size

max_step_size 定義了每個物理步驟(Simulation Step)時間間隔:

  • 預設為 0.001s(1 ms),即每秒 1000 次模擬步驟。
  • 若設為 0.005s(5 ms),則每秒只有 200 次模擬步驟,使 Gazebo 變慢。

✅ 修改 max_step_size

.world 文件內:

xml
<physics type="ode"> <max_step_size>0.005</max_step_size> <!-- 讓模擬步驟變慢 --> </physics>

這樣 Gazebo 每個時間步長增加,導致整體模擬速度變慢


5️⃣ 方法 5:降低 CPU 核心使用

Gazebo 可能會佔用所有可用的 CPU 資源來運行模擬,可以透過 限制 CPU 使用率 來間接減慢模擬速度。

✅ 限制 CPU 使用

在啟動 Gazebo 時,加上 CPU 限制

bash
taskset -c 0 roslaunch gazebo_ros empty_world.launch

這將 限制 Gazebo 只使用 CPU 核心 0,導致模擬變慢。


📌 結論

方法適用場景設定方式
降低 real_time_factor調整模擬速度,讓時間比率變慢GUI 或 rosservice call
降低 max_update_rate降低物理模擬更新頻率修改 .world 文件
降低 sensor update_rate減少感測器更新頻率修改 .urdf / .sdf
降低 max_step_size增加模擬步長,降低計算負擔修改 .world 文件
限制 CPU 使用降低 Gazebo 計算速度taskset -c 0

🚀 建議

  • 如果要降低整體模擬速度 → 使用 real_time_factor
  • 如果要減少 Gazebo 計算負擔 → 降低 max_update_ratemax_step_size
  • 如果要降低感測器頻率 → 調整 sensor update_rate

🎯 試試這些方法,讓 Gazebo 以更慢的速度運行,適應你的開發需求! 🚀

沒有留言: