ADC Device
ADC implementation of Device.
- class pymoa.device.adc.ADCPort(active_channels=None, num_channels=1, bit_depth=0, scale=1.0, offset=0, frequency=0, **kwargs)
Bases:
DeviceAbstract class that represents a multi-channel ADC.
For ADCs whose channels are sampled independently, each should be given their own
ADCPortinstance. It typically only makes sense to bundle multiple channels in one instance if they are sampled synchronously.- active_channels = None
A list of booleans with length
num_channelsindicating whether each corresponding channel is active. Inactive channels are ones that don’t get data.active_channelsis aListPropertyand defaults to None.
- bit_depth = 0
The number of bits of
raw_datadata points. If zero, onlydatais populated.bit_depthis aNumericPropertyand defaults to 0.
- data = None
A list of length
num_channelscontaining the properly rescaled floating point data for each channel.Each element in the list is a list type containing the most recent data read by the ADC. Rather than appending new data to old data, each new data slice read replaces the previously read data so that
dataonly contains the most recently read data for each channel. Channels that are not read at a particular update will be represented by a empty list type.datais aListPropertyand defaults to None.
- frequency = 0
The frequency at which each ADC channel is sampled.
frequencyis aNumericPropertyand defaults to 0.0.
- num_channels = 1
The number of channels in the ADC.
num_channelsis aNumericPropertyand defaults to 1.
- offset = 0
The offset when converting
raw_datatodata.offsetis aNumericPropertyand defaults to 0.0.
- raw_data = None
A list of length
num_channelscontaining the raw data for each channel. The structure is similar todata.As opposed to
datawhich keeps the data as floats representing the actual signal being sensed,raw_datastores the data as a raw n-bit unsigned integer. Each value indatais derived from an identical element inraw_datausing the following formula:data=raw_data*scale/ (2 ^bit_depth) -offset.The reverse conversion:
raw_data= (data+offset) * (2 ^bit_depth) /scale.raw_datais aListPropertyand defaults to None.
- scale = 1.0
The scale when converting
raw_datatodata.scaleis aNumericPropertyand defaults to 1.0.
- ts_idx = None
A list of length
num_channels, where each element in the list indicates the index indataandraw_datathat is timestamped bytimestamp. Thetimestampis the time of a data point for each channel. This data point can be different for each channel, so the index indicates the corresponding data point read at the time oftimestamp.ts_idxis aListPropertyand defaults to None.
- class pymoa.device.adc.VirtualADCPort(active_channels=None, num_channels=1, bit_depth=0, scale=1.0, offset=0, frequency=0, **kwargs)
Bases:
ADCPortA virtual implementation of
ADCPort.The class simulates an ADC device with channel data generation. The
ADCPortconversion parameters must be initialized and once activated will start updating the channel data using thedata_funccallback.For example:
>>> from pymoa.device.adc import VirtualADCPort >>> from math import cos, pi >>> class ADC(VirtualADCPort): ... def __init__(self, **kwargs): ... super(ADC, self).__init__(**kwargs) ... self.scale = 10 ... self.offset = 5 ... ... def next_point(idx, channel): ... rate = 1 ... if channel == 0: ... return .2 * cos(2 * pi * idx * rate / float(self.frequency)) ... return 4 * cos(2 * pi * idx * rate / float(self.frequency)) ... self.data_func = next_point ... ... def on_data_update(self, *largs): ... print('Data ({:.2f}): {}'.format(self.timestamp, self.data)) ... print('Raw data ({:.2f}): {}'.format(self.timestamp, self.raw_data)) >>> adc = ADC(num_channels=2, active_channels=[True, True], data_size=2, frequency=4, bit_depth=16) >>> adc.activate(adc) Data (1.26): [[0.2, 0.0], [4.0, 0.0]] Raw data (1.26): [[34078, 32768], [58982, 32768]] Data (1.52): [[-0.2, 0.0], [-4.0, 0.0]] Raw data (1.52): [[31457, 32768], [6553, 32767]] Data (1.77): [[0.2, 0.0], [4.0, 0.0]] Raw data (1.77): [[34078, 32768], [58982, 32768]] ...
- data_func(sample, channel)
A callback that we call when we need a new data point. The callback takes two parameters; a index parameter which is the index of the data point requested, and the channel number for which the data is requested. The function returns the generated sample value for that index and channel.
Note
The data returned is for
ADCPort.data,ADCPort.raw_datavalues are computed from it.- Parameters:
sample
channel
- Returns: