How to add the suspension model method in Gazebo / how the model in Gazebo does not fall due to gravity: modify the sdf and urdf models

Table of contents

1. Problem description:

2. Solution:

2.1 SDF model:

2.2 URDF model:

2.3 Test Add Model

 3. Add the model in Gazebo through the Python program


1. Problem description:

When using ros to do simulation experiments, sometimes it is necessary to add a model file in the space to make it float at a certain coordinate in the space, but often the model will fall directly to the ground due to gravity

2. Solution:

Modify the model file to disable the gravity label

2.1 SDF model:

Find your model file directory, if your model file is in .sdf format, find the gravity tag <gravity> part of the model code

 <gravity>1</gravity>

Change the number 1 to 0 to disable gravity

<gravity>0</gravity> <!-- 禁用重力 -->

This will stop Gazebo from applying gravity to the model, making it levitate in mid-air. Other properties such as inertia and collision remain unchanged, just modify <gravity>the label. After saving the file, reload the model into Gazebo and it will be suspended in mid-air without being affected by gravity.

After saving the file and re-adding the model in Gazebo, it can be suspended in mid-air;

2.2 URDF model:

Find your model file directory, if your model file is in .urdf format, to make the model suspended in mid-air without being affected by gravity, you can set the model's inertia property to zero appropriately.

In URDF files, the inertia attribute can <inertial>be set on each link (link) element. Make sure to set the inertia property to an appropriate zero value in the link you want to float

An example is as follows:

<link name="floating_link">
  <inertial>
    <mass value="0.0"/>
    <!-- Set all inertia values to zero -->
    <inertia ixx="0.0" ixy="0.0" ixz="0.0" iyy="0.0" iyz="0.0" izz="0.0"/>
  </inertial>
  <!-- Other visual, collision, and other elements -->
</link>

By setting the mass value to 0, and setting all inertia values ​​to 0, you can make the link float in mid-air and not be affected by gravity. It should be noted that if the link is connected to other links, you may also need to check the properties of its parent or child links to ensure that the entire model meets your requirements.

Likewise, you can make some adjustments as needed to achieve the right balance for your model. After making changes, reload the URDF file into ROS and Gazebo, and your model should be suspended in mid-air without being affected by gravity.

2.3 Test Add Model

Use the program to add the model at coordinates (1, 1, 1), you can see that it can be suspended in this coordinate system

 3. Add the model in Gazebo through the Python program

One thing to note here is that after the gravity label of the model file is disabled, the model is in a state of zero gravity. Imagine the state of an astronaut in a space station, and combined with middle school physics knowledge, as long as there is an initial velocity, he will carry out It moves in a straight line at a constant speed and is difficult to stop.

So if you add the model directly with the mouse, there is a high probability that there will be an upward initial velocity. At this time, it is recommended to use additional script commands to add the model. Here I use the Python program

# -*- coding: utf-8 -*-
#!/usr/bin/env python

import os
import rospy
from gazebo_msgs.msg import ModelState
from gazebo_msgs.srv import DeleteModel, SpawnModel
from std_msgs.msg import Header
from geometry_msgs.msg import Pose, Point

# 初始化ROS节点
rospy.init_node('spawn_aruco_cubo_hover', anonymous=True)

# 定义生成模型的函数
def spawn_aruco_cubo_hover():
    model_name = "aruco_cubo_hover"
    model_path = "/home/sjh/project/Tiago_ws/src/pal_gazebo_worlds/models/aruco_cube_hover/aruco_cube_hover.sdf"
    initial_pose = Pose(position=Point(x=1, y=1, z=1))

    # 从文件加载模型
    with open(model_path, "r") as f:
        model_xml = f.read()

    # 调用Gazebo的SpawnModel服务
    spawn_model = rospy.ServiceProxy('/gazebo/spawn_sdf_model', SpawnModel)
    resp_sdf = spawn_model(model_name, model_xml, "", initial_pose, "world")

    if resp_sdf.success:
        rospy.loginfo("模型 '{}' 生成成功。".format(model_name))
    else:
        rospy.logerr("模型 '{}' 生成失败。".format(model_name))

# 调用生成模型的函数
if __name__ == '__main__':
    try:
        spawn_aruco_cubo_hover()
    except rospy.ROSInterruptException:
        pass

When using the program, pay attention to modify the model path and specified coordinates in the spawn_aruco_cubo_hover function to what you need

def spawn_aruco_cubo_hover():
    model_name = "你的模型的名称"
    model_path = "你的模型的路径"
    initial_pose = Pose(position=Point(x=1, y=1, z=1)) # xyz值就是指定坐标位置

Guess you like

Origin blog.csdn.net/weixin_45498383/article/details/132100415