001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj;
006
007/** Interface for pneumatics devices. */
008public interface PneumaticsBase extends AutoCloseable {
009  /**
010   * For internal use to get a module for a specific type.
011   *
012   * @param busId The bus ID
013   * @param module module number
014   * @param type module type
015   * @return module
016   */
017  static PneumaticsBase getForType(int busId, int module, PneumaticsModuleType type) {
018    return switch (type) {
019      case CTREPCM -> new PneumaticsControlModule(busId, module);
020      case REVPH -> new PneumaticHub(busId, module);
021    };
022  }
023
024  /**
025   * For internal use to get the default for a specific type.
026   *
027   * @param type module type
028   * @return module default
029   */
030  static int getDefaultForType(PneumaticsModuleType type) {
031    return switch (type) {
032      case CTREPCM -> SensorUtil.getDefaultCTREPCMModule();
033      case REVPH -> SensorUtil.getDefaultREVPHModule();
034    };
035  }
036
037  /**
038   * Sets solenoids on a pneumatics module.
039   *
040   * @param mask Bitmask indicating which solenoids to set. The LSB represents solenoid 0.
041   * @param values Bitmask indicating the desired states of the solenoids. The LSB represents
042   *     solenoid 0.
043   */
044  void setSolenoids(int mask, int values);
045
046  /**
047   * Gets a bitmask of solenoid values.
048   *
049   * @return Bitmask containing the state of the solenoids. The LSB represents solenoid 0.
050   */
051  int getSolenoids();
052
053  /**
054   * Get module number for this module.
055   *
056   * @return module number
057   */
058  int getModuleNumber();
059
060  /**
061   * Get a bitmask of disabled solenoids.
062   *
063   * @return Bitmask indicating disabled solenoids. The LSB represents solenoid 0.
064   */
065  int getSolenoidDisabledList();
066
067  /**
068   * Fire a single solenoid shot.
069   *
070   * @param index solenoid index
071   */
072  void fireOneShot(int index);
073
074  /**
075   * Set the duration for a single solenoid shot.
076   *
077   * @param index solenoid index
078   * @param durMs shot duration
079   */
080  void setOneShotDuration(int index, int durMs);
081
082  /**
083   * Returns whether the compressor is active or not.
084   *
085   * @return True if the compressor is on - otherwise false.
086   */
087  boolean getCompressor();
088
089  /**
090   * Returns the state of the pressure switch.
091   *
092   * @return True if pressure switch indicates that the system is not full, otherwise false.
093   */
094  boolean getPressureSwitch();
095
096  /**
097   * Returns the current drawn by the compressor in amps.
098   *
099   * @return The current drawn by the compressor.
100   */
101  double getCompressorCurrent();
102
103  /** Disables the compressor. */
104  void disableCompressor();
105
106  /**
107   * Enables the compressor in digital mode using the digital pressure switch. The compressor will
108   * turn on when the pressure switch indicates that the system is not full, and will turn off when
109   * the pressure switch indicates that the system is full.
110   */
111  void enableCompressorDigital();
112
113  /**
114   * If supported by the device, enables the compressor in analog mode. This mode uses an analog
115   * pressure sensor connected to analog channel 0 to cycle the compressor. The compressor will turn
116   * on when the pressure drops below {@code minPressure} and will turn off when the pressure
117   * reaches {@code maxPressure}. This mode is only supported by the REV PH with the REV Analog
118   * Pressure Sensor connected to analog channel 0.
119   *
120   * <p>On CTRE PCM, this will enable digital control.
121   *
122   * @param minPressure The minimum pressure in PSI. The compressor will turn on when the pressure
123   *     drops below this value.
124   * @param maxPressure The maximum pressure in PSI. The compressor will turn off when the pressure
125   *     reaches this value.
126   */
127  void enableCompressorAnalog(double minPressure, double maxPressure);
128
129  /**
130   * If supported by the device, enables the compressor in hybrid mode. This mode uses both a
131   * digital pressure switch and an analog pressure sensor connected to analog channel 0 to cycle
132   * the compressor. This mode is only supported by the REV PH with the REV Analog Pressure Sensor
133   * connected to analog channel 0.
134   *
135   * <p>The compressor will turn on when <i>both</i>:
136   *
137   * <ul>
138   *   <li>The digital pressure switch indicates the system is not full AND
139   *   <li>The analog pressure sensor indicates that the pressure in the system is below the
140   *       specified minimum pressure.
141   * </ul>
142   *
143   * <p>The compressor will turn off when <i>either</i>:
144   *
145   * <ul>
146   *   <li>The digital pressure switch is disconnected or indicates that the system is full OR
147   *   <li>The pressure detected by the analog sensor is greater than the specified maximum
148   *       pressure.
149   * </ul>
150   *
151   * <p>On CTRE PCM, this will enable digital control.
152   *
153   * @param minPressure The minimum pressure in PSI. The compressor will turn on when the pressure
154   *     drops below this value and the pressure switch indicates that the system is not full.
155   * @param maxPressure The maximum pressure in PSI. The compressor will turn off when the pressure
156   *     reaches this value or the pressure switch is disconnected or indicates that the system is
157   *     full.
158   */
159  void enableCompressorHybrid(double minPressure, double maxPressure);
160
161  /**
162   * If supported by the device, returns the raw voltage of the specified analog input channel.
163   *
164   * <p>This function is only supported by the REV PH. On CTRE PCM, this will return 0.
165   *
166   * @param channel The analog input channel to read voltage from.
167   * @return The voltage of the specified analog input channel.
168   */
169  double getAnalogVoltage(int channel);
170
171  /**
172   * If supported by the device, returns the pressure (in PSI) read by an analog pressure sensor on
173   * the specified analog input channel.
174   *
175   * <p>This function is only supported by the REV PH. On CTRE PCM, this will return 0.
176   *
177   * @param channel The analog input channel to read pressure from.
178   * @return The pressure (in PSI) read by an analog pressure sensor on the specified analog input
179   *     channel.
180   */
181  double getPressure(int channel);
182
183  /**
184   * Returns the active compressor configuration.
185   *
186   * @return The active compressor configuration.
187   */
188  CompressorConfigType getCompressorConfigType();
189
190  /**
191   * Check if a solenoid channel is valid.
192   *
193   * @param channel Channel to check
194   * @return True if channel exists
195   */
196  boolean checkSolenoidChannel(int channel);
197
198  /**
199   * Check to see if the solenoids marked in the bitmask can be reserved, and if so, reserve them.
200   *
201   * @param mask The bitmask of solenoids to reserve. The LSB represents solenoid 0.
202   * @return 0 if successful; mask of solenoids that couldn't be allocated otherwise
203   */
204  int checkAndReserveSolenoids(int mask);
205
206  /**
207   * Unreserve the solenoids marked in the bitmask.
208   *
209   * @param mask The bitmask of solenoids to unreserve. The LSB represents solenoid 0.
210   */
211  void unreserveSolenoids(int mask);
212
213  /**
214   * Reserve the compressor.
215   *
216   * @return true if successful; false if compressor already reserved
217   */
218  boolean reserveCompressor();
219
220  /** Unreserve the compressor. */
221  void unreserveCompressor();
222
223  @Override
224  void close();
225
226  /**
227   * Create a solenoid object for the specified channel.
228   *
229   * @param channel solenoid channel
230   * @return Solenoid object
231   */
232  Solenoid makeSolenoid(int channel);
233
234  /**
235   * Create a double solenoid object for the specified channels.
236   *
237   * @param forwardChannel solenoid channel for forward
238   * @param reverseChannel solenoid channel for reverse
239   * @return DoubleSolenoid object
240   */
241  DoubleSolenoid makeDoubleSolenoid(int forwardChannel, int reverseChannel);
242
243  /**
244   * Create a compressor object.
245   *
246   * @return Compressor object
247   */
248  Compressor makeCompressor();
249
250  /**
251   * Report usage.
252   *
253   * @param device device and channel as appropriate
254   * @param data arbitrary usage data
255   */
256  void reportUsage(String device, String data);
257}