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

  <!-- ─────────────────────────────────────────────────────────────────────
       HC-SR04 ultrasonic range sensor

       frame_id "ultrasonic" matches what ultrasonic_node publishes on
       /ultrasonic/range.  Mounted on the front face of the chassis,
       pointing forward.
       ───────────────────────────────────────────────────────────────────── -->

  <link name="ultrasonic">
    <visual>
      <geometry><box size="0.02 0.04 0.02"/></geometry>
      <material name="light_grey"/>
    </visual>
    <collision>
      <geometry><box size="0.02 0.04 0.02"/></geometry>
    </collision>
    <inertial>
      <mass value="0.01"/>
      <inertia ixx="0.000001" ixy="0" ixz="0"
               iyy="0.000001" iyz="0" izz="0.000001"/>
    </inertial>
  </link>

  <joint name="ultrasonic_joint" type="fixed">
    <parent link="base_link"/>
    <child  link="ultrasonic"/>
    <!-- Front-centre of the chassis, mid-height -->
    <origin xyz="${base_length/2 + 0.01} 0.0 0.0" rpy="0 0 0"/>
  </joint>

  <!-- ─────────────────────────────────────────────────────────────────────
       Gazebo sonar sensor  (Gazebo Harmonic / gz-sim)

       gz-sim has no native sonar sensor type.  The HC-SR04 is approximated
       as a narrow-cone gpu_lidar — a 5×5 ray grid spanning ±7.5° (≈15° FOV)
       in both axes, which matches the HC-SR04's beam pattern.

       The sensor publishes a gz LaserScan on /ultrasonic/scan.  Bridge this
       to ROS 2 via ros_gz_bridge, then convert to sensor_msgs/Range with a
       small relay node (or just consume the LaserScan directly):
         /ultrasonic/scan  (gz LaserScan) → /ultrasonic/scan  (ROS 2)
       ───────────────────────────────────────────────────────────────────── -->
  <gazebo reference="ultrasonic">
    <sensor name="sonar" type="gpu_lidar">
      <always_on>1</always_on>
      <update_rate>10</update_rate>
      <visualize>true</visualize>
      <topic>/ultrasonic/scan</topic>
      <gz_frame_id>ultrasonic</gz_frame_id>
      <lidar>
        <scan>
          <horizontal>
            <samples>5</samples>
            <resolution>1</resolution>
            <min_angle>-0.1309</min_angle>  <!-- −7.5° -->
            <max_angle> 0.1309</max_angle>  <!-- +7.5° -->
          </horizontal>
          <vertical>
            <samples>5</samples>
            <min_angle>-0.1309</min_angle>
            <max_angle> 0.1309</max_angle>
          </vertical>
        </scan>
        <range>
          <min>${sonar_min_range}</min>
          <max>${sonar_max_range}</max>
          <resolution>0.01</resolution>
        </range>
        <noise>
          <type>gaussian</type>
          <mean>0.0</mean>
          <stddev>0.005</stddev>
        </noise>
      </lidar>
    </sensor>
  </gazebo>

</robot>
