<?xml version="1.0"?>
<!--

  Copyright (c) 2021 PAL Robotics S.L. All rights reserved.
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<robot xmlns:xacro="http://ros.org/wiki/xacro">

  <xacro:include filename="$(find pmb2_description)/urdf/sensors/structure_sensor.gazebo.xacro" />

  <!-- Macro -->
  <xacro:macro name="structure_sensor" params="name parent *origin *optical_origin">

    <!-- frames in the center of the camera -->
    <joint name="${name}_joint" type="fixed">
      <xacro:insert_block name="origin"/>
      <axis xyz="0 0 1"/>
      <!-- <limit lower="0" upper="0.001" effort="100" velocity="0.01"/> -->
      <parent link="${parent}_link"/>
      <child link="${name}_link"/>
    </joint>

    <link name="${name}_link">
      <inertial>
        <mass value="0.01" />
        <origin xyz="0 0 0" rpy="0 0 0"/>
        <!-- inertia tensor computed analytically for a solid cuboid -->
        <inertia ixx="0.000030"  ixy="0.0"  ixz="0.0"
                 iyy="0.000030"  iyz="0.0"
                 izz="0.000002" />
      </inertial>
      <visual>
        <origin xyz="0 0 0" rpy="0 0 0"/>
        <geometry>
          <mesh filename="package://pmb2_description/meshes/sensors/structure_sensor.stl" scale="0.001 0.001 0.001" />
        </geometry>
        <material name="Grey">
          <color rgba="0.5 0.5 0.5 1"/>
        </material>
      </visual>
      <collision>
        <origin xyz="-0.01 0.0025 0" rpy="0 0 0"/>
        <geometry>
          <box size="0.04 0.185 0.03"/>
        </geometry>
      </collision>
    </link>

    <joint name="${name}_optical_joint" type="fixed">
      <xacro:insert_block name="optical_origin" />
      <parent link="${name}_link"/>
      <child link="${name}_optical_frame"/>
    </joint>

    <link name="${name}_optical_frame">
      <inertial>
        <mass value="0.01" />
        <origin xyz="0 0 0" rpy="0 0 0"/>
        <inertia ixx="0.0"  ixy="0.0"  ixz="0.0"
                 iyy="0.0"  iyz="0.0"
                 izz="0.0" />
      </inertial>
    </link>

    <!-- frames of the depth sensor -->
    <joint name="${name}_depth_joint" type="fixed">
      <origin xyz="0.0 0.03825 0.0" rpy="0 0 0" />
      <parent link="${name}_link" />
      <child link="${name}_depth_frame"/>
    </joint>

    <link name="${name}_depth_frame"/>

    <joint name="${name}_depth_optical_joint" type="fixed">
      <origin xyz="0 0 0" rpy="${-90.0 * deg_to_rad} 0.0 ${-90.0 * deg_to_rad}" />
      <parent link="${name}_depth_frame" />
      <child link="${name}_depth_optical_frame"/>
    </joint>

    <link name="${name}_depth_optical_frame"/>

    <!-- extensions -->
    <xacro:structure_sensor_rgbd_camera_gazebo name="${name}" />

  </xacro:macro>

</robot>
