roslaunch 中的条件语句(字符串与bool类型)

2023-05-16

工作原因,在配置机器人系统的时候为了统一管理,在roslaunch的启动脚本中经常需要根据不同的传入参数启动不同的launch文件。但是写法总是不记得,临时到处找以前的文件复制粘贴。

在这里记录一下:

<launch>
  <arg name="model_type"/> 
  <arg name="side_detect_check" /> 
  <arg name="visual" default="false"/>
  <group if="$(eval arg('model_type') == 'openvino')">

    <group unless="$(arg side_detect_check)">
      <include file="$(find human_detection_pkg)/launch/ov.launch">     
      </include>
    </group>

    <group if="$(arg side_detect_check)">
      <include file="$(find human_detection_pkg)/launch/ov_sidedet.launch">     
      </include>
    </group>
    <group if="$(arg visual)">
      <node pkg="rviz" type="rviz" name="rviz" args="-d $(find human_detection_pkg)/rviz/visual.rviz"/>
    </group>    
  </group>

  <group if="$(eval arg('model_type') == 'trt')">

    <group unless="$(arg side_detect_check)">
      <include file="$(find human_detection_pkg)/launch/trt.launch">     
      </include>
    </group>

    <group if="$(arg side_detect_check)">
      <include file="$(find human_detection_pkg)/launch/trt_sidedet.launch">     
      </include>
    </group>
    <group if="$(arg visual)">
      <node pkg="rviz" type="rviz" name="rviz" args="-d $(find human_detection_pkg)/rviz/visual.rviz"/>
    </group>

  </group>

  <group unless="$(eval arg('model_type') == 'trt')"> 
    <group unless="$(eval arg('model_type') == 'openvino')"> 
      <arg name="msg" default="Model type must be 'trt' or 'openvino'." />
      <node name="pub_text" pkg="rostopic" type="rostopic"
        args="pub /msg std_msgs/String '$(arg msg)'"/>
      <node name="print_text" pkg="rostopic" type="rostopic"
        args="echo /msg/data" output="screen" />
    </group>
  </group>  

</launch>

这是一段行人检测代码,叫human_detection.launch,

但是模型的类型有trt和openvino两类,以及还有一个侧视角的检测开关,因此启动方式实际上是:

roslaunch human_detection_pkg human_detection.launch model_type:=trt  side_detect_check:=true

如果是bool类型,就使用<group unless="$(arg xxx)">表示当xxx是false的时候执行,或<group if="$(arg xxx)">表示xxx是true的时候执行。

如果是字符串类型,就使用<group if="$(eval arg('model_type') == 'trt')"> 表示model_type是trt的时候执行,<group unless="$(eval arg('model_type') == 'trt')"> 则表示model_type不是trt的时候执行。

注意,最后我放了一段检测提示,即发现model_type既不是trt也不是openvino的时候,就输出一段提示Model type must be 'trt' or 'openvino'.

另外,我还尝试对eval字符串的判断这里使用“与”指令,但是语法怎么都搞不对,所以我懒得搞了,直接用了嵌套的条件命令。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

roslaunch 中的条件语句(字符串与bool类型) 的相关文章

随机推荐