-
Notifications
You must be signed in to change notification settings - Fork 2
Live tuning raspberry #496
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
Open
sokosam
wants to merge
1
commit into
main
Choose a base branch
from
live-tuning-raspberry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #pragma once | ||
|
|
||
| #include "inc/Screen.hpp" | ||
|
|
||
| class Tuning : public Screen { | ||
| public: | ||
| Tuning(Display* display); | ||
|
|
||
| void CreateGUI() override; | ||
| void Update() override; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ui looks good |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from nicegui import ui | ||
| from nicegui.events import UploadEventArguments | ||
| from flash_logic import save_uploaded_file, flash_file | ||
| from constants import ECU_CONFIG | ||
| import os | ||
|
|
||
|
|
||
| class CanFlashApp: | ||
| def __init__(self): | ||
| self.starting_ecu = "Select ECU" | ||
| self.selected_ecu = self.starting_ecu | ||
| self.uploaded_file_path = None | ||
| self.build_ui() | ||
|
|
||
| def handle_upload(self, file: UploadEventArguments): | ||
| self.uploaded_file_path = save_uploaded_file(self.selected_ecu, file.name, file.content.read()) | ||
| self.flash_button.enable() | ||
|
|
||
| def handle_flash(self): | ||
| exception = flash_file(self.selected_ecu, self.uploaded_file_path) | ||
|
|
||
| if not exception: | ||
| uploaded_file_name = os.path.split(self.uploaded_file_path)[-1] | ||
| ui.notify( | ||
| f"Successfully flashed {uploaded_file_name} to {self.selected_ecu}", | ||
| position="center", | ||
| ) | ||
|
|
||
| # Reset upload box and disable flash button | ||
| self.upload_box.reset() | ||
| self.flash_button.disable() | ||
|
|
||
| # Reset ECU selection after a short delay (or else the select box won't reset) | ||
| ui.timer(0.1, lambda: self.ecu_select.set_value(self.starting_ecu), once=True) | ||
| else: | ||
| ui.notify(str(exception), type="negative") | ||
|
|
||
|
|
||
| def build_ui(self): | ||
| ui.markdown("Welcome to CAN Flash! Select an ECU and upload a file to flash. See [docs](https://macformula.github.io/racecar/) for more information.") | ||
|
|
||
| # ECU Selection | ||
| self.ecu_select = ui.select([self.starting_ecu] + list(ECU_CONFIG.keys())) | ||
| self.ecu_select.bind_value(self, "selected_ecu") | ||
|
|
||
| # File Upload | ||
| self.upload_box = ui.upload( | ||
| label="Select File", | ||
| on_upload=self.handle_upload, | ||
| auto_upload=True, | ||
| ).props("accept=.bin") | ||
| self.upload_box.bind_enabled_from( | ||
| self.ecu_select, "value", lambda v: v != self.starting_ecu | ||
| ) | ||
|
|
||
| # Flash Button | ||
| self.flash_button = ui.button("Flash", on_click=self.handle_flash) | ||
| self.flash_button.disable() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| from nicegui import ui, events | ||
| import subprocess | ||
|
|
||
|
|
||
| class LiveTuning: | ||
|
|
||
| def __init__(self, iface: str = "vcan0") -> None: | ||
| self.iface = iface | ||
| self.aggressiveness = 0 | ||
| self.dampness = 0 | ||
| self._build_ui() | ||
|
|
||
| def _slider_row( | ||
| self, | ||
| title: str, | ||
| field_name: str, | ||
| init_val: int = 0, | ||
| *, | ||
| on_change_cb, | ||
| ): | ||
| with ui.row().classes("items-center gap-4 w-full"): | ||
| ui.label(title).classes("w-32 text-right") | ||
|
|
||
| slider = ( | ||
| ui.slider(min=0, max=100, value=init_val, on_change=on_change_cb) | ||
| .props("step=1") | ||
| .classes("flex-1") | ||
| ) | ||
|
|
||
| number = ( | ||
| ui.number(value=init_val, min=0, max=100) | ||
| .props("style='width:80px'") | ||
| ) | ||
|
|
||
| number.bind_value_from(slider, "value").bind_value_to(slider, "value") | ||
|
|
||
| setattr(self, f"{field_name}_slider", slider) | ||
| setattr(self, f"{field_name}_number", number) | ||
|
|
||
| def _build_ui(self) -> None: | ||
| """Compose the entire visible UI.""" | ||
| with ui.card().classes("w-full max-w-md mx-auto mt-14 p-6 shadow-lg"): | ||
| ui.label("Live Tuning").classes("text-2xl font-semibold mb-6 text-center") | ||
|
|
||
| # Aggressiveness row | ||
| self._slider_row( | ||
| "Aggressiveness", | ||
| "aggressiveness", | ||
| init_val=self.aggressiveness, | ||
| on_change_cb=lambda e: self._on_change("aggressiveness", e), | ||
| ) | ||
|
|
||
| ui.separator() | ||
|
|
||
| # Dampness row | ||
| self._slider_row( | ||
| "Dampness", | ||
| "dampness", | ||
| init_val=self.dampness, | ||
| on_change_cb=lambda e: self._on_change("dampness", e), | ||
| ) | ||
|
|
||
| ui.button( | ||
| "Submit", | ||
| icon="send", | ||
| on_click=self._submit, | ||
| ).classes("w-full bg-primary text-white mt-6") | ||
|
|
||
| def _on_change(self, field: str, e: events.ValueChangeEventArguments) -> None: | ||
| # Clamp, validate, and store slider changes | ||
| value = e.value or 0 | ||
| value = max(0, min(100, int(value))) | ||
| setattr(self, field, value) | ||
| print(f"{field.capitalize()} set to {value}") | ||
|
|
||
| def _submit(self, _=None) -> None: | ||
| print(f"Submit pressed (agg={self.aggressiveness}, damp={self.dampness})") | ||
| subprocess.run( | ||
| ["bash", "./scripts/tune.sh", | ||
| str(self.aggressiveness), str(self.dampness), self.iface], | ||
| check=True, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ in {"__mp_main__", "__main__"}: | ||
| LiveTuning() | ||
| ui.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,74 +1,58 @@ | ||
| from nicegui import ui | ||
| from nicegui.events import UploadEventArguments | ||
| from flash_logic import save_uploaded_file, flash_file | ||
| from constants import ECU_CONFIG | ||
| import os | ||
|
|
||
|
|
||
| class CanFlashApp: | ||
| def __init__(self): | ||
| self.starting_ecu = "Select ECU" | ||
| self.selected_ecu = self.starting_ecu | ||
| self.uploaded_file_path = None | ||
| self.build_ui() | ||
|
|
||
| def handle_upload(self, file: UploadEventArguments): | ||
| self.uploaded_file_path = save_uploaded_file(self.selected_ecu, file.name, file.content.read()) | ||
| self.flash_button.enable() | ||
|
|
||
| def handle_flash(self): | ||
| exception = flash_file(self.selected_ecu, self.uploaded_file_path) | ||
|
|
||
| if not exception: | ||
| uploaded_file_name = os.path.split(self.uploaded_file_path)[-1] | ||
| ui.notify( | ||
| f"Successfully flashed {uploaded_file_name} to {self.selected_ecu}", | ||
| position="center", | ||
| ) | ||
|
|
||
| # Reset upload box and disable flash button | ||
| self.upload_box.reset() | ||
| self.flash_button.disable() | ||
|
|
||
| # Reset ECU selection after a short delay (or else the select box won't reset) | ||
| ui.timer(0.1, lambda: self.ecu_select.set_value(self.starting_ecu), once=True) | ||
| else: | ||
| ui.notify(str(exception), type="negative") | ||
|
|
||
|
|
||
| def build_ui(self): | ||
| ui.colors(primary="#AA1F26") | ||
| ui.markdown("Welcome to CAN Flash! Select an ECU and upload a file to flash. See [docs](https://macformula.github.io/racecar/) for more information.") | ||
|
|
||
| # ECU Selection | ||
| self.ecu_select = ui.select([self.starting_ecu] + list(ECU_CONFIG.keys())) | ||
| self.ecu_select.bind_value(self, "selected_ecu") | ||
|
|
||
| # File Upload | ||
| self.upload_box = ui.upload( | ||
| label="Select File", | ||
| on_upload=self.handle_upload, | ||
| auto_upload=True, | ||
| ).props("accept=.bin") | ||
| self.upload_box.bind_enabled_from( | ||
| self.ecu_select, "value", lambda v: v != self.starting_ecu | ||
| ) | ||
|
|
||
| # Flash Button | ||
| self.flash_button = ui.button("Flash", on_click=self.handle_flash) | ||
| self.flash_button.disable() | ||
|
|
||
|
|
||
| @ui.page("/") | ||
| from CanFlashApp import CanFlashApp | ||
| from LiveTuning import LiveTuning | ||
| from contextlib import contextmanager | ||
|
|
||
| @contextmanager | ||
| def frame(): | ||
| ui.colors(primary="#AA1F26") | ||
| with ui.header(elevated=True).classes('width-full text-white bg-[#AA1F26] '): | ||
| ui.label("Raspberry Pi Resources").classes("text-xl font-bold mr-4 ") | ||
| ui.button("Home", on_click=lambda : ui.navigate.to('/')).classes("!bg-[#df535a]") | ||
| ui.button('Can Flash', on_click= lambda : ui.navigate.to("/canflash")).classes("!bg-[#df535a]") | ||
| ui.button('Tuning', on_click= lambda : ui.navigate.to("/tune")).classes("!bg-[#df535a]") | ||
| yield | ||
|
|
||
| @ui.page("/canflash") | ||
| def main_page(): | ||
| CanFlashApp() | ||
|
|
||
|
|
||
| with frame(): | ||
| CanFlashApp() | ||
|
|
||
| @ui.page("/tune") | ||
| def tuning_page(): | ||
| with frame(): | ||
| LiveTuning() | ||
|
|
||
| @ui.page('/') | ||
| def home() -> None: | ||
| with frame(): # keep the shared header | ||
| with ui.column().classes('items-center q-mt-xl gap-4'): | ||
| ui.label('Welcome to the Formula‑Electric Raspberry Pi Interface') \ | ||
| .classes('text-2xl font-bold') | ||
|
|
||
| ui.markdown( | ||
| 'This dashboard lets you interact with the **Formula Electric** car’s ' | ||
| 'Raspberry Pi. \n' | ||
| 'Use it to *live‑tune* parameters while the car is running or to ' | ||
| '*flash* new firmware over CAN. ' | ||
| 'More modules will be added soon!' | ||
| ).classes('max-w-xl text-center') | ||
|
|
||
| with ui.row().classes('gap-4'): | ||
| ui.button('Go to Live Tuning', on_click=lambda: ui.navigate.to('/tune')) \ | ||
| .classes('!bg-[#df535a] text-white') | ||
| ui.button('Go to CAN Flash', on_click=lambda: ui.navigate.to('/canflash')) \ | ||
| .classes('!bg-[#df535a] text-white') | ||
|
|
||
| ui.link('Read the full documentation', | ||
| 'https://macformula.github.io/racecar/', | ||
| new_tab=True) \ | ||
| .classes('text-primary underline q-mt-md') | ||
| if __name__ in {"__main__", "__mp_main__"}: | ||
| ui.run( | ||
| title="CAN Flash", | ||
| favicon="favicon.ico", | ||
| show=False, | ||
| dark=None, | ||
| port=8000, | ||
| ) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't think we need a tuning menu, the driver should just be able to go