blob: d8749869cd6988f0b8e2757a2d13931310c8fa23 [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{
jerrym37afdca2013-03-03 01:17:57 +000050 m_table = NULL;
jerrymf1579332013-02-07 01:56:28 +000051 m_enabled = false;
52 m_pressureSwitch = new DigitalInput(pressureSwitchModuleNumber, pressureSwitchChannel);
53 m_relay = new Relay(compresssorRelayModuleNumber, compressorRelayChannel, Relay::kForwardOnly);
54
55 nUsageReporting::report(nUsageReporting::kResourceType_Compressor, 0);
56
57 if (!m_task.Start((INT32)this))
58 {
59 wpi_setWPIError(CompressorTaskError);
60 }
61}
62
63/**
64 * Compressor constructor.
65 * Given a fully specified relay channel and pressure switch channel, initialize the Compressor object.
66 *
67 * You MUST start the compressor by calling the Start() method.
68 *
69 * @param pressureSwitchModuleNumber The digital module that the pressure switch is attached to.
70 * @param pressureSwitchChannel The GPIO channel that the pressure switch is attached to.
71 * @param compresssorRelayModuleNumber The digital module that the compressor relay is attached to.
72 * @param compressorRelayChannel The relay channel that the compressor relay is attached to.
73 */
74Compressor::Compressor(UINT8 pressureSwitchModuleNumber,
75 UINT32 pressureSwitchChannel,
76 UINT8 compresssorRelayModuleNumber,
77 UINT32 compressorRelayChannel)
78 : m_task ("Compressor", (FUNCPTR)CompressorChecker)
79{
80 InitCompressor(pressureSwitchModuleNumber,
81 pressureSwitchChannel,
82 compresssorRelayModuleNumber,
83 compressorRelayChannel);
84}
85
86/**
87 * Compressor constructor.
88 * Given a relay channel and pressure switch channel (both in the default digital module), initialize
89 * the Compressor object.
90 *
91 * You MUST start the compressor by calling the Start() method.
92 *
93 * @param pressureSwitchChannel The GPIO channel that the pressure switch is attached to.
94 * @param compressorRelayChannel The relay channel that the compressor relay is attached to.
95 */
96Compressor::Compressor(UINT32 pressureSwitchChannel, UINT32 compressorRelayChannel)
97 : m_task ("Compressor", (FUNCPTR)CompressorChecker)
98{
99 InitCompressor(GetDefaultDigitalModule(),
100 pressureSwitchChannel,
101 GetDefaultDigitalModule(),
102 compressorRelayChannel);
103}
104
105/**
106 * Delete the Compressor object.
107 * Delete the allocated resources for the compressor and kill the compressor task that is polling
108 * the pressure switch.
109 */
110Compressor::~Compressor()
111{
112 delete m_pressureSwitch;
113 delete m_relay;
114}
115
116/**
117 * Operate the relay for the compressor.
118 * Change the value of the relay output that is connected to the compressor motor.
119 * This is only intended to be called by the internal polling thread.
120 */
121void Compressor::SetRelayValue(Relay::Value relayValue)
122{
123 m_relay->Set(relayValue);
124}
125
126/**
127 * Get the pressure switch value.
128 * Read the pressure switch digital input.
129 *
130 * @return The current state of the pressure switch.
131 */
132UINT32 Compressor::GetPressureSwitchValue()
133{
134 return m_pressureSwitch->Get();
135}
136
137/**
138 * Start the compressor.
139 * This method will allow the polling loop to actually operate the compressor. The
140 * is stopped by default and won't operate until starting it.
141 */
142void Compressor::Start()
143{
144 m_enabled = true;
145}
146
147/**
148 * Stop the compressor.
149 * This method will stop the compressor from turning on.
150 */
151void Compressor::Stop()
152{
153 m_enabled = false;
154}
155
156/**
157 * Get the state of the enabled flag.
158 * Return the state of the enabled flag for the compressor and pressure switch
159 * combination.
160 *
161 * @return The state of the compressor thread's enable flag.
162 */
163bool Compressor::Enabled()
164{
165 return m_enabled;
166}
167
168void Compressor::UpdateTable() {
169 if (m_table != NULL) {
170 m_table->PutBoolean("Enabled", m_enabled);
171 m_table->PutBoolean("Pressure switch", GetPressureSwitchValue());
172 }
173}
174
175void Compressor::StartLiveWindowMode() {
176
177}
178
179void Compressor::StopLiveWindowMode() {
180
181}
182
183std::string Compressor::GetSmartDashboardType() {
184 return "Compressor";
185}
186
187void Compressor::InitTable(ITable *subTable) {
188 m_table = subTable;
189 UpdateTable();
190}
191
192ITable * Compressor::GetTable() {
193 return m_table;
194}
195