Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle laser timeouts #3

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions firmware/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# noinspection PyProtectedMember
from mag_cal import MagneticAnomalyError, DipAnomalyError, GravityAnomalyError, NotCalibrated

LASER_TIMEOUT = 4.5

try:
# noinspection PyUnresolvedReferences
from typing import Dict
Expand All @@ -21,6 +23,7 @@

ERROR_MESSAGES: Dict[type, str] = {
LaserError: "Laser\nRead\nFailed",
asyncio.TimeoutError: "Laser\nRead\nTimeout",
MagneticAnomalyError: "Magnetic\nAnomaly:\nIron nearby?",
DipAnomalyError: "Magnetic\nAnomaly:\nIron nearby?",
NotCalibrated: "Calibration\nneeded\nHold B 3s",
Expand All @@ -44,7 +47,11 @@ async def measure(devices: hardware.Hardware, cfg: config.Config, disp: display.
logger.debug("turning on laser light")
await devices.laser.set_buzzer(False)
while True:
await devices.laser.set_laser(True)
try:
await devices.laser.set_laser(True)
except LaserError:
# possibly received a timeout last time around
pass
btn, click = await devices.both_buttons.wait(a=[Button.SINGLE, Button.LONG],
b=Button.SINGLE)
check_mem("button pressed")
Expand All @@ -66,7 +73,8 @@ async def get_raw_measurement(devices: hardware.Hardware, disp: display.Display,
try:
disp.oled.sleep()
if with_laser:
distance = await asyncio.wait_for(devices.laser.measure(), 3.0) / 1000
devices.laser.async_reader.s.read() # clear the buffer
distance = await asyncio.wait_for(devices.laser.measure(), LASER_TIMEOUT) / 1000
else:
distance = None
logger.debug(f"Raw Distance: {distance}m")
Expand Down Expand Up @@ -99,12 +107,14 @@ async def take_reading(devices: hardware.Hardware,
for key in ERROR_MESSAGES.keys():
if isinstance(exc, key):
disp.show_big_info(ERROR_MESSAGES[key])
logger.info(exc)
for i in range(5):
await devices.laser.set_laser(False)
await asyncio.sleep(0.1)
await devices.laser.set_laser(True)
await asyncio.sleep(0.1)
logger.info(f"Measurement error: {repr(exc)}")
if not isinstance(exc, asyncio.TimeoutError):
# don't wibble the laser if it's timed out, it'll just get more confused
for i in range(5):
await devices.laser.set_laser(False)
await asyncio.sleep(0.1)
await devices.laser.set_laser(True)
await asyncio.sleep(0.1)
devices.beep_sad()
return False
else:
Expand Down
Loading