|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# *********************************************************** |
| 3 | +# aitk.utils: Python utils for AI |
| 4 | +# |
| 5 | +# Copyright (c) 2025 AITK Developers |
| 6 | +# |
| 7 | +# https://github.com/ArtificialIntelligenceToolkit/aitk.utils |
| 8 | +# |
| 9 | +# *********************************************************** |
| 10 | + |
| 11 | +from . import World, Robot |
| 12 | + |
| 13 | +from ipywidgets import ( |
| 14 | + HBox, |
| 15 | + VBox, |
| 16 | + Layout, |
| 17 | + FloatSlider, |
| 18 | + Label, |
| 19 | + SliderStyle, |
| 20 | + Button, |
| 21 | +) |
| 22 | +from typing import List, Tuple, Callable |
| 23 | + |
| 24 | + |
| 25 | +def dashboard( |
| 26 | + world: World, |
| 27 | + robot: Robot, |
| 28 | + data: List[Tuple[List[float], Tuple[float, float]]], |
| 29 | + move: Callable, |
| 30 | +) -> VBox: |
| 31 | + """ |
| 32 | + Create an interactive dashboard widget for controlling a robot in a world. |
| 33 | +
|
| 34 | + This function creates a Jupyter widget interface with controls for steering, |
| 35 | + power, and simulation timing, along with a visual representation of the world. |
| 36 | +
|
| 37 | + Args: |
| 38 | + world: The simulation world containing the robot |
| 39 | + robot: The robot to control |
| 40 | + data: The list to store movement history |
| 41 | + move: A callable that executes robot movement. Should have the signature |
| 42 | + move(world, robot, steering, power, seconds), where it updates the robot's |
| 43 | + state in the world according to the given steering, power, and time step. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + VBox: A Jupyter widget containing the control interface and world display |
| 47 | +
|
| 48 | + The dashboard includes: |
| 49 | + - Steering wheel slider (-1 to 1) |
| 50 | + - Gas/power slider (0 to 1) |
| 51 | + - Step button to execute movement |
| 52 | + - Seconds per step slider (0.1 to 5.0) |
| 53 | + - Visual world display widget |
| 54 | + """ |
| 55 | + # Reset world and clear data |
| 56 | + world.reset() |
| 57 | + data.clear() |
| 58 | + |
| 59 | + # Create centered layout for labels |
| 60 | + center_layout = Layout(display="flex", justify_content="center") |
| 61 | + |
| 62 | + # Create step button |
| 63 | + step = Button( |
| 64 | + description="Step", |
| 65 | + layout=Layout( |
| 66 | + display="flex", |
| 67 | + justify_content="center", |
| 68 | + align_items="center", |
| 69 | + width="99%", |
| 70 | + ), |
| 71 | + ) |
| 72 | + |
| 73 | + # Define step button click handler |
| 74 | + def step_click(button: Button) -> None: |
| 75 | + """Handle step button click by executing robot movement.""" |
| 76 | + move(world, robot, steering.value, power.value, seconds.value) |
| 77 | + |
| 78 | + step.on_click(step_click) |
| 79 | + step.style.button_color = "lightblue" |
| 80 | + |
| 81 | + # Create control label |
| 82 | + label = Label(value="Robot Control", layout=center_layout) |
| 83 | + |
| 84 | + # Create steering control slider |
| 85 | + steering = FloatSlider( |
| 86 | + description="Steering wheel:", |
| 87 | + min=-1, |
| 88 | + max=1, |
| 89 | + value=0, |
| 90 | + layout=Layout(width="400px"), |
| 91 | + style=SliderStyle(description_width="100px"), |
| 92 | + ) |
| 93 | + |
| 94 | + # Create power control slider |
| 95 | + power = FloatSlider( |
| 96 | + description="Gas:", |
| 97 | + min=0, |
| 98 | + max=1, |
| 99 | + value=0, |
| 100 | + layout=Layout(width="400px"), |
| 101 | + style=SliderStyle(description_width="100px"), |
| 102 | + ) |
| 103 | + |
| 104 | + # Create timing control slider |
| 105 | + seconds = FloatSlider( |
| 106 | + description="Seconds per step:", |
| 107 | + min=0.1, |
| 108 | + max=5, |
| 109 | + value=0.1, |
| 110 | + layout=Layout(width="400px"), |
| 111 | + style=SliderStyle(description_width="200px"), |
| 112 | + ) |
| 113 | + |
| 114 | + # Create main layout |
| 115 | + layout = Layout() |
| 116 | + controls = VBox( |
| 117 | + children=[ |
| 118 | + HBox( |
| 119 | + children=[ |
| 120 | + VBox( |
| 121 | + [label, steering, power, step], |
| 122 | + layout=Layout(border="solid 2px"), |
| 123 | + ), |
| 124 | + world.get_widget(width=500), |
| 125 | + ] |
| 126 | + ), |
| 127 | + seconds, |
| 128 | + ], |
| 129 | + layout=layout, |
| 130 | + ) |
| 131 | + |
| 132 | + return controls |
0 commit comments