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