Ros/Gazebo defining (and changing) colors within a xacro file

asked 2022-03-02 13:17:33 -0500

zippyzoo77 gravatar image

updated 2022-03-02 13:50:10 -0500

I am trying to make a sphere in my simulation to represent a task location that my robot will be visiting. I would like to be able to recolor the sphere preferably based on a ros channel, but the first challenge is getting it to show up in the assigned color in gazebo at all. Below is the macro I am using to create the links, which has the correct behavior except for the color. I have several colors defined in the included file (red, blue, green, etc). How do I get these colors to pass into gazebo so the xacro that calls the macro can assign different colors to each task, without making separate macros for each color?

<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro">

  <xacro:include filename="$(find uuv_descriptions)/urdf/common.urdf.xacro"/>

  <xacro:macro name="task_vis" params="x y z color number">
    <link name="world"/>
    <link name="taskvisualizer/task_${number}">
      <xacro:no_inertial>
      </xacro:no_inertial>
      <visual>
        <origin xyz="${x} ${y} ${z}" rpy="0 0 0"/>
        <geometry>
          <sphere radius=".2"/>
          <material name="${color}"/>
        </geometry>
      </visual>
      <static>1</static>
    </link>
    <gazebo reference="task_vis/task_${number}">
      <is_static>true</is_static>
    </gazebo>
    <gazebo reference="taskvisualizer/task_${number}">
       <material>Gazebo/Red</material>
    </gazebo>
    <joint name="taskvisualizer/task_${number}_joint" type="fixed">
      <parent link="world"/>
      <child link="taskvisualizer/task_${number}"/>
    </joint>

  </xacro:macro>   

</robot>

Edit: I can get it to do a single color as above, but doing it this way would have me make a separate macro for each color I want to use, rather than being able to specify the color as a parameter, which is what I would like to do. I could maybe make this work if there were a way to do some sort of if/else loop, but that still seems cumbersome, and I expect it wouldn't work if I wanted to do the color changing I hope to do with it.

edit retag flag offensive close merge delete

Comments

I was able to also get it to work with the gazebo reference as

<gazebo reference="taskvisualizer/task_${number}">
      <material value="Gazebo/${color}"/>
</gazebo>

Where the color is capitalized in the file that calls the macro. Is there a way to specify my own materials in a way that gazebo will reference them? It still doesn't seem to work when I just put in "blue" as the value, despite the other file defining blue as a material, or if I move the reference to this file. (as below). I also can't seem to find a way to customize some of the other features of the gazebo visual link, like which shader it uses, which I would also like to do, so that they will be easy to see, rather than being shaded and super dark.

<material name="green">
<color rgba="0.0 0.8 ...
(more)
zippyzoo77 gravatar image zippyzoo77  ( 2022-03-02 14:52:16 -0500 )edit