Skip to content

Commit

Permalink
Fix bed selection (#237)
Browse files Browse the repository at this point in the history
It wasn't possible to start an automation without selecting beds, now it
just takes all rows as intended.
Additionally, the odometry info on the dev page is fixed
  • Loading branch information
pascalzauberzeug authored Nov 20, 2024
1 parent 5a1c3ce commit 1651b81
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
10 changes: 8 additions & 2 deletions field_friend/automations/field_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ def __init__(self) -> None:
self.FIELDS_CHANGED = rosys.event.Event()
"""The dict of fields has changed."""

self.FIELDS_CHANGED.register(self.refresh_fields)

self.selected_field: Field | None = None
self.FIELD_SELECTED = rosys.event.Event()
"""A field has been selected."""

self.FIELDS_CHANGED.register(self.refresh_fields)
self.FIELD_SELECTED.register(self.clear_selected_beds)

self._only_specific_beds: bool = False
self._selected_beds: list[int] = []

@property
Expand Down Expand Up @@ -54,6 +56,7 @@ def invalidate(self) -> None:
self.FIELDS_CHANGED.emit()
if self.selected_field and self.selected_field not in self.fields:
self.selected_field = None
self._only_specific_beds = False
self.selected_beds = []
self.FIELD_SELECTED.emit()

Expand Down Expand Up @@ -128,6 +131,7 @@ def update_field_parameters(self, *,
self.invalidate()

def clear_selected_beds(self) -> None:
self._only_specific_beds = False
self.selected_beds = []

def get_rows_to_work_on(self) -> list[Row]:
Expand All @@ -136,6 +140,8 @@ def get_rows_to_work_on(self) -> list[Row]:
return []
if self.selected_field.bed_count == 1:
return self.selected_field.rows
if not self._only_specific_beds:
return self.selected_field.rows
if len(self.selected_beds) == 0:
self.log.warning('No beds selected. Cannot get rows to work on.')
return []
Expand Down
4 changes: 2 additions & 2 deletions field_friend/interface/components/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ def field_setting(self) -> None:
.tooltip('Delete the selected field')
if self.system.field_provider.selected_field.bed_count > 1:
with ui.row().classes('w-full'):
beds_checkbox = ui.checkbox('Select specific beds').classes(
'w-full').on_value_change(self.system.field_provider.clear_selected_beds)
beds_checkbox = ui.checkbox('Select specific beds').classes('w-full') \
.bind_value(self.system.field_provider, '_only_specific_beds')
with ui.row().bind_visibility_from(beds_checkbox, 'value').classes('w-full'):
ui.select(list(range(1, int(self.system.field_provider.selected_field.bed_count) + 1)), multiple=True, label='selected beds', clearable=True) \
.classes('grow').props('use-chips').bind_value(self.system.field_provider, 'selected_beds')
3 changes: 2 additions & 1 deletion field_friend/interface/components/status_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def status_dev_page(robot: FieldFriend, system: System):

with ui.row().classes('place-items-center'):
ui.label('Odometry:').style('color: #EDF4FB').classes('font-bold')
ui.label().bind_text_from(system.odometer, 'prediction')
odometry_label = ui.label()

with ui.row().classes('place-items-center'):
ui.label('Since last update:').style('color: #EDF4FB').classes('font-bold')
Expand Down Expand Up @@ -265,6 +265,7 @@ def update_status() -> None:
localization.reference.lat == 0 and localization.reference.long == 0) else str(localization.reference)
heading_label.text = f'{system.gnss.current.heading:.2f}° {direction_flag}' if system.gnss.current is not None and system.gnss.current.heading is not None else 'No heading'
rtk_fix_label.text = f'gps_qual: {system.gnss.current.gps_qual}, mode: {system.gnss.current.mode}' if system.gnss.current is not None else 'No fix'
odometry_label.text = str(system.odometer.prediction)
update_label.text = f'{timedelta(seconds=rosys.time() - system.gnss._last_gnss_pose.time)}' # pylint: disable=protected-access

ui.timer(rosys.config.ui_update_interval, update_status)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
from field_friend.automations import Field
from field_friend.automations.implements import Implement, Recorder
from field_friend.automations.navigation import StraightLineNavigation
from field_friend.automations.navigation.field_navigation import (
State as FieldNavigationState,
)
from field_friend.automations.navigation.field_navigation import State as FieldNavigationState
from field_friend.localization import GnssSimulation


Expand Down Expand Up @@ -411,8 +409,9 @@ async def test_complete_field_with_selected_beds(system: System, field_with_beds
# pylint: disable=protected-access
assert system.gnss.current
assert system.gnss.current.location.distance(ROBOT_GEO_START_POSITION) < 0.01
system.field_provider.selected_beds = [1, 3]
system.field_provider.select_field(field_with_beds.id)
system.field_provider._only_specific_beds = True
system.field_provider.selected_beds = [1, 3]
system.current_navigation = system.field_navigation
system.automator.start()
await forward(until=lambda: system.automator.is_running)
Expand All @@ -427,8 +426,9 @@ async def test_complete_field_without_first_beds(system: System, field_with_beds
# pylint: disable=protected-access
assert system.gnss.current
assert system.gnss.current.location.distance(ROBOT_GEO_START_POSITION) < 0.01
system.field_provider.selected_beds = [2, 3, 4]
system.field_provider.select_field(field_with_beds.id)
system.field_provider._only_specific_beds = True
system.field_provider.selected_beds = [2, 3, 4]
system.current_navigation = system.field_navigation
system.automator.start()
await forward(until=lambda: system.automator.is_running)
Expand Down

0 comments on commit 1651b81

Please sign in to comment.