-
Notifications
You must be signed in to change notification settings - Fork 20
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
magicbot: Allow typed feedbacks using return type hints #208
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
59e0804
magicbot: Grab entry setters in collect_feedbacks
auscompgeek 9c5ea4d
magicbot: Use return type hint to type feedback topics
auscompgeek 4f2427a
magicbot: Allow feedbacks returning Sequence
auscompgeek 8eaf5ff
magicbot: Add type-hinted feedback struct support
auscompgeek cd2a9f5
magicbot: Test feedback
auscompgeek 8d90817
magicbot: Add struct feedback tests
auscompgeek 47e6923
magicbot: Test empty string array feedback
auscompgeek 972fbf3
magicbot: Allow struct tunables
auscompgeek ecd8a4c
magicbot: Document tunable and feedback changes
auscompgeek 8b94e8e
magicbot: Add tests for tunable
auscompgeek 0c6b539
magicbot: feedback: Allow fixed-length homogenous tuples
auscompgeek 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 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 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 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 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,105 @@ | ||
from typing import Sequence, Tuple | ||
|
||
import ntcore | ||
from wpimath import geometry | ||
|
||
import magicbot | ||
|
||
|
||
class BasicComponent: | ||
@magicbot.feedback | ||
def get_number(self): | ||
return 0 | ||
|
||
@magicbot.feedback | ||
def get_ints(self): | ||
return (0,) | ||
|
||
@magicbot.feedback | ||
def get_floats(self): | ||
return (0.0, 0) | ||
|
||
def execute(self): | ||
pass | ||
|
||
|
||
class TypeHintedComponent: | ||
@magicbot.feedback | ||
def get_rotation(self) -> geometry.Rotation2d: | ||
return geometry.Rotation2d() | ||
|
||
@magicbot.feedback | ||
def get_rotation_array(self) -> Sequence[geometry.Rotation2d]: | ||
return [geometry.Rotation2d()] | ||
|
||
@magicbot.feedback | ||
def get_rotation_2_tuple(self) -> Tuple[geometry.Rotation2d, geometry.Rotation2d]: | ||
return (geometry.Rotation2d(), geometry.Rotation2d()) | ||
|
||
@magicbot.feedback | ||
def get_int(self) -> int: | ||
return 0 | ||
|
||
@magicbot.feedback | ||
def get_float(self) -> float: | ||
return 0.5 | ||
|
||
@magicbot.feedback | ||
def get_ints(self) -> Sequence[int]: | ||
return (0,) | ||
|
||
@magicbot.feedback | ||
def get_empty_strings(self) -> Sequence[str]: | ||
return () | ||
|
||
def execute(self): | ||
pass | ||
|
||
|
||
class Robot(magicbot.MagicRobot): | ||
basic: BasicComponent | ||
type_hinted: TypeHintedComponent | ||
|
||
def createObjects(self): | ||
pass | ||
|
||
|
||
def test_feedbacks_with_type_hints(): | ||
robot = Robot() | ||
robot.robotInit() | ||
nt = ntcore.NetworkTableInstance.getDefault().getTable("components") | ||
|
||
robot._do_periodics() | ||
|
||
for name, type_str, value in ( | ||
("basic/number", "double", 0.0), | ||
("basic/ints", "int[]", [0]), | ||
("basic/floats", "double[]", [0.0, 0.0]), | ||
("type_hinted/int", "int", 0), | ||
("type_hinted/float", "double", 0.5), | ||
("type_hinted/ints", "int[]", [0]), | ||
("type_hinted/empty_strings", "string[]", []), | ||
): | ||
topic = nt.getTopic(name) | ||
assert topic.getTypeString() == type_str | ||
assert topic.genericSubscribe().get().value() == value | ||
|
||
for name, value in [ | ||
("type_hinted/rotation", geometry.Rotation2d()), | ||
]: | ||
struct_type = type(value) | ||
assert nt.getTopic(name).getTypeString() == f"struct:{struct_type.__name__}" | ||
topic = nt.getStructTopic(name, struct_type) | ||
assert topic.subscribe(None).get() == value | ||
|
||
for name, struct_type, value in ( | ||
("type_hinted/rotation_array", geometry.Rotation2d, [geometry.Rotation2d()]), | ||
( | ||
"type_hinted/rotation_2_tuple", | ||
geometry.Rotation2d, | ||
[geometry.Rotation2d(), geometry.Rotation2d()], | ||
), | ||
): | ||
assert nt.getTopic(name).getTypeString() == f"struct:{struct_type.__name__}[]" | ||
topic = nt.getStructArrayTopic(name, struct_type) | ||
assert topic.subscribe([]).get() == value |
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.
This protocol probably shouldn't belong here...
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.
Probably should end up in wpimath, but I don't see why it can't be in two places.
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 presume you mean wpiutil? I'll follow up with a PR over there then.