In / Out
ADS1115
- class crappy.inout.ads1115.Ads1115(backend: str, device_address: int = 72, i2c_port: int = 1, sample_rate: int = 128, v_range: float = 2.048, multiplexer: str = 'A1', dry_pin: str | int | None = None, gain: float = 1, offset: float = 0, ft232h_ser_num: str | None = None)[source]
A class for controlling Adafruit’s ADS1115 16-bits ADC.
The Ads1115 InOut block is meant for reading output values from a 16-bits ADS1115 ADC, using the I2C protocol. The output is in Volts by default, but a
gain
and anoffset
can be specified.- __init__(backend: str, device_address: int = 72, i2c_port: int = 1, sample_rate: int = 128, v_range: float = 2.048, multiplexer: str = 'A1', dry_pin: str | int | None = None, gain: float = 1, offset: float = 0, ft232h_ser_num: str | None = None) None [source]
Checks arguments validity.
- Parameters:
backend (
str
) –The backend for communicating with the ADS1115. Should be one of:
'Pi4', 'ft232h', 'blinka'
The ‘Pi4’ backend is optimized but only works on boards supporting the
smbus2
module, like the Raspberry Pis. The ‘blinka’ backend may be less performant and requires installing Adafruit’s modules, but these modules are compatible with and maintained on a wide variety of boards. The ‘ft232h’ backend allows controlling the ADS1115 from a PC using Adafruit’s FT232H USB to I2C adapter. See Crappy for embedded hardware for details.device_address (
int
, optional) – The I2C address of the ADS1115. The default address is 0x48, but it is possible to change this setting using the ADDR pin.i2c_port (
int
, optional) – The I2C port over which the ADS1115 should communicate. On most Raspberry Pi models the default I2C port is 1.sample_rate (
int
, optional) –The sample rate for data conversion (in SPS). Available sample rates are:
8, 16, 32, 64, 128, 250, 475, 860
v_range (
float
, optional) –The value (in Volts) of the measured signal corresponding to the 0x7FFF output in bits, i.e. that saturates the sensor. A signal of
-v_range
Volts gives a 0x8000 output in bits. Availablev_range
values are:0.256, 0.512, 1.024, 2.048, 4.096, 6.144
multiplexer (
str
, optional) –Choice of the inputs to consider. Single-input modes actually measure Ax - GND. The available
multiplexer
values are:'A0', 'A1', 'A2', 'A3', 'A0 - A1', 'A0 - A3', 'A1 - A3', 'A2 - A3'
dry_pin (
int
orstr
, optional) – Optionally, reads the end of conversion signal from a GPIO rather than from an I2C message. Speeds up the reading and decreases the traffic on the bus, but requires one extra wire. With the backend ‘Pi4’, give the index of the GPIO in BCM convention. With the ‘ft232h’ backend, give the name of the GPIO in the format Dx or Cx. This feature is not available with the ‘blinka’ backend.gain (
float
, optional) –Allows to tune the output value according to the formula:
output = gain * tension + offset.
offset (
float
, optional) –Allows to tune the output value according to the formula:
output = gain * tension + offset.
ft232h_ser_num (
str
, optional) – If backend is ‘ft232h’, the serial number of the FT232H to use for communication.
Warning
AINx voltages should not be higher than VDD+0.3V nor lower than GND-0.3V. Setting high
v_range
values does not allow measuring voltages higher than VDD !!
Agilent 34420A
- class crappy.inout.agilent34420A.Agilent34420a(mode: bytes = b'VOLT', device: str = '/dev/ttyUSB0', baudrate: int = 9600, timeout: float = 1)[source]
Sensor class for Agilent34420A devices.
This class contains method to measure values of resistance or voltage on Agilent34420A devices.
Note
May work for other devices too, but not tested.
If you have issues with this class returning a lot of ‘bad serial’, make sure you have the last version of
serial
.- __init__(mode: bytes = b'VOLT', device: str = '/dev/ttyUSB0', baudrate: int = 9600, timeout: float = 1) None [source]
Sets the args and initializes parent class.
Comedi
- class crappy.inout.comedi.Comedi(device: str = '/dev/comedi0', subdevice: int = 0, channels: List[int] | None = None, range_num: List[int] | None = None, gain: List[float] | None = None, offset: List[float] | None = None, make_zero: List[bool] | None = None, out_subdevice: int = 1, out_channels: List[int] | None = None, out_range_num: List[int] | None = None, out_gain: List[float] | None = None, out_offset: List[float] | None = None)[source]
This class can control acquisition boards relying on the Comedi driver.
It can read data from ADCs on input channels, and set voltages of DACs on output channels. Each channel can be tuned independently in terms of range, gan, offset, and for input channels it’s possible to decide whether they should be offset to 0 at the beginning of the test.
- __init__(device: str = '/dev/comedi0', subdevice: int = 0, channels: List[int] | None = None, range_num: List[int] | None = None, gain: List[float] | None = None, offset: List[float] | None = None, make_zero: List[bool] | None = None, out_subdevice: int = 1, out_channels: List[int] | None = None, out_range_num: List[int] | None = None, out_gain: List[float] | None = None, out_offset: List[float] | None = None) None [source]
Sets the args and initializes the parent class.
- Parameters:
device – The address of the device, as a
str
.subdevice – The id of the subdevice to use for input channels, as an
int
.channels – A
list
containing the indexes of the channels to use as inputs, given asint
.range_num – A
list
containing for each input channel the index of the range to set for that channel, as anint
. Refer to the documentation of the board to get the correspondence between range indexes and Volts. If not given, all input channels will be set to the range 0.gain –
A
list
containing for each input channel the gain to apply to the measured voltage, as afloat
. The returned voltage is calculated as follows :returned_voltage = gain * measured_voltage + offset
If not given, no gain is applied to the measured values.
offset –
A
list
containing for each input channel the offset to apply to the measured voltage, as afloat
. The returned voltage is calculated as follows :returned_voltage = gain * measured_voltage + offset
If not given, no offset is applied to the measured values.
make_zero – A
list
containing for each input channel abool
indicating whether the channel should be zeroed or not. If so, data will be acquired on this channel before the test starts, and a compensation value will be deduced so that the offset of this channel is 0. It will only take effect if the ``make_zero_delay`` argument of the :ref:`IOBlock` controlling the Comedi is set ! If not given, the channels are by default not zeroed.out_subdevice – The id of the subdevice to use for output channels, as an
int
.out_channels – A
list
containing the indexes of the channels to use as outputs, given asint
.out_range_num – A
list
containing for each output channel the index of the range to set for that channel, as anint
. Refer to the documentation of the board to get the correspondence between range indexes and Volts. If not given, all output channels will be set to the range 0.out_gain –
A
list
containing for each output channel the gain to apply to the command voltage, as afloat
. The set voltage is calculated as follows :set_voltage = out_gain * command_voltage + out_offset
If not given, no gain is applied to the command values.
out_offset –
A
list
containing for each output channel the offset to apply to the command voltage, as afloat
. The set voltage is calculated as follows :set_voltage = out_gain * command_voltage + out_offset
If not given, no offset is applied to the command values.
Note
All the
list
given as arguments for the input channels should have the same length, and same for the output channels. If that’s not the case, all the given lists are treated as if they had the same length as the shortest given list.
- get_data() List[float] [source]
Simply reads and returns the value of each channel, adjusted with the given gain and offset.
DaQMX
Fake Inout
GPIO PWM
- class crappy.inout.gpio_pwm.Gpio_pwm(pin_out: int, duty_cycle: float | None = None, frequency: float | None = None)[source]
Class for performing PWM on a Raspberry Pi.
The Gpio_pwn InOut block is meant for controlling a PWM output from a Raspberry Pi GPIO. It allows to control the duty cycle, the frequency, or both at the same time. When controlling both, the duty cycle should be first and the frequency second in the list of inputs.
Warning
Only works on Raspberry Pi !
- __init__(pin_out: int, duty_cycle: float | None = None, frequency: float | None = None) None [source]
Checks the arguments validity.
- Parameters:
pin_out (
int
) – The GPIO pin to be controlled (BCM convention).duty_cycle (
float
) – If provided, sets a fixed duty cycle for the entire assay. Only the frequency can then be tuned. If not provided, the block will expect the duty cycle values to be given as the first input. It will also start the PWM with a duty cycle of 0% before the first value is received and set.frequency (
float
) – If provided, sets a fixed PWM frequency for the entire assay. Only the duty cycle can then be tuned. If not provided, the block will expect the frequency values to be given as the first input if theduty_cycle
argument is provided, or else as the second input. It will also start the PWM with a frequency of 10kHz before the first value is received and set.
Note
duty_cycle
: The duty cycle inputs are clamped between 0 and 100.frequency
: The frequency inputs are clamped between 10Hz and 1MhZ. Sending other values to the bloc doesn’t raise any error, but the assay may not run as expected.Hardware PWM pins: On the Raspberry Pi 4, only the GPIO pins 12, 13, 18 and 19 support hardware PWM. Trying to get a PWM output from other pins might work but may decrease the available frequency range.
GPIO switch
- class crappy.inout.gpio_switch.Gpio_switch(pin_out: int | str, backend: str, ft232h_ser_num: int | None = None)[source]
Class for setting a GPIO high or low.
The Gpio_switch InOut block is meant for switching a GPIO high or low according to the input signal value. When the input signal is 1 the GPIO is turned high, when the signal is 0 it is turned low. Any value other than 0 and 1 raises an error.
- __init__(pin_out: int | str, backend: str, ft232h_ser_num: int | None = None) None [source]
Checks the argument validity.
- Parameters:
pin_out – The GPIO pin to be controlled. On Raspberry Pi, should be an integer corresponding to a GPIO in BCM convention. On FT232H, should be a string corresponding to the name of a GPIO. With the ‘blinka’ backend, should be a string holding the name of the pin. Refer to blinka’s specific documentation for each board for more information.
backend –
Should be one of :
'Pi4', 'blinka', 'ft232h'
The ‘Pi4’ backend only works on the Raspberry Pis. The ‘blinka’ backend requires installing Adafruit’s modules, but is compatible with and maintained on a wide variety of boards. The ‘ft232h’ backend allows controlling the GPIO from a PC using Adafruit’s FT232H USB to I2C adapter. See Crappy for embedded hardware for details.
ft232h_ser_num – If backend is ‘ft232h’, the serial number of the FT232H to use for communication.
GSM
- class crappy.inout.gsm.Gsm(numbers: List[str], port: str = '/dev/ttyUSB0', baudrate: int = 115200)[source]
Block for sending messages by SMS to given phone numbers.
Important
This block should be associated with a modifier to manage the messages to send.
InOut
- class crappy.inout.inout.InOut[source]
Base class for all InOut objects. Implements methods shared by all the these objects, and ensures their dataclass is MetaIO.
- close() None [source]
This method should perform any action required for properly ending the test and closing the communication with hardware.
It will be called when the associated IOBlock receives the order to stop, either because the user hit CTRL+C, or because a Generator block reached the end of its path, or because an exception was raised in any of the blocks.
- get_data() list | Dict[str, Any] | None [source]
This method should acquire data from a device and return it in a
list
along with a timestamp.The timestamp must always be the first returned value, and there can be any number of other acquired channels. The same number of values should always be returned, and they should be in the same order.
Alternatively, the values can be returned in a
dict
. In that case, thelabels
argument of the IOBlock is ignored and the returned labels correspond to the keys of the dict.It is alright for this method to return
None
if there’s no data to acquire.
- get_stream() List[ndarray] | None [source]
This method should acquire a stream as a numpy array, and return it in a
list
along with an array carrying the timestamps.The time array must be the first element of the list, the stream array the second element. The time array should have only one column, the stream array can have any number of columns representing the different channels acquired.
It is also possible to return the two arrays in a
dict
, in which case thelabels
argument is ignored and the keys of the dict set the returned labels.It is alright for this method to return
None
if there’s no data to acquire.
- make_zero(delay: float) None [source]
Acquires data for a given delay, averages it for each channel, and stores the average.
Does not work for pure streams, as it requires a
get_data()
for acquiring the data.The average values will then be used to remove the offset of the acquired data during the test.
- open() None [source]
This method should perform any action that’s required for initializing the hardware and the communication with it.
Communication with hardware should be avoided in the
__init__()
method, and this method is where it should start happening. This method is called after Crappy’s processes start, i.e. when the associated IOBlock already runs separately from all the other blocks.
- return_data() list | Dict[str, Any] | None [source]
Returns the data from
get_data()
, corrected by an offset if themake_zero_delay
argument of the IOBlock is set.
- return_stream() List[ndarray] | None [source]
Returns the data from
get_stream()
, corrected by an offset if themake_zero_delay
argument of the IOBlock is set.
- set_cmd(*cmd) None [source]
This method should handle commands received from the upstream blocks.
Usually the command is meant to be set on a device, but any other behavior is possible. The commands will be passed to this method as args (not kwargs), in the same order as the
cmd_labels
are given in the IOBlock.If the expected number of commands is always the same, you can simply put as many args to your
set_cmd
method as there are commands. For example for three commands:def set_cmd(self, cmd0, cmd1, cmd2): ...
Alternatively, or if the number of commands may vary from one test to another, you can get all the commands at once in a
tuple
by putting a single unpacking argument. Example:def set_cmd(self, *cmds): number_of_commands = len(cmds) cmd0 = cmds[0] ...
Kollmorgen
- class crappy.inout.kollmorgen.Koll(axes: List[int], mode: str = 'position', host: str = '192.168.0.109', port: int = 502)[source]
This class can communicate with a KollMorgen AKD PDMM programmable multi-axis controller.
It can either drive it in speed or in position. Multiple axes can be driven. The values of the current speeds or positions can also be retrieved.
- __init__(axes: List[int], mode: str = 'position', host: str = '192.168.0.109', port: int = 502) None [source]
Sets the args and initializes the parent class.
- Parameters:
axes – A
list
containing the motors/axes to drive, given asint
.mode – Should be either ‘speed’ or ‘position’. Whether the axes should be driven in speed or in position.
host – The IP address of the variator, given as a
str
.port – The network port over which to communicate with the variator, as an
int
.
Labjack T7
- class crappy.inout.labjackT7.Labjack_t7(channels: List[Dict[str, Any]], device: str = 'ANY', connection: str = 'ANY', identifier: str = 'ANY', write_at_open: List[tuple] | None = None, no_led: bool = False)[source]
This InOut object allows controlling a Labjack T7 device. It can use any channel as input/output.
The Labjack T7 is a very complete DAQ board. It features several ADC, several DAC, as well as multiple GPIOs. It can also read thermocouples, and run LUA code on an integrated microcontroller. These features can all be controlled from Crappy.
This class is not capable of streaming. For higher frequency, refer to the T7 Streamer class.
- __init__(channels: List[Dict[str, Any]], device: str = 'ANY', connection: str = 'ANY', identifier: str = 'ANY', write_at_open: List[tuple] | None = None, no_led: bool = False) None [source]
Sets the args and initializes the parent class.
- Parameters:
channels – A
list
of the channels to interface with on the Labjack. Each object in this list should be adict
representing a single channel, and whose keys provide information on the channel to use. Refer to the note below for more information on the possible keys.device –
The type of Labjack to open. Possible values include :
'ANY', 'T7', 'T4', 'DIGIT'
Only tested with ‘T7’ in Crappy.
connection –
The type of connection used for interfacing with the Labjack. Possible values include :
'ANY', 'TCP', 'USB', 'ETHERNET', 'WIFI'
identifier – Any extra information allowing to further identify the Labjack to open, like a serial number, an IP address, or a device name.
write_at_open – If specific names or registers have to be written when opening the channel, they can be given here as a
list
oftuple
. They will be written in the same order as in the given list. Refer to the note below for the accepted formats.no_led – If
True
, turns off the LED on the Labjack. This led can generate noise on the channels AIN0 and AIN1.
Note
channels
keys:name: The name of the channel to interface with, as written on the Labjack’s case. Ex: ‘AIN0’. The available settings as well as the direction of the channel depend on the given name.
- The name can be:
AINx: Analog input, linked to an ADC. A gain and offset can be provided, and the range and resolution can be adjusted. It is always an input. These channels can also be used with thermocouples (see below).
(T)DACx: Analog output, linked to a DAC. A gain and an offset can be specified. It is always an output.
(E/F/C/M IOx): Digital inputs/outputs. A gain and an offset can be specified. It can be either an input or an output, the default is output.
gain: If the channel is an input, the measured value will be modified directly by the Labjack as follows :
returned_value = gain * measured_value + offset
If the channel is an output, the command value will be modified in Crappy as follows before being sent to the Labjack :
sent_value = gain * command + offset.
offset: If the channel is an input, the measured value will be modified directly by the Labjack as follows :
returned_value = gain * measured_value + offset
If the channel is an output, the command value will be modified in Crappy as follows before being sent to the Labjack :
sent_value = gain * command + offset
make_zero: If
True
, data will be acquired on this channel before the test starts, and a compensation value will be deduced so that the offset of this channel is 0. The compensation is performed directly by the Labjack. This setting only has effect for AIN channels defined as inputs. It will only take effect if the ``make_zero_delay`` argument of the :ref:`IOBlock` controlling the Labjack is set !direction: If
True
, the channel is considered as an output, else as an input. Only has effect for IO channels, the default is output.resolution: The resolution of the acquisition as an integer, refer to Labjack documentation for more details. The higher this value the better the resolution, but the lower the speed. The possible range is either 1 to 8 or to 12 depending on the model. The default is 1. Only has effect for AIN channels.
range: The range of the acquisition in Volts. A range of x means that values can be read between -x and x Volts. The possible values are :
0.01, 0.1, 1, 10
Only has effect for AIN channels.
limits: A
tuple
containing the minimum and maximum allowed command values to set for this channel. After applying the gain and offset to the command, it is then clamped between these two values before being sent to the Labjack. Only has effect for output channels.thermocouple: The type of thermocouple to read data from. Possible values are:
'E', 'J', 'K', 'R', 'T', 'S', 'C'
If specified, it will use the EF to read a temperature directly from the thermocouples. Only has effect for AIN channels.
write_at_open: A
list
containing commands for writing specific names or registers when opening the channel. The commands should be given astuple
either in the format (name (str), value (int/float)) or (register (int), type (int), value (float/int)).
Warning
Do not consider the
limits
key as a safety feature. It should not go beyond/below the given values, but this is not meant to replace hardware safety !
- get_data() list | Dict[str, Any] | None [source]
Read the signal on all pre-defined input channels.
- make_zero(delay: float) None [source]
Overriding of the method of the parent class, because the Labjack T7 allows setting offsets directly on the board.
Setting the offsets on the Labjack is slightly quicker than correcting the received values afterwards.
- Parameters:
delay – The delay during which the data should be acquired for determining the offset.
Labjack UE9
MCP9600
- class crappy.inout.mcp9600.Mcp9600(backend: str, thermocouple_type: str, i2c_port: int = 1, device_address: int = 103, adc_resolution: int = 18, sensor_resolution: float = 0.0625, filter_coefficient: int = 0, mode: str = 'Hot Junction Temperature', ft232h_ser_num: str | None = None)[source]
Class for controlling Adafruit’s MCP9600 thermocouple reader.
The Mcp9600 InOut block is meant for reading temperature from an MCP9600 board, using the I2C protocol. The output is in °C, except for one operating mode that returns Volts.
- __init__(backend: str, thermocouple_type: str, i2c_port: int = 1, device_address: int = 103, adc_resolution: int = 18, sensor_resolution: float = 0.0625, filter_coefficient: int = 0, mode: str = 'Hot Junction Temperature', ft232h_ser_num: str | None = None) None [source]
Checks arguments validity.
- Parameters:
backend (
str
) –The backend for communicating with the MCP9600. Should be one of:
'Pi4', 'ft232h', 'blinka'
The ‘Pi4’ backend is optimized but only works on boards supporting the
smbus2
module, like the Raspberry Pis. The ‘blinka’ backend may be less performant and requires installing Adafruit’s modules, but these modules are compatible with and maintained on a wide variety of boards. The ‘ft232h’ backend allows controlling the ADS1115 from a PC using Adafruit’s FT232H USB to I2C adapter. See Crappy for embedded hardware for details.thermocouple_type (
str
) –The type of thermocouple plugged in the MCP9600. The available types are:
'J', 'K', 'T', 'N', 'S', 'E', 'B', 'R'
i2c_port (
int
, optional) – The I2C port over which the MCP9600 should communicate. On most Raspberry Pi models the default I2C port is 1.device_address (
int
, optional) – The I2C address of the MCP9600. The default address is 0x67, but it is possible to change this setting using a specific setup involving the ADDR pin.adc_resolution (
int
, optional) –The number of bits the ADC output is encoded on. The greater the resolution, the lower the sample rate. The available resolutions are:
12, 14, 16, 18
sensor_resolution (
float
, optional) – The temperature measurement resolution in °C. It should be either 0.0625 or 0.25. Setting the resolution to 0.25 will increase the sample rate, but the output temperature will be encoded on two bits less.filter_coefficient (
int
, optional) – The MCP9600 features an integrated filter (see its documentation for the exact filter formula). When set to 0, the filter is inactive. It is maximal when set to 7. When active, the filter will prohibit fast temperature changes, thus limiting noise and smoothening the signal.mode (
str
, optional) –Four different values can be accessed when measuring a temperature: the temperature of the thermocouple (hot junction temperature), the temperature of the MCP9600 board (cold junction temperature), the temperature calculated from the ADC data and thermocouple type but not yet cold junction-compensated (junction temperature delta), and the raw ADC measurement of the voltage difference in the thermocouple (raw data ADC, in Volts). The available modes are thus:
'Hot Junction Temperature', 'Junction Temperature Delta', 'Cold Junction Temperature', 'Raw Data ADC'
ft232h_ser_num (
str
, optional) – If backend is ‘ft232h’, the serial number of the ft232h to use for communication.
MPRLS
- class crappy.inout.mprls.Mprls(backend: str, eoc_pin: str | int | None = None, device_address: int = 24, i2c_port: int = 1, ft232h_ser_num: str | None = None)[source]
The Mprls inout is meant for reading pressure from Adafruit’s Mprls pressure sensor.
It communicates over I2C with the sensor.
- __init__(backend: str, eoc_pin: str | int | None = None, device_address: int = 24, i2c_port: int = 1, ft232h_ser_num: str | None = None) None [source]
Initializes the parent class and opens the I2C bus.
- Parameters:
backend (
str
) –Should be one of :
'Pi4', 'blinka', 'ft232h'
The ‘Pi4’ backend is optimized but only works on boards supporting the
smbus2
module, like the Raspberry Pis. The ‘blinka’ backend may be less performant and requires installing Adafruit’s modules, but these modules are compatible with and maintained on a wide variety of boards. The ‘ft232h’ backend allows controlling the MPRLS from a PC using Adafruit’s FT232H USB to I2C adapter. See Crappy for embedded hardware for details.eoc_pin (
int
orstr
, optional) – Optionally, reads the end of conversion signal from a GPIO rather than from an I2C message. Speeds up the reading and decreases the traffic on the bus, but requires one extra wire. With the backend ‘Pi4’, give the index of the GPIO in BCM convention. With the ‘ft232h’ backend, give the name of the GPIO in the format Dx or Cx. With the backend ‘blinka’, it should be a string but the syntax varies according to the board. Refer to blinka’s documentation for more information.device_address (
int
, optional) – The I2C address of the MPRLS. The address of the devices sold by Adafruit is 0x18, but other suppliers may sell it with another address.i2c_port (
int
, optional) – The I2C port over which the MPRLS should communicate. On most Raspberry Pi models the default I2C port is 1.ft232h_ser_num (
str
, optional) – If backend is ‘ft232h’, the serial number of the FT232H to use for communication.
NAU7802
- class crappy.inout.nau7802.Nau7802(backend: str, i2c_port: int = 1, device_address: int = 42, gain_hardware: int = 128, sample_rate: int = 80, int_pin: str | int | None = None, gain: float = 1, offset: float = 0, ft232h_ser_num: str | None = None)[source]
Class for controlling Sparkfun’s NAU7802 load cell conditioner.
The Nau7802 InOut block is meant for reading output values from a NAU7802 load cell conditioner, using the I2C protocol. The output is in Volts by default, but can be converted to Newtons using
gain
andoffset
.- __init__(backend: str, i2c_port: int = 1, device_address: int = 42, gain_hardware: int = 128, sample_rate: int = 80, int_pin: str | int | None = None, gain: float = 1, offset: float = 0, ft232h_ser_num: str | None = None) None [source]
Checks the validity of the arguments..
- Parameters:
backend (
str
) –The backend for communicating with the NAU7802. Should be one of:
'Pi4', 'ft232h'
to use the device from a Raspberry Pi or from a PC through an FT232H USB to I2C converter, respectively. See Crappy for embedded hardware for details.
i2c_port (
int
, optional) – The I2C port over which the NAU7802 should communicate. On most Raspberry Pi models the default I2C port is 1.device_address (
int
, optional) – The I2C address of the NAU7802. It is impossible to change this address, so it is not possible to have several NAU7802 on the same i2c bus.gain_hardware (
int
, optional) –The gain to be used by the programmable gain amplifier. Setting a high gain allows reading small voltages with a better precision, but it might saturate the sensor for higher voltages. Available gains are:
1, 2, 4, 8, 16, 32, 64, 128
sample_rate (
int
, optional) –The sample rate for data conversion. The higher the rate, the greater the noise. Available sample rates are:
10, 20, 40, 80, 320
int_pin (
int
orstr
, optional) – Optionally, reads the end of conversion signal from a GPIO rather than from an I2C message. Speeds up the reading and decreases the traffic on the bus, but requires one extra wire. With the backend ‘Pi4’, give the index of the GPIO in BCM convention. With the ‘ft232h’ backend, give the name of the GPIO in the format Dx or Cx.gain (
float
, optional) –Allows to tune the output value according to the formula:
output = gain * tension + offset.
offset (
float
, optional) –Allows to tune the output value according to the formula:
output = gain * tension + offset.
ft232h_ser_num (
str
, optional) – If backend is ‘ft232h’, the serial number of the ft232h to use for communication.
NI DaQMX
- class crappy.inout.ni_daqmx.Nidaqmx(channels: List[Dict[str, Any]], sample_rate: float = 100, n_samples: int | None = None)[source]
This class can communicate with NI DAQmx devices using the
pydaqmx
module.It can read single data points from digital and analog channels, read streams of data from analog channels, and set the voltage of analog and digital output channels. For analog input channels, several types of acquisition can be performed, like voltage, resistance, current, etc.
- __init__(channels: List[Dict[str, Any]], sample_rate: float = 100, n_samples: int | None = None) None [source]
Sets the args and initializes the parent class.
- Parameters:
channels – A
list
containingdict
holding information on the channels to read data from or write data to. See below for the mandatory and optional keys for the dicts. Note that in streamer mode, the digital input channels are not available for reading. Also, only one type of analog input channel at a time can be read in streamer mode, with no restriction on the number of channels of this type.sample_rate – The target sample rate for data acquisition in streamer mode, given as a
float
. Default is 100 SPS.n_samples – The number of samples to acquire per chunk of data in streamer mode. Default is 20% of
sample_rate
.
Note
channels
keys:name: The name of the channel to interface with, given with the following syntax :
'DevX/[a/d][i/o]Y'
With X the index of the device, and Y the line on which the channel is. d stands for digital, a for analog, i for input and o for output. For digital channels, DevX/d[i/o]Y is internally converted to DevX/port<Y // 8>/line<Y % 8>. Example of a valid name : Dev1/ao3.
type: The type of data to read, for analog input channels. This field can take many different values, refer to the documentation of the
nidaqmx
for more details. This field is internally used for calling the method :nidaqmx.task.add_ai_[type]_chan()
. The default for this field is ‘voltage’, possible values include ‘thrmcpl’, ‘bridge’, ‘current’ and ‘resistance’.All the other keys will be given as kwargs to the
nidaqmx.task.add_ai_[type]_chan()
method for analog input channels, to thenidaqmx.task.add_ao_voltage_chan()
for analog output channels, tonidaqmx.task.add_do_chan()
for digital output channels, and tonidaqmx.task.add_di_chan()
for digital input channels. Refer tonidaqmx
documentation for the possible arguments and values. Note that for the ‘thrmcpl’ analog input channel type, the ‘thermocouple_type’ argument must be given as a letter, same for the ‘units’ argument. They will be parsed internally. Also note that for the analog output channels and the analog input channels of type ‘voltage’, the ‘min_val’ and ‘max_val’ arguments are internally set by default to 0 and 5.
- get_data() List[float] [source]
Reads data from the analog and digital input channels, and returns it along with a timestamp.
Data from the analog channels is read first, and then data from the digital channels. Data is returned in the same order as it was acquired.
- get_stream() List[ndarray] | None [source]
Reads data from the NI DAQmx, and returns it in an array along with an array holding the timestamps.
Only data from analog input channels can be read, this method cannot read stream data from digital input channels.
- open() None [source]
Creates tasks and streams for analog output, digital input, digital output, and each type of analog input channels.
- set_cmd(*cmd: float) None [source]
Sets the analog and digital output channels according to the given command values.
The first command values correspond to the analog channels, the remaining ones correspond the the digital channels. It might be that not all channels are set if the number of commands doesn’t match the number of channels.
Opsens
- class crappy.inout.opsens.Opsens(device: str = '/dev/ttyUSB0')[source]
This class allows reading data from an Opsens PicoSens fiber optics signal conditioner.
It can read data from various fiber optics sensors like temperature, pressure, position or strain.
PiJuice
- class crappy.inout.piJuice.Pijuice(i2c_port: int = 1, address: int = 20, backend: str = 'Pi4')[source]
Block getting various information about a piJuice power platform, including the charge level and the power supply status.
Warning
Only available on Raspberry Pi !
- __init__(i2c_port: int = 1, address: int = 20, backend: str = 'Pi4') None [source]
Checks arguments validity.
- Parameters:
backend (
str
, optional) –Should be one of :
'Pi4', 'pijuice'
The ‘Pi4’ backend is based on the
smbus2
module, while the ‘pijuice’ backend is based on thepijuice
.i2c_port (
int
, optional) – The I2C port over which the PiJuice should communicate.address (
int
, optional) – The I2C address of the piJuice. The default address is 0x14.
- get_data() Dict[str, Any] [source]
Reads all the available information on the battery status.
- Returns:
dict
–Returns a dict containing:
the timeframe in seconds as a
float
in labelt(s)
the battery status as a
str
in labelbattery_status
the USB status as a
str
in labelUSB_status
the GPIO status as a
str
in labelGPIO_status
the charge level as an
int
in labelcharge_level
the battery temperature in °C as an
int
in labelbattery_temperature
the battery voltage in mV as an
int
in labelbattery_voltage
the battery current in mA as an
int
in labelbattery_current
the GPIO voltage in mV as an
int
in labelGPIO_voltage
the GPIO current in mA as an
int
in labelGPIO_current
Spectrum
- class crappy.inout.spectrum.Spectrum(channels: List[int], device: str = '/dev/spcm0', ranges: List[int] | None = None, sample_rate: int = 100000, buff_size: int = 67108864, notify_size: int = 65536)[source]
This class can read data from a Spectrum high speed ADC interfacing over PCIe.
It can acquire data over multiple channels, and set for each channel a different voltage range. It is possible to tune the sample rate, the chunk size and the memory allocated to the data buffer.
This class can only acquire data by streaming, it cannot acquire single data points.
- __init__(channels: List[int], device: str = '/dev/spcm0', ranges: List[int] | None = None, sample_rate: int = 100000, buff_size: int = 67108864, notify_size: int = 65536) None [source]
Sets the args and initializes the parent class.
- Parameters:
channels – A
list
of all the channels to read data from, given asint
. Refer to the documentation to know which combinations of channels are allowed.device – The address of the device to read data from, as a
str
.ranges – A
list
indicating for each channel the range of the acquisition in mV, as anint
. There should be as many values in this list as there are channels. If not given, all ranges default to 10000 mV.sample_rate – The sample rate of the acquisition for all channels, in Hz. The default is 100KHz.
buff_size – The size of the memory allocated as a rolling buffer to copy the data from the card, in bytes. The default is 67MB.
notify_size – The size of each chunk of data to copy from the card, in bytes. The default is 65kB.
- get_stream() List[ndarray] [source]
Waits for data to be available, and returns it along with an array of timestamps.
T7 Streamer
- class crappy.inout.t7Streamer.T7_streamer(channels: List[Dict[str, Any]], device: str = 'ANY', connection: str = 'ANY', identifier: str = 'ANY', scan_rate: int = 100000, scan_per_read: int = 10000, resolution: int = 1)[source]
This InOut object allows controlling a Labjack T7 device in stream mode.
It can only acquire data on the AIN channels. For single point mode, and acquisition on all channels, use the Labjack T7 InOut.
Compared with single point acquisition, the streaming mode can achieve much higher data rates and has a much greater regularity in the frequency of the acquisition. However, fewer options are available and not all types of channels can be read in the streamer mode.
For each channel, the voltage range can be tuned, and a gain and offset can be defined. Also, the user can decide whether the channel should be zeroed before starting the test or not.
Important
The
streamer
argument of the IOBlock controlling this InOut must be set toTrue
to enable streaming in this class. Otherwise, only single point acquisition can be performed.- __init__(channels: List[Dict[str, Any]], device: str = 'ANY', connection: str = 'ANY', identifier: str = 'ANY', scan_rate: int = 100000, scan_per_read: int = 10000, resolution: int = 1) None [source]
Sets the args and initializes the parent class.
- Parameters:
channels – A
list
of the channels to interface with on the Labjack. Each object in this list should be adict
representing a single channel, and whose keys provide information on the channel to use. Refer to the note below for more information on the possible keys.device –
The type of Labjack to open. Possible values include :
'ANY', 'T7', 'T4', 'DIGIT'
Only tested with ‘T7’ in Crappy.
connection –
The type of connection used for interfacing with the Labjack. Possible values include :
'ANY', 'TCP', 'USB', 'ETHERNET', 'WIFI'
identifier – Any extra information allowing to further identify the Labjack to open, like a serial number, an IP address, or a device name.
scan_rate – The acquisition frequency in Hz for all channels. Note that the sample rate (scan_rate * num of chan) cannot exceed 100000. If it is too high it will be lowered to the highest possible value.
scan_per_read – The number of points to read at each loop.
resolution – The resolution of the acquisition as an integer for all channels. Refer to Labjack documentation for more details. The higher this value the better the resolution, but the lower the speed. The possible range is either 1 to 8 or to 12 depending on the model. The default is 1.
Note
channels
keys:name: The name of the channel to interface with, as written on the Labjack’s case. Ex: ‘AIN0’. In streamer mode, only the AIN channels, i.e. the analog inputs, are available.
gain: The measured value will be modified in Crappy as follows :
returned_value = gain * measured_value + offset
offset: The measured value will be modified in Crappy as follows :
returned_value = gain * measured_value + offset
make_zero: If
True
, data will be acquired on this channel before the test starts, and a compensation value will be deduced so that the offset of this channel is 0. It will only take effect if the ``make_zero_delay`` argument of the :ref:`IOBlock` controlling the Labjack is set !range: The range of the acquisition in Volts. A range of x means that values can be read between -x and x Volts. The possible values are :
0.01, 0.1, 1, 10
- get_data() List[float] [source]
Reads single data points, applies the given gains and offsets, and returns the data.
- get_stream() List[ndarray] | None [source]
Acquires the stream, reshapes the data, applies the gains and offsets, and returns the data along with a time array.
- make_zero(delay: float) None [source]
Overriding of the method of the parent class, because the user can choose which channels should be zeroed or not.
It simply performs the regular zeroing, and resets the compensation for the channels that shouldn’t be zeroed.