<?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/deg_to_rad.xacro"/>

  <xacro:include filename="$(find pmb2_description)/urdf/wheels/wheel.urdf.xacro"/>
  <xacro:include filename="$(find pmb2_description)/urdf/wheels/caster.urdf.xacro"/>

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

  <xacro:include filename="$(find pmb2_description)/urdf/objects/cover.urdf.xacro"/>
  <xacro:include filename="$(find pmb2_description)/urdf/objects/antenna.urdf.xacro"/>

  <!-- Wheel characteristics -->
  <xacro:property name="wheel_radius"     value="0.0985"/>
  <xacro:property name="wheel_width"      value="0.040"/>
  <xacro:property name="wheel_separation" value="0.4044"/>
  <xacro:property name="wheel_torque"     value="6.0"/>
  <xacro:property name="wheel_velocity"   value="1.0"/>

  <!-- Caster wheel characteristics -->
  <xacro:property name="caster_radius"       value="0.025"/>
  <xacro:property name="caster_width"        value="0.015"/>
  <xacro:property name="caster_separation_x" value="0.343"/>
  <xacro:property name="caster_separation_y" value="0.204"/>
  <xacro:property name="caster_offset_x"     value="-0.002"/>
  <xacro:property name="caster_offset_y"     value="0.0"/>
  <xacro:property name="caster_offset_z"     value="-0.0335"/>

  <xacro:macro name="base" params="name">

    <!-- Base -->
    <link name="${name}_link">
      <inertial>
        <origin xyz="-0.00000106 0.00000109 0.0000387" rpy="0 0 0"/>
        <mass value="28.26649"/>
        <inertia ixx="0.465408937" ixy="0.002160024" ixz="-0.001760255"
                 iyy="0.483193291" iyz="-0.000655952"
                 izz="0.550939703"/>
      </inertial>
      <visual>
        <origin xyz="0 0 0" rpy="0 0 0" />
        <geometry>
          <mesh filename="package://pmb2_description/meshes/base/base.stl" />
        </geometry>
        <material name="White" />
      </visual>
      <collision>
        <origin xyz="0 0 0" rpy="0 0 0" />
        <geometry>
          <mesh filename="package://pmb2_description/meshes/base/base_collision.stl" />
        </geometry>
        <material name="White" />
      </collision>
    </link>
    <gazebo reference="${name}_link">
      <material>Gazebo/White</material>
    </gazebo>

    <!-- Base footprint -->
    <link name="${name}_footprint"/>

    <joint name="${name}_footprint_joint" type="fixed">
      <origin xyz="0 0 ${wheel_radius}" rpy="0 0 0" />
      <child link="${name}_link" />
      <parent link="${name}_footprint"/>
    </joint>

  <!-- gazebo parameters are given here for base_link thru base_footprint -->
    <gazebo reference="${name}_footprint">
      <kp>100000000.0</kp>
      <kd>10.0</kd>
      <mu1>0.1</mu1>
      <mu2>0.1</mu2>
      <fdir1>1 0 0</fdir1>
      <maxVel>10.0</maxVel>
      <minDepth>0.0005</minDepth>
      <laserRetro>0</laserRetro>
    </gazebo>

    <!-- Orange cover & antennas -->
    <xacro:cover name="${name}_cover" parent="${name}_footprint"/>
    <xacro:antenna name="${name}_antenna_left" parent="${name}_link">
      <origin xyz="-0.201 0.1062 0.195" rpy="0 0 0"/>
    </xacro:antenna>
    <xacro:antenna name="${name}_antenna_right" parent="${name}_link">
      <origin xyz="-0.201 -0.1062 0.195" rpy="0 0 0"/>
    </xacro:antenna>

    <!-- IMU -->
    <!-- @todo pmb2_hardware_gazebo needs an IMU!  -->
    <xacro:imu_sensor name="${name}_imu" parent="${name}_link" update_rate="100.0">
      <origin xyz="0.10171 0 0.11282" rpy="0 0 ${-90 * deg_to_rad}"/>
    </xacro:imu_sensor>

    <!-- Wheels -->
    <xacro:wheel side="right" reflect=" 1.0" radius="${wheel_radius}" width="${wheel_width}" torque="${wheel_torque}" velocity="${wheel_velocity}" separation="${wheel_separation}" parent="${name}"/>
    <xacro:wheel side="left"  reflect="-1.0" radius="${wheel_radius}" width="${wheel_width}" torque="${wheel_torque}" velocity="${wheel_velocity}" separation="${wheel_separation}" parent="${name}"/>

    <!-- Casters wheels -->
    <xacro:caster side="front_right" radius="${caster_radius}" width="${caster_width}" separation_x="${ caster_separation_x}" separation_y="${ caster_separation_y}" offset_x="${caster_offset_x}" offset_y="${caster_offset_y}" offset_z="${caster_offset_z}" parent="${name}"/>
    <xacro:caster side="front_left"  radius="${caster_radius}" width="${caster_width}" separation_x="${ caster_separation_x}" separation_y="${-caster_separation_y}" offset_x="${caster_offset_x}" offset_y="${caster_offset_y}" offset_z="${caster_offset_z}" parent="${name}"/>

    <xacro:caster side="back_right"  radius="${caster_radius}" width="${caster_width}" separation_x="${-caster_separation_x}" separation_y="${ caster_separation_y}" offset_x="${caster_offset_x}" offset_y="${caster_offset_y}" offset_z="${caster_offset_z}" parent="${name}"/>
    <xacro:caster side="back_left"   radius="${caster_radius}" width="${caster_width}" separation_x="${-caster_separation_x}" separation_y="${-caster_separation_y}" offset_x="${caster_offset_x}" offset_y="${caster_offset_y}" offset_z="${caster_offset_z}" parent="${name}"/>

  </xacro:macro>

</robot>
