Java - setting variables inside subsystems

Yet another java code question. We use context - sensitive controls. So the gunner has a shoot button, and that is the shoot button no matter what we are doing. This is how we have it structured:

Robot Container
If (getShooterMode()=“Trap”)
driver.shoot.shootTrap()
else If (getShooterMode()=“Amp”)
driver.shoot.shootAmp()

Commands
shootTrap()
shootAmp()
setMode()

Subsystems
Shooter

In the shooter subsystem, we have a set method called “shooter mode”. This has a variable inside it. When we call it from a command, it gets passed a string.

We have a get method in the shooter subsystem that returns “shooter mode”. Robot container calls this method to determine the mode and evaluate the if/else statement at the start of this post. In robot container there are also triggers connected to operator buttons that use the setmode command to select the shooter mode.

Is the ‘right’ way to do it?

Depends on your robot design and how you’ve organized the code. If everything relevant to that decision is contained within the same subsystem, then just do it all within the subsystem. You can have a method that builds and returns a Command instance (i.e. return Commands.run(() -> { ..... }); or whatever), and in the body of that lambda, you can add your conditional tests and call other methods on the command. Since its all within the same class, you can natively interact with class members without having to expose them as public methods you only ever call once.

If you have a separate subsystem that is involved (i.e. you have an arm subsystem and need to check the position of it), then that’s a little more involved.

Can you give some more context? I’m not sure what the code block in question is doing.

Yeah, it’s tough to explain the whole flow in pseudotext.

Here is the repository. The currently active branch is ‘addmotors’, which contains the code where we are trying to make this all work.

I’m sure there is a better way to do it, but will what we are doing work?

1 Like

Thanks for sharing the repository. I don’t see the code from the OP, though - have you pushed recently?

Figures. It looks like it didn’t get pushed correctly. I’ll have to rectify that tomorrow.

We are having the operator push different buttons for Amp, Speaker, etc. and the driver has a button for shoot. The operator buttons call methods in the shooter subsystem to set the motor speeds for the different shots and the driver shoot button triggers the indexer to feed the note into the shooter.

In short I wouldn’t recommend complex “joystick” button else/ifs but would internalize it into the subsystem.