Sonar

sonar.py

Overview

sonar.py is a synthetic ultrasound range-finder component that emits constant sensor_msgs/Range messages at 10 Hz. It enables sensor-fusion, navigation and obstacle-avoidance pipelines to be exercised even without real hardware or ROS bags.

Interfaces (strongly-typed, stateless)

Direction

Topic

Message type / Notes

Provided

/{robot}/sonar

sensor_msgs/Range – single-beam, 10 Hz

Contract

Pre-conditions

  • Node launched with a valid robot_id CLI argument.

  • Downstream expects Range.ULTRASOUND readings at ∼10 Hz.

Post-conditions

  • Publishes exactly one Range per loop iteration.

  • range field is fixed at 3.0 m.

  • Header uses frame_id “sonar” and current time stamp.

Invariants

  • Loop frequency = 10 Hz ± 5%.

  • field_of_view = 0.1 rad, min_range = 0.2 m, max_range = 4.0 m.

Quality-of-Service KPIs

Metric

Target

Rationale

Message rate

10 Hz ± 0.5 Hz

Keeps real-time filters stable.

Latency

< 50 ms

Avoids stale obstacle data.

CPU load

< 1 %

Safe on embedded CPUs.

Implementation notes

  • Uses rospy.Rate(10) for timing.

  • Message fields are hard-coded for simplicity; adjust constants below to emulate other sensors.

sonar.publish()[source]

Advertise /sonar and stream constant Range messages until shutdown.

Workflow

  1. Initialise ROS node <robot>_sonar_node, anonymous=True for multiples.

  2. Advertise /robot/sonar (queue_size=10).

  3. In a 10 Hz loop:

    1. Create sensor_msgs/Range.

    2. Set header.stamp = now(), header.frame_id = _FRAME_ID.

    3. Set radiation_type = Range.ULTRASOUND.

    4. Assign _FOV, _MIN_RANGE, _MAX_RANGE, _FIXED_RANGE.

    5. Publish and sleep to maintain rate.