Skip to content

Allow passing DS Instance to Robot and OpModes#8626

Open
ThadHouse wants to merge 12 commits intowpilibsuite:2027from
ThadHouse:dsinstance
Open

Allow passing DS Instance to Robot and OpModes#8626
ThadHouse wants to merge 12 commits intowpilibsuite:2027from
ThadHouse:dsinstance

Conversation

@ThadHouse
Copy link
Member

Some discussion with the tech team showed that there were some real advantages to being able to pass a 2nd type. It allows separating the DS and Robot. Additionally, we can make the DriverStationBase class actually usable instead of the existing DriverStation class which is impossible to handle in intellisense because it has too much.

This won't fully be doable in C++, but we will need to implement something similar in python.

Some discussion with the tech team showed that there were some real advantages to being able to pass a 2nd type. It allows separating the DS
and Robot. Additionally, we can make the DriverStationBase class actually usable instead of the existing DriverStation class which is impossible
to handle in intellisense because it has too much.
@rzblue
Copy link
Member

rzblue commented Feb 19, 2026

It think it would make more sense to name that something other than DriverStation(Base/Instance). I'm not sure what to name it, but the Driver Station isn't a (user configurable) collection of gamepads/high level user controller abstraction.

@ThadHouse
Copy link
Member Author

Well a driver station is actually a user configurable collection of gamepads. Thats like the one user configurable thing.

@virtuald
Copy link
Member

You mentioned there was an advantage to adding this additional layer of indirection, but it's not obvious what it is.

PeterJohnson pushed a commit that referenced this pull request Feb 21, 2026
#8627)

#8626 needs to switch to using reflection to load the robot class. Do
that with this PR so it's separate.

Also, remove the duplicated main files from the template, and instead
fixup vscode to handle this properly.
@ThadHouse
Copy link
Member Author

It becomes more obvious what things are part of the DS, and what things are a part of the robot. It also gives a good place to put driver station specific code, like alliance flipping if a team wants to handle that. It also gives a uniform place to store all gamepad and joystick instances, which with opmodes becomes more important because we want to limit to a single instance, and we can imagine teams putting joysticks inside each opmode. Which we want to avoid.

Comment on lines +7 to +14
/**
* An interface representing user controls such as gamepads or joysticks.
* If your main robot class has a UserControlsInstance attribute with a
* class implementing this interface, the constructor is able to receive
* an instance of that class. Additionally, any OpModes can also receive
* that same instance.
*/
public interface UserControls {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document (either here or in @UserControlsInterface, or both) that the class must have a default constructor.

System.out.println("Constructor parameter types: " + java.util.Arrays.toString(paramTypes));
if (dsInstance != null
&& paramTypes.length == 1
&& UserControls.class.isAssignableFrom(paramTypes[0])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&& UserControls.class.isAssignableFrom(paramTypes[0])) {
&& paramTypes[0].isAssignableFrom(UserControls.class)) {

private static boolean m_suppressExitWarning;

private static <T extends RobotBase> T constructRobot(Class<T> robotClass) throws Throwable {
UserControlsInstance dsAttribute = robotClass.getDeclaredAnnotation(UserControlsInstance.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to update the variable names from using ds to userControls.

for (Constructor<?> constructor : constructors) {
Class<?>[] paramTypes = constructor.getParameterTypes();
if (paramTypes.length == 0) {
System.out.println("Constructor parameter types: " + java.util.Arrays.toString(paramTypes));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this print staying long-term? If so, we we get an import of Arrays?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2027 2027 target component: wpilibj WPILib Java

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants