blob: 97f8af1ba6143072c45bb114843ea02c13969a26 [file] [log] [blame]
Parker Schuhd3b7a8872018-02-19 16:42:27 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2014-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "frc971/wpilib/ahal/Compressor.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -08009
Austin Schuh9950f682021-11-06 15:27:58 -070010#include "frc971/wpilib/ahal/WPIErrors.h"
11#include "hal/CTREPCM.h"
Austin Schuhf6b94632019-02-02 22:11:27 -080012#include "hal/HAL.h"
13#include "hal/Ports.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080014
15using namespace frc;
16
17/**
18 * Constructor.
19 *
20 * @param module The PCM ID to use (0-62)
21 */
22Compressor::Compressor(int pcmID) : m_module(pcmID) {
23 int32_t status = 0;
Austin Schuh9950f682021-11-06 15:27:58 -070024 m_compressorHandle = HAL_InitializeCTREPCM(m_module, nullptr, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080025 if (status != 0) {
26 wpi_setErrorWithContextRange(status, 0, HAL_GetNumPCMModules(), pcmID,
27 HAL_GetErrorMessage(status));
28 return;
29 }
30 SetClosedLoopControl(true);
31}
32
33/**
34 * Starts closed-loop control. Note that closed loop control is enabled by
35 * default.
36 */
37void Compressor::Start() {
38 if (StatusIsFatal()) return;
39 SetClosedLoopControl(true);
40}
41
42/**
43 * Stops closed-loop control. Note that closed loop control is enabled by
44 * default.
45 */
46void Compressor::Stop() {
47 if (StatusIsFatal()) return;
48 SetClosedLoopControl(false);
49}
50
51/**
52 * Check if compressor output is active.
53 *
54 * @return true if the compressor is on
55 */
56bool Compressor::Enabled() const {
57 if (StatusIsFatal()) return false;
58 int32_t status = 0;
59 bool value;
60
Austin Schuh9950f682021-11-06 15:27:58 -070061 value = HAL_GetCTREPCMCompressor(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080062
63 if (status) {
64 wpi_setWPIError(Timeout);
65 }
66
67 return value;
68}
69
70/**
71 * Check if the pressure switch is triggered.
72 *
73 * @return true if pressure is low
74 */
75bool Compressor::GetPressureSwitchValue() const {
76 if (StatusIsFatal()) return false;
77 int32_t status = 0;
78 bool value;
79
Austin Schuh9950f682021-11-06 15:27:58 -070080 value = HAL_GetCTREPCMPressureSwitch(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080081
82 if (status) {
83 wpi_setWPIError(Timeout);
84 }
85
86 return value;
87}
88
89/**
90 * Query how much current the compressor is drawing.
91 *
92 * @return The current through the compressor, in amps
93 */
94double Compressor::GetCompressorCurrent() const {
95 if (StatusIsFatal()) return 0;
96 int32_t status = 0;
97 double value;
98
Austin Schuh9950f682021-11-06 15:27:58 -070099 value = HAL_GetCTREPCMCompressorCurrent(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800100
101 if (status) {
102 wpi_setWPIError(Timeout);
103 }
104
105 return value;
106}
107
108/**
109 * Enables or disables automatically turning the compressor on when the
110 * pressure is low.
111 *
112 * @param on Set to true to enable closed loop control of the compressor. False
113 * to disable.
114 */
115void Compressor::SetClosedLoopControl(bool on) {
116 if (StatusIsFatal()) return;
117 int32_t status = 0;
118
Austin Schuh9950f682021-11-06 15:27:58 -0700119 HAL_SetCTREPCMClosedLoopControl(m_compressorHandle, on, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800120
121 if (status) {
122 wpi_setWPIError(Timeout);
123 }
124}
125
126/**
127 * Returns true if the compressor will automatically turn on when the
128 * pressure is low.
129 *
130 * @return True if closed loop control of the compressor is enabled. False if
131 * disabled.
132 */
133bool Compressor::GetClosedLoopControl() const {
134 if (StatusIsFatal()) return false;
135 int32_t status = 0;
136 bool value;
137
Austin Schuh9950f682021-11-06 15:27:58 -0700138 value = HAL_GetCTREPCMClosedLoopControl(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800139
140 if (status) {
141 wpi_setWPIError(Timeout);
142 }
143
144 return value;
145}
146
147/**
148 * Query if the compressor output has been disabled due to high current draw.
149 *
150 * @return true if PCM is in fault state : Compressor Drive is
151 * disabled due to compressor current being too high.
152 */
153bool Compressor::GetCompressorCurrentTooHighFault() const {
154 if (StatusIsFatal()) return false;
155 int32_t status = 0;
156 bool value;
157
Philipp Schrader790cb542023-07-05 21:06:52 -0700158 value =
159 HAL_GetCTREPCMCompressorCurrentTooHighFault(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800160
161 if (status) {
162 wpi_setWPIError(Timeout);
163 }
164
165 return value;
166}
167
168/**
169 * Query if the compressor output has been disabled due to high current draw
170 * (sticky).
171 *
172 * A sticky fault will not clear on device reboot, it must be cleared through
173 * code or the webdash.
174 *
175 * @return true if PCM sticky fault is set : Compressor Drive is
176 * disabled due to compressor current being too high.
177 */
178bool Compressor::GetCompressorCurrentTooHighStickyFault() const {
179 if (StatusIsFatal()) return false;
180 int32_t status = 0;
181 bool value;
182
Philipp Schrader790cb542023-07-05 21:06:52 -0700183 value = HAL_GetCTREPCMCompressorCurrentTooHighStickyFault(m_compressorHandle,
184 &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800185
186 if (status) {
187 wpi_setWPIError(Timeout);
188 }
189
190 return value;
191}
192
193/**
194 * Query if the compressor output has been disabled due to a short circuit
195 * (sticky).
196 *
197 * A sticky fault will not clear on device reboot, it must be cleared through
198 * code or the webdash.
199 *
200 * @return true if PCM sticky fault is set : Compressor output
201 * appears to be shorted.
202 */
203bool Compressor::GetCompressorShortedStickyFault() const {
204 if (StatusIsFatal()) return false;
205 int32_t status = 0;
206 bool value;
207
Philipp Schrader790cb542023-07-05 21:06:52 -0700208 value =
209 HAL_GetCTREPCMCompressorShortedStickyFault(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800210
211 if (status) {
212 wpi_setWPIError(Timeout);
213 }
214
215 return value;
216}
217
218/**
219 * Query if the compressor output has been disabled due to a short circuit.
220 *
221 * @return true if PCM is in fault state : Compressor output
222 * appears to be shorted.
223 */
224bool Compressor::GetCompressorShortedFault() const {
225 if (StatusIsFatal()) return false;
226 int32_t status = 0;
227 bool value;
228
Austin Schuh9950f682021-11-06 15:27:58 -0700229 value = HAL_GetCTREPCMCompressorShortedFault(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800230
231 if (status) {
232 wpi_setWPIError(Timeout);
233 }
234
235 return value;
236}
237
238/**
239 * Query if the compressor output does not appear to be wired (sticky).
240 *
241 * A sticky fault will not clear on device reboot, it must be cleared through
242 * code or the webdash.
243 *
244 * @return true if PCM sticky fault is set : Compressor does not
245 * appear to be wired, i.e. compressor is not drawing enough current.
246 */
247bool Compressor::GetCompressorNotConnectedStickyFault() const {
248 if (StatusIsFatal()) return false;
249 int32_t status = 0;
250 bool value;
251
Philipp Schrader790cb542023-07-05 21:06:52 -0700252 value = HAL_GetCTREPCMCompressorNotConnectedStickyFault(m_compressorHandle,
253 &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800254
255 if (status) {
256 wpi_setWPIError(Timeout);
257 }
258
259 return value;
260}
261
262/**
263 * Query if the compressor output does not appear to be wired.
264 *
265 * @return true if PCM is in fault state : Compressor does not
266 * appear to be wired, i.e. compressor is not drawing enough current.
267 */
268bool Compressor::GetCompressorNotConnectedFault() const {
269 if (StatusIsFatal()) return false;
270 int32_t status = 0;
271 bool value;
272
Philipp Schrader790cb542023-07-05 21:06:52 -0700273 value =
274 HAL_GetCTREPCMCompressorNotConnectedFault(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800275
276 if (status) {
277 wpi_setWPIError(Timeout);
278 }
279
280 return value;
281}
282
283/**
284 * Clear ALL sticky faults inside PCM that Compressor is wired to.
285 *
286 * If a sticky fault is set, then it will be persistently cleared. Compressor
287 * drive maybe momentarily disable while flags are being cleared. Care should
288 * be taken to not call this too frequently, otherwise normal compressor
289 * functionality may be prevented.
290 *
291 * If no sticky faults are set then this call will have no effect.
292 */
293void Compressor::ClearAllPCMStickyFaults() {
294 if (StatusIsFatal()) return;
295 int32_t status = 0;
296
Austin Schuh9950f682021-11-06 15:27:58 -0700297 HAL_ClearAllCTREPCMStickyFaults(m_module, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800298
299 if (status) {
300 wpi_setWPIError(Timeout);
301 }
302}