ROS学习笔记44(使用URDF文件代替Xacro)

原文链接: http://wiki.ros.org/urdf/Tutorials/Using%20Xacro%20to%20Clean%20Up%20a%20URDF%20File

现在,如果你遵循上面的这些步骤进行自己的机器人设计,你可能很烦做各种数学计算以正确得到的机器人的描述。幸运的是,你可以使用xacro包,这令这个事情变的简单。它做了三件事情是非常有帮助的。

  •     常量
  •     简单的数学
  •     宏

在本教程中,我们就来看看these shortcuts,以帮助减少URDF文件的总大小,使其更易于阅读和维护。

1 使用Xacro文件

正如它的名字所暗示的,xacro是宏语言。该xacro程序运行所有的宏并输出结果。典型用法看起来是这样的:

xacro --inorder model.xacro > model.urdf 

 您还可以自动生成一个启动文件的urdf。这是方便,因为它保持最新,并且不占用硬盘空间。然而,它确实需要时间来产生,所以要知道,你的启动文件可能需要更长的时间来启动。

 

在URDF文件的顶部,你必须要指定一个命名空间的文件,以正确地解析。例如,这些都是有效的xacro文件的前两行:



2 常量

让我们快速浏览一下我们的R2D2 base_link。


    
      
        
      
      
    
    
      
        
      
    
  

这里的信息是有点多余。我们两次指定圆柱体的长度和半径。更糟的是,如果我们想改变这种状况,我们需要在两个不同的地方这样做。
幸运的是,xacro允许您指定作为常量属性。取而代之的是,上面的代码中,我们可以这样写。




    
        
            
        
        
    
    
        
            
        
    

这两个值在头两行指定。他们可以在任何地方(假设有效的XML),在任何级别的,在使用前或后,他们被定义。通常他们被放在最上面。
相反,在几何元素指定实际半径,我们使用一个美元符号和大括号来表示的值。
此代码与最上面的那些程序是一样的。

The value of the contents of the ${} construct are then used to replace the ${}.这意味着你可以在属性其他文字结合起来。



这将产生


3 Math

You can build up 任意的 complex expressions in the ${} construct using the four basic operations (+,-,*,/), the unary minus, and parenthesis. Examples:



所有的数学运算都是在大括号内完成的。


计算结果为:


 当然也可以进行更多的操作,比如正弦和余弦。

4 宏

这里是最大的,最实用的组件对于xacro包而言。

4.1 简单的宏

让我们来看看一个简单的无用的宏。


    


(This is useless, since if the origin is not specified, it has the same value as this.) 代码将产生如下结果:


 这个名字是不是技术上必需的元素,但你需要指定它是能够使用它。

Every instance of the 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.

如果没有找到具有指定名称的xacro,它不会产生错误。

4.2 参数宏

您也可以参数化宏,以便它们不会产生the same exact text every time. 当与数学相结合时,这更是如虎添翼。

首先,让我们在使用R2D2一个简单的宏的例子。


        
                
                
        

This can be used with the code 


参数行为就像属性,你可以在表达式中使用它们
你也可以用整个块作为参数。 


    
        
            
                
            
            
        
        
            
                
            
        
    



    

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.

5 实际使用

xacro语言是什么让你做相当大的灵活性。以下是该xacro在R2D2模型中,除了上面显示的默认惯性宏一些有用的方法。

若要查看由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)

5.1 Leg macro

通常你想在不同位置的创建多个同类找对象。通常它们位于对称的位置。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.


    
        
            
                
            
            
            
        
        
            
                
            
            
        
        
    

    
        
        
        
    
    



 Common Trick 1: 使用一个名称前缀,以获得两个类似的命名对象。

Common Trick 2:使用数学计算关节起点。在您更改机器人的大小的情况下,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.

5.2 Other tricks

Feel free to append your own tricks here.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(ROS学习笔记)