<?xml version="1.0"?>

<robot xmlns:xacro="http://ros.org/wiki/xacro">
    <xacro:property name="M_PI" value="3.1415926535897931" />
  
    <xacro:macro name="asus_camera_model" params="name parent *origin">
      <joint name="${name}_joint" type="fixed">
        <xacro:insert_block name="origin" />
        <parent link="${parent}"/>
        <child link="${name}_link"/>
      </joint>
      
      <link name="${name}_link">
        <inertial>
          <mass value="0.200" />
          <origin xyz="0 0 0" rpy="0 0 0" />
          <inertia ixx="5.8083e-4" ixy="0" ixz="0" iyy="3.0833e-5" iyz="0" izz="5.9083e-4" />
        </inertial>
        <visual>
          <origin xyz="0 0 0" rpy="0 0 0" />
          <geometry>
            <mesh filename="package://hector_sensors_description/meshes/asus_camera/asus_camera_simple.dae"/>
          </geometry>
        </visual>
       <!--        
       <collision>
          <origin xyz="0 0 0" rpy="0 0 0" />
          <geometry>
            <box size="0.035 0.185 0.025"/>
          </geometry>
        </collision>
        -->
        <collision>
          <origin xyz="0 0 0" rpy="0 0 0" />
          <geometry>
            <mesh filename="package://hector_sensors_description/meshes/asus_camera/asus_camera_simple.dae"/>
          </geometry>
        </collision>

      </link>

      <joint name="${name}_depth_joint" type="fixed">
        <origin xyz="-0.03 0.049 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="${-M_PI/2} 0.0 ${-M_PI/2}" />
        <parent link="${name}_depth_frame" />
        <child link="${name}_depth_optical_frame"/>
      </joint>

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

      <joint name="${name}_rgb_joint" type="fixed">
        <origin xyz="0.0 0.022 0.0" rpy="0 0 0" />
        <parent link="${name}_link" />
        <child link="${name}_rgb_frame"/>
      </joint>

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

      <joint name="${name}_rgb_optical_joint" type="fixed">
        <origin xyz="0 0 0" rpy="${-M_PI/2} 0.0 ${-M_PI/2}" />
        <parent link="${name}_rgb_frame" />
        <child link="${name}_rgb_optical_frame"/>
      </joint>

      <link name="${name}_rgb_optical_frame"/>
    </xacro:macro>

    <xacro:macro name="asus_camera" params="name parent *origin">
      <xacro:asus_camera_model name="${name}" parent="${parent}">
        <xacro:insert_block name="origin" />
      </xacro:asus_camera_model>
  
      <!-- ASUS Xtion PRO camera for simulation -->
      <gazebo reference="${name}_depth_frame">
        <sensor type="depth" name="${name}">
          <update_rate>20</update_rate>
          <camera>
            <horizontal_fov>${62.8 * M_PI/180.0}</horizontal_fov>
            <image>
              <format>R8G8B8</format>
              <width>640</width>
              <height>480</height>
            </image>
            <clip>
              <near>0.5</near>
              <far>9</far>
            </clip>
          </camera>
          <plugin name="${name}_camera_controller" filename="libgazebo_ros_openni_kinect.so">
            <alwaysOn>true</alwaysOn>
            <updateRate>20</updateRate>
            <imageTopicName>${name}/rgb/image_raw</imageTopicName>
            <cameraInfoTopicName>${name}/rgb/camera_info</cameraInfoTopicName>
            <depthImageTopicName>${name}/depth/image_raw</depthImageTopicName>
            <depthImageCameraInfoTopicName>${name}/depth/camera_info</depthImageCameraInfoTopicName>
            <pointCloudTopicName>${name}/depth/points</pointCloudTopicName>
            <frameName>${name}_depth_optical_frame</frameName>
            <distortion_k1>0.0</distortion_k1>
            <distortion_k2>0.0</distortion_k2>
            <distortion_k3>0.0</distortion_k3>
            <distortion_t1>0.0</distortion_t1>
            <distortion_t2>0.0</distortion_t2>
          </plugin>
        </sensor>
      </gazebo>
    </xacro:macro>
  </robot>
  
