Skip to content

Programming Solenoids and the Compressor in WPILib

learnfrc.com
learnfrc.comAuthor
Veer Bajaj
Veer BajajMaintainer

Every pneumatics class takes a PneumaticsModuleType — REVPH for the REV Pneumatic Hub or CTREPCM for the CTRE PCM. WPILib’s Solenoid, DoubleSolenoid, and Compressor classes do the rest; the controller hardware handles closed-loop compressor control automatically.

A Solenoid controls one channel. Construct it with the module type and channel, then turn it on or off:

Solenoid exampleSolenoid = new Solenoid(PneumaticsModuleType.REVPH, 0);
exampleSolenoid.set(true); // energize
exampleSolenoid.set(false); // de-energize (springs back)
exampleSolenoid.toggle(); // flip current state

A DoubleSolenoid takes a forward channel and a reverse channel, and is set to one of three states: kForward, kReverse, or kOff:

DoubleSolenoid exampleDouble =
new DoubleSolenoid(PneumaticsModuleType.CTREPCM, 1, 2);
exampleDouble.set(DoubleSolenoid.Value.kForward);
exampleDouble.set(DoubleSolenoid.Value.kReverse);
exampleDouble.set(DoubleSolenoid.Value.kOff); // both off, holds last air state

A DoubleSolenoid defaults to kOff, so you must set() it before toggle() will work. You can also pass an explicit CAN ID, e.g. new DoubleSolenoid(9, PneumaticsModuleType.REVPH, 4, 5).

The Compressor class manages automatic compressor control. There are three modes:

Compressor compressor = new Compressor(PneumaticsModuleType.REVPH);
// 1) Digital: run until the pressure switch reports full. Works on PH and PCM.
compressor.enableDigital();
// 2) Analog: PH only - run while pressure is between min and max (psi)
compressor.enableAnalog(70, 120);
// 3) Hybrid: PH only - analog range AND the digital switch agree it is not full
compressor.enableHybrid(70, 120);
compressor.disable(); // stop automatic control
double psi = compressor.getPressure(); // live pressure (PH analog only)

Closed-loop (digital) control is enabled by default when a Compressor is constructed, so simply creating the object usually starts the compressor running under pressure-switch control once the robot is enabled.

  • Put solenoid objects in a subsystem and expose methods like extend() / retract() rather than calling set() from all over your code.
  • For double solenoids, decide a sensible disabled/init state (kForward, kReverse, or kOff).
  • getPressure() returns valid data only with the REV analog sensor on a PH; on a PCM use the digital switch state instead.
  • Test on the bench with the REV Hardware Client before relying on robot code.
  • Every pneumatics class takes PneumaticsModuleType.REVPH or CTREPCM; the hardware handles compressor closed-loop control.
  • Single solenoid: Solenoid(module, channel) with set(true/false) and toggle().
  • Double solenoid: DoubleSolenoid(module, fwd, rev) set to kForward, kReverse, or kOff; it defaults to kOff.
  • Compressor modes: enableDigital() (any module), enableAnalog(min,max) and enableHybrid(min,max) (PH only); getPressure() needs the PH analog sensor.

This lesson was adapted from learnfrc.com.