-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalogChannel.cpp
384 lines (351 loc) · 10.7 KB
/
AnalogChannel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "AnalogChannel.h"
#include "AnalogModule.h"
#include "Resource.h"
#include "Utility.h"
#include "WPIStatus.h"
static Resource *channels = NULL;
const UINT32 AnalogChannel::kAccumulatorChannels[] = {1, 2};
/**
* Common initialization.
*/
void AnalogChannel::InitChannel(UINT32 slot, UINT32 channel)
{
Resource::CreateResourceObject(&channels, kAnalogModules * kAnalogChannels);
CheckAnalogModule(slot);
CheckAnalogChannel(slot);
channels->Allocate(AnalogModule::SlotToIndex(slot) * kAnalogModules + channel - 1);
m_channel = channel;
m_module = AnalogModule::GetInstance(slot);
if (IsAccumulatorChannel())
{
m_accumulator = new tAccumulator(channel - 1, &status);
m_accumulatorOffset=0;
}
else
{
m_accumulator = NULL;
}
}
/**
* Construct an analog channel on a specified module.
*
* @param slot The slot that the analog module is plugged into.
* @param channel The channel number to represent.
*/
AnalogChannel::AnalogChannel(UINT32 slot, UINT32 channel)
{
InitChannel(slot, channel);
}
/**
* Construct an analog channel on the default module.
*
* @param channel The channel number to represent.
*/
AnalogChannel::AnalogChannel(UINT32 channel)
{
InitChannel(GetDefaultAnalogModule(), channel);
}
/**
* Channel destructor.
*/
AnalogChannel::~AnalogChannel()
{
channels->Free(AnalogModule::SlotToIndex(GetSlot()) * kAnalogModules + m_channel - 1);
}
/**
* Get the analog module that this channel is on.
* @return A pointer to the AnalogModule that this channel is on.
*/
AnalogModule *AnalogChannel::GetModule()
{
return m_module;
}
/**
* Get a sample straight from this channel on the module.
* The sample is a 12-bit value representing the -10V to 10V range of the A/D converter in the module.
* The units are in A/D converter codes. Use GetVoltage() to get the analog value in calibrated units.
* @return A sample straight from this channel on the module.
*/
INT16 AnalogChannel::GetValue()
{
return m_module->GetValue(m_channel);
}
/**
* Get a sample from the output of the oversample and average engine for this channel.
* The sample is 12-bit + the value configured in SetOversampleBits().
* The value configured in SetAverageBits() will cause this value to be averaged 2**bits number of samples.
* This is not a sliding window. The sample will not change until 2**(OversamplBits + AverageBits) samples
* have been acquired from the module on this channel.
* Use GetAverageVoltage() to get the analog value in calibrated units.
* @return A sample from the oversample and average engine for this channel.
*/
INT32 AnalogChannel::GetAverageValue()
{
return m_module->GetAverageValue(m_channel);
}
/**
* Get a scaled sample straight from this channel on the module.
* The value is scaled to units of Volts using the calibrated scaling data from GetLSBWeight() and GetOffset().
* @return A scaled sample straight from this channel on the module.
*/
float AnalogChannel::GetVoltage()
{
return m_module->GetVoltage(m_channel);
}
/**
* Get a scaled sample from the output of the oversample and average engine for this channel.
* The value is scaled to units of Volts using the calibrated scaling data from GetLSBWeight() and GetOffset().
* Using oversampling will cause this value to be higher resolution, but it will update more slowly.
* Using averaging will cause this value to be more stable, but it will update more slowly.
* @return A scaled sample from the output of the oversample and average engine for this channel.
*/
float AnalogChannel::GetAverageVoltage()
{
return m_module->GetAverageVoltage(m_channel);
}
/**
* Get the factory scaling least significant bit weight constant.
* The least significant bit weight constant for the channel that was calibrated in
* manufacturing and stored in an eeprom in the module.
*
* Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
*
* @return Least significant bit weight.
*/
UINT32 AnalogChannel::GetLSBWeight()
{
return m_module->GetLSBWeight(m_channel);
}
/**
* Get the factory scaling offset constant.
* The offset constant for the channel that was calibrated in manufacturing and stored
* in an eeprom in the module.
*
* Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
*
* @return Offset constant.
*/
INT32 AnalogChannel::GetOffset()
{
return m_module->GetOffset(m_channel);
}
/**
* Get the channel number.
* @return The channel number.
*/
UINT32 AnalogChannel::GetChannel()
{
return m_channel;
}
/**
* Get the slot that the analog module is plugged into.
* @return The slot that the analog module is plugged into.
*/
UINT32 AnalogChannel::GetSlot()
{
return m_module->GetSlot();
}
/**
* Set the number of averaging bits.
* This sets the number of averaging bits. The actual number of averaged samples is 2**bits.
* Use averaging to improve the stability of your measurement at the expense of sampling rate.
* The averaging is done automatically in the FPGA.
*
* @param bits Number of bits of averaging.
*/
void AnalogChannel::SetAverageBits(UINT32 bits)
{
m_module->SetAverageBits(m_channel, bits);
}
/**
* Get the number of averaging bits previously configured.
* This gets the number of averaging bits from the FPGA. The actual number of averaged samples is 2**bits.
* The averaging is done automatically in the FPGA.
*
* @return Number of bits of averaging previously configured.
*/
UINT32 AnalogChannel::GetAverageBits()
{
return m_module->GetAverageBits(m_channel);
}
/**
* Set the number of oversample bits.
* This sets the number of oversample bits. The actual number of oversampled values is 2**bits.
* Use oversampling to improve the resolution of your measurements at the expense of sampling rate.
* The oversampling is done automatically in the FPGA.
*
* @param bits Number of bits of oversampling.
*/
void AnalogChannel::SetOversampleBits(UINT32 bits)
{
m_module->SetOversampleBits(m_channel, bits);
}
/**
* Get the number of oversample bits previously configured.
* This gets the number of oversample bits from the FPGA. The actual number of oversampled values is
* 2**bits. The oversampling is done automatically in the FPGA.
*
* @return Number of bits of oversampling previously configured.
*/
UINT32 AnalogChannel::GetOversampleBits()
{
return m_module->GetOversampleBits(m_channel);
}
/**
* Is the channel attached to an accumulator.
*
* @return The analog channel is attached to an accumulator.
*/
bool AnalogChannel::IsAccumulatorChannel()
{
if(m_module->GetSlot() != kAccumulatorSlot) return false;
for (UINT32 i=0; i<kAccumulatorNumChannels; i++)
{
if (m_channel == kAccumulatorChannels[i]) return true;
}
return false;
}
/**
* Initialize the accumulator.
*/
void AnalogChannel::InitAccumulator()
{
m_accumulatorOffset = 0;
SetAccumulatorCenter(0);
ResetAccumulator();
}
/**
* Set an inital value for the accumulator.
*
* This will be added to all values returned to the user.
* @param initialValue The value that the accumulator should start from when reset.
*/
void AnalogChannel::SetAccumulatorInitialValue(INT64 initialValue)
{
m_accumulatorOffset = initialValue;
}
/**
* Resets the accumulator to the initial value.
*/
void AnalogChannel::ResetAccumulator()
{
if (m_accumulator == NULL)
{
wpi_fatal(NullParameter);
return;
}
m_accumulator->strobeReset(&status);
wpi_assertCleanStatus(status);
}
/**
* Set the center value of the accumulator.
*
* The center value is subtracted from each A/D value before it is added to the accumulator. This
* is used for the center value of devices like gyros and accelerometers to make integration work
* and to take the device offset into account when integrating.
*
* This center value is based on the output of the oversampled and averaged source from channel 1.
* Because of this, any non-zero oversample bits will affect the size of the value for this field.
*/
void AnalogChannel::SetAccumulatorCenter(INT32 center)
{
if (m_accumulator == NULL)
{
wpi_fatal(NullParameter);
return;
}
m_accumulator->writeCenter(center, &status);
wpi_assertCleanStatus(status);
}
/**
* Set the accumulator's deadband.
*/
void AnalogChannel::SetAccumulatorDeadband(INT32 deadband)
{
if (m_accumulator == NULL)
{
wpi_fatal(NullParameter);
return;
}
m_accumulator->writeDeadband(deadband, &status);
wpi_assertCleanStatus(status);
}
/**
* Read the accumulated value.
*
* Read the value that has been accumulating on channel 1.
* The accumulator is attached after the oversample and average engine.
*
* @return The 64-bit value accumulated since the last Reset().
*/
INT64 AnalogChannel::GetAccumulatorValue()
{
if (m_accumulator == NULL)
{
wpi_fatal(NullParameter);
return 0;
}
INT64 value = m_accumulator->readOutput_Value(&status) + m_accumulatorOffset;
wpi_assertCleanStatus(status);
return value;
}
/**
* Read the number of accumulated values.
*
* Read the count of the accumulated values since the accumulator was last Reset().
*
* @return The number of times samples from the channel were accumulated.
*/
UINT32 AnalogChannel::GetAccumulatorCount()
{
status = 0;
if (m_accumulator == NULL)
{
wpi_fatal(NullParameter);
return 0;
}
UINT32 count = m_accumulator->readOutput_Count(&status);
wpi_assertCleanStatus(status);
return count;
}
/**
* Read the accumulated value and the number of accumulated values atomically.
*
* This function reads the value and count from the FPGA atomically.
* This can be used for averaging.
*
* @param value Pointer to the 64-bit accumulated output.
* @param count Pointer to the number of accumulation cycles.
*/
void AnalogChannel::GetAccumulatorOutput(INT64 *value, UINT32 *count)
{
status = 0;
if (m_accumulator == NULL)
{
wpi_fatal(NullParameter);
return;
}
if (value == NULL || count == NULL)
{
wpi_fatal(NullParameter);
return;
}
tAccumulator::tOutput output = m_accumulator->readOutput(&status);
*value = output.Value + m_accumulatorOffset;
*count = output.Count;
wpi_assertCleanStatus(status);
}
/**
* Get the Average voltage for the PID Source base object.
*
* @return The average voltage.
*/
double AnalogChannel::PIDGet()
{
return GetAverageValue();
}