文章目录
roscore
rosrun xacro xacro demo01_helloworld.urdf.xacro
rosrun xacro xacro demo01_helloworld.urdf.xacro > demo01_helloworld.urdf 由xacro文件生成urdf文件
Xacro 语法
属性
属性定义
属性调用
算术运算
<robot name = "mycar" xmlns:xacro = "http://wiki.ros.org/xacro">
<!-- 1. 属性定义 属性名称,属性的值-->
<xacro:property name = "PI" value = "3.1415927" />
<xacro:property name = "radius" value = "0.03" />
<!-- 2. 属性调用 -->
<myUsrPropertyxxx name = "${PI}" />
<myUsrPropertyxxx name = "${radius}" />
<!-- 3. 算术运算 -->
<myUsrPropertyxxx result = "${PI / 2}" />
<myUsrPropertyxxx result = "${radius * 2}" />
</robot>
宏
宏定义
宏调用
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 1. 宏的定义 参数1 参数2-->
<xacro:macro name = "getSum" params = "num1 num2">
<result value = "${num1 + num2}" />
</xacro:macro>
<!-- 2. 宏的调用 -->
<xacro:getSum num1 = "1" num2 = "5" />
</robot>
文件包含
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 演示文件包含 -->
<xacro:include filename = "demo02_field.urdf.xacro" />
<xacro:include filename = "demo03_macro.urdf.xacro" />
</robot>
Xacro 实例
roslaunch urdf01_rviz demo06_car_base.launch
底盘launch文件 demo06_car_base.launch
<launch>
<!-- 1. 在参数服务器中载入URDF 建议用第二种方式-->
<!-- <param name = "robot_description" textfile = "$(find urdf01_rviz)/urdf/xacro/demo05_car_base.urdf" /> -->
<param name = "robot_description" command = "$(find xacro)/xacro $(find urdf01_rviz)/urdf/xacro/demo05_car_base.urdf.xacro" />
<!-- 2. 启动rviz -->
<node pkg = "rviz" type = "rviz" name = "rviz" args = "-d $(find urdf01_rviz)/config/show_mycar.rviz"/>
<!-- 3. 添加关节状态发布节点 -->
<node pkg = "joint_state_publisher" type = "joint_state_publisher" name = "joint_state_publisher" />
<!-- 4. 添加机器人状态发布节点 -->
<node pkg = "robot_state_publisher" type = "robot_state_publisher" name = "robot_state_publisher" />
<!-- 5. 关节运动控制节点 -->
<node pkg = "joint_state_publisher_gui" type = "joint_state_publisher_gui" name = "joint_state_publisher_gui" />
</launch>
底盘xacro文件 demo05_car_base.urdf.xacro
<robot name = "mycar" xmlns:xacro = "http://wiki.ros.org/xacro">
<xacro:property name = "footprint_radius" value = "0.001" />
<!-- 1. 添加 base_footprint -->
<link name = "base_footprint">
<visual>
<geometry>
<sphere radius = "${footprint_radius}" />
</geometry>
</visual>
</link>
<!-- 2. 添加底盘 -->
<!--
形状: 底盘为圆柱状,半径 0.1m,高 0.08m
离地间距: 0.015m
-->
<xacro:property name = "base_radius" value = "0.1" />
<xacro:property name = "base_length" value = "0.08" />
<xacro:property name = "lidi_space" value = "0.015" />
<xacro:property name = "base_joint_z" value = "${(base_length) / 2 + lidi_space}" />
<!-- 2-1 link -->
<link name = "base_link">
<visual>
<geometry>
<cylinder radius = "0.1" length = "0.08" />
</geometry>
<origin xyz = "0 0 0" rpy = "0 0 0"/>
<material name = "baselink_color">
<color rgba = "1 0.5 0.2 0.5" />
</material>
</visual>
</link>
<!-- 2-2 joint 半个车体的高度0.08m/2+0.015m-->
<joint name = "link2footprint" type = "fixed">
<parent link = "base_footprint" />
<child link = "base_link" />
<!-- 关节z上的设置 = 车体高度/2 + 离地高度 -->
<origin xyz = "0 0 0.055" rpy = "0 0 0" />
</joint>
<!-- 3. 添加驱动轮 -->
<!--
形状: 圆柱
半径: 0.0325m
长度: 0.015m
-->
<!-- 属性 -->
<xacro:property name = "wheel_radius" value = "0.0325" />
<xacro:property name = "wheel_length" value = "0.015" />
<xacro:property name = "PI" value = "3.1415927" />
<!-- 注意: 结果是负数 -->
<xacro:property name = "wheel_joint_z" value = "${(base_length / 2 + lidi_space - wheel_radius) * (-1)}" />
<!--
wheel_name: left或right
flag: 1 或 -1
-->
<!-- 宏定义 -->
<xacro:macro name = "wheel_func" params = "wheel_name flag">
<!-- 3-1 link -->
<link name = "${wheel_name}_wheel">
<visual>
<geometry>
<cylinder radius = "${wheel_radius}" length = "${wheel_length}" />
</geometry>
<!-- 正常情况下圆柱面朝上,这里驱动轮在左右,需要修改欧拉角,沿x轴翻滚90度,约为1.57弧度 -->
<origin xyz = "0 0 0" rpy = "${PI / 2} 0 0"/>
<material name = "wheel_color">
<color rgba = "0 0 0 0.3" />
</material>
</visual>
</link>
<!-- 3-2 joint 关节 -->
<joint name = "${wheel_name}2link" type = "continuous">
<parent link = "base_link" />
<child link = "${wheel_name}_wheel" />
<!--
x: 0 无偏移
y: 0.1 车体半径
z: 0.04 + 0.015 - 0.0325 = 0.055 - 0.0325 = 0.0225,在下面为负数
关节高度 = 车体高度 / 2 + 离地间距 - 车轮半径
-->
<origin xyz = "0 ${0.1 * flag} ${wheel_joint_z}" rpy = "0 0 0" />
<!-- 转动时绕Y轴 -->
<axis xyz = "0 1 0" />
</joint>
</xacro:macro>
<!-- 调用才生效 -->
<xacro:wheel_func wheel_name = "left" flag = "1" />
<xacro:wheel_func wheel_name = "right" flag = "-1" />
<!-- 4. 添加万向轮 -->
<!--
形状: 球
半径: 0.0075m
-->
<!-- 属性 -->
<xacro:property name = "small_wheel_radius" value = "0.0075" />
<!-- 关节高度: 车体高度 / 2 + 离地间距 - 万向轮半径 = 0.04 + 0.015 - 0.0075 / 2 = 0.0475 -->
<xacro:property name = "small_joint_z" value = "${(base_length / 2 + lidi_space - small_wheel_radius) * (-1)}" />
<!-- 宏 -->
<xacro:macro name = "small_wheel_func" params = "small_wheel_name flag" >
<!-- 4-1 link -->
<link name = "${small_wheel_name}_wheel">
<visual>
<geometry>
<sphere radius = "${small_wheel_radius}" />
</geometry>
<origin xyz = "0 0 0" rpy = "0 0 0"/>
<material name = "wheel_color">
<color rbga = "0 0 0 0.3" />
</material>
</visual>
</link>
<!-- 4-2 joint -->
<joint name = "${small_wheel_name}2link" type = "continuous">
<parent link = "base_link" />
<child link = "${small_wheel_name}_wheel" />
<!--
x: 0.08 小于半径
y: 0
z: 0.04 + 0.015 - 0.0075 = 0.0475
-->
<origin xyz = "${0.08 * flag} 0 ${small_joint_z}" rpy = "0 0 0" />
<!-- 转动时绕Y轴 -->
<axis xyz = "0 1 0" />
</joint>
</xacro:macro >
<!-- 调用 -->
<xacro:small_wheel_func small_wheel_name = "front" flag = "1" />
<xacro:small_wheel_func small_wheel_name = "back" flag = "-1" />
</robot>
Xacro 实操
在前面小车底盘基础之上,添加摄像头和雷达传感器
摄像头xacro文件 demo06_car_camera.urdf.xacro
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 摄像头部件 -->
<!-- 1. 参数 -->
<!--
参数:
连杆属性: 厚度(x方向)、宽度(y方向)、高度(z方向)
关节属性: x y z 偏移量,z偏移量 = 底盘高度的一半+摄像头高度的一半
-->
<xacro:property name = "camera_length" value = "0.02" /> <!-- 厚度(x)-->
<xacro:property name = "camera_width" value = "0.05" /> <!-- 宽度(y)-->
<xacro:property name = "camera_height" value = "0.05" /> <!-- 高度(z)-->
<xacro:property name = "joint_camera_x" value = "0.08" /> <!-- 关节(x)-->
<xacro:property name = "joint_camera_y" value = "0" /> <!-- 关节(y)-->
<xacro:property name = "joint_camera_z" value = "${base_length / 2 + camera_height / 2}" /> <!-- 关节(z)-->
<!-- 2. 设计连杆关节 -->
<!-- 2-1 link -->
<link name = "camera">
<visual>
<geometry>
<box size = "${camera_length} ${camera_width} ${camera_height}" />
</geometry>
<material name = "black">
<color rgba = "0 0 0 0.8" />
</material>
</visual>
</link>
<!-- 2-2 关节 -->
<joint name = "camera2base" type = "fixed">
<parent link = "base_link" />
<child link = "camera" />
<origin xyz = "${joint_camera_x} ${joint_camera_y} ${joint_camera_z}" rpy = "0 0 0" />
</joint>
</robot>
雷达xacro文件 demo07_car_laser.urdf.xacro
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 雷达部件 -->
<!-- 参数 -->
<!--
1. 支架
支架尺寸: 半径 高度
关节偏移量: x y z
2. 雷达
支架尺寸: 半径 高度
关节偏移量: x y z
-->
<xacro:property name = "support_radius" value = "0.01" />
<xacro:property name = "support_length" value = "0.15" />
<xacro:property name = "laser_radius" value = "0.03" />
<xacro:property name = "laser_length" value = "0.05" />
<xacro:property name = "joint_support_x" value = "0" />
<xacro:property name = "joint_support_y" value = "0" />
<!-- joint1 z = 车体高度 / 2 + 支架高度 / 2 -->
<xacro:property name = "joint_support_z" value = "${base_length / 2 + support_length / 2}" />
<xacro:property name = "joint_laser_x" value = "0" />
<xacro:property name = "joint_laser_y" value = "0" />
<!-- joint2 z = 支架高度 / 2 + 雷达高度 / 2 -->
<xacro:property name = "joint_laser_z" value = "${support_length / 2 + laser_length / 2}" />
<!-- 1. 支架 -->
<link name = "support">
<visual>
<geometry>
<cylinder radius = "${support_radius}" length = "${support_length}" />
</geometry>
<material name = "yellow">
<color rgba = "0.8 0.5 0 0.5" />
</material>
</visual>
</link>
<joint name = "support2base" type = "fixed">
<parent link = "base_link" />
<child link = "support" />
<origin xyz = "${joint_support_x} ${joint_support_y} ${joint_support_z}" rpy = "0 0 0"/>
</joint>
<!-- 2. 雷达 固定-->
<link name = "laser">
<visual>
<geometry>
<cylinder radius = "${laser_radius}" length = "${laser_length}" />
</geometry>
<material name = "black">
<color rgba = "0 0 0 0.5" />
</material>
</visual>
</link>
<joint name = "laser2support" type = "fixed">
<parent link = "support" />
<child link = "laser" />
<origin xyz = "${joint_laser_x} ${joint_laser_y} ${joint_laser_z}" rpy = "0 0 0"/>
</joint>
</robot>
组合底盘、摄像头、雷达xacro文件 car.urdf.xacro
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 包含底盘、摄像头、雷达的xacro文件 -->
<xacro:include filename = "demo05_car_base.urdf.xacro" />
<xacro:include filename = "demo06_car_camera.urdf.xacro" />
<xacro:include filename = "demo07_car_laser.urdf.xacro" />
</robot>
launch文件 car.launch
<launch>
<!-- 1. 在参数服务器中载入URDF 建议用第二种方式-->
<!-- <param name = "robot_description" textfile = "$(find urdf01_rviz)/urdf/xacro/demo05_car_base.urdf" /> -->
<param name = "robot_description" command = "$(find xacro)/xacro $(find urdf01_rviz)/urdf/xacro/car.urdf.xacro" />
<!-- 2. 启动rviz -->
<node pkg = "rviz" type = "rviz" name = "rviz" args = "-d $(find urdf01_rviz)/config/show_mycar.rviz"/>
<!-- 3. 添加关节状态发布节点 -->
<node pkg = "joint_state_publisher" type = "joint_state_publisher" name = "joint_state_publisher" />
<!-- 4. 添加机器人状态发布节点 -->
<node pkg = "robot_state_publisher" type = "robot_state_publisher" name = "robot_state_publisher" />
<!-- 5. 关节运动控制节点 -->
<node pkg = "joint_state_publisher_gui" type = "joint_state_publisher_gui" name = "joint_state_publisher_gui" />
</launch>