文章目录
参考: https://blog.csdn.net/aazjt/article/details/70920087
roslaunch 是如何解析的 launch
Includes are processed in depth-first traversal order. Tags are evaluated serially and the last setting wins. Thus, if there are multiple settings of a parameter, the last value specified for the parameter will be used.
include的文件是深度优先解析,tag是顺序解析,tag值是最终被设置那个值。
arg
arg
相当于launch
的局部变量,default
说明此变量可修改,value
说明变量的值不可被修改。
// 定义
<arg name="root" default="$(find shell_car)"/>
// 使用 $(arg var_name)
<arg name="urdf_robot_file" value="$(arg root)/urdf/smart.xacro"/>
<?xml version="1.0" encoding="UTF-8"?>
<launch>
<arg name="root" default="$(find shell_car)"/>
<include file="$(arg root)/launch/spawn_xacro.launch">
<arg name="x" value="0.0"/>
<arg name="y" value="0.0"/>
<arg name="z" value="0.0"/>
<arg name="urdf_robot_file" value="$(arg root)/urdf/smart.xacro"/>
<arg name="robot_name" value="smart"/>
</include>
</launch>
param
param
是launch
向可执行程序传递参数的方法。
group
被group
包围的节点,发布的parameters、topics、services
均会被加上<命名空间>/
前缀。
<group ns="命名空间">
...
</group>
launch之间传递参数
好像没有什么好的办法
1 https://answers.ros.org/question/220296/roslaunch-forwarding-args-to-included-launch-file/
3 https://blog.csdn.net/cookie909/article/details/79700354
两种可行的方法:
-
利用
inlcude
的文件内部arg
是default
,外面override
一下,要在include
标签内重写。(下面的例子是这样的)在
inchude
标签内的arg
是给被include
的launch
文件提供的arg
,不是为本launch
文件提供的。且被include
的launch
文件不能继承上层的arg
。// 最外层的 launch文件 <?xml version="1.0" encoding="UTF-8"?> <launch> <arg name="pkg_name" value="$(find shell_car)"/> <include file="$(arg pkg_name)/launch/spawn_xacro.launch"> <arg name="root" value="$(arg pkg_name)"/> // 外层覆盖掉内层的value <arg name="x" value="0.0"/> <arg name="y" value="0.0"/> <arg name="z" value="0.0"/> <arg name="urdf_robot_file" value="$(arg pkg_name)/urdf/smart.xacro"/> <arg name="robot_name" value="smart"/> </include> </launch> // 里层的 launch文件 <?xml version="1.0" encoding="UTF-8"?> <launch> <arg name="root" default=""/> // 内层定义了‘root’ 默认值随意,反正外层的include标签内都会修改它 <arg name="x" default="0.0"/> <arg name="y" default="0.0"/> <arg name="z" default="0.0"/> <arg name="urdf_robot_file" default=""/> <arg name="robot_name" default=""/> <param name="robot_description" command="$(find xacro)/xacro '$(arg urdf_robot_file)' roboname:='smart' root:=$(arg root)"/> <node name="spawn_model" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen" args="-urdf -x $(arg x) -y $(arg y) -z $(arg z) -model $(arg robot_name) -param robot_description"/> </launch>
-
利用
optenv
设置环境变量,试过不成功gg
launch中向xacro传递参数,并解析xacro
launch中向xacro
传递参数的方法,xacro
是一个可执行程序或者脚本,launch通过param
可以向其传递参数。
name必须是这个robot_description
,可执行程序xacro
自动解析这个参数,
调用xacro.py
解析xacro
文件,并且传入ns
参数,
在xacro文件中可以通过$(arg ns)
来获取传入的参数,
将解析出来的urdf模型设置给<命名空间>/robot_description
参数。
<param name="robot_description"
command="$(find xacro)/xacro.py
$(find battlebot)/urdf/$(arg model) ns:=<命名空间>"
/>
launch中向gazebo添加模型
<node name="spawn_model" pkg="gazebo_ros"
type="spawn_model" respawn="false" output="screen"
args="-urdf -x $(arg x) -y $(arg y) -z $(arg z)
-model $(arg robot_name)
-param robot_description"
/>
xacro
添加xacro属性:<xacro:property name="local_param" value="$(arg launch_param)" />
解析本地参数: <link name = "${local_param}">
一个例子
shell命令
// 一个窗口
source devel/setup.bash
roslaunch gazebo_ros empty_world.launch
// 另一个窗口
source devel/setup.bash
roslaunch shell_car spawn_car.launch
所有文件
2个launch文件,1个xacro文件,xacro文件用到了2个3D模型库(这里没给出)。
launch/spawn_car.launch
运行的是这个文件
launch/spawn_xacro.launch
解析xacro
文件,并将模型加入gazebo
urdf/smart.xacro
模型文件xacro
,定义了关节链接关系等。
目录结构如下:
launch/spawn_car.launch
<?xml version="1.0" encoding="UTF-8"?>
<launch>
<!-- 'pkg_name' is package name, there is the path of 'shell_car' -->
<arg name="pkg_name" value="$(find shell_car)"/>
<include file="$(arg pkg_name)/launch/spawn_xacro.launch">
<!-- arg 'root' value is modified here, there is it's final value -->
<arg name="root" value="$(arg pkg_name)"/>
<arg name="x" value="0.0"/>
<arg name="y" value="0.0"/>
<arg name="z" value="0.0"/>
<arg name="urdf_robot_file" value="$(arg pkg_name)/urdf/smart.xacro"/>
<arg name="robot_name" value="smart"/>
</include>
</launch>
launch/spawn_xacro.launch
<?xml version="1.0" encoding="UTF-8"?>
<launch>
<!-- 'root' in '$(arg root)' is from launch/spawn_car.launch
Note:
the arg 'root' is default,
because it's not the final last here, it is modified by 'spawn_car.launch'
‘root’ must be defined here, included file cannot inherit upper launch args.
-->
<arg name="root" default="" />
<arg name="x" default="0.0"/>
<arg name="y" default="0.0"/>
<arg name="z" default="0.0"/>
<arg name="urdf_robot_file" default=""/>
<arg name="robot_name" default=""/>
<!-- <param name="robot_description" command="$(arg urdf_robot_file)"/> -->
<param name="robot_description" command="$(find xacro)/xacro '$(arg urdf_robot_file)' roboname:='smart' root:=$(arg root)"/>
<node name="spawn_model" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen" args="-urdf -x $(arg x) -y $(arg y) -z $(arg z) -model $(arg robot_name) -param robot_description"/>
</launch>
urdf/smart.xacro
<?xml version="1.0" ?>
<robot name="$(arg roboname)" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- 'root_pkg' is used 3D model file path,
pkg_name/urdf/smart.xacro , arg 'root' from launch/spawn_xacro.launch -->
<xacro:property name="root_pkg" value="$(arg root)"/>
<xacro:property name="PI" value="3.1415926835897931"/>
<!-- base -->
<xacro:property name="base_length" value="2.695"/>
<xacro:property name="base_width" value="0.68"/>
<xacro:property name="base_height" value="1.370"/>
<xacro:property name="base_collision_height" value="0.875"/>
<xacro:property name="base_mass" value="956"/>
<xacro:property name="base_mass_ixx" value="343"/>
<xacro:property name="base_mass_iyy" value="728"/>
<xacro:property name="base_mass_izz" value="772"/>
<!-- rear tyre -->
<xacro:property name="rear_tyre_x" value="0.945"/>
<xacro:property name="rear_tyre_y" value="0.642"/>
<xacro:property name="rear_tyre_r" value="0.3"/>
<xacro:property name="rear_tyre_length" value="0.165"/>
<xacro:property name="rear_tyre_mass" value="20"/>
<xacro:property name="rear_tyre_mass_ixx" value="0.5"/>
<xacro:property name="rear_tyre_mass_iyy" value="0.9"/>
<xacro:property name="rear_tyre_mass_izz" value="0.5"/>
<!-- front tyre -->
<xacro:property name="front_tyre_x" value="0.923"/>
<xacro:property name="front_tyre_y" value="0.642"/>
<xacro:property name="front_tyre_r" value="0.3"/>
<xacro:property name="front_tyre_length" value="0.165"/>
<xacro:property name="front_tyre_mass" value="20"/>
<xacro:property name="front_tyre_mass_ixx" value="0.5"/>
<xacro:property name="front_tyre_mass_iyy" value="0.9"/>
<xacro:property name="front_tyre_mass_izz" value="0.5"/>
<!-- steering -->
<xacro:property name="str_angle" value="0.6"/>
<xacro:property name="str_length" value="0.01"/>
<xacro:property name="str_radius" value="0.1"/>
<xacro:property name="str_mass" value="5"/>
<xacro:property name="str_mass_ixx" value="0.012"/>
<xacro:property name="str_mass_iyy" value="0.025"/>
<xacro:property name="str_mass_izz" value="0.012"/>
<!--Car Body-->
<link name="base_link">
<collision>
<origin xyz="0 0 ${base_collision_height}" rpy="0 0 0"/>
<geometry>
<box size="${base_length} ${base_width} ${base_height}"/>
</geometry>
</collision>
<visual>
<origin xyz="0.05 0 ${base_collision_height}" rpy="0 0 ${PI/2}"/>
<geometry>
<mesh filename="${root_pkg}/meshes/vehicle_body.dae"/>
<!-- <box size="${base_length} ${base_width} ${base_height}"/> -->
</geometry>
</visual>
</link>
<joint name="inertial_joint" type="fixed">
<parent link="base_link"/>
<child link="main_mass"/>
<origin xyz="0 0 0" rpy="0 0 0"/>
</joint>
<link name="main_mass" type="fixed">
<inertial>
<origin xyz="0 0 0" rpy="0 0 0"/>
<mass value="${base_mass}"/>
<inertia
ixx="${base_mass_ixx}" ixy="0.000000" ixz="0.000000"
iyy="${base_mass_iyy}" iyz="0.000000"
izz="${base_mass_izz}"/>
</inertial>
</link>
<!--Rear Right Wheel-->
<joint name="rear_right_wheel_joint" type="continuous">
<parent link="base_link"/>
<child link="rear_right_wheel_link"/>
<origin xyz="${-rear_tyre_x} ${-rear_tyre_y} ${rear_tyre_r}" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<dynamics damping="0.1"/>
<limit effort="100000" velocity="10000" />
<joint_properties damping="0.0" friction="0.0" />
</joint>
<link name="rear_right_wheel_link">
<collision>
<origin xyz="0 0 0" rpy="${PI/2} 0 0"/>
<geometry>
<cylinder length="${rear_tyre_length}" radius="${rear_tyre_r}"/>
</geometry>
</collision>
<visual>
<origin xyz="0 0 0" rpy="0 0 -${PI/2}"/>
<geometry>
<mesh filename="${root_pkg}/meshes/wheel.dae"/>
<!-- <cylinder length="${rear_tyre_length}" radius="${rear_tyre_r}"/> -->
</geometry>
</visual>
<inertial>
<origin xyz="0 0 0" rpy="0 0 0"/>
<mass value="${rear_tyre_mass}"/>
<inertia
ixx="${rear_tyre_mass_ixx}" ixy="0.000000" ixz="0.000000"
iyy="${rear_tyre_mass_iyy}" iyz="0.000000"
izz="${rear_tyre_mass_izz}"/>
</inertial>
</link>
<!--Rear Left Wheel-->
<joint name="rear_left_wheel_joint" type="continuous">
<parent link="base_link"/>
<child link="rear_left_wheel_link"/>
<origin xyz="${-rear_tyre_x} ${rear_tyre_y} ${rear_tyre_r}" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<dynamics damping="0.1"/>
<limit effort="100000" velocity="10000" />
<joint_properties damping="0.0" friction="0.0" />
</joint>
<link name="rear_left_wheel_link">
<collision>
<origin xyz="0 0 0" rpy="${PI/2} 0 0"/>
<geometry>
<cylinder length="${rear_tyre_length}" radius="${rear_tyre_r}"/>
</geometry>
</collision>
<visual>
<origin xyz="0 0 0" rpy="0 0 ${PI/2}"/>
<geometry>
<mesh filename="${root_pkg}/meshes/wheel.dae"/>
<cylinder length="${rear_tyre_length}" radius="${rear_tyre_r}"/>
</geometry>
</visual>
<inertial>
<origin xyz="0 0 0" rpy="0 0 0"/>
<mass value="${rear_tyre_mass}"/>
<inertia
ixx="${rear_tyre_mass_ixx}" ixy="0.000000" ixz="0.000000"
iyy="${rear_tyre_mass_iyy}" iyz="0.000000"
izz="${rear_tyre_mass_izz}"/>
</inertial>
</link>
<!--Front Right Steering-->
<joint name="front_right_steering_joint" type="revolute">
<parent link="base_link"/>
<child link="front_right_steering_link"/>
<origin xyz="${front_tyre_x} ${-front_tyre_y} ${front_tyre_r}" rpy="0 0 0"/>
<axis xyz="0 0 1"/>
<limit lower="${-str_angle}" upper="${str_angle}" effort="1000000.0" velocity="10000.0"/>
<dynamics damping="10.0" friction="0.0"/>
</joint>
<link name="front_right_steering_link">
<visual>
<origin xyz="0 0 0" rpy="${PI/2} 0 0"/>
<geometry>
<cylinder length="${str_length}" radius="${str_radius}"/>
</geometry>
</visual>
<inertial>
<origin xyz="0 0 0" rpy="0 0 0"/>
<mass value="${str_mass}"/>
<inertia
ixx="${str_mass_ixx}" ixy="0.000000" ixz="0.000000"
iyy="${str_mass_iyy}" iyz="0.000000"
izz="${str_mass_izz}"/>
</inertial>
</link>
<!--Front Right Wheel-->
<joint name="front_right_wheel_joint" type="continuous">
<parent link="front_right_steering_link"/>
<child link="front_right_wheel_link"/>
<origin xyz="0 0 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<dynamics damping="0.1"/>
<limit effort="1000000" velocity="10000" />
<joint_properties damping="0.0" friction="0.0" />
</joint>
<link name="front_right_wheel_link">
<collision>
<origin xyz="0 0 0" rpy="${PI/2} 0 0"/>
<geometry>
<cylinder length="${front_tyre_length}" radius="${front_tyre_r}"/>
</geometry>
</collision>
<visual>
<origin xyz="0 0 0" rpy="0 0 -${PI/2}"/>
<geometry>
<mesh filename="${root_pkg}/meshes/wheel.dae"/>
<cylinder length="${front_tyre_length}" radius="${front_tyre_r}"/>
</geometry>
</visual>
<inertial>
<origin xyz="0 0 0" rpy="0 0 0"/>
<mass value="${front_tyre_mass}"/>
<inertia
ixx="${front_tyre_mass_ixx}" ixy="0.000000" ixz="0.000000"
iyy="${front_tyre_mass_iyy}" iyz="0.000000"
izz="${front_tyre_mass_izz}"/>
</inertial>
</link>
<!--Front Left Steering-->
<joint name="front_left_steering_joint" type="revolute">
<parent link="base_link"/>
<child link="front_left_steering_link"/>
<origin xyz="${front_tyre_x} ${front_tyre_y} ${front_tyre_r}" rpy="0 0 0"/>
<axis xyz="0 0 1"/>
<limit lower="${-str_angle}" upper="${str_angle}" effort="1000000.0" velocity="10000.0"/>
<dynamics damping="10.0" friction="0.0"/>
</joint>
<link name="front_left_steering_link">
<visual>
<origin xyz="0 0 0" rpy="${PI/2} 0 0"/>
<geometry>
<cylinder length="${str_length}" radius="${str_radius}"/>
</geometry>
</visual>
<inertial>
<origin xyz="0 0 0" rpy="0 0 0"/>
<mass value="${str_mass}"/>
<inertia
ixx="${str_mass_ixx}" ixy="0.000000" ixz="0.000000"
iyy="${str_mass_iyy}" iyz="0.000000"
izz="${str_mass_izz}"/>
</inertial>
</link>
<!--Front Left Wheel-->
<joint name="front_left_wheel_joint" type="continuous">
<parent link="front_left_steering_link"/>
<child link="front_left_wheel_link"/>
<origin xyz="0 0 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
<dynamics damping="0.1"/>
<limit effort="1000000" velocity="10000" />
<joint_properties damping="0.0" friction="0.0" />
</joint>
<link name="front_left_wheel_link">
<collision>
<origin xyz="0 0 0" rpy="${PI/2} 0 0"/>
<geometry>
<cylinder length="${front_tyre_length}" radius="${front_tyre_r}"/>
</geometry>
</collision>
<visual>
<origin xyz="0 0 0" rpy="0 0 ${PI/2}"/>
<geometry>
<mesh filename="${root_pkg}/meshes/wheel.dae"/>
<!-- <cylinder length="${front_tyre_length}" radius="${front_tyre_r}"/> -->
</geometry>
</visual>
<inertial>
<origin xyz="0 0 0" rpy="0 0 0"/>
<mass value="${front_tyre_mass}"/>
<inertia
ixx="${front_tyre_mass_ixx}" ixy="0.000000" ixz="0.000000"
iyy="${front_tyre_mass_iyy}" iyz="0.000000"
izz="${front_tyre_mass_izz}"/>
</inertial>
</link>
</robot>