如何计算Gazebo下的惯性参数 How to compute inertia in gazebo

Overview

An accurate simulation requires physically plausible inertial parameters: the mass, center of mass location, and the moment of inertia matrix of all links. This tutorial will guide you through the process of obtaining and setting these parameters if you have 3D models of the links.

Assuming homogeneous bodies (uniform mass density), it is shown how to obtain inertial data using the free software MeshLab. If you wish to skip the setup and only compute the volume, center of mass, or inertia properties of your model, or quickly clean the model, you can use Mesh Cleaner, a tool which runs MeshLab internally for this purpose. You can also use the commercial product SolidWorks to compute these information. For a guide on using SolidWorks, please refer to this question on answers.ros.org.

精确的模拟需要物理上可信的惯性参数:质量、质心位置和所有连杆的转动惯量矩阵。如果你有连杆的三维模型,本教程将指导您完成获取和设置这些参数的过程。
假设均匀体(质量密度均匀),说明如何使用免费软件Meshlab获得惯性数据。如果您希望跳过设置,只计算模型的体积、重心或惯性属性,或者快速清理模型,则可以使用Mesh Cleaner,一种在内部为此目的运行MeshLab的工具。您还可以使用商业产品solidworks来计算这些信息。有关使用solidworks的指南,请参阅answers.ros.org上的此问题。

Summary of inertial parameters

Mass

The mass is most easily measured by weighing an object. It is a scalar with default units in Gazebo of kilograms (kg). For a 3D uniform mesh, mass is computed by calculating the geometric volume [length3] and multiplying by density [mass / length3].

质量最容易通过称物体来测量。它是一个标量,默认单位为千克(kg)。对于三维均匀网格,通过计算几何体积[长度3]并乘以密度[质量/长度3]来计算质量。

Center of Mass

The center of mass is the point where the sum of weighted mass moments is zero. For a uniform body, this is equivalent to the geometric centroid. This parameter is a Vector3 with units of position [length].

质心是加权质量矩之和为零的点。对于均匀体,这相当于几何形心。此参数是一个以位置[长度]为单位的三维矢量。

Moment of Inertia Matrix

The moments of inertia represent the spatial distribution of mass in a rigid body. It depends on the mass, size, and shape of a body with units of [mass * length2]. The moments of inertia can be expressed as the components of a symmetric positive-definite 3x3 matrix, with 3 diagonal elements, and 3 unique off-diagonal elements. Each inertia matrix is defined relative to a coordinate frame or set of axes. Diagonalizing the matrix yields its principal moments of inertia (the eigenvalues) and the orientation of its principal axes (the eigenvectors).

The moments of inertia are proportional to mass but vary in a non-linear manner with respect to size. Additionally, there are constraints on the relative values of the principal moments that typically make it much more difficult to estimate moments of inertia than mass or center of mass location. This difficulty motivates the use of software tools for computing moment of inertia.

If you're curious about the math behind the inertia matrix, or just want an easy way to calculate the tensor for simple shapes, this wikipedia entry is a great resource.

惯性矩代表了刚体中质量的空间分布。它取决于身体的质量、大小和形状,单位为[质量*长度2]。转动惯量可以表示为对称正定3x3矩阵的分量,其中有3个对角元素和3个唯一的非对角元素。每个惯性矩阵都是相对于一个坐标系或一组轴定义的。矩阵对角化产生其主惯性矩(特征值)和主轴方向(特征向量)。
惯性矩与质量成正比,但与尺寸成非线性变化。此外,主力矩的相对值也受到限制,这通常使估算惯性矩比质量或质心位置更困难。这一困难促使使用软件工具计算惯性矩。
如果你对惯性矩阵背后的数学很好奇,或者只是想用一种简单的方法来计算简单形状的张量,那么这个维基百科条目是一个很好的资源。

Preparation

Installing MeshLab

Download MeshLab from the official website and install it on your computer. The installation should be straightforward.

Once installed, you can view your meshes in MeshLab (both DAE and STL formats are supported, which are those ones supported by Gazebo/ROS).

Computing the inertial parameters

Computing inertia of sphere

Open the mesh file in MeshLab. For this example, a sphere.dae mesh is used. To compute the inertial parameters, you first need to display the Layers dialog - View->Show Layer Dialog. A panel opens in the right part of the window which is split in half - we're interested in the lower part containing text output.

Next, command MeshLab to compute the inertial parameters. Choose Filters->Quality Measure and Computations->Compute Geometric Measures from the menu. The lower part of the Layers dialog should now show some info about the inertial measures. The sphere gives the following output:

在MeshLab中打开网格文件。在本例中,使用了sphere.dae网格。要计算惯性参数,首先需要显示“图层”对话框-“视图”->“显示图层”对话框。一个面板打开在窗口的右半部分,我们对包含文本输出的下半部分感兴趣。
接下来,命令meshlab计算惯性参数。从菜单中选择过滤器->质量测量和计算->计算几何测量。图层对话框的下部现在应该显示一些关于惯性测量的信息。球体提供以下输出:

Mesh Bounding Box Size 2.000000 2.000000 2.000000
Mesh Bounding Box Diag 3.464102
Mesh Volume is 4.094867
Mesh Surface is 12.425012
Thin shell barycenter -0.000000 -0.000000 -0.000000
Center of Mass is -0.000000 0.000000 -0.000000
Inertia Tensor is :
| 1.617916 -0.000000 0.000000 |
| -0.000000 1.604620 -0.000000 |
| 0.000000 -0.000000 1.617916 |
Principal axes are :
| 0.000000 1.000000 0.000000 |
| -0.711101 -0.000000 0.703089 |
| -0.703089 0.000000 -0.711101 |
axis momenta are :
| 1.604620 1.617916 1.617916 |

Radius 半径

The bounding box of the sphere is a cube with side length 2.0, which implies that the sphere has a radius of 1.0.
球体的边界框是边长为2.0的立方体,这意味着球体的半径为1.0。

Volume 体积

A sphere of radius 1.0 should have a volume of 4/3*PI (4.189), which is close to the computed value of 4.095. It is not exact since it is a triangular approximation.

半径为1.0的球体的体积应为4/3*pi(4.189),接近计算值4.095。这是不精确的,因为它是一个三角形近似值。

Surface Area 表面积

The surface area should be 4*PI (12.566), which is close to the computed value of 12.425.

表面积应为4*pi(12.566),接近12.425的计算值

Center of Mass

The center of mass is given as the origin (0,0,0).

Inertia matrix 惯性矩阵

The inertia matrix (aka inertia tensor) of a sphere should be diagonal with principal moments of inertia of 2/5 mass since radius = 1. It is not explicitly stated in the output, but the mass is equal to the volume (implicitly using a density of 1), so we would expect diagonal matrix entries of 8/15*PI (1.676). The computed inertia tensor appears diagonal for the given precision with principal moments ranging from [1.604,1.618], which is close to the expected value.

由于半径=1,球体的惯量矩阵(亦称惯量张量)应与质量为2/5的主惯量成对角线。输出中没有明确说明,但质量等于体积(隐式使用密度为1),因此我们希望对角矩阵条目为8/15*pi(1.676)。对于给定精度,计算的惯性张量呈对角线,主力矩范围为[1.604,1.618],接近预期值。

Duplicate faces 复制面

One thing to keep in mind is that duplicate faces within a mesh will affect the calculation of volume and moment of inertia. For example, consider another spherical mesh: ball.dae. Meshlab gives the following output for this mesh:
要记住的一点是,网格内的复制面将影响体积和惯性矩的计算。例如,考虑另一个球面网格:ball.dae。meshlab为此网格提供以下输出:

Mesh Bounding Box Size 1.923457 1.990389 1.967965
Mesh Bounding Box Diag 3.396207
Mesh Volume is 7.690343
Mesh Surface is 23.967396
Thin shell barycenter 0.000265 0.000185 0.000255
Center of Mass is 0.000257 0.000195 0.000292
Inertia Tensor is :
| 2.912301 0.001190 0.000026 |
| 0.001190 2.903731 0.002124 |
| 0.000026 0.002124 2.906963 |
Principal axes are :
| 0.108262 -0.895479 0.431738 |
| -0.120000 0.419343 0.899862 |
| 0.986853 0.149229 0.062058 |
axis momenta are :
| 2.902563 2.907949 2.912483 |

This mesh is approximately the same size, with bounding box dimensions in the range [1.92,1.99], but its calculations are different by nearly double:

  • volume: 7.69 vs. 4.09
  • principal moments: [2.90,2.91] vs. [1.60,1.62]

There is a clue to the difference when you look at the numbers of vertices and faces (listed in the bottom of the MeshLab window):

  • sphere.dae: 382 vertices, 760 faces
  • ball.dae: 362 vertices, 1440 faces

Each mesh has a similar number of vertices, but ball.dae has roughly twice as many faces. Running the command Filters -> Cleaning and Repairing -> Remove Duplicate Faces reduces the number of faces in ball.dae to 720 and gives more reasonable values for the volume (3.84) and principal moments of inertia (1.45). It makes sense that these values are slightly smaller since the bounding box is slightly smaller as well.

每个网格都有相似数量的顶点,但ball.dae的面数大约是其两倍。运行命令filters->cleaning and repairing->remove duplicate faces可将ball.dae中的面数减少到720,并为体积(3.84)和主惯性矩(1.45)提供更合理的值。因为边界框也稍微小一些,所以这些值稍微小一些是有意义的。

Scaling to increase numerical precision

Meshlab currently prints the geometric information with 6 digits of fixed point precision. If your mesh is too small, this may substantially limit the precision of the inertia tensor, for example:

Meshlab目前以6位定点精度打印几何信息。如果网格太小,这可能会大大限制惯性张量的精度,例如:

Mesh Bounding Box Size 0.044000 0.221000 0.388410
Mesh Bounding Box Diag 0.449043
Mesh Volume is 0.001576
Mesh Surface is 0.136169
Thin shell barycenter -0.021954 0.008976 0.012835
Center of Mass is -0.021993 0.001259 0.001489
Inertia Tensor is :
| 0.000008 -0.000000 -0.000000 |
| -0.000000 0.000001 -0.000000 |
| -0.000000 -0.000000 0.000007 |
Principal axes are :
| 0.999999 0.000166 0.001241 |
| -0.000113 0.999104 -0.042310 |
| -0.001247 0.042310 0.999104 |
axis momenta are :
| 0.000008 0.000001 0.000007 |

It seems like we have what we were seeking for. But when you look thoroughly, you will see one bad thing - the output is written out only up to 6 decimal digits. As a consequence, we lose most of the valuable information in the inertia tensor. To overcome lack of precision in the Inertia Tensor, you can scale up the model so that the magnitude of the inertia is increased. The model can be scaled using Filters->Normals, Curvatures and Orientation->Transform: Scale. Enter a scale in the dialog and hit Apply.

我们似乎有我们想要的东西。但是当你仔细观察的时候,你会发现一件不好的事情——输出最多只能写6位小数。因此,我们失去了。为了克服惯性张量精度不足的问题,可惯性张量中大部分有价值的信息以将模型放大,以增加惯性的大小。可以使用过滤器->法线、曲率和方向->转换:缩放来缩放模型。在对话框中输入比例,然后单击应用。

To decide the scaling factor s to choose, recall that MeshLab uses the volume as a proxy for mass, which will vary as s3. Furthermore, the inertia has an addition dependence on length2, so the moment of inertia will change according to s5. Since there is such a large dependence on s, scaling by a factor of 10 or 100 may be sufficient.

Now, instruct MeshLab to recompute the geometrical measures again, and the Inertia Tensor entry should have more precision. Then multiply the inertia tensor by 1/s5 to undo the scaling.

要确定要选择的比例因子s,请记住,meshlab使用体积作为质量的代理,其变化为s3。此外,惯性对长度2有附加依赖性,因此惯性矩将根据S5变化。由于对s的依赖性如此之大,所以按10或100的比例缩放就足够了。

现在,指令MESHLAB重新计算几何量,惯性张量输入应具有更高的精度。然后将惯性张量乘以1/s5,以取消缩放。

Getting the Center of Mass

It is not always the case that MeshLab uses the same length units as you'd want (meters for Gazebo). However, you can easily tell the ratio of MeshLab units to your desired units by looking at the Mesh Bounding Box Size entry. You can e.g. compute the bounding box size in your desired units and compare to the MeshLab's one.

Multiply the Center of Mass entry with the computed ratio and you have the coordinates of the Center of Mass of your mesh. However, if the link you are modeling is not homogeneous, you will have to compute the Center of Mass using other methods (most probably by real experiments).

获取质心
并非所有情况下,Meshlab使用的长度单位都与您想要的相同(米)。但是,通过查看“网格边界框大小”条目,可以很容易地分辨出MeshLab单位与所需单位的比率。例如,可以用所需的单位计算边界框大小,并与MeshLab的边界框大小进行比较。
将质心条目乘以计算出的比率,得到网格质心的坐标。但是,如果正在建模的链接不均匀,则必须使用其他方法(最可能是通过实际实验)计算质心。

Rescaling the moment of inertia values

Just like the center of mass location must be scaled to the correct units, the moment of inertia should be scaled as well, though the scale factor should be squared to account for the length2 dependence in the moment of inertia. In addition, the inertia should be multiplied by the measured mass and divided by the computed volume from the text output.

Filling in the tags in URDF or SDF

The next step is to record the computed values to the URDF or SDF file containing your robot (it is assumed you already have the robot model; if not, follow the tutorial Make a Model).

In each link you should have the tag. It should look like the following (in SDF):


  
    -0.022 0.0203 0.02917 0 0 0
    0.56
    
      0.004878
      -6.2341e-07
      -7.4538e-07
      0.00090164
      -0.00014394
      0.0042946
    
  
  
    ...
  
  
    ...
  
  ...

or like this one (in URDF):


  
    
    
    
  
  
    ...
  
  
    ...
  

 

The should be entered in kilograms and you have to find it out experimentally (or from specifications).

The or are used to enter the Center of Mass position (relative to the link's origin; especially not relative to the link's visual or collision origin). The rotational elements can define a different coordinate from for the moment of inertia axes. If you've found out the center of mass experimentally, fill in this value, otherwise fill in the correctly scaled value computed by MeshLab.

The tag contains the inertia tensor you have computed in the previous step. Since the matrix is symmetric, only 6 numbers are sufficient to represent it. The mapping from MeshLab's output is the following:

| ixx ixy ixz |
| ixy iyy iyz |
| ixz iyz izz |

As a quick check that the matrix is sane, you can use the rule that the diagonal entries should have the largest values and be positive, and the off-diagonal numbers should more or less approach zero.

Precisely, the matrix has to be positive definite (use your favorite maths tool to verify that). Its diagonal entries also have to satisfy the triangle inequality, ie. ixx + iyy >= izz, ixx + izz >= iyy and iyy + izz >= ixx.

Checking in Gazebo

To check if everything is done correctly, you can use Gazebo's GUI client.

  • Using Gazebo standalone

    1. Run Gazebo

      gazebo
    2. Spawn your robot

      gz model -f my_robot.sdf
  • Using Gazebo with ROS

    1. Run Gazebo

      roslaunch gazebo_ros empty_world.launch
    2. Spawn your robot (substitute my_robot, my_robot_description and MyRobot with your robot's package/name):

      • SDF model:

        rosrun gazebo_ros spawn_model -sdf -file `rospack find my_robot_description`/urdf/my_robot.sdf -model MyRobot
      • URDF model:

        rosrun gazebo_ros spawn_model -urdf -file `rospack find my_robot_description`/urdf/my_robot.urdf -model MyRobot

As soon as your model loads, pause the world and delete the ground_plane (this is not needed, but it usually makes debugging easier).

Go to the Gazebo menu and select View->Inertia. Every link should now display a purple box with green axes. The center of each box is aligned with the specified center of mass of its link. The sizes and orientations of the boxes correspond to unit-mass boxes with the same inertial behavior as their corresponding links. This is useful for debugging the inertial parameters, but we can make one more thing to have the debugging easier.

You can temporarily set all the links to have a mass of 1.0 (by editing the URDF or SDF file). Then all the purple boxes should have more or less the same shapes as the bounding boxes of their links. This way you can easily detect problems like misplaced Center of Mass or wrongly rotated Inertia Matrix. Do not forget to enter the correct masses when you finish debugging.

To fix a wrongly rotated Inertia Matrix (which in fact happens often), just swap the ixx, iyy, izz entries in the model file until the purple box aligns with its link. Then you obviously also have to appropriately swap the ixy, ixz and iyz values (when you swap ixx<->iyy, then you should negate ixy and swap ixz<->iyz).

一旦你的模型加载,暂停世界并删除地平面(这是不需要的,但它通常使调试更容易)。

进入Gazebo菜单,选择View(视图)->Inertia(惯性)。现在,每个链接都应该显示一个带有绿色轴的紫色框。每个框的中心与其链接的指定重心对齐。盒子的大小和方向与单位质量盒子相对应,其惯性行为与其相应的链接相同。这对于调试惯性参数很有用,但是我们可以做更多的事情使调试更容易。

您可以临时将所有链接设置为质量为1.0(通过编辑URDF或SDF文件)。然后,所有紫色框的形状应该或多或少与它们链接的边界框相同。这样,您可以很容易地检测到问题,如重心错位或转动惯量矩阵错误。完成调试后,不要忘记输入正确的质量。

要修复错误旋转的惯性矩阵(事实上经常发生这种情况),只需交换模型文件中的ixx、iy、izz条目,直到紫色框与其链接对齐。显然,您还必须适当地交换Ixy、Ixz和Iyz值(当您交换Ixx<->Iyy时,您应该忽略Ixy和交换Ixz<->Iyz)。

Further improvements

Simplify the model

MeshLab only computes correct inertia parameters for closed shapes. If your link is open or if it is a very complex or concave shape, it might be a good idea to simplify the model (e.g. in Blender) before computing the inertial parameters. Or, if you have the collision shapes for your model, use them in place of the full-resolution model.

Non-homogeneous bodies

For strongly non-homogeneous bodies, this tutorial might not work. There are two problems. The first one is that MeshLab assumes uniform-density bodies. The other is that MeshLab computes the Inertia Tensor relative to the computed center of mass. However, for strongly non-homogeneous bodies, the computed center of mass will be far from the real center of mass, and therefore the computed inertia tensor might be just wrong.

One solution is to subdivide your link to more homogeneous parts and connect them with fixed joints, but that is not always possible. The only other solution would be to find out the inertia tensor experimentally, which would surely take a lot of time and effort.

Conclusion

We have shown the process of getting the correct inertia parameters for your robot model, the way how to enter them in a URDF or SDF file, and also the way how to make sure the parameters are entered correctly.

进一步改进

简化模型
Meshlab仅计算闭合形状的正确惯性参数。如果你的链接是开放的,或者它是一个非常复杂或凹形的,在计算惯性参数之前,最好简化模型(例如在搅拌机中)。或者,如果您的模型具有碰撞形状,请使用它们代替完全分辨率模型。

非均匀体
对于强非齐次体,本教程可能不起作用。有两个问题。第一个问题是,Meshlab假设均匀密度体。另一种方法是,Meshlab计算相对于计算质心的惯性张量。然而,对于强非齐次体,计算的质心将远离实际质心,因此计算的惯性张量可能是错误的。
一种解决方案是将链接细分为更均匀的部分,并用固定接头连接它们,但这并不总是可能的。唯一的解决办法是通过实验找出惯性张量,这肯定需要花费大量的时间和精力。

结论
我们已经演示了为您的机器人模型获取正确惯性参数的过程,如何在URDF或SDF文件中输入这些参数,以及如何确保正确输入参数。

你可能感兴趣的:(如何计算Gazebo下的惯性参数 How to compute inertia in gazebo)