ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

xacro prase the value from YAML by giving variable

asked 2021-08-26 08:56:16 -0600

NEET gravatar image

updated 2021-08-26 10:16:25 -0600

Environment: ROS Noetic, Gazebo11

I try to use xacro with YAML, but there is a problem about argument of variable:

My yaml:

---
  color_0: "1 1 1 1.0"
  color_1: "0 0 0 1.0"
  color_2: "0.84 0 0 1.0"
  color_3: "0.55 0.24 1 1.0"
  color_4: "0.01 0.53 0 1.0"

Because I want to use different value at different time.

So I tried to load the value as below:

  <xacro:property name="YAML_FILE" value="$(find gazebo_ros_model_color)/configs/colors.yaml" />
  <xacro:property name="COLORS" value="${load_yaml(YAML_FILE)}"/>

  <xacro:arg name="color" default="color_0" />  <!-- need to use variable -->
  <color>${COLORS[${color}]}</color>

But it cause the ERROR :((

...
invalid expression: ${color 
when evaluating expression 'COLORS['${color'
...

Is it possible to get the value from YAML by key variable given?

Any suggestion is welcome

Thank you

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2021-08-26 10:33:50 -0600

NEET gravatar image

The different use of xacro:property, xacro:arg, and xacro:params is:

xacro:arg is use $(arg VAR). xacro:property and xacro:params are use ${VAR}

So, I solve my problem as below:

<xacro:arg name="color" default="color_${id_num}" />
<color>${COLORS['$(arg color)']}</color>

And I also tried to use xacro:property, but CAN NOT use it like:

<xacro:property name="color" value="color_0" />
<color>${COLORS['${color}']}</color>
edit flag offensive delete link more
1

answered 2021-08-26 09:26:26 -0600

Ranjit Kathiriya gravatar image

updated 2021-08-26 09:43:26 -0600

Hello @NEET,

My XML File: colors.yaml

  color_0: "1 1 1 1.0"
  color_1: "0 0 0 1.0"
  color_2: "0.84 0 0 1.0"
  color_3: "0.55 0.24 1 1.0"
  color_4: "0.01 0.53 0 1.0"

For accessing Yaml file data,

<xacro:property name="properties" value="${load_yaml(package + '/<path>/colors.yaml')}"/>
<xacro:property name="color_0" value="${properties['color_0']}"/>

OR You can also load Yaml file like this way,

<xacro:property name="properties" value="${load_yaml($(find package)/filename.yaml)}"/>

I hope It will work, If you having any issues feel free to drop a comment.

edit flag offensive delete link more

Comments

Sorry...I didn't express the problem well. I need to pass another variable as key like ${properties[${var}]. because I want to pass the key that I want.

NEET gravatar image NEET  ( 2021-08-26 09:51:33 -0600 )edit

${var} Can you please! explain this? How you are entering this? thanks

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-08-26 09:58:35 -0600 )edit

Just to Note:

If you are having a variable let's say: M_body and its value is color_0.

<xacro:property name="M_body" value="color_0"/>

And do you want to access this value and you want to pass:

<xacro:property name="properties" value="${load_yaml(package + '/<path>/colors.yaml')}"/>

<xacro:property name="color_0" value="${properties[M_body]}"/>

I without " " -> I think it will work.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-08-26 10:05:09 -0600 )edit

Hi, I found the different between xacro property and arg

And I solve my problem like:

<xacro:arg name="color" default="color_${id_num}" />
<color>${COLORS['$(arg color)']}</color>

Thank you for your reply Really grateful

NEET gravatar image NEET  ( 2021-08-26 10:22:26 -0600 )edit

Can you please! post your solution as an answer and Tick your solution. It can help others.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-08-26 10:24:41 -0600 )edit

Yes, I posted my answer. Any advice is welcome. Thanks again :)

NEET gravatar image NEET  ( 2021-08-26 10:36:43 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2021-08-26 08:56:16 -0600

Seen: 278 times

Last updated: Aug 26 '21