robotpy / examples

Repository of RobotPy example projects
45 stars 53 forks source link

Add selectcommand example #62

Closed megarubber closed 1 year ago

megarubber commented 1 year ago

Issue #49 (Convert Java examples to Python syntax) With the update of commands-v2 package and resolving my question in #14, here's the SelectCommand example.

PS: I didn't convert the Constants.java because the robot.py and robotcontainer.py never import a "constants file", so I think that it isn't necessary.

megarubber commented 1 year ago

I think the test failures should be fixed by robotpy/robotpy-commands-v2#18, but I can't say I really understand the errors in the tests either.

./run_tests returns this:

/home/guilherme/Projects/examples/commands-v2/selectcommand
/home/guilherme/.local/lib/python3.10/site-packages/paramiko/transport.py:236: CryptographyDeprecationWarning: Blowfish has been deprecated
  "class": algorithms.Blowfish,
14:47:26:368 INFO    : wpilib              : WPILib version 2023.0.0b7
14:47:26:369 INFO    : wpilib              : HAL version 2023.0.0b7
14:47:26:369 INFO    : wpilib              : Running with simulated HAL.
14:47:26:369 INFO    : wpilib              : robotpy-cscore version 2021.0.2
14:47:26:370 INFO    : wpilib              : robotpy-commands-v2 version 2023.0.0b7.post1
14:47:26:370 INFO    : wpilib              : robotpy-wpimath version 2023.0.0b7
14:47:26:370 INFO    : wpilib              : robotpy-halsim-ds-socket version 2023.0.0b7
14:47:26:370 INFO    : wpilib              : robotpy-wpiutil version 2023.0.0b7
14:47:26:370 INFO    : wpilib              : robotpy-wpinet version 2023.0.0b7
14:47:26:370 INFO    : wpilib              : robotpy-halsim-ws version 2023.0.0b7
14:47:26:370 INFO    : wpilib              : robotpy-halsim-gui version 2023.0.0b7
14:47:26:370 INFO    : wpilib              : pyntcore version 2023.0.0b7
14:47:26:370 INFO    : wpilib              : robotpy-ctre version 2023.0.0b6
14:47:26:370 INFO    : wpilib              : robotpy-rev version 2023.0.0b2
14:47:26:370 INFO    : wpilib              : robotpy-navx version 2023.0.0b1
14:47:26:370 INFO    : wpilib              : robotpy-commands-v1 version 2022.4.1.0
14:47:26:370 INFO    : wpilib              : robotpy-pathplannerlib version 2022.2.3
14:47:26:370 INFO    : wpilib              : robotpy-romi version 2022.2.1.0
14:47:26:370 INFO    : wpilib              : robotpy-photonvision version 2022.1.1
14:47:26:370 INFO    : wpilib              : robotpy-playingwithfusion version 2022.0.4
14:47:26:370 INFO    : wpilib              : robotpy-build version 2023.0.0b16
14:47:26:371 INFO    : faulthandler        : registered SIGUSR2 for PID 1577
14:47:26:372 WARNING : pyfrc.physics       : Cannot enable physics support, /home/guilherme/Projects/examples/commands-v2/selectcommand/physics.py not found
========================================================= test session starts =========================================================
platform linux -- Python 3.10.8, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/guilherme
plugins: reraise-2.1.1
collected 4 items                                                                                                                     

../../../../.local/lib/python3.10/site-packages/pyfrc/tests/basic.py ....                                                       [100%]

========================================================== 4 passed in 0.74s ==========================================================
auscompgeek commented 1 year ago

./run_tests returns this:

And I'm still confused as to how it could possibly work at all on Linux in the current state of the robotpy-commands-v2 package; the test I added clearly shows it's broken. 😕

Have you tried running the example and seeing whether it does the right thing?

megarubber commented 1 year ago

./run_tests returns this:

And I'm still confused as to how it could possibly work at all on Linux in the current state of the robotpy-commands-v2 package; the test I added clearly shows it's broken. confused

Have you tried running the example and seeing whether it does the right thing?

Well, I'm not with a RoboRIO now, but I tested on Robot Simulator calling the initialize() method on RobotContainer's constructor, and this print returns when MyRobot instantiates a RobotContainer object in robotInit.

image

BTW, here's the code

def __init__(self) -> None:
    # An example selectcommand. Will select from the three commands based on the value returned
    # by the selector method at runtime. Note that selectcommand takes a generic type, so the
    # selector does not have to be an enum; it could be any desired type (string, integer,
    # boolean, double...)
    self.example_select_command = commands2.SelectCommand(
        self.select,
        # Maps selector values to commands
        [
            (
                self.CommandSelector.ONE,
                commands2.PrintCommand("Command one was selected!"),
            ),
            (
                self.CommandSelector.TWO,
                commands2.PrintCommand("Command two was selected!"),
            ),
            (
                self.CommandSelector.THREE,
                commands2.PrintCommand("Command three was selected!"),
            ),
        ],
    )  
    # Configure the button bindings
    self.configureButtonBindings()
    # Executes & print
    self.example_select_command.initialize()

pip3 list

Package                       Version
----------------------------- ----------------
alabaster                     0.7.12
anytree                       2.8.0
appdirs                       1.4.4
apriltag                      0.0.16
arandr                        0.1.10
attrs                         21.4.0
autocommand                   2.2.2
autopep8                      1.7.0
Babel                         2.11.0
bcrypt                        3.2.0
Beaker                        1.12.0
black                         22.8.0
Brlapi                        0.8.4
btrfsutil                     6.0.2
CacheControl                  0.12.11
certifi                       2022.9.24
cffi                          1.15.1
chardet                       5.1.0
click                         8.0.3
colorama                      0.4.6
commonmark                    0.9.1
contextlib2                   21.6.0
coverage                      6.3
cryptography                  38.0.4
Cython                        0.29.32
distlib                       0.3.6
distro                        1.8.0
dnspython                     2.2.1
docutils                      0.19
future                        0.18.2
header2whatever               0.4.8
html5lib                      1.1
idna                          3.4
imagesize                     1.4.1
importlib-metadata            5.0.0
imutils                       0.5.4
inflect                       6.0.2
iniconfig                     1.1.1
jaraco.context                4.2.0
jaraco.functools              3.5.2
jaraco.text                   3.11.0
Jinja2                        3.1.2
lensfun                       0.3.3
libfdt                        1.6.1
louis                         3.24.0
lxml                          4.9.2
Mako                          1.2.4
mallard-ducktype              1.0.2
Markdown                      3.4.1
MarkupSafe                    2.1.1
more-itertools                9.0.0
msgpack                       1.0.4
mypy-extensions               0.4.3
netsnmp-python                1.0a1
nspektr                       0.4.0
numpy                         1.22.3
opencv-contrib-python         4.5.5.64
ordered-set                   4.1.0
packaging                     21.3
paramiko                      2.9.2
patch                         1.16
pathspec                      0.10.1
pep517                        0.13.0
Pint                          0.18
pip                           22.3.1
platformdirs                  2.6.0
pluggy                        1.0.0
ply                           3.11
progress                      1.6
pwquality                     1.4.5
py                            1.11.0
pybind11-stubgen              0.11.0
pycairo                       1.23.0
pycodestyle                   2.9.1
pycparser                     2.21
pydantic                      1.10.2
pyfrc                         2023.0.0b4
Pygments                      2.13.0
PyGObject                     3.42.2
PyNaCl                        1.5.0
pynetconsole                  2.0.2
pynetworktables               2021.0.0
pyntcore                      2023.0.0b7
pyOpenSSL                     22.1.0
pyparsing                     3.0.9
pytest                        6.2.5
pytest-reraise                2.1.1
pytz                          2022.6
pyxdg                         0.28
PyYAML                        6.0
requests                      2.28.1
resolvelib                    0.9.0
retrying                      1.3.3
rich                          12.6.0
robotpy                       2023.0.0b7
robotpy-build                 2023.0.0b16
robotpy-commands-v1           2022.4.1.0
robotpy-commands-v2           2023.0.0b7.post1
robotpy-cppheaderparser       5.1.0
robotpy-cscore                2021.0.2
robotpy-ctre                  2023.0.0b6
robotpy-hal                   2023.0.0b7
robotpy-halsim-ds-socket      2023.0.0b7
robotpy-halsim-gui            2023.0.0b7
robotpy-halsim-ws             2023.0.0b7
robotpy-installer             2023.0.0b5
robotpy-navx                  2023.0.0b1
robotpy-pathplannerlib        2022.2.3
robotpy-photonvision          2022.1.1
robotpy-playingwithfusion     2022.0.4
robotpy-rev                   2023.0.0b2
robotpy-romi                  2022.2.1.0
robotpy-wpilib-utilities      2023.0.0b1
robotpy-wpimath               2023.0.0b7
robotpy-wpinet                2023.0.0b7
robotpy-wpiutil               2023.0.0b7
schematics                    2.1.1
setproctitle                  1.3.2
setuptools                    65.6.3
setuptools-scm                7.0.5
six                           1.16.0
snowballstemmer               2.2.0
Sphinx                        5.3.0
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        2.0.0
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.5
sphinxify                     0.9
TBB                           0.2
team                          1.0
tenacity                      8.1.0
toml                          0.10.2
tomli                         2.0.1
tomli_w                       1.0.0
toposort                      1.7
trove-classifiers             2022.12.22
typing_extensions             4.4.0
urllib3                       1.26.12
validate-pyproject            0.10.1
webencodings                  0.5.1
wheel                         0.38.4
wpilib                        2023.0.0b7
youtube-dl                    2021.12.17
zipp                          3.11.0
virtuald commented 1 year ago

auscompgeek had pushed an updated version of robotpy-commands-v2 (2023.0.0b7.post2), that might fix it?

pip install robotpy-commands-v2==2023.0.0b7.post2