Ultrasonic Sensor¶
raspbot.sensors.ultrasonic.UltrasonicSensor
Reads distance from the HC-SR04-compatible ultrasonic sensor wired through the Raspbot V2 microcontroller.
Protocol:
- Enable the sensor via register
0x07 - Wait 60 ms for the first measurement to stabilise
- Read high byte from register
0x1Band low byte from0x1A - Distance (mm) =
(high_byte << 8) | low_byte - Disable the sensor when done
Access via Robot.ultrasonic.
Methods¶
enable()¶
Power on the ultrasonic sensor and apply the 60 ms warmup delay.
disable()¶
Power off the ultrasonic sensor.
read_mm()¶
Return the current distance reading in millimetres.
If the sensor is not yet enabled, enable() is called automatically (including the warmup delay).
Returns: Distance in mm. Returns 0 if the reading is invalid.
read_cm()¶
Return the current distance reading in centimetres (read_mm() / 10.0).
Context manager¶
UltrasonicSensor supports the context manager protocol.
enable() is called on entry and disable() on exit.