blob: 254d1447df49b6c7c1be61f761a3f90a295d4584 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
7
8#include "SolenoidBase.h"
9
10void* SolenoidBase::m_ports[m_maxModules][m_maxPorts];
11std::unique_ptr<Resource> SolenoidBase::m_allocated;
12
13/**
14 * Constructor
15 *
16 * @param moduleNumber The CAN PCM ID.
17 */
18SolenoidBase::SolenoidBase(uint8_t moduleNumber)
19 : m_moduleNumber(moduleNumber) {
20 for (uint32_t i = 0; i < kSolenoidChannels; i++) {
21 void* port = getPortWithModule(moduleNumber, i);
22 int32_t status = 0;
23 SolenoidBase::m_ports[moduleNumber][i] =
24 initializeSolenoidPort(port, &status);
25 wpi_setErrorWithContext(status, getHALErrorMessage(status));
26 freePort(port);
27 }
28}
29
30/**
31 * Set the value of a solenoid.
32 *
33 * @param value The value you want to set on the module.
34 * @param mask The channels you want to be affected.
35 */
36void SolenoidBase::Set(uint8_t value, uint8_t mask, int module) {
37 int32_t status = 0;
38 for (int i = 0; i < m_maxPorts; i++) {
39 uint8_t local_mask = 1 << i;
40 if (mask & local_mask)
41 setSolenoid(m_ports[module][i], value & local_mask, &status);
42 }
43 wpi_setErrorWithContext(status, getHALErrorMessage(status));
44}
45
46/**
47 * Read all 8 solenoids as a single byte
48 *
49 * @return The current value of all 8 solenoids on the module.
50 */
51uint8_t SolenoidBase::GetAll(int module) const {
52 uint8_t value = 0;
53 int32_t status = 0;
54 value = getAllSolenoids(m_ports[module][0], &status);
55 wpi_setErrorWithContext(status, getHALErrorMessage(status));
56 return value;
57}
58/**
59 * Reads complete solenoid blacklist for all 8 solenoids as a single byte.
60 *
61 * If a solenoid is shorted, it is added to the blacklist and
62 * disabled until power cycle, or until faults are cleared.
63 * @see ClearAllPCMStickyFaults()
64 *
65 * @return The solenoid blacklist of all 8 solenoids on the module.
66 */
67uint8_t SolenoidBase::GetPCMSolenoidBlackList(int module) const {
68 int32_t status = 0;
69 return getPCMSolenoidBlackList(m_ports[module][0], &status);
70}
71/**
72 * @return true if PCM sticky fault is set : The common
73 * highside solenoid voltage rail is too low,
74 * most likely a solenoid channel is shorted.
75 */
76bool SolenoidBase::GetPCMSolenoidVoltageStickyFault(int module) const {
77 int32_t status = 0;
78 return getPCMSolenoidVoltageStickyFault(m_ports[module][0], &status);
79}
80/**
81 * @return true if PCM is in fault state : The common
82 * highside solenoid voltage rail is too low,
83 * most likely a solenoid channel is shorted.
84 */
85bool SolenoidBase::GetPCMSolenoidVoltageFault(int module) const {
86 int32_t status = 0;
87 return getPCMSolenoidVoltageFault(m_ports[module][0], &status);
88}
89/**
90 * Clear ALL sticky faults inside PCM that Compressor is wired to.
91 *
92 * If a sticky fault is set, then it will be persistently cleared. Compressor
93 * drive
94 * maybe momentarily disable while flags are being cleared. Care
95 * should be
96 * taken to not call this too frequently, otherwise normal
97 * compressor
98 * functionality may be prevented.
99 *
100 * If no sticky faults are set then this call will have no effect.
101 */
102void SolenoidBase::ClearAllPCMStickyFaults(int module) {
103 int32_t status = 0;
104 return clearAllPCMStickyFaults_sol(m_ports[module][0], &status);
105}