Digtial Input firing a Command

Using the command architecture what is the recommended way to have a command fire when a digital input (like a proximity sensor) is tripped? InterruptableSensorBase.WaitResult seems promising but I haven’t seen it used anywhere and can’t find any examples with it. Triggers are also interesting and what seems work with buttons but I am not sure they will work here

Although I never tried it, it seems Trigger is the way to go.

My bleary thought this morning was to extend button and take a digital input, but Trigger seems like it would work easier. Something like this

new Trigger(aDigitalInput::get).whenActive(new InstantCommand(() -> subsystem.foo()));

Indeed this is very straightforward with the Button or Trigger Classes. 3468 has three IR Photoelectric Reflection Diffuse Sensors, that we use in triggering actions within our intake and launcher this year. We included the DigitalInput objects themselves as part of the subsystems they were attached to (our Ball Indexer/Conveyor), and easily passed the get method for those sensors from our subsystem to the Button class with a lambda.

Button initialConveyorDetector = new Button(() -> conveyor.InitialConveyorSensorGet());
Button finalConveyorDetector = new Button(() -> conveyor.FinalConveyorSensorGet());
Button launcherConveyorDetector = new Button(() -> conveyor.LauncherConveyorSensorGet());

Another fun thing to look into is Trigger's ability to chain Trigger objects (which include Button/JoystickButton) using its and(), or(), and negate() methods. Example from 3468:

// Intake
).whileActiveContinuous(new Intake(ballIntake));

// Conveyor
).whileActiveContinuous(new Advance(conveyor));

Just wanted to say that this could be a bit shorter using method referencing:

Button initialConveyorDetector = new Button(conveyor::InitialConveyorSensorGet);
Button finalConveyorDetector = new Button(conveyor::FinalConveyorSensorGet);
Button launcherConveyorDetector = new Button(conveyor::LauncherConveyorSensorGet);

Also, I did not know about the and and or functions, so thank you for your reply!


I had enough of a time trying to get the students (of which only one had prior programming or FRC experience) to mostly understand the lambdas being used in the WPILIB examples, I’m not about to shove another similar piece of syntax on them and make sure they understand the differences and intricacies between then. But yes, you are absolutely correct!

I enjoy just browsing through the WPILIB Java Docs. Especially with the whole re-write this year, you never know what you’re gonna find!


I do that often too, but the library is so rich you can’t know everything.


Thanks @Dan_Katzuv, @gixxy, @jdaming’s post came out of a discussion we had this morning and you you have filled in the grey areas for me. :+1:

1 Like

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