Command requirements in sim

I have been trying to use the command based framework to program my team’s yet-to-be-finished 2022 robot. Because the hardware hasn’t been finished yet, I have been trying to test my code using the built-in simulator, however I have encountered an issue. When trying to set default subsystem commands, I run into the following error(s), with the error of choice being determined seemingly randomly.

I wrote a very simple script that reproduces the same behavior I was experiencing with my primary code.

It should be noted that I am running this from a virtualenv, but I have tested and the same behavior is exhibited when run from outside a venv.

09:54:14:925 INFO    : wpilib              : WPILib version 2022.2.1.5
09:54:14:926 INFO    : wpilib              : HAL version 2022.2.1.1
09:54:14:926 INFO    : wpilib              : Running with simulated HAL.
09:54:14:927 INFO    : wpilib              : robotpy-wpimath version 2022.2.1.4
09:54:14:928 INFO    : wpilib              : pyntcore version 2022.2.1.1
09:54:14:928 INFO    : wpilib              : robotpy-commands-v2 version 2022.2.1.1
09:54:14:929 INFO    : wpilib              : robotpy-wpiutil version 2022.2.1.1
09:54:14:929 INFO    : wpilib              : robotpy-halsim-gui version 2022.2.1.0
09:54:14:930 INFO    : wpilib              : robotpy-rev version 2022.0.1
09:54:14:935 INFO    : halsim_gui          : WPILib HAL Simulation 2022.2.1.0
HAL Extensions: Attempting to load: halsim_gui
Simulator GUI Initializing.
Simulator GUI Initialized!
HAL Extensions: Successfully loaded extension
09:54:15:160 WARNING : pyfrc.physics       : Cannot enable physics support, C:\Users\[name]\Documents\GitHub\[reponame]\physics.py not found
Not loading CameraServerShared
Windows fatal exception: access violation

Thread 0x000027ac (most recent call first):
  File "C:\Users\[name]\.virtualenvs\[reponame]-fmEJ15Z7\lib\site-packages\_pyntcore\_logutil.py", line 60 in _logging_thread
  File "C:\Users\[name]\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946 in run
  File "C:\Users\[name]\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009 in _bootstrap_inner
  File "C:\Users\[name]\AppData\Local\Programs\Python\Python310\lib\threading.py", line 966 in _bootstrap

Current thread 0x000052b8 (most recent call first):
  File "C:\Users\[name]\Documents\GitHub\[reponame]\robot.py", line 11 in __init__
  File "C:\Users\[name]\Documents\GitHub\[reponame]\robot.py", line 19 in robotInit
  File "C:\Users\[name]\.virtualenvs\[reponame]-fmEJ15Z7\lib\site-packages\wpilib\_impl\start.py", line 124 in _start
  File "C:\Users\[name]\.virtualenvs\[reponame]-fmEJ15Z7\lib\site-packages\wpilib\_impl\start.py", line 58 in start
  File "C:\Users\[name]\.virtualenvs\[reponame]-fmEJ15Z7\lib\site-packages\wpilib\_impl\start.py", line 34 in _start
  File "C:\Users\[name]\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946 in run
  File "C:\Users\[name]\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009 in _bootstrap_inner
  File "C:\Users\[name]\AppData\Local\Programs\Python\Python310\lib\threading.py", line 966 in _bootstrap

Thread 0x00006804 (most recent call first):
  File "C:\Users\[name]\.virtualenvs\[reponame]-fmEJ15Z7\lib\site-packages\wpilib\_impl\start.py", line 40 in run
  File "C:\Users\[name]\.virtualenvs\[reponame]-fmEJ15Z7\lib\site-packages\pyfrc\mains\cli_sim.py", line 86 in run
  File "C:\Users\[name]\.virtualenvs\[reponame]-fmEJ15Z7\lib\site-packages\wpilib\_impl\main.py", line 193 in run
  File "C:\Users\[name]\Documents\GitHub\[reponame]\robot.py", line 24 in <module>
09:54:20:804 INFO    : wpilib              : WPILib version 2022.2.1.5
09:54:20:805 INFO    : wpilib              : HAL version 2022.2.1.1
09:54:20:805 INFO    : wpilib              : Running with simulated HAL.
09:54:20:806 INFO    : wpilib              : robotpy-wpimath version 2022.2.1.4
09:54:20:807 INFO    : wpilib              : pyntcore version 2022.2.1.1
09:54:20:807 INFO    : wpilib              : robotpy-commands-v2 version 2022.2.1.1
09:54:20:808 INFO    : wpilib              : robotpy-wpiutil version 2022.2.1.1
09:54:20:809 INFO    : wpilib              : robotpy-halsim-gui version 2022.2.1.0
09:54:20:809 INFO    : wpilib              : robotpy-rev version 2022.0.1
09:54:20:815 INFO    : halsim_gui          : WPILib HAL Simulation 2022.2.1.0
HAL Extensions: Attempting to load: halsim_gui
Simulator GUI Initializing.
Simulator GUI Initialized!
HAL Extensions: Successfully loaded extension
09:54:21:033 WARNING : pyfrc.physics       : Cannot enable physics support, C:\Users\[name]\Documents\GitHub\[reponame]\physics.py not found
Not loading CameraServerShared
09:54:21:048 ERROR   : your.robot          : Unhandled exception
Traceback (most recent call last):
  File "C:\Users\[name]\.virtualenvs\[reponame]-fmEJ15Z7\lib\site-packages\wpilib\_impl\start.py", line 124, in _start
    self.robot.startCompetition()
  File "C:\Users\[name]\Documents\GitHub\[reponame]\robot.py", line 19, in robotInit
    example_subsystem_instance.setDefaultCommand(DefaultCommand())
RuntimeError: Illegal use of Command: Default commands must require their subsystem!


Locals at innermost frame:

{ '__class__': <class '__main__.Robot'>,
  'self': <__main__.Robot object at 0x0000019DB5F4D8F0>}


09:54:21:051 WARNING : robotpy             : The robot program quit unexpectedly. This is usually due to a code error.
The above stacktrace can help determine where the error occurred.

And the code:

import wpilib
import commands2

class ExampleSubsystem(commands2.SubsystemBase):
    ...

example_subsystem_instance = ExampleSubsystem()

class DefaultCommand(commands2.CommandBase):
    def __init__(self):
        super().addRequirements(example_subsystem_instance) # line 11
        super().__init__()
    
    def isFinished(self):
        return False

class Robot(commands2.TimedCommandRobot):
    def robotInit(self) -> None:
        example_subsystem_instance.setDefaultCommand(DefaultCommand()) # line 19

        return super().robotInit()

if __name__ == '__main__':
    wpilib.run(Robot) # line 24

Also, this same behavior was exhibited whether line 11 was written in any of the following ways

super().addRequirements(example_subsystem_instance)
self.addRequirements(example_subsystem_instance)
commands2.CommandBase.addRequirements(self, example_subsystem_instance)

I tried recreating this same code in Java, and it seemed to work fine in the sim there.

Any help would be appreciated

When overriding a class using RobotPy’s bindings, if you overrride __init__ then you must call super().__init__() before calling anything else or you will end up with undefined behavior. When I move __init__ your example works fine for me.

I don’t think that we can add a check that you call it first without significantly affecting performance negatively, but I’ll contemplate it.

We discuss this a little bit in the Upgrade Notes that we started in 2020.

Filed a bug on pybind11 just to track this, thanks!

In hindsight, that makes complete sense in this context; I’m just too used to calling super().__init__ at the end with some of my pure python projects.

Thanks for the help!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.