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 module module number
013   * @param type module type
014   * @return module
015   */
016  static PneumaticsBase getForType(int module, PneumaticsModuleType type) {
017    if (type == PneumaticsModuleType.CTREPCM) {
018      return new PneumaticsControlModule(module);
019    } else if (type == PneumaticsModuleType.REVPH) {
020      return new PneumaticHub(module);
021    }
022    throw new IllegalArgumentException("Unknown module type");
023  }
024
025  /**
026   * For internal use to get the default for a specific type.
027   *
028   * @param type module type
029   * @return module default
030   */
031  static int getDefaultForType(PneumaticsModuleType type) {
032    if (type == PneumaticsModuleType.CTREPCM) {
033      return SensorUtil.getDefaultCTREPCMModule();
034    } else if (type == PneumaticsModuleType.REVPH) {
035      return SensorUtil.getDefaultREVPHModule();
036    }
037    throw new IllegalArgumentException("Unknown module type");
038  }
039
040  /**
041   * Sets solenoids on a pneumatics module.
042   *
043   * @param mask mask
044   * @param values values
045   */
046  void setSolenoids(int mask, int values);
047
048  /**
049   * Gets a bitmask of solenoid values.
050   *
051   * @return values
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 of disabled solenoids
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 masked solenoids can be reserved, and if not reserve them.
202   *
203   * @param mask The bitmask of solenoids to reserve
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 masked solenoids.
210   *
211   * @param mask The bitmask of solenoids to unreserve
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}