fusion_hat.modules.ultrasonic module

class fusion_hat.modules.ultrasonic.Ultrasonic(trig, echo, timeout=0.02)[source]

Bases: object

Ultrasonic sensor module.

Parameters:
  • trig (Pin) – Trigger pin object.

  • echo (Pin) – Echo pin object.

  • timeout (float, optional) – Timeout duration in seconds. Default is 0.02.

Raises:

TypeError – If trig or echo is not a Pin object.

SOUND_SPEED = 343.3

Sound speed in meters per second.

read_raw()[source]

Read raw distance value from ultrasonic sensor.

Returns:

Distance in centimeters. Returns -1 if timeout occurs,

-2 if pulse start or end is 0, or any other error.

Return type:

float

read_with_retry(times=10)[source]

Read distance value with retry mechanism.

Parameters:

times (int, optional) – Number of retry attempts. Default is 10.

Returns:

Distance in centimeters. Returns -1 if all attempts fail.

Return type:

float

read()[source]

Read distance value.

Returns:

Distance in centimeters. Returns -1 if thread is running,

otherwise returns the last read value.

Return type:

float

thread_read_loop()[source]

Thread loop for reading distance value periodically.

start_thread(interval=0.01)[source]

Start the thread for reading distance value periodically.

Parameters:

interval (float, optional) – Interval duration in seconds. Default is 0.01.

stop_thread()[source]

Stop the thread for reading distance value.