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: pymoa.device.Device

Abstract class that represents a multi-channel ADC.

For ADCs whose channels are sampled independently, each should be given their own ADCPort instance. 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_channels indicating whether each corresponding channel is active. Inactive channels are ones that don’t get data.

active_channels is a ListProperty and defaults to None.

bit_depth = 0

The number of bits of raw_data data points. If zero, only data is populated.

bit_depth is a NumericProperty and defaults to 0.

data = None

A list of length num_channels containing 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 data only 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.

data is a ListProperty and defaults to None.

frequency = 0

The frequency at which each ADC channel is sampled.

frequency is a NumericProperty and defaults to 0.0.

num_channels = 1

The number of channels in the ADC.

num_channels is a NumericProperty and defaults to 1.

offset = 0

The offset when converting raw_data to data.

offset is a NumericProperty and defaults to 0.0.

raw_data = None

A list of length num_channels containing the raw data for each channel. The structure is similar to data.

As opposed to data which keeps the data as floats representing the actual signal being sensed, raw_data stores the data as a raw n-bit unsigned integer. Each value in data is derived from an identical element in raw_data using the following formula:

data = raw_data * scale / (2 ^ bit_depth ) - offset.

The reverse conversion: raw_data = (data + offset ) * (2 ^ bit_depth) / scale.

raw_data is a ListProperty and defaults to None.

scale = 1.0

The scale when converting raw_data to data.

scale is a NumericProperty and defaults to 1.0.

ts_idx = None

A list of length num_channels, where each element in the list indicates the index in data and raw_data that is timestamped by timestamp. The timestamp is 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 of timestamp.

ts_idx is a ListProperty and 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: pymoa.device.adc.ADCPort

A virtual implementation of ADCPort.

The class simulates an ADC device with channel data generation. The ADCPort conversion parameters must be initialized and once activated will start updating the channel data using the data_func callback.

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_data values are computed from it.

Parameters
  • sample

  • channel

Returns