<?xml version="1.0"?>
<robot name="sensor_astra" xmlns:xacro="http://ros.org/wiki/xacro">

    <!-- Xacro properties -->
    <xacro:property name="astra_cam_py" value="-0.0125"/>
    <xacro:property name="astra_depth_rel_rgb_py" value="0.0250"/>
    <xacro:property name="astra_cam_rel_rgb_py" value="-0.0125"/>
    <xacro:property name="astra_dae_display_scale" value="0.1"/>
    <!-- Parameterised in part by the values in turtlebot_properties.urdf.xacro -->
    <xacro:macro name="sensor_astra" params="parent prefix *origin">
        <joint name="${prefix}_joint" type="fixed">
            <xacro:insert_block name="origin"/>
            <parent link="${parent}"/>
            <child link="${prefix}_link"/>
        </joint>
        <link name="${prefix}_link">

            <collision>
                <!-- <origin xyz="0.0 0.0 0.0" rpy="0 0 0" /> -->
                <origin xyz="-0.01 -0.06 0" rpy="0 0 0"/>
                <geometry>
                    <box size="0.0400 0.1650 0.0300"/>
                </geometry>
            </collision>
            <inertial>
                <mass value="0.564"/>
                <origin xyz="0 0 0"/>
                <inertia ixx="0.003881243" ixy="0.0" ixz="0.0"
                         iyy="0.000498940" iyz="0.0"
                         izz="0.003879257"/>
            </inertial>
        </link>
        <gazebo reference="${prefix}_link">
            <selfCollide>false</selfCollide>
        </gazebo>

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

        <joint name="${prefix}_rgb_optical_joint" type="fixed">
            <origin xyz="0 0 0" rpy="${-pi/2} 0 ${-pi/2}"/>
            <!--            <origin xyz="0 0 0" rpy="0 0 0"/>-->

            <parent link="${prefix}_rgb_frame"/>
            <child link="${prefix}_rgb_optical_frame"/>
        </joint>

        <link name="${prefix}_rgb_optical_frame">
            <visual>
                <origin xyz="0 0 0" rpy="0 0 0"/>
                <geometry>
                    <mesh filename="package://larcc_description/meshes/atom_astra_rgb/atom_astra_rgb.dae" scale="1 1 1"/>
                </geometry>
            </visual>
        </link>

        <joint name="${prefix}_depth_joint" type="fixed">
            <origin xyz="0 -0.02 0" rpy="0 0 0"/>
            <parent link="${prefix}_link"/>
            <child link="${prefix}_depth_frame"/>
        </joint>
        <link name="${prefix}_depth_frame"/>

        <joint name="${prefix}_depth_optical_joint" type="fixed">
            <origin xyz="0 0 0" rpy="${-pi/2} 0 ${-pi/2}"/>
            <parent link="${prefix}_depth_frame"/>
            <child link="${prefix}_depth_optical_frame"/>
        </joint>

        <link name="${prefix}_depth_optical_frame">
            <visual>
                <origin xyz="0 0 0" rpy="0 0 0"/>
                <geometry>
                    <!-- Use a slightly smaller scale than 1, so that when both models overlap its the rgb that appears.-->
                    <mesh filename="package://larcc_description/meshes/atom_astra_depth/atom_astra_depth.dae" scale="0.98 0.98 0.98"/>
                </geometry>
            </visual>
        </link>

        <gazebo reference="${prefix}_depth_frame">
            <sensor type="depth" name="camera">
                <always_on>true</always_on>
                <update_rate>10.0</update_rate>
                <camera>
                    <horizontal_fov>${60.0*pi/180.0}</horizontal_fov>
                    <image>
                        <format>B8G8R8</format>
                        <!--                      <format>RGB_INT8</format>-->
                        <width>640</width>
                        <height>480</height>
                    </image>
                    <clip>
                        <near>0.10</near>
                        <far>8.0</far>
                    </clip>
                </camera>
                <plugin name="${prefix}_depth_camera_controller" filename="libgazebo_ros_openni_kinect.so">
                    <cameraName>${prefix}</cameraName>
                    <alwaysOn>true</alwaysOn>
                    <updateRate>10.0</updateRate>
                    <!--                                      <imageTopicName>rgb/image_raw</imageTopicName>-->

                    <imageTopicName>null</imageTopicName>
                    <cameraInfoTopicName>null/camera_info</cameraInfoTopicName>
                    <depthImageTopicName>depth/image_raw</depthImageTopicName>
                    <pointCloudTopicName>depth/points</pointCloudTopicName>
                    <!--                  <cameraInfoTopicName>rgb/camera_info</cameraInfoTopicName>-->
                    <depthImageCameraInfoTopicName>depth/camera_info</depthImageCameraInfoTopicName>
                    <frameName>${prefix}_depth_optical_frame</frameName>
                    <!--                  <frameName>${prefix}_rgb_optical_frame</frameName>-->
                    <!--                  <frameName>${prefix}_link</frameName>-->
                    <!-- Apparently this is not used-->
                    <baseline>0.1</baseline>
                    <!--                  <hackBaseline>2</hackBaseline>-->
                    <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>
                    <pointCloudCutoff>0.4</pointCloudCutoff>
                </plugin>
            </sensor>
        </gazebo>
        <gazebo reference="${prefix}_rgb_frame">
            <sensor type="camera" name="camera">
                <update_rate>30.0</update_rate>
                <camera>
                    <!--                    <pose>0 0 0 ${M_PI/2} ${-M_PI/2} 0</pose>-->
                    <horizontal_fov>${60.0*pi/180.0}</horizontal_fov>
                    <image>
                        <format>B8G8R8</format>
                        <!--                      <format>RGB_INT8</format>-->
                        <width>640</width>
                        <height>480</height>
                    </image>
                    <clip>
                        <near>0.10</near>
                        <far>8.0</far>
                    </clip>
                </camera>
                <plugin name="${prefix}_rgb_camera_controller" filename="libgazebo_ros_camera.so">
                    <cameraName>${prefix}</cameraName>
                    <alwaysOn>true</alwaysOn>
                    <updateRate>0</updateRate>
                    <imageTopicName>rgb/image_raw</imageTopicName>
                    <!--                    <depthImageTopicName>depth/image_raw</depthImageTopicName>-->
                    <!--                    <pointCloudTopicName>depth/points</pointCloudTopicName>-->
                    <cameraInfoTopicName>rgb/camera_info</cameraInfoTopicName>
                    <!--                    <depthImageCameraInfoTopicName>depth/camera_info</depthImageCameraInfoTopicName>-->
                    <!--                    <frameName>${prefix}_depth_optical_frame</frameName>-->
                    <frameName>${prefix}_rgb_optical_frame</frameName>
                    <!--                  <frameName>${prefix}_link</frameName>-->
                    <!-- Apparently this is not used-->
                    <baseline>0.1</baseline>
                    <!--                  <hackBaseline>2</hackBaseline>-->
                    <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>
                    <!--                    <pointCloudCutoff>0.4</pointCloudCutoff>-->
                </plugin>
            </sensor>
        </gazebo>
    </xacro:macro>
</robot>
