<?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/wheels/caster.gazebo.xacro"/>
  <xacro:macro name="caster" params="side radius width separation_x separation_y offset_x offset_y offset_z parent">
    <link name="caster_${side}_1_link">
      <inertial>
        <origin xyz="-0.00634599 0.00005347 -0.02037519" rpy="0 0 0"/>
        <mass value="0.05144761"/>
        <inertia  ixx="0.00001513" ixy="-0.00000003" ixz="0.00000267"
                  iyy="0.00001309" iyz="-0.00000005"
                  izz="0.00001198"/>
      </inertial>
      <visual>
        <origin xyz="0 0 0" rpy="0 0 0"/>
        <geometry>
          <mesh filename="package://pmb2_description/meshes/wheels/caster_1.stl"/>
        </geometry>
        <material name="Grey"/>
      </visual>
      <!--collision>
        <origin xyz="0 0 0" rpy="0 0 0"/>
        <geometry>
          <cylinder radius="${radius}" length="${width}"/>
        </geometry>
      </collision-->
    </link>

    <link name="caster_${side}_2_link">
      <inertial>
        <origin xyz="0 0 0" rpy="0 0 0"/>
        <mass value="0.08855784"/>
        <inertia  ixx="0.00001075" ixy="0" ixz="0"
                  iyy="0.00001541" iyz="0"
                  izz="0.00001075"/>
        </inertial>
      <visual>
        <origin xyz="0 0 0" rpy="0 0 0"/>
        <geometry>
          <mesh filename="package://pmb2_description/meshes/wheels/caster_2.stl"/>
        </geometry>
        <material name="DarkGrey"/>
      </visual>
      <collision>
        <origin xyz="0 0 0" rpy="0 0 0"/>
        <geometry>
	  <sphere radius="${radius}" /> 
        </geometry>
      </collision>
    </link>

    <joint name="caster_${side}_1_joint" type="continuous">
      <parent link="${parent}_link"/>
      <child link="caster_${side}_1_link"/>
      <origin xyz="${offset_x + separation_x / 2} ${offset_y - separation_y / 2} ${offset_z}" rpy="0 0 0"/>
      <axis xyz="0 0 1"/>
      <dynamics damping="0.005" friction="0.0"/>
    </joint>

    <joint name="caster_${side}_2_joint" type="continuous">
      <parent link="caster_${side}_1_link"/>
      <child link="caster_${side}_2_link"/>
      <origin xyz="-0.016 0.0000 -0.040" rpy="${-90.0 * deg_to_rad} 0 0"/>
      <axis xyz="0 0 1"/>
    </joint>

    <!-- Gazebo extensions -->
    <xacro:caster_gazebo side="${side}"/>

  </xacro:macro>
</robot>
