blob: ba04e520ffab6e466223227ec3fe549c4f667f9f [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
Austin Schuh9950f682021-11-06 15:27:58 -0700158 value = HAL_GetCTREPCMCompressorCurrentTooHighFault(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800159
160 if (status) {
161 wpi_setWPIError(Timeout);
162 }
163
164 return value;
165}
166
167/**
168 * Query if the compressor output has been disabled due to high current draw
169 * (sticky).
170 *
171 * A sticky fault will not clear on device reboot, it must be cleared through
172 * code or the webdash.
173 *
174 * @return true if PCM sticky fault is set : Compressor Drive is
175 * disabled due to compressor current being too high.
176 */
177bool Compressor::GetCompressorCurrentTooHighStickyFault() const {
178 if (StatusIsFatal()) return false;
179 int32_t status = 0;
180 bool value;
181
182 value =
Austin Schuh9950f682021-11-06 15:27:58 -0700183 HAL_GetCTREPCMCompressorCurrentTooHighStickyFault(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800184
185 if (status) {
186 wpi_setWPIError(Timeout);
187 }
188
189 return value;
190}
191
192/**
193 * Query if the compressor output has been disabled due to a short circuit
194 * (sticky).
195 *
196 * A sticky fault will not clear on device reboot, it must be cleared through
197 * code or the webdash.
198 *
199 * @return true if PCM sticky fault is set : Compressor output
200 * appears to be shorted.
201 */
202bool Compressor::GetCompressorShortedStickyFault() const {
203 if (StatusIsFatal()) return false;
204 int32_t status = 0;
205 bool value;
206
Austin Schuh9950f682021-11-06 15:27:58 -0700207 value = HAL_GetCTREPCMCompressorShortedStickyFault(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800208
209 if (status) {
210 wpi_setWPIError(Timeout);
211 }
212
213 return value;
214}
215
216/**
217 * Query if the compressor output has been disabled due to a short circuit.
218 *
219 * @return true if PCM is in fault state : Compressor output
220 * appears to be shorted.
221 */
222bool Compressor::GetCompressorShortedFault() const {
223 if (StatusIsFatal()) return false;
224 int32_t status = 0;
225 bool value;
226
Austin Schuh9950f682021-11-06 15:27:58 -0700227 value = HAL_GetCTREPCMCompressorShortedFault(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800228
229 if (status) {
230 wpi_setWPIError(Timeout);
231 }
232
233 return value;
234}
235
236/**
237 * Query if the compressor output does not appear to be wired (sticky).
238 *
239 * A sticky fault will not clear on device reboot, it must be cleared through
240 * code or the webdash.
241 *
242 * @return true if PCM sticky fault is set : Compressor does not
243 * appear to be wired, i.e. compressor is not drawing enough current.
244 */
245bool Compressor::GetCompressorNotConnectedStickyFault() const {
246 if (StatusIsFatal()) return false;
247 int32_t status = 0;
248 bool value;
249
Austin Schuh9950f682021-11-06 15:27:58 -0700250 value = HAL_GetCTREPCMCompressorNotConnectedStickyFault(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800251
252 if (status) {
253 wpi_setWPIError(Timeout);
254 }
255
256 return value;
257}
258
259/**
260 * Query if the compressor output does not appear to be wired.
261 *
262 * @return true if PCM is in fault state : Compressor does not
263 * appear to be wired, i.e. compressor is not drawing enough current.
264 */
265bool Compressor::GetCompressorNotConnectedFault() const {
266 if (StatusIsFatal()) return false;
267 int32_t status = 0;
268 bool value;
269
Austin Schuh9950f682021-11-06 15:27:58 -0700270 value = HAL_GetCTREPCMCompressorNotConnectedFault(m_compressorHandle, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800271
272 if (status) {
273 wpi_setWPIError(Timeout);
274 }
275
276 return value;
277}
278
279/**
280 * Clear ALL sticky faults inside PCM that Compressor is wired to.
281 *
282 * If a sticky fault is set, then it will be persistently cleared. Compressor
283 * drive maybe momentarily disable while flags are being cleared. Care should
284 * be taken to not call this too frequently, otherwise normal compressor
285 * functionality may be prevented.
286 *
287 * If no sticky faults are set then this call will have no effect.
288 */
289void Compressor::ClearAllPCMStickyFaults() {
290 if (StatusIsFatal()) return;
291 int32_t status = 0;
292
Austin Schuh9950f682021-11-06 15:27:58 -0700293 HAL_ClearAllCTREPCMStickyFaults(m_module, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800294
295 if (status) {
296 wpi_setWPIError(Timeout);
297 }
298}