Now you can download a copy of these docs so you can use them offline! Download now
AnalogChannel.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "AnalogChannel.h" 00008 #include "AnalogModule.h" 00009 #include "Resource.h" 00010 #include "WPIErrors.h" 00011 00012 static Resource *channels = NULL; 00013 00014 const UINT8 AnalogChannel::kAccumulatorModuleNumber; 00015 const UINT32 AnalogChannel::kAccumulatorNumChannels; 00016 const UINT32 AnalogChannel::kAccumulatorChannels[] = {1, 2}; 00017 00021 void AnalogChannel::InitChannel(UINT8 moduleNumber, UINT32 channel) 00022 { 00023 char buf[64]; 00024 Resource::CreateResourceObject(&channels, kAnalogModules * kAnalogChannels); 00025 if (!CheckAnalogModule(moduleNumber)) 00026 { 00027 snprintf(buf, 64, "Analog Module %d", moduleNumber); 00028 wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf); 00029 return; 00030 } 00031 if (!CheckAnalogChannel(channel)) 00032 { 00033 snprintf(buf, 64, "Analog Channel %d", channel); 00034 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); 00035 return; 00036 } 00037 00038 snprintf(buf, 64, "Analog Input %d (Module: %d)", channel, moduleNumber); 00039 if (channels->Allocate((moduleNumber - 1) * kAnalogChannels + channel - 1, buf) == ~0ul) 00040 { 00041 CloneError(channels); 00042 return; 00043 } 00044 m_channel = channel; 00045 m_module = AnalogModule::GetInstance(moduleNumber); 00046 if (IsAccumulatorChannel()) 00047 { 00048 tRioStatusCode localStatus = NiFpga_Status_Success; 00049 m_accumulator = tAccumulator::create(channel - 1, &localStatus); 00050 wpi_setError(localStatus); 00051 m_accumulatorOffset=0; 00052 } 00053 else 00054 { 00055 m_accumulator = NULL; 00056 } 00057 } 00058 00065 AnalogChannel::AnalogChannel(UINT8 moduleNumber, UINT32 channel) 00066 { 00067 InitChannel(moduleNumber, channel); 00068 } 00069 00075 AnalogChannel::AnalogChannel(UINT32 channel) 00076 { 00077 InitChannel(GetDefaultAnalogModule(), channel); 00078 } 00079 00083 AnalogChannel::~AnalogChannel() 00084 { 00085 channels->Free((m_module->GetNumber() - 1) * kAnalogChannels + m_channel - 1); 00086 } 00087 00092 AnalogModule *AnalogChannel::GetModule() 00093 { 00094 if (StatusIsFatal()) return NULL; 00095 return m_module; 00096 } 00097 00104 INT16 AnalogChannel::GetValue() 00105 { 00106 if (StatusIsFatal()) return 0; 00107 return m_module->GetValue(m_channel); 00108 } 00109 00119 INT32 AnalogChannel::GetAverageValue() 00120 { 00121 if (StatusIsFatal()) return 0; 00122 return m_module->GetAverageValue(m_channel); 00123 } 00124 00130 float AnalogChannel::GetVoltage() 00131 { 00132 if (StatusIsFatal()) return 0.0f; 00133 return m_module->GetVoltage(m_channel); 00134 } 00135 00143 float AnalogChannel::GetAverageVoltage() 00144 { 00145 if (StatusIsFatal()) return 0.0f; 00146 return m_module->GetAverageVoltage(m_channel); 00147 } 00148 00158 UINT32 AnalogChannel::GetLSBWeight() 00159 { 00160 if (StatusIsFatal()) return 0; 00161 return m_module->GetLSBWeight(m_channel); 00162 } 00163 00173 INT32 AnalogChannel::GetOffset() 00174 { 00175 if (StatusIsFatal()) return 0; 00176 return m_module->GetOffset(m_channel); 00177 } 00178 00183 UINT32 AnalogChannel::GetChannel() 00184 { 00185 if (StatusIsFatal()) return 0; 00186 return m_channel; 00187 } 00188 00193 UINT8 AnalogChannel::GetModuleNumber() 00194 { 00195 if (StatusIsFatal()) return 0; 00196 return m_module->GetNumber(); 00197 } 00198 00207 void AnalogChannel::SetAverageBits(UINT32 bits) 00208 { 00209 if (StatusIsFatal()) return; 00210 m_module->SetAverageBits(m_channel, bits); 00211 } 00212 00220 UINT32 AnalogChannel::GetAverageBits() 00221 { 00222 if (StatusIsFatal()) return 0; 00223 return m_module->GetAverageBits(m_channel); 00224 } 00225 00234 void AnalogChannel::SetOversampleBits(UINT32 bits) 00235 { 00236 if (StatusIsFatal()) return; 00237 m_module->SetOversampleBits(m_channel, bits); 00238 } 00239 00247 UINT32 AnalogChannel::GetOversampleBits() 00248 { 00249 if (StatusIsFatal()) return 0; 00250 return m_module->GetOversampleBits(m_channel); 00251 } 00252 00258 bool AnalogChannel::IsAccumulatorChannel() 00259 { 00260 if (StatusIsFatal()) return false; 00261 if(m_module->GetNumber() != kAccumulatorModuleNumber) return false; 00262 for (UINT32 i=0; i<kAccumulatorNumChannels; i++) 00263 { 00264 if (m_channel == kAccumulatorChannels[i]) return true; 00265 } 00266 return false; 00267 } 00268 00272 void AnalogChannel::InitAccumulator() 00273 { 00274 if (StatusIsFatal()) return; 00275 m_accumulatorOffset = 0; 00276 SetAccumulatorCenter(0); 00277 ResetAccumulator(); 00278 } 00279 00280 00287 void AnalogChannel::SetAccumulatorInitialValue(INT64 initialValue) 00288 { 00289 if (StatusIsFatal()) return; 00290 m_accumulatorOffset = initialValue; 00291 } 00292 00296 void AnalogChannel::ResetAccumulator() 00297 { 00298 if (StatusIsFatal()) return; 00299 if (m_accumulator == NULL) 00300 { 00301 wpi_setWPIError(NullParameter); 00302 return; 00303 } 00304 tRioStatusCode localStatus = NiFpga_Status_Success; 00305 m_accumulator->strobeReset(&localStatus); 00306 wpi_setError(localStatus); 00307 } 00308 00319 void AnalogChannel::SetAccumulatorCenter(INT32 center) 00320 { 00321 if (StatusIsFatal()) return; 00322 if (m_accumulator == NULL) 00323 { 00324 wpi_setWPIError(NullParameter); 00325 return; 00326 } 00327 tRioStatusCode localStatus = NiFpga_Status_Success; 00328 m_accumulator->writeCenter(center, &localStatus); 00329 wpi_setError(localStatus); 00330 } 00331 00335 void AnalogChannel::SetAccumulatorDeadband(INT32 deadband) 00336 { 00337 if (StatusIsFatal()) return; 00338 if (m_accumulator == NULL) 00339 { 00340 wpi_setWPIError(NullParameter); 00341 return; 00342 } 00343 tRioStatusCode localStatus = NiFpga_Status_Success; 00344 m_accumulator->writeDeadband(deadband, &localStatus); 00345 wpi_setError(localStatus); 00346 } 00347 00356 INT64 AnalogChannel::GetAccumulatorValue() 00357 { 00358 if (StatusIsFatal()) return 0; 00359 if (m_accumulator == NULL) 00360 { 00361 wpi_setWPIError(NullParameter); 00362 return 0; 00363 } 00364 tRioStatusCode localStatus = NiFpga_Status_Success; 00365 INT64 value = m_accumulator->readOutput_Value(&localStatus) + m_accumulatorOffset; 00366 wpi_setError(localStatus); 00367 return value; 00368 } 00369 00377 UINT32 AnalogChannel::GetAccumulatorCount() 00378 { 00379 if (StatusIsFatal()) return 0; 00380 if (m_accumulator == NULL) 00381 { 00382 wpi_setWPIError(NullParameter); 00383 return 0; 00384 } 00385 tRioStatusCode localStatus = NiFpga_Status_Success; 00386 UINT32 count = m_accumulator->readOutput_Count(&localStatus); 00387 wpi_setError(localStatus); 00388 return count; 00389 } 00390 00391 00401 void AnalogChannel::GetAccumulatorOutput(INT64 *value, UINT32 *count) 00402 { 00403 if (StatusIsFatal()) return; 00404 if (m_accumulator == NULL) 00405 { 00406 wpi_setWPIError(NullParameter); 00407 return; 00408 } 00409 if (value == NULL || count == NULL) 00410 { 00411 wpi_setWPIError(NullParameter); 00412 return; 00413 } 00414 00415 tRioStatusCode localStatus = NiFpga_Status_Success; 00416 tAccumulator::tOutput output = m_accumulator->readOutput(&localStatus); 00417 *value = output.Value + m_accumulatorOffset; 00418 *count = output.Count; 00419 wpi_setError(localStatus); 00420 } 00421 00427 double AnalogChannel::PIDGet() 00428 { 00429 if (StatusIsFatal()) return 0.0; 00430 return GetAverageValue(); 00431 }
Generated on Thu Jan 12 2012 22:35:17 for WPILibC++ by
1.7.1