blob: 4760c02f9a7cc0fc82bcc3d1efb1e2317df938dc [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#include "Compressor.h"
8#include "DigitalInput.h"
9#include "NetworkCommunication/UsageReporting.h"
10#include "Timer.h"
11#include "WPIErrors.h"
12
13/**
14 * Internal task.
15 *
16 * Task which checks the compressor pressure switch and operates the relay as necessary
17 * depending on the pressure.
18 *
19 * Do not call this function directly.
20 */
21static void CompressorChecker(Compressor *c)
22{
23 while (1)
24 {
25 if (c->Enabled())
26 {
27 c->SetRelayValue( c->GetPressureSwitchValue() == 0 ? Relay::kOn : Relay::kOff );
28 }
29 else
30 {
31 c->SetRelayValue(Relay::kOff);
32 }
33 Wait(0.5);
34 }
35}
36
37/**
38 * Initialize the Compressor object.
39 * This method is the common initialization code for all the constructors for the Compressor
40 * object. It takes the relay channel and pressure switch channel and spawns a task that polls the
41 * compressor and sensor.
42 *
43 * You MUST start the compressor by calling the Start() method.
44 */
45void Compressor::InitCompressor(UINT8 pressureSwitchModuleNumber,
46 UINT32 pressureSwitchChannel,
47 UINT8 compresssorRelayModuleNumber,
48 UINT32 compressorRelayChannel)
49{
50 m_enabled = false;
51 m_pressureSwitch = new DigitalInput(pressureSwitchModuleNumber, pressureSwitchChannel);
52 m_relay = new Relay(compresssorRelayModuleNumber, compressorRelayChannel, Relay::kForwardOnly);
53
54 nUsageReporting::report(nUsageReporting::kResourceType_Compressor, 0);
55
56 if (!m_task.Start((INT32)this))
57 {
58 wpi_setWPIError(CompressorTaskError);
59 }
60}
61
62/**
63 * Compressor constructor.
64 * Given a fully specified relay channel and pressure switch channel, initialize the Compressor object.
65 *
66 * You MUST start the compressor by calling the Start() method.
67 *
68 * @param pressureSwitchModuleNumber The digital module that the pressure switch is attached to.
69 * @param pressureSwitchChannel The GPIO channel that the pressure switch is attached to.
70 * @param compresssorRelayModuleNumber The digital module that the compressor relay is attached to.
71 * @param compressorRelayChannel The relay channel that the compressor relay is attached to.
72 */
73Compressor::Compressor(UINT8 pressureSwitchModuleNumber,
74 UINT32 pressureSwitchChannel,
75 UINT8 compresssorRelayModuleNumber,
76 UINT32 compressorRelayChannel)
77 : m_task ("Compressor", (FUNCPTR)CompressorChecker)
78{
79 InitCompressor(pressureSwitchModuleNumber,
80 pressureSwitchChannel,
81 compresssorRelayModuleNumber,
82 compressorRelayChannel);
83}
84
85/**
86 * Compressor constructor.
87 * Given a relay channel and pressure switch channel (both in the default digital module), initialize
88 * the Compressor object.
89 *
90 * You MUST start the compressor by calling the Start() method.
91 *
92 * @param pressureSwitchChannel The GPIO channel that the pressure switch is attached to.
93 * @param compressorRelayChannel The relay channel that the compressor relay is attached to.
94 */
95Compressor::Compressor(UINT32 pressureSwitchChannel, UINT32 compressorRelayChannel)
96 : m_task ("Compressor", (FUNCPTR)CompressorChecker)
97{
98 InitCompressor(GetDefaultDigitalModule(),
99 pressureSwitchChannel,
100 GetDefaultDigitalModule(),
101 compressorRelayChannel);
102}
103
104/**
105 * Delete the Compressor object.
106 * Delete the allocated resources for the compressor and kill the compressor task that is polling
107 * the pressure switch.
108 */
109Compressor::~Compressor()
110{
111 delete m_pressureSwitch;
112 delete m_relay;
113}
114
115/**
116 * Operate the relay for the compressor.
117 * Change the value of the relay output that is connected to the compressor motor.
118 * This is only intended to be called by the internal polling thread.
119 */
120void Compressor::SetRelayValue(Relay::Value relayValue)
121{
122 m_relay->Set(relayValue);
123}
124
125/**
126 * Get the pressure switch value.
127 * Read the pressure switch digital input.
128 *
129 * @return The current state of the pressure switch.
130 */
131UINT32 Compressor::GetPressureSwitchValue()
132{
133 return m_pressureSwitch->Get();
134}
135
136/**
137 * Start the compressor.
138 * This method will allow the polling loop to actually operate the compressor. The
139 * is stopped by default and won't operate until starting it.
140 */
141void Compressor::Start()
142{
143 m_enabled = true;
144}
145
146/**
147 * Stop the compressor.
148 * This method will stop the compressor from turning on.
149 */
150void Compressor::Stop()
151{
152 m_enabled = false;
153}
154
155/**
156 * Get the state of the enabled flag.
157 * Return the state of the enabled flag for the compressor and pressure switch
158 * combination.
159 *
160 * @return The state of the compressor thread's enable flag.
161 */
162bool Compressor::Enabled()
163{
164 return m_enabled;
165}
166
167void Compressor::UpdateTable() {
168 if (m_table != NULL) {
169 m_table->PutBoolean("Enabled", m_enabled);
170 m_table->PutBoolean("Pressure switch", GetPressureSwitchValue());
171 }
172}
173
174void Compressor::StartLiveWindowMode() {
175
176}
177
178void Compressor::StopLiveWindowMode() {
179
180}
181
182std::string Compressor::GetSmartDashboardType() {
183 return "Compressor";
184}
185
186void Compressor::InitTable(ITable *subTable) {
187 m_table = subTable;
188 UpdateTable();
189}
190
191ITable * Compressor::GetTable() {
192 return m_table;
193}
194