<?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/wheel.transmission.xacro"/>
  <xacro:include filename="$(find pmb2_description)/urdf/wheels/wheel.gazebo.xacro"/>

  <xacro:macro name="wheel" params="side reflect radius width torque velocity separation parent">
    <link name="wheel_${side}_link">
      <inertial>
        <origin xyz="0 0 ${-0.00207 * reflect}" rpy="0 0 0"/>
        <mass value="1.82362"/>
        <inertia ixx="0.00499743171" ixy="${ 0.00000004464 * reflect}" ixz="-0.00000002245"
          iyy="0.00499741733" iyz="${-0.00000000164 * reflect}"
          izz="0.00839239692"/>
        </inertial>
        <visual>
          <origin xyz="0 0 0" rpy="${-90.0 * (1 - reflect) * deg_to_rad} 0 0"/>
          <geometry>
            <mesh filename="package://pmb2_description/meshes/wheels/wheel.stl"/>
          </geometry>
          <material name="DarkGrey"/>
        </visual>
        <collision>
          <origin xyz="0 0 0" rpy="0 0 0"/>
          <geometry>
            <sphere radius="${radius}" /> <!-- length="${width}"/> -->
          </geometry>
        </collision>
      </link>
      
      <link name="suspension_${side}_link">
        <inertial>
          <origin xyz="0 0 -0.02" rpy="0 0 0"/>
          <mass value="10"/>
          <inertia ixx="1.0" ixy="0" ixz="0"
                   iyy="1.0" iyz="0"
                   izz="1.0"/>
         </inertial>
        <kinematic>0</kinematic>
        <gravity>1</gravity>
      </link>


      <joint name="suspension_${side}_joint" type="prismatic">
        <origin xyz="0 0 0" rpy="0 0 0"/>
        <axis xyz="0 0 1"/>
        <limit effort="100.0" lower="-0.005" upper="0.005" velocity="1.0"/>
        <child link="suspension_${side}_link"/>
        <parent link="${parent}_link"/>
        <calibration rising="0.0" />
        <dynamics damping="100"/>

        <safety_controller k_position="0"
          k_velocity="10"
          soft_lower_limit="-0.005"
          soft_upper_limit="0.005" />
        </joint>
       

        <joint name="wheel_${side}_joint" type="continuous">
          <parent link="suspension_${side}_link"/>
          <child link="wheel_${side}_link"/>
          <origin xyz="0 ${-separation / 2 * reflect} 0.0" rpy="${-90.0 * deg_to_rad} 0 0"/>
          <axis xyz="0 0 1"/>
          <limit effort="${torque}" velocity="${velocity / radius}"/>
        </joint>

        <!-- Transmission -->
        <xacro:wheel_transmission side="${side}"/>

        <!-- Gazebo extensions -->
        <xacro:wheel_gazebo side="${side}"/>
      </xacro:macro>
    </robot>
