当前位置:网站首页>【ROS进阶篇】第九讲 URDF的编程优化Xacro使用
【ROS进阶篇】第九讲 URDF的编程优化Xacro使用
2022-07-25 11:32:00 【生如昭诩】
【ROS进阶篇】第九讲 URDF的编程优化Xacro使用

前言
在上一节博客中我们系统的学习了URDF的具体使用方法和实例练习,但是在学习的过程中我们也能够发现直接编辑URDF文件存在诸多问题,例如关节参数问题和代码复用性问题,本节博客就主要介绍一种关于XML语言的编程优化方法,Xacro,在实际的URDF编程中热门的优化方法,从基本概念、语法讲解到实际机器人举例进行深入探讨。
一、Xacro的基本概念
概念:XML Macros的缩写,本质上是一种XML宏语言,是可编程的XML
目的:解决URDF在构建机器人模型过程中存在的固有问题:
1. 连杆、关节的重要参数计算问题:手动通过固定公式计算,容易出现失误,且效率很低;
2. 内容高度重复性:对于相同的机器人部件,URDF文件代码相同,但是需要重复编程,缺乏复用性;
- 原理:通过变量和函数优化代码构建过程
1. 声明变量:通过数学运算计算重要参数,将数据记录在变量中,提高效率及安全性;
2. 封装函数:使用流程控制顺序,封装固定逻辑,提高代码复用性
作用:提高编写效率,通过使用宏命令构建更精悍短小但又具有更高可读性的XML文件,扩展达到更大的XML表达范围
特点:
\qquad 1. 提供了可编程接口,包含变量声明调用、函数声明与调用等语法实现;
\qquad 2. 在使用xacro生成文件时,根标签robot必须包含命名空间声明:xmlns:xacro="http://wiki.ros.org/xacro"
二、Xacro的语法详解
1. 属性与算术运算:
\qquad 作用:封装URDF中的字段,例如小车尺寸、轮子半径等
\qquad 属性定义:即声明变量,定义格式如下
<xacro:property name="xxxx" value="yyyy" />
\qquad 属性调用:变量调用格式:${属性名称}
\qquad 算术运算:变量计算格式:${数学表达式}
2. 宏:
\qquad 作用:函数实现,提高代码复用率,优化代码结构,提高安全性
\qquad 宏定义:即函数定义,定义格式如下
<xacro:macro name="宏名称" params="参数列表(多参数之间使用空格分隔)">
.....
参数调用格式: ${参数名}
</xacro:macro>
\qquad 宏调用:函数调用格式:
<xacro:宏名称 参数1=xxx 参数2=xxx/>
3. 文件包含集成:
\qquad 作用:将机器人的不同部件封装为单独的xacro文件,将文件集成组成为完整机器人,可以使用文件包含实现
\qquad 文件包含格式 :
<robot name="xxx" xmlns:xacro="http://wiki.ros.org/xacro">
<xacro:include filename="my_base.xacro" />
<xacro:include filename="my_camera.xacro" />
<xacro:include filename="my_laser.xacro" />
....
</robot>
三、实际机器人模型示例
1. 目标机器人与实现流程:
- 目标:拥有两个驱动轮、两个支撑轮的圆柱状小车,并添加摄像头和雷达传感器,效果如下图:

- 实现流程:
1. 编写底盘的xacro文件;
2. 编写摄像头和雷达的xacro文件;
3. 编写组合文件,组合小车各部分;
4. 编写launch文件启动Rviz显示模型;
2. 编写xacro文件:
- 小车底盘实现:
<!-- 使用 xacro 优化 URDF 版的小车底盘实现: 实现思路: 1.将一些常量、变量封装为 xacro:property 比如:PI 值、小车底盘半径、离地间距、车轮半径、宽度 .... 2.使用 宏 封装驱动轮以及支撑轮实现,调用相关宏生成驱动轮与支撑轮 -->
<!-- 根标签,必须声明 xmlns:xacro -->
<robot name="my_base" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- 封装变量、常量 -->
<xacro:property name="PI" value="3.141"/>
<!-- 宏:黑色设置 -->
<material name="black">
<color rgba="0.0 0.0 0.0 1.0" />
</material>
<!-- 底盘属性 -->
<xacro:property name="base_footprint_radius" value="0.001" /> <!-- base_footprint 半径 -->
<xacro:property name="base_link_radius" value="0.1" /> <!-- base_link 半径 -->
<xacro:property name="base_link_length" value="0.08" /> <!-- base_link 长 -->
<xacro:property name="earth_space" value="0.015" /> <!-- 离地间距 -->
<!-- 底盘 -->
<link name="base_footprint">
<visual>
<geometry>
<sphere radius="${base_footprint_radius}" />
</geometry>
</visual>
</link>
<link name="base_link">
<visual>
<geometry>
<cylinder radius="${base_link_radius}" length="${base_link_length}" />
</geometry>
<origin xyz="0 0 0" rpy="0 0 0" />
<material name="yellow">
<color rgba="0.5 0.3 0.0 0.5" />
</material>
</visual>
</link>
<joint name="base_link2base_footprint" type="fixed">
<parent link="base_footprint" />
<child link="base_link" />
<origin xyz="0 0 ${earth_space + base_link_length / 2 }" />
</joint>
<!-- 驱动轮 -->
<!-- 驱动轮属性 -->
<xacro:property name="wheel_radius" value="0.0325" /><!-- 半径 -->
<xacro:property name="wheel_length" value="0.015" /><!-- 宽度 -->
<!-- 驱动轮宏实现 -->
<xacro:macro name="add_wheels" params="name flag">
<link name="${name}_wheel">
<visual>
<geometry>
<cylinder radius="${wheel_radius}" length="${wheel_length}" />
</geometry>
<origin xyz="0.0 0.0 0.0" rpy="${PI / 2} 0.0 0.0" />
<material name="black" />
</visual>
</link>
<joint name="${name}_wheel2base_link" type="continuous">
<parent link="base_link" />
<child link="${name}_wheel" />
<origin xyz="0 ${flag * base_link_radius} ${-(earth_space + base_link_length / 2 - wheel_radius) }" />
<axis xyz="0 1 0" />
</joint>
</xacro:macro>
<xacro:add_wheels name="left" flag="1" />
<xacro:add_wheels name="right" flag="-1" />
<!-- 支撑轮 -->
<!-- 支撑轮属性 -->
<xacro:property name="support_wheel_radius" value="0.0075" /> <!-- 支撑轮半径 -->
<!-- 支撑轮宏 -->
<xacro:macro name="add_support_wheel" params="name flag" >
<link name="${name}_wheel">
<visual>
<geometry>
<sphere radius="${support_wheel_radius}" />
</geometry>
<origin xyz="0 0 0" rpy="0 0 0" />
<material name="black" />
</visual>
</link>
<joint name="${name}_wheel2base_link" type="continuous">
<parent link="base_link" />
<child link="${name}_wheel" />
<origin xyz="${flag * (base_link_radius - support_wheel_radius)} 0 ${-(base_link_length / 2 + earth_space / 2)}" />
<axis xyz="1 1 1" />
</joint>
</xacro:macro>
<xacro:add_support_wheel name="front" flag="1" />
<xacro:add_support_wheel name="back" flag="-1" />
</robot>
- 摄像头xacro文件:
<!-- 摄像头相关的 xacro 文件 -->
<robot name="my_camera" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 摄像头属性 -->
<xacro:property name="camera_length" value="0.01" /> <!-- 摄像头长度(x) -->
<xacro:property name="camera_width" value="0.025" /> <!-- 摄像头宽度(y) -->
<xacro:property name="camera_height" value="0.025" /> <!-- 摄像头高度(z) -->
<xacro:property name="camera_x" value="0.08" /> <!-- 摄像头安装的x坐标 -->
<xacro:property name="camera_y" value="0.0" /> <!-- 摄像头安装的y坐标 -->
<xacro:property name="camera_z" value="${base_link_length / 2 + camera_height / 2}" /> <!-- 摄像头安装的z坐标:底盘高度 / 2 + 摄像头高度 / 2 -->
<!-- 摄像头关节以及link -->
<link name="camera">
<visual>
<geometry>
<box size="${camera_length} ${camera_width} ${camera_height}" />
</geometry>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
<material name="black" />
</visual>
</link>
<joint name="camera2base_link" type="fixed">
<parent link="base_link" />
<child link="camera" />
<origin xyz="${camera_x} ${camera_y} ${camera_z}" />
</joint>
</robot>
- 雷达xacro文件:
<!-- 小车底盘添加雷达 -->
<robot name="my_laser" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 雷达支架 -->
<xacro:property name="support_length" value="0.15" /> <!-- 支架长度 -->
<xacro:property name="support_radius" value="0.01" /> <!-- 支架半径 -->
<xacro:property name="support_x" value="0.0" /> <!-- 支架安装的x坐标 -->
<xacro:property name="support_y" value="0.0" /> <!-- 支架安装的y坐标 -->
<xacro:property name="support_z" value="${base_link_length / 2 + support_length / 2}" /> <!-- 支架安装的z坐标:底盘高度 / 2 + 支架高度 / 2 -->
<link name="support">
<visual>
<geometry>
<cylinder radius="${support_radius}" length="${support_length}" />
</geometry>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
<material name="red">
<color rgba="0.8 0.2 0.0 0.8" />
</material>
</visual>
</link>
<joint name="support2base_link" type="fixed">
<parent link="base_link" />
<child link="support" />
<origin xyz="${support_x} ${support_y} ${support_z}" />
</joint>
<!-- 雷达属性 -->
<xacro:property name="laser_length" value="0.05" /> <!-- 雷达长度 -->
<xacro:property name="laser_radius" value="0.03" /> <!-- 雷达半径 -->
<xacro:property name="laser_x" value="0.0" /> <!-- 雷达安装的x坐标 -->
<xacro:property name="laser_y" value="0.0" /> <!-- 雷达安装的y坐标 -->
<xacro:property name="laser_z" value="${support_length / 2 + laser_length / 2}" /> <!-- 雷达安装的z坐标:支架高度 / 2 + 雷达高度 / 2 -->
<!-- 雷达关节以及link -->
<link name="laser">
<visual>
<geometry>
<cylinder radius="${laser_radius}" length="${laser_length}" />
</geometry>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
<material name="black" />
</visual>
</link>
<joint name="laser2support" type="fixed">
<parent link="support" />
<child link="laser" />
<origin xyz="${laser_x} ${laser_y} ${laser_z}" />
</joint>
</robot>
3. 组合与启动:
- 组合小车模型文件:
<!-- 组合小车底盘与摄像头与雷达 -->
<robot name="my_car_camera" xmlns:xacro="http://wiki.ros.org/xacro">
<xacro:include filename="my_base.urdf.xacro" />
<xacro:include filename="my_camera.urdf.xacro" />
<xacro:include filename="my_laser.urdf.xacro" />
</robot>
- 集成launch文件:
\qquad 1) 先转换成urdf文件后集成:
- 先将 xacro 文件解析成 urdf 文件:
rosrun xacro xacro xxx.xacro > xxx.urdf
- 直接整合launch文件:
<launch> <param name="robot_description" textfile="$(find demo01_urdf_helloworld)/urdf/xacro/my_base_camera_laser.urdf.xacro" />" /> <node pkg="rviz" type="rviz" name="rviz" args="-d $(find demo01_urdf_helloworld)/config/helloworld.rviz" /> <node pkg="joint_state_publisher" type="joint_state_publisher" name="joint_state_publisher" output="screen" /> <node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher" output="screen" /> <node pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" name="joint_state_publisher_gui" output="screen" /> </launch>
\qquad 2) 在 launch 文件中直接加载 xacro
<launch>
<param name="robot_description" command="$(find xacro)/xacro $(find demo01_urdf_helloworld)/urdf/xacro/my_base_camera_laser.urdf.xacro" />
<node pkg="rviz" type="rviz" name="rviz" args="-d $(find demo01_urdf_helloworld)/config/helloworld.rviz" />
<node pkg="joint_state_publisher" type="joint_state_publisher" name="joint_state_publisher" output="screen" />
<node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher" output="screen" />
<node pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" name="joint_state_publisher_gui" output="screen" />
</launch>
总结
- 声明:本节博客部分参考了CSDN用户赵虚左的ROS教程,本文主要介绍了URDF文件的一种优化编程方法,即Xacro,通过声明变量和函数提高代码安全性、效率和复用性,分别从基本概念、语法详解和最终的实际机器人实例进行了详细分析,在之后的教程中还会推出关于URDF文件与RVIZ和Gazebo组件联合仿真的教程以及关于传感器和导航的内容,敬请期待。
边栏推荐
- Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database
- 协程
- MySQL练习二
- Transformer variants (spark transformer, longformer, switch transformer)
- mysql的表分区
- 919. 完全二叉树插入器 : 简单 BFS 运用题
- scrapy 设置随机的user_agent
- 【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)
- Client open download, welcome to try
- Meta learning (meta learning and small sample learning)
猜你喜欢

Basic concepts of NLP 1

GPT plus money (OpenAI CLIP,DALL-E)

web编程(二)CGI相关

Multi-Label Image Classification(多标签图像分类)

Multi label image classification

Figure neural network for recommending system problems (imp-gcn, lr-gcn)

Brpc source code analysis (II) -- the processing process of brpc receiving requests

Transformer variants (routing transformer, linformer, big bird)

第一个scrapy爬虫

LeetCode第303场周赛(20220724)
随机推荐
Meta learning (meta learning and small sample learning)
Introduction to redis
[RS sampling] a gain tuning dynamic negative sampler for recommendation (WWW 2022)
Those young people who left Netease
【多模态】《HiT: Hierarchical Transformer with Momentum Contrast for Video-Text Retrieval》ICCV 2021
selenium使用———安装、测试
[multimodal] hit: hierarchical transformer with momentum contract for video text retrieval iccv 2021
【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)
919. 完全二叉树插入器 : 简单 BFS 运用题
[dark horse morning post] eBay announced its shutdown after 23 years of operation; Wei Lai throws an olive branch to Volkswagen CEO; Huawei's talented youth once gave up their annual salary of 3.6 mil
Knowledge maps are used to recommend system problems (mvin, Ctrl, ckan, Kred, gaeat)
【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21
PHP one server sends pictures to another. Curl post file_ get_ Contents save pictures
keepalived实现mysql的高可用
【RS采样】A Gain-Tuning Dynamic Negative Sampler for Recommendation (WWW 2022)
Solutions to the failure of winddowns planning task execution bat to execute PHP files
R语言ggplot2可视化:可视化散点图并为散点图中的部分数据点添加文本标签、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(为数据点标签添加线段、指定线段的角度
'C:\xampp\php\ext\php_ zip. Dll'-%1 is not a valid Win32 Application Solution
Heterogeneous graph neural network for recommendation system problems (ackrec, hfgn)
3.2.1 什么是机器学习?