learning-urdf-02

本文介绍如何使用xacro简化URDF模型定义,通过定义属性、宏及宏参数提高URDF文件的可读性和可维护性。xacro类似于预处理器,能够进行常量替换,并支持数学运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用xacro简化urdf代码

xacro相当于C语言中的#define用于替换一些常量,使得代码易读易改。
典型用法:

 rosrun xacro xacro model.xacro > model.urdf 

也可以在启动文件中自动生成urdf

<param name="robot_description"
 command="$(find xacro)/xacro '$(find pr2_description)/robots/pr2.urdf.xacro'" />

在urdf文件头需要声明是xacro文件

<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="firefighter">
1.定义一个属性
<xacro:property name="width" value=".2" />
2.属性引用
${width}
3.属性中可以进行数学运算
${width/2}

4.定义宏
<xacro:macro name="default_origin">
     <origin xyz="0 0 0" rpy="0 0 0"/>
</xacro:macro>
5.引用宏
<xacro:default_origin />
6.带参数的宏(可以看成函数)
<xacro:macro name="default_inertial" params="mass">
         <inertial>
                 <mass value="${mass}" />
                 <inertia 
                  ixx="1.0" ixy="0.0" ixz="0.0"
                      iyy="1.0" iyz="0.0" izz="1.0" 
                 />
         </inertial>
</xacro:macro>
7.引用宏函数
<xacro:default_inertial mass="10"/>
ros2 launch learning_urdf display.launch.py [INFO] [launch]: All log files can be found below /home/wheeltec/.ros/log/2025-07-11-20-46-04-934215-galactic-16082 [INFO] [launch]: Default logging verbosity is set to INFO Task exception was never retrieved future: <Task finished name='Task-2' coro=<LaunchService._process_one_event() done, defined at /opt/ros/galactic/lib/python3.8/site-packages/launch/launch_service.py:226> exception=PackageNotFoundError("package 'joint_state_publisher' not found, searching: ['/home/wheeltec/dev_ws1/install/learning_urdf', '/home/wheeltec/dev_ws1/install/learning_topic_cpp', '/home/wheeltec/dev_ws1/install/learning_topic', '/home/wheeltec/dev_ws1/install/learning_tf_cpp', '/home/wheeltec/dev_ws1/install/learning_tf', '/home/wheeltec/dev_ws1/install/learning_service_cpp', '/home/wheeltec/dev_ws1/install/learning_service', '/home/wheeltec/dev_ws1/install/learning_qos', '/home/wheeltec/dev_ws1/install/learning_pkg_python', '/home/wheeltec/dev_ws1/install/learning_pkg_c', '/home/wheeltec/dev_ws1/install/learning_parameter_cpp', '/home/wheeltec/dev_ws1/install/learning_parameter', '/home/wheeltec/dev_ws1/install/learning_node_cpp', '/home/wheeltec/dev_ws1/install/learning_node', '/home/wheeltec/dev_ws1/install/learning_launch', '/home/wheeltec/dev_ws1/install/learning_action_cpp', '/home/wheeltec/dev_ws1/install/learning_action', '/home/wheeltec/dev_ws1/install/learning_interface', '/home/wheeltec/dev_ws1/install/learning_gazebo_harmonic', '/home/wheeltec/dev_ws1/install/learning_gazebo', '/home/wheeltec/dev_ws1/install/learning_cv', '/home/wheeltec/usb_cam_ws/install/usb_cam', '/opt/ros/galactic']")> Traceback (most recent call last): File "/opt/ros/galactic/lib/python3.8/site-packages/ament_index_python/packages.py", line 50, in get_package_prefix content, package_prefix = get_resource('packages', package_name) File "/opt/ros/galactic/lib/python3.8/site-packages/ament_index_python/resources.py", line 48, in get_resource raise LookupError( LookupError: Could not find the resource 'joint_state_publisher' of type 'packages' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/ros/galactic/lib/python3.8/site-packages/launch/launch_service.py", line 228, in _process_one_event await self.__process_event(next_event) File "/opt/ros/galactic/lib/python3.8/site-packages/launch/launch_service.py", line 248, in __process_event visit_all_entities_and_collect_futures(entity, self.__context)) File "/opt/ros/galactic/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context) File "/opt/ros/galactic/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context) File "/opt/ros/galactic/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context) [Previous line repeated 1 more time] File "/opt/ros/galactic/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 38, in visit_all_entities_and_collect_futures sub_entities = entity.visit(context) File "/opt/ros/galactic/lib/python3.8/site-packages/launch/action.py", line 108, in visit return self.execute(context) File "/opt/ros/galactic/lib/python3.8/site-packages/launch_ros/actions/node.py", line 484, in execute ret = super().execute(context) File "/opt/ros/galactic/lib/python3.8/site-packages/launch/actions/execute_process.py", line 837, in execute self.__expand_substitutions(context) File "/opt/ros/galactic/lib/python3.8/site-packages/launch/actions/execute_process.py", line 681, in __expand_substitutions cmd = [perform_substitutions(context, x) for x in self.__cmd] File "/opt/ros/galactic/lib/python3.8/site-packages/launch/actions/execute_process.py", line 681, in <listcomp> cmd = [perform_substitutions(context, x) for x in self.__cmd] File "/opt/ros/galactic/lib/python3.8/site-packages/launch/utilities/perform_substitutions_impl.py", line 26, in perform_substitutions return ''.join([context.perform_substitution(sub) for sub in subs]) File "/opt/ros/galactic/lib/python3.8/site-packages/launch/utilities/perform_substitutions_impl.py", line 26, in <listcomp> return ''.join([context.perform_substitution(sub) for sub in subs]) File "/opt/ros/galactic/lib/python3.8/site-packages/launch/launch_context.py", line 197, in perform_substitution return substitution.perform(self) File "/opt/ros/galactic/lib/python3.8/site-packages/launch_ros/substitutions/executable_in_package.py", line 76, in perform package_prefix = super().perform(context) File "/opt/ros/galactic/lib/python3.8/site-packages/launch_ros/substitutions/find_package.py", line 79, in perform result = self.find(package) File "/opt/ros/galactic/lib/python3.8/site-packages/launch_ros/substitutions/find_package.py", line 96, in find return get_package_prefix(package_name) File "/opt/ros/galactic/lib/python3.8/site-packages/ament_index_python/packages.py", line 52, in get_package_prefix raise PackageNotFoundError( ament_index_python.packages.PackageNotFoundError: "package 'joint_state_publisher' not found, searching: ['/home/wheeltec/dev_ws1/install/learning_urdf', '/home/wheeltec/dev_ws1/install/learning_topic_cpp', '/home/wheeltec/dev_ws1/install/learning_topic', '/home/wheeltec/dev_ws1/install/learning_tf_cpp', '/home/wheeltec/dev_ws1/install/learning_tf', '/home/wheeltec/dev_ws1/install/learning_service_cpp', '/home/wheeltec/dev_ws1/install/learning_service', '/home/wheeltec/dev_ws1/install/learning_qos', '/home/wheeltec/dev_ws1/install/learning_pkg_python', '/home/wheeltec/dev_ws1/install/learning_pkg_c', '/home/wheeltec/dev_ws1/install/learning_parameter_cpp', '/home/wheeltec/dev_ws1/install/learning_parameter', '/home/wheeltec/dev_ws1/install/learning_node_cpp', '/home/wheeltec/dev_ws1/install/learning_node', '/home/wheeltec/dev_ws1/install/learning_launch', '/home/wheeltec/dev_ws1/install/learning_action_cpp', '/home/wheeltec/dev_ws1/install/learning_action', '/home/wheeltec/dev_ws1/install/learning_interface', '/home/wheeltec/dev_ws1/install/learning_gazebo_harmonic', '/home/wheeltec/dev_ws1/install/learning_gazebo', '/home/wheeltec/dev_ws1/install/learning_cv', '/home/wheeltec/usb_cam_ws/install/usb_cam', '/opt/ros/galactic']"
最新发布
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值