【urdf_tutorials】Using Xacro to Clean Up a URDF File 使用Xacro清理URDF文件
Using Xacro to Clean Up a URDF File 使用Xacro清理URDF文件
原文链接: https://wiki.ros.org/urdf/Tutorials/Using Xacro to Clean Up a URDF File
Description: Learn some tricks to reduce the amount of code in a URDF file using Xacro
描述:学习使用Xacro减少URDF文件中代码量的一些技巧
Keywords: URDF, Xacro
关键词:URDF,Xacro
Tutorial Level: BEGINNER
教程级别:初学者
By now, if you’re following all these steps at home with your own robot design, you might be sick of doing all sorts of math to get very simple robot descriptions to parse correctly. Fortunately, you can use the xacro package to make your life simpler. It does three things that are very helpful.
现在,如果你在家里用自己的机器人设计完成所有这些步骤,你可能会厌倦做各种数学来正确解析非常简单的机器人描述。幸运的是,你可以使用xacro包让你的生活更简单。它做了三件非常有用的事情。
-
Constants
-
Simple Math
-
Macros
-
常量
-
简单数学
-
宏
In this tutorial, we take a look at all these shortcuts to help reduce the overall size of the URDF file and make it easier to read and maintain.
在本教程中,我们将查看所有这些快捷方式,以帮助减少URDF文件的总体大小,并使其更易于阅读和维护。
1 Using Xacro
As its name implies, xacro is a macro language for XML. The xacro program runs all of the macros and outputs the result. Typical usage looks something like this:
顾名思义,xacro是一种用于XML的宏语言。xacro程序运行所有宏并输出结果。典型用法如下:
1 | xacro --inorder model.xacro > model.urdf |
On ROS distros melodic and later, you should omit the {–inorder} argument.
在ROS发行版molodic和以后的版本中,您应该省略{–inorder}参数。
You can also automatically generate the urdf in a launch file. This is convenient because it stays up to date and doesn’t use up hard drive space. However, it does take time to generate, so be aware that your launch file might take longer to start up. (I’m looking at you pr2_description)
您还可以在启动文件中自动生成urdf。这很方便,因为它可以保持最新状态,而且不会占用硬盘空间。但是,生成确实需要时间,因此请注意启动启动文件可能需要更长的时间。(说的就是你pr2_description)
1 | <param name="robot_description" |
At the top of the URDF file, you must specify a namespace in order for the file to parse properly. For example, these are the first two lines of a valid xacro file:
在URDF文件的顶部,必须指定一个名称空间,以便正确解析文件。例如,以下是有效xacro文件的前两行:
1 |
|
2 Constants
Let’s take a quick look at our base_link in R2D2.
让我们快速查看一下R2D2中的base_link。
1 | <link name="base_link"> |
The information here is a little redundant. We specify the length and radius of the cylinder twice. Worse, if we want to change that, we need to do so in two different places.
这里的信息有点多余。我们指定圆柱体的长度和半径两次。更糟糕的是,如果我们想改变这一点,我们需要在两个不同的地方这样做。
Fortunately, xacro allows you to specify properties which act as constants. Instead, of the above code, we can write this.
幸运的是,xacro允许您指定充当常量的属性。相反,我们可以使用上面的代码编写这个。
1 | <xacro:property name="width" value="0.2" /> |
-
The two values are specified in the first two lines. They can be defined just about anywhere (assuming valid XML), at any level, before or after they are used. Usually they go at the top.
-
Instead of specifying the actual radius in the geometry element, we use a dollar sign and curly brackets to signify the value.
-
This code will generate the same code shown above.
-
这两个值在前两行中指定。它们可以在任何地方(假设有效的XML)、任何级别、使用之前或之后进行定义。通常他们都在顶端。
-
我们使用美元符号和大括号来表示值,而不是在几何元素中指定实际半径。
-
此代码将生成与上面所示相同的代码。
The value of the contents of the ${} construct are then used to replace the ${}. This means you can combine it with other text in the attribute.
然后使用${}构造的内容值替换${}。这意味着您可以将其与属性中的其他文本组合。
1 | <xacro:property name=”robotname” value=”marvin” /> |
This will generate
1 | <link name=”marvins_leg” /> |
However, the contents in the ${} don’t have to only be a property, which brings us to our next point…
然而,${}中的内容不一定只是一个属性,这就引出了我们的下一点。。。
3 Math
You can build up arbitrarily complex expressions in the ${} construct using the four basic operations (+,-,*,/), the unary minus, and parenthesis. Examples:
您可以使用四个基本操作(+、-、*、/)、一元减号和括号在${}构造中构建任意复杂的表达式。示例:
1 | <cylinder radius="${wheeldiam/2}" length="0.1"/> |
All of the math is done using floats, hence
所有的数学运算都是使用浮点数完成的,因此
1 | <link name="${5/6}"/> |
evaluates to
计算结果为
1 | <link name="0.833333333333"/> |
In Jade and later distros, you can use more than the basic operations listed above, notably sin and cos.
在Jade和以后的发行版中,您可以使用上面列出的基本操作,特别是sin和cos。
4 Macros
Here’s the biggest and most useful component to the xacro package.
这是xacro包中最大、最有用的组件。
4.1 Simple Macro
Let’s take a look at a simple useless macro.
让我们来看一个简单的无用宏。
1 | <xacro:macro name="default_origin"> |
(This is useless, since if the origin is not specified, it has the same value as this.) This code will generate the following.
(这是无用的,因为如果未指定原点,它的值与此值相同。)此代码将生成以下内容。
1 | <origin rpy="0 0 0" xyz="0 0 0"/> |
-
The name is not technically a required element, but you need to specify it to be able to use it.
-
Every instance of the xacro:$NAME/ is replaced with the contents of the xacro:macro tag.
-
Note that even though its not exactly the same (the two attributes have switched order), the generated XML is equivalent.
-
If the xacro with a specified name is not found, it will not be expanded and will NOT generate an error.
-
从技术上讲,名称不是必需的元素,但您需要指定它才能使用它。
-
xacro:$NAME/的每个实例都被xacro:macro标记的内容替换。
-
注意,即使它不完全相同(两个属性的顺序发生了变化),生成的XML也是等效的。
-
如果找不到具有指定名称的xacro,则不会展开它,也不会生成错误。
4.2 Parameterized Macro
You can also parameterize macros so that they don’t generate the same exact text every time. When combined with the math functionality, this is even more powerful.
您还可以参数化宏,使它们不会每次都生成相同的精确文本。当与数学功能相结合时,这将更加强大。
First, let’s take an example of a simple macro used in R2D2.
首先,让我们以R2D2中使用的一个简单宏为例。
1 | <xacro:macro name="default_inertial" params="mass"> |
This can be used with the code
1 | <xacro:default_inertial mass="10"/> |
The parameters act just like properties, and you can use them in expressions
参数的作用与属性类似,您可以在表达式中使用它们
You can also use entire blocks as parameters too.
也可以将整个块用作参数。
1 | <xacro:macro name="blue_shape" params="name *shape"> |
-
To specify a block parameter, include an asterisk before its parameter name.
-
A block can be inserted using the insert_block command
-
Insert the block as many times as you wish.
-
要指定块参数,请在其参数名称前包含星号。
-
可以使用insert_block命令插入块
-
根据需要插入块多次。
5 Practical Usage
The xacro language is rather flexible in what it allows you to do. Here are a few useful ways that xacro is used in the R2D2 model, in addition to the default inertial macro shown above.
xacro语言在它允许你做的事情上非常灵活。除了上面显示的默认惯性宏之外,以下是一些在R2D2模型中使用xacro的有用方法。
To see the model generated by a xacro file, run the same command as with previous tutorials: roslaunch urdf_tutorial display.launch model:=urdf/08-macroed.urdf.xacro
要查看xacro文件生成的模型,请运行与前面教程相同的命令:roslaunch urdf_tutorial display.launch model:=urdf/08-macroed.urdf.xacro
(The launch file has been running the xacro command this whole time, but since there were no macros to expand, it didn’t matter)
(启动文件一直在运行xacro命令,但由于没有可扩展的宏,这无关紧要)
5.1 Leg macro
Often you want to create multiple similar looking objects in different locations. Often, there will be some symmetry to the locations. You can use a macro and some simple math to reduce the amount of code you have to write, like we do with R2’s two legs.
通常,您希望在不同位置创建多个外观相似的对象。通常,位置会有一些对称性。你可以使用一个宏和一些简单的数学来减少你必须写的代码量,就像我们用R2的两条腿做的那样。
1 | <xacro:macro name="leg" params="prefix reflect"> |
-
Common Trick 1: Use a name prefix to get two similarly named objects.
-
Common Trick 2: Use math to calculate joint origins. In the case that you change the size of your robot, changing a property with some math to calculate the joint offset will save a lot of trouble.
-
Common Trick 3: Using a reflect parameter, and setting it to 1 or -1. See how we use the reflect parameter to put the legs on either side of the body in the base_to_${prefix}_leg origin.
-
常见技巧1:使用名称前缀获取两个名称相似的对象。
-
常见技巧2:使用数学计算关节原点。如果你改变了机器人的大小,用一些数学方法改变一个属性来计算关节偏移会省去很多麻烦。
-
常见技巧3:使用反射参数,并将其设置为1或-1。看看我们如何使用reflect参数将腿放在base_to_${prefix}_leg原点的身体两侧。
5.2 Other tricks
Feel free to append your own tricks here.
5.3 Next Steps
This concludes this section however if you have completed all these steps you are well prepared to move into simulation. Continue on to Using a URDF in Gazebo
然而,如果您已经完成了所有这些步骤,那么您就已经做好了进入模拟的准备。继续在Gazebo中使用URDF