-
Notifications
You must be signed in to change notification settings - Fork 126
keyboard support #3738
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
Draft
GrayHoang
wants to merge
2
commits into
UBC-Thunderbots:master
Choose a base branch
from
GrayHoang:wasdqe-control
base: master
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.
Draft
keyboard support #3738
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
30 changes: 30 additions & 0 deletions
30
src/software/thunderscope/robot_diagnostics/controller_base.py
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,30 @@ | ||
| from abc import ABC, abstractmethod | ||
|
|
||
|
|
||
| class ControllerBase(ABC): | ||
| """Abstract base class for controller input sources.""" | ||
|
|
||
| @abstractmethod | ||
| def name(self) -> str: | ||
| """Get the display name of the input source.""" | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def connected(self) -> bool: | ||
| """Return true if the input source is active and available.""" | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def key_down(self, key_code: int) -> bool: | ||
| """Return true if the given key/button code is currently pressed.""" | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def abs_value(self, abs_code: int) -> float: | ||
| """Return the current value of an axis, normalized to [-1, 1].""" | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def close(self) -> None: | ||
| """Release any resources held by the input source.""" | ||
| ... |
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
90 changes: 90 additions & 0 deletions
90
src/software/thunderscope/robot_diagnostics/keyboard_controller.py
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,90 @@ | ||
| from abc import ABCMeta | ||
|
|
||
| from pyqtgraph.Qt.QtCore import Qt, QObject, QEvent | ||
| from pyqtgraph.Qt.QtWidgets import QApplication | ||
|
|
||
| from software.thunderscope.robot_diagnostics.controller_base import ControllerBase | ||
|
|
||
| # evdev-independent copies of the ecodes values used by HandheldControllerWidget | ||
| _ABS_X = 0 | ||
| _ABS_Y = 1 | ||
| _ABS_Z = 2 | ||
| _ABS_RX = 3 | ||
| _ABS_RZ = 5 | ||
| _ABS_HAT0X = 16 | ||
| _ABS_HAT0Y = 17 | ||
| _BTN_A = 304 | ||
| _BTN_B = 305 | ||
|
|
||
| # Maps abs_code -> (negative_key, positive_key). | ||
| # A held negative key returns -1.0; a held positive key returns +1.0. | ||
| _ABS_KEY_MAP: dict[int, tuple[Qt.Key | None, Qt.Key | None]] = { | ||
| _ABS_Y: (Qt.Key.Key_W, Qt.Key.Key_S), # forward / back | ||
| _ABS_X: (Qt.Key.Key_A, Qt.Key.Key_D), # strafe left / right | ||
| _ABS_RX: (Qt.Key.Key_Q, Qt.Key.Key_E), # rotate CCW / CW | ||
| _ABS_Z: (None, Qt.Key.Key_Shift), # slowdown (left trigger) | ||
| _ABS_HAT0X: (Qt.Key.Key_Left, Qt.Key.Key_Right), # step kick power | ||
| _ABS_HAT0Y: (Qt.Key.Key_Up, Qt.Key.Key_Down), # step dribbler RPM | ||
| _ABS_RZ: (None, Qt.Key.Key_R), # dribbler hold (right trigger) | ||
| } | ||
|
|
||
| # Maps key_code (ecodes int) -> Qt key for digital button inputs | ||
| _BTN_KEY_MAP: dict[int, Qt.Key] = { | ||
| _BTN_A: Qt.Key.Key_X, # kick | ||
| _BTN_B: Qt.Key.Key_C, # chip | ||
| } | ||
|
|
||
|
|
||
| class _QABCMeta(type(QObject), ABCMeta): | ||
| pass | ||
|
|
||
|
|
||
| class KeyboardController(QObject, ControllerBase, metaclass=_QABCMeta): | ||
| """Keyboard input source. | ||
|
|
||
| Installs a QApplication-level event filter so key events are captured | ||
| regardless of which widget currently has focus. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self._held_keys: set[Qt.Key] = set() | ||
| self._active = True | ||
| QApplication.instance().installEventFilter(self) | ||
|
|
||
| def name(self) -> str: | ||
| return "Keyboard" | ||
|
|
||
| def connected(self) -> bool: | ||
| return self._active | ||
|
|
||
| def key_down(self, key_code: int) -> bool: | ||
| qt_key = _BTN_KEY_MAP.get(key_code) | ||
| if qt_key is None: | ||
| return False | ||
| return qt_key in self._held_keys | ||
|
|
||
| def abs_value(self, abs_code: int) -> float: | ||
| key_pair = _ABS_KEY_MAP.get(abs_code) | ||
| if key_pair is None: | ||
| return 0.0 | ||
| neg_key, pos_key = key_pair | ||
| if neg_key is not None and neg_key in self._held_keys: | ||
| return -1.0 | ||
| if pos_key is not None and pos_key in self._held_keys: | ||
|
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. Add time-based speed ramping for movement. Or else robot will either move in full speed or zero speed. |
||
| return 1.0 | ||
| return 0.0 | ||
|
|
||
| def close(self) -> None: | ||
| self._active = False | ||
| self._held_keys.clear() | ||
| app = QApplication.instance() | ||
| if app is not None: | ||
| app.removeEventFilter(self) | ||
|
|
||
| def eventFilter(self, obj: QObject, event: QEvent) -> bool: | ||
| if event.type() == QEvent.Type.KeyPress and not event.isAutoRepeat(): | ||
| self._held_keys.add(Qt.Key(event.key())) | ||
| elif event.type() == QEvent.Type.KeyRelease and not event.isAutoRepeat(): | ||
| self._held_keys.discard(Qt.Key(event.key())) | ||
| return False | ||
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.
semi colon