Now you can download a copy of these docs so you can use them offline! Download now
AnalogChannel.cpp
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 "AnalogChannel.h"
8 #include "AnalogModule.h"
9 #include "NetworkCommunication/UsageReporting.h"
10 #include "Resource.h"
11 #include "WPIErrors.h"
12 #include "LiveWindow/LiveWindow.h"
13 
14 static Resource *channels = NULL;
15 
16 const uint8_t AnalogChannel::kAccumulatorModuleNumber;
17 const uint32_t AnalogChannel::kAccumulatorNumChannels;
18 const uint32_t AnalogChannel::kAccumulatorChannels[] = {1, 2};
19 
23 void AnalogChannel::InitChannel(uint8_t moduleNumber, uint32_t channel)
24 {
25  m_table = NULL;
26  char buf[64];
27  Resource::CreateResourceObject(&channels, kAnalogModules * kAnalogChannels);
28  if (!CheckAnalogModule(moduleNumber))
29  {
30  snprintf(buf, 64, "Analog Module %d", moduleNumber);
31  wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf);
32  return;
33  }
34  if (!CheckAnalogChannel(channel))
35  {
36  snprintf(buf, 64, "Analog Channel %d", channel);
37  wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
38  return;
39  }
40 
41  snprintf(buf, 64, "Analog Input %d (Module: %d)", channel, moduleNumber);
42  if (channels->Allocate((moduleNumber - 1) * kAnalogChannels + channel - 1, buf) == ~0ul)
43  {
44  CloneError(channels);
45  return;
46  }
47  m_channel = channel;
48  m_module = AnalogModule::GetInstance(moduleNumber);
50  {
51  tRioStatusCode localStatus = NiFpga_Status_Success;
52  m_accumulator = tAccumulator::create(channel - 1, &localStatus);
53  wpi_setError(localStatus);
54  m_accumulatorOffset=0;
55  }
56  else
57  {
58  m_accumulator = NULL;
59  }
60  m_shouldUseVoltageForPID = false;
61  LiveWindow::GetInstance()->AddSensor("AnalogChannel",channel, GetModuleNumber(), this);
62  nUsageReporting::report(nUsageReporting::kResourceType_AnalogChannel, channel, GetModuleNumber() - 1);
63 }
64 
71 AnalogChannel::AnalogChannel(uint8_t moduleNumber, uint32_t channel)
72 {
73  InitChannel(moduleNumber, channel);
74 }
75 
82 {
83  InitChannel(GetDefaultAnalogModule(), channel);
84 }
85 
90 {
91  channels->Free((m_module->GetNumber() - 1) * kAnalogChannels + m_channel - 1);
92 }
93 
99 {
100  if (StatusIsFatal()) return NULL;
101  return m_module;
102 }
103 
111 {
112  if (StatusIsFatal()) return 0;
113  return m_module->GetValue(m_channel);
114 }
115 
126 {
127  if (StatusIsFatal()) return 0;
128  return m_module->GetAverageValue(m_channel);
129 }
130 
137 {
138  if (StatusIsFatal()) return 0.0f;
139  return m_module->GetVoltage(m_channel);
140 }
141 
150 {
151  if (StatusIsFatal()) return 0.0f;
152  return m_module->GetAverageVoltage(m_channel);
153 }
154 
165 {
166  if (StatusIsFatal()) return 0;
167  return m_module->GetLSBWeight(m_channel);
168 }
169 
180 {
181  if (StatusIsFatal()) return 0;
182  return m_module->GetOffset(m_channel);
183 }
184 
190 {
191  if (StatusIsFatal()) return 0;
192  return m_channel;
193 }
194 
200 {
201  if (StatusIsFatal()) return 0;
202  return m_module->GetNumber();
203 }
204 
213 void AnalogChannel::SetAverageBits(uint32_t bits)
214 {
215  if (StatusIsFatal()) return;
216  m_module->SetAverageBits(m_channel, bits);
217 }
218 
227 {
228  if (StatusIsFatal()) return 0;
229  return m_module->GetAverageBits(m_channel);
230 }
231 
241 {
242  if (StatusIsFatal()) return;
243  m_module->SetOversampleBits(m_channel, bits);
244 }
245 
254 {
255  if (StatusIsFatal()) return 0;
256  return m_module->GetOversampleBits(m_channel);
257 }
258 
265 {
266  if (StatusIsFatal()) return false;
267  if(m_module->GetNumber() != kAccumulatorModuleNumber) return false;
268  for (uint32_t i=0; i<kAccumulatorNumChannels; i++)
269  {
270  if (m_channel == kAccumulatorChannels[i]) return true;
271  }
272  return false;
273 }
274 
279 {
280  if (StatusIsFatal()) return;
281  m_accumulatorOffset = 0;
284 }
285 
286 
294 {
295  if (StatusIsFatal()) return;
296  m_accumulatorOffset = initialValue;
297 }
298 
303 {
304  if (StatusIsFatal()) return;
305  if (m_accumulator == NULL)
306  {
307  wpi_setWPIError(NullParameter);
308  return;
309  }
310  tRioStatusCode localStatus = NiFpga_Status_Success;
311  m_accumulator->strobeReset(&localStatus);
312  wpi_setError(localStatus);
313 }
314 
326 {
327  if (StatusIsFatal()) return;
328  if (m_accumulator == NULL)
329  {
330  wpi_setWPIError(NullParameter);
331  return;
332  }
333  tRioStatusCode localStatus = NiFpga_Status_Success;
334  m_accumulator->writeCenter(center, &localStatus);
335  wpi_setError(localStatus);
336 }
337 
342 {
343  if (StatusIsFatal()) return;
344  if (m_accumulator == NULL)
345  {
346  wpi_setWPIError(NullParameter);
347  return;
348  }
349  tRioStatusCode localStatus = NiFpga_Status_Success;
350  m_accumulator->writeDeadband(deadband, &localStatus);
351  wpi_setError(localStatus);
352 }
353 
363 {
364  if (StatusIsFatal()) return 0;
365  if (m_accumulator == NULL)
366  {
367  wpi_setWPIError(NullParameter);
368  return 0;
369  }
370  tRioStatusCode localStatus = NiFpga_Status_Success;
371  INT64 value = m_accumulator->readOutput_Value(&localStatus) + m_accumulatorOffset;
372  wpi_setError(localStatus);
373  return value;
374 }
375 
384 {
385  if (StatusIsFatal()) return 0;
386  if (m_accumulator == NULL)
387  {
388  wpi_setWPIError(NullParameter);
389  return 0;
390  }
391  tRioStatusCode localStatus = NiFpga_Status_Success;
392  uint32_t count = m_accumulator->readOutput_Count(&localStatus);
393  wpi_setError(localStatus);
394  return count;
395 }
396 
397 
407 void AnalogChannel::GetAccumulatorOutput(INT64 *value, uint32_t *count)
408 {
409  if (StatusIsFatal()) return;
410  if (m_accumulator == NULL)
411  {
412  wpi_setWPIError(NullParameter);
413  return;
414  }
415  if (value == NULL || count == NULL)
416  {
417  wpi_setWPIError(NullParameter);
418  return;
419  }
420 
421  tRioStatusCode localStatus = NiFpga_Status_Success;
422  tAccumulator::tOutput output = m_accumulator->readOutput(&localStatus);
423  *value = output.Value + m_accumulatorOffset;
424  *count = output.Count;
425  wpi_setError(localStatus);
426 }
427 
436 void AnalogChannel::SetVoltageForPID(bool shouldUseVoltageForPID) {
437  m_shouldUseVoltageForPID = shouldUseVoltageForPID;
438 }
439 
446 {
447  if (StatusIsFatal()) return 0.0;
448  if (m_shouldUseVoltageForPID) {
449  return GetAverageVoltage();
450  } else {
451  return GetAverageValue();
452  }
453 }
454 
456  if (m_table != NULL) {
457  m_table->PutNumber("Value", GetAverageVoltage());
458  }
459 }
460 
462 
463 }
464 
466 
467 }
468 
470  return "Analog Input";
471 }
472 
474  m_table = subTable;
475  UpdateTable();
476 }
477 
479  return m_table;
480 }
481 
482 
void StartLiveWindowMode()
int32_t GetAverageValue()
void GetAccumulatorOutput(INT64 *value, uint32_t *count)
uint32_t GetChannel()
void InitTable(ITable *subTable)
static bool CheckAnalogModule(uint8_t moduleNumber)
Definition: SensorBase.cpp:74
int32_t GetOffset(uint32_t channel)
void StopLiveWindowMode()
virtual void PutNumber(std::string key, double value)=0
AnalogChannel(uint8_t moduleNumber, uint32_t channel)
void SetAccumulatorDeadband(int32_t deadband)
void SetAccumulatorInitialValue(INT64 value)
Definition: ITable.h:26
uint8_t GetModuleNumber()
uint32_t GetOversampleBits()
int32_t GetOffset()
static AnalogModule * GetInstance(uint8_t moduleNumber)
INT64 GetAccumulatorValue()
void SetVoltageForPID(bool shouldUseVoltageForPID)
int16_t GetValue()
void SetOversampleBits(uint32_t channel, uint32_t bits)
void AddSensor(const char *subsystem, const char *name, LiveWindowSendable *component)
Definition: LiveWindow.cpp:82
int16_t GetValue(uint32_t channel)
float GetAverageVoltage()
float GetAverageVoltage(uint32_t channel)
AnalogModule * GetModule()
bool IsAccumulatorChannel()
uint32_t GetAccumulatorCount()
void Free(uint32_t index)
Definition: Resource.cpp:105
std::string GetSmartDashboardType()
uint32_t Allocate(const char *resourceDesc)
Definition: Resource.cpp:62
void ResetAccumulator()
ITable * GetTable()
virtual ~AnalogChannel()
uint32_t GetLSBWeight(uint32_t channel)
static LiveWindow * GetInstance()
Definition: LiveWindow.cpp:13
uint32_t GetOversampleBits(uint32_t channel)
void SetAverageBits(uint32_t channel, uint32_t bits)
void InitAccumulator()
uint32_t GetAverageBits()
virtual bool StatusIsFatal() const
Check if the current error code represents a fatal error.
Definition: ErrorBase.cpp:178
void SetAccumulatorCenter(int32_t center)
static void CreateResourceObject(Resource **r, uint32_t elements)
Definition: Resource.cpp:39
uint32_t GetLSBWeight()
void SetAverageBits(uint32_t bits)
int32_t GetAverageValue(uint32_t channel)
uint32_t GetAverageBits(uint32_t channel)
float GetVoltage(uint32_t channel)
static bool CheckAnalogChannel(uint32_t channel)
Definition: SensorBase.cpp:174
void SetOversampleBits(uint32_t bits)

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6