brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 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 | */ |
| 21 | static 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 | */ |
Brian Silverman | 6874947 | 2014-01-05 14:50:00 -0800 | [diff] [blame] | 45 | void Compressor::InitCompressor(uint8_t pressureSwitchModuleNumber, |
| 46 | uint32_t pressureSwitchChannel, |
| 47 | uint8_t compresssorRelayModuleNumber, |
| 48 | uint32_t compressorRelayChannel) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 49 | { |
Brian Silverman | 6874947 | 2014-01-05 14:50:00 -0800 | [diff] [blame] | 50 | m_table = NULL; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 51 | 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 | |
Brian Silverman | 6874947 | 2014-01-05 14:50:00 -0800 | [diff] [blame] | 57 | if (!m_task.Start((int32_t)this)) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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 | */ |
Brian Silverman | 6874947 | 2014-01-05 14:50:00 -0800 | [diff] [blame] | 74 | Compressor::Compressor(uint8_t pressureSwitchModuleNumber, |
| 75 | uint32_t pressureSwitchChannel, |
| 76 | uint8_t compresssorRelayModuleNumber, |
| 77 | uint32_t compressorRelayChannel) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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 | */ |
Brian Silverman | 6874947 | 2014-01-05 14:50:00 -0800 | [diff] [blame] | 96 | Compressor::Compressor(uint32_t pressureSwitchChannel, uint32_t compressorRelayChannel) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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 | */ |
| 110 | Compressor::~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 | */ |
| 121 | void 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 | */ |
Brian Silverman | 6874947 | 2014-01-05 14:50:00 -0800 | [diff] [blame] | 132 | uint32_t Compressor::GetPressureSwitchValue() |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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 | */ |
| 142 | void 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 | */ |
| 151 | void 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 | */ |
| 163 | bool Compressor::Enabled() |
| 164 | { |
| 165 | return m_enabled; |
| 166 | } |
| 167 | |
brians | ab45cad | 2013-03-03 05:31:33 +0000 | [diff] [blame] | 168 | void Compressor::UpdateTable() { |
| 169 | if (m_table != NULL) { |
| 170 | m_table->PutBoolean("Enabled", m_enabled); |
| 171 | m_table->PutBoolean("Pressure switch", GetPressureSwitchValue()); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void Compressor::StartLiveWindowMode() { |
| 176 | |
| 177 | } |
| 178 | |
| 179 | void Compressor::StopLiveWindowMode() { |
| 180 | |
| 181 | } |
| 182 | |
| 183 | std::string Compressor::GetSmartDashboardType() { |
| 184 | return "Compressor"; |
| 185 | } |
| 186 | |
| 187 | void Compressor::InitTable(ITable *subTable) { |
| 188 | m_table = subTable; |
| 189 | UpdateTable(); |
| 190 | } |
| 191 | |
| 192 | ITable * Compressor::GetTable() { |
| 193 | return m_table; |
| 194 | } |
| 195 | |