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', gain: float = 1, offset: float = 0, ft232h_ser_num: Optional[str] = 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.Warning
Only available on Raspberry Pi for now !
- __init__(backend: str, device_address: int = 72, i2c_port: int = 1, sample_rate: int = 128, v_range: float = 2.048, multiplexer: str = 'A1', gain: float = 1, offset: float = 0, ft232h_ser_num: Optional[str] = None) → None[source]¶
Checks arguments validity.
- Parameters
backend (
str
) –The backend for communicating with the NAU7802. Should be one of:
'Pi4', 'ft232h'
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. 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'
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 to measure voltages higher than VDD !!
Agilent 34420A¶
- class crappy.inout.agilent34420A.Agilent34420a(mode=b'VOLT', device='/dev/ttyUSB0', baudrate=9600, timeout=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
.
Comedi¶
- class crappy.inout.comedi.Comedi(**kwargs)[source]¶
Comedi object, for IO with cards using comedi driver.
Note
The channel-specific args can be given as a list to set it for each channel or as a single value to apply it to every channel.
- Parameters
device (str (-) – “/dev/comedi0”): The address of the device.
default – “/dev/comedi0”): The address of the device.
subdevice (int (-) – 0): The id of the subdevice.
default – 0): The id of the subdevice.
channels (list (-) – [0]): The list of the input channels.
default – [0]): The list of the input channels.
range_num (list/int (-) – 0): The range to use on each channel.
default – 0): The range to use on each channel.
gain (list/float (-) – 1): The return value for each chan will be multiplied by the gain.
default – 1): The return value for each chan will be multiplied by the gain.
offset (list/float (-) – 0): The offset will be added to the return value for each chan.
default – 0): The offset will be added to the return value for each chan.
make_zero (list/bool (-) – True): If True, the value read at the beginning will be removed to the offset to take it as a reference.
default – True): If True, the value read at the beginning will be removed to the offset to take it as a reference.
channels (list – []): The list of the output channels.
default – []): The list of the output channels.
out_range_num (list/int (-) – 0): The range to use on each output.
default – 0): The range to use on each output.
out_gain (list/float (-) – 1): The output value for each chan will be multiplied by the gain.
default – 1): The output value for each chan will be multiplied by the gain.
out_offset (list/float (-) – 0): The offset will be added to the output value for each chan.
default – 0): The offset will be added to the output value for each chan.
out_subdevice (int (-) – 1): The id of the output subdevice.
default – 1): The id of the output subdevice.
- get_data(channel='all')[source]¶
To read the value on input_channels.
Note
If channel is specified, it will only read and return these channels.
‘all’ (default) will read all opened channels.
DaQMX¶
- class crappy.inout.daqmx.Daqmx(device='Dev1', channels=None, gain=1, offset=0, range=5, make_zero=True, nperscan=1000, sample_rate=10000, out_channels=None, out_gain=1, out_offset=0, out_range=5)[source]¶
Class to use DAQmx devices.
- __init__(device='Dev1', channels=None, gain=1, offset=0, range=5, make_zero=True, nperscan=1000, sample_rate=10000, out_channels=None, out_gain=1, out_offset=0, out_range=5)[source]¶
Sets the args and initializes the parent class.
- Parameters
device (
str
, optional) – Name of the device to open.channels (
list
, optional) – Names or ids of the channels to read.gain (
list
, optional) – Gains to apply to each reading.offset (
list
, optional) – Offset to apply to each reading.range (
list
, optional) –Max value for the reading. Should be a
list
offloat
in:0.5, 1., 2.5, 5.
Refer to the niDAQ api for more details.
make_zero (
list
, optional) – IfTrue
, the average value on the channel at opening will be evaluated and subtracted to the actual reading.nperscan (
int
, optional) – If using streamer mode, number of readings to acquire on eachget_stream()
call.sample_rate (
float
, optional) – If using streamer mode, frequency of acquisition when callingget_stream()
.out_channels (
list
, optional) – Names or ids of the output channels.out_gain (
list
, optional) – Gains to apply to the commands.out_offset (
list
, optional) – Offset to apply to the commands.out_range (
list
, optional) –Max value of the output. Should be a
list
offloat
in:0.5, 1., 2.5, 5.
Refer to the niDAQ api for more details.
Note
If an argument supposed to be a
list
is given as a single value, this value will be applied to all channels.
- check_vars()[source]¶
Turns the settings into
list
of the same length, each index standing for one channel.
- get_data()[source]¶
Returns a
tuple
of lengthlen(self.channels) + 1
.First element is the time, others are readings of each channel.
- set_cmd(*args)[source]¶
Set the output(s) to the specified value.
Note
Takes n arguments, n being the number of channels opened at
__init__()
. The ith argument is the value to set to the ith channel.
Fake Inout¶
GPIO PWM¶
- class crappy.inout.gpio_pwm.Gpio_pwm(pin_out: int, duty_cycle: Optional[float] = None, frequency: Optional[float] = 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: Optional[float] = None, frequency: Optional[float] = 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)[source]¶
Class for setting a GPIO high or low.
The Gpio_switch InOut block is meant for switching a Raspberry Pi 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.
Warning
Only works on Raspberry Pi !
GSM¶
- class crappy.inout.gsm.Gsm(numbers: Optional[list] = None, port: str = '/dev/ttyUSB0', baudrate: int = 115200)[source]¶
Block for sending any 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.MetaIO(name, bases, dict_)[source]¶
Metaclass that will define all IO objects.
All IO classes should be of this type.
To do so, simply add
__metaclass__ = MetaIO
in the class definition. (Obviously, you must import this Metaclass first.)MetaIO is a MetaClass: we will NEVER do
c = MetaIO(...)
.The
__init__()
is used to init the classes of type MetaIO (with__metaclass__ = MetaIO
as a class attribute) and NOT an instance of MetaClass.
Kollmorgen¶
- class crappy.inout.kollmorgen.Koll(data='position', axis='all', speed=360, acc=3600, decc=3600, labels=None, host='192.168.0.109', port=502)[source]¶
Class to communicate to Kollmorgen devices via Crappy.
- class crappy.inout.kollmorgen.KollMorgenVariator(host='192.168.0.109', port=502)[source]¶
Main class to test communication with kollmorgen variator.
Every variable and its address has been defined in the Kollmorgen Integrated Suite.
To add or remove some, update the dictionaries of
__init__()
.There are 3 motors, so the tens are for each motor, the units for the address.
- set_accelerations(motor, **kwargs)[source]¶
To set acceleration, deceleration (for positioning) and fast deceleration (boolean stop).
- set_rotation(motor, rotation)[source]¶
To set a rotation (in degrees) of the motor axis. Rotation is signed.
Labjack T7¶
- class crappy.inout.labjackT7.Labjack_t7(device='ANY', connection='ANY', identifier='ANY', channels=None, write_at_open=None, no_led=False)[source]¶
Class for LabJack T7 devices.
It can use any channel as input/output, it can be used with an IOBlock.
This class is NOT capable of streaming. For higher frequency, see T7 Streamer class.
The keyword argument
channels
is used to specify the channels.Each channel must be represented as a
dict
including all the parameters. See below for more details on the parameters of the channels.- __init__(device='ANY', connection='ANY', identifier='ANY', channels=None, write_at_open=None, no_led=False)[source]¶
Sets the args and initializes the parent class.
- Parameters
device (
str
, optional) –The type of the device to open. Possible values include:
'ANY', 'T7', 'T4', 'DIGIT'
Only tested with ‘T7’ in Crappy.
connection (
str
, optional) –How is the Labjack connected ? Possible values include:
'ANY', 'TCP', 'USB', 'ETHERNET', 'WIFI'
identifier (
str
, optional) – Something to identify the Labjack. It can be a serial number, an IP address, or a device name.channels (
list
, optional) – Channels to use and their settings. It must be alist
ofdict
.write_at_open (
list
, optional) – If you need to write specific names or registers when opening the channel, you can give them as alist
oftuple
. They will be written in the order of the list.no_led (
list
, optional) – IfTrue
, turns off the LED on the Labjack. This led can cause noise on the channels AIN0 and AIN1.
Note
channels
keys:name (
str
): The name of the channel according to Labjack’s naming convention. Ex: ‘AIN0’. This will be used to define the direction (in/out) and the available settings.- It can be:
AINx: An analog input, if gain and/or offset is given, the integrated slope mechanism will be used with the extended features registers. It can also be used for thermocouples (see below). You can use any EF by using the
write_at_open
andto_read
keys if necessary.(T)DACx: An analog output, you can specify gain and/or offset.
(E/F/C/M IOx): Digital in/outputs. You can specify the direction.
gain (
float
, default: 1): A numeric value that will multiply the given value for inputs and outputs.offset (
float
, default: 0): Will be added to the value.For inputs:
returned_value = gain * measured_value + offset
For outputs:
set_value = gain * given_value + offset.
Where measured_value and set_values are in Volts.
make_zero (
bool
): AIN only, ifTrue
the input value will be evaluated at startup and the offset will be adjusted to return 0 (or the offset if any).direction (
bool
): DIO only, ifTrue
(or 1), the port will be used as an output else as an input.resolution (
int
, default: 1): The resolution of the acquisition, see Labjack documentation for more details. The bigger this value the better the resolution, but the lower the speed. The possible range is either 1 to 8 or to 12 according to the model.range (
float
, default: 10): 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
limits (
tuple
, default: None): To clamp the output values to a given range. Thetuple
should contain two values: the min and the max limit.thermocouple (
str
): The type of thermocouple (AIN only). 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.
write_at_open (
list
): If you need to write specific names or registers when opening the channel, you can give them as alist
oftuple
. They will be written in the order of the list.The tuples can either be (name (str), value (int/float)) or (register (int), type (int), value (float/int)).
Warning
DO NOT CONSIDER the
limits
KEY AS A SAFETY IMPLEMENTATION. It should not go beyond/below the given values, but this is not meant to replace hardware safety!
- set_cmd(*cmd)[source]¶
Converts the tension value to a digital value and send it to the output.
Note
Once a value has been written, it will not be written again until it changes! This is meant to lower the communication and Labjack activity.
It relies on the fact that these registers will not change between writes (Which is true unless the card runs a lua script writing the same registers as the user).
Labjack UE9¶
- class crappy.inout.labjackUE9.Labjack_ue9(channels=0, gain=1, offset=0, make_zero=True, resolution=12)[source]¶
Can read data from a LabJack UE9.
Important
Streamer mode and DAC are not supported yet.
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: Optional[str] = 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.
Warning
Only available on Raspberry Pi for now !
- __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: Optional[str] = None) → None[source]¶
Checks arguments validity.
- Parameters
backend (
str
) –The backend for communicating with the NAU7802. Should be one of:
'Pi4', 'ft232h'
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¶
NAU7802¶
- class crappy.inout.nau7802.Nau7802(backend: str, i2c_port: int = 1, device_address: int = 42, gain_hardware: int = 128, sample_rate: int = 80, gain: float = 1, offset: float = 0, ft232h_ser_num: Optional[str] = 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 into Newtons using
gain
andoffset
.Warning
Only available on Raspberry Pi for now !
- __init__(backend: str, i2c_port: int = 1, device_address: int = 42, gain_hardware: int = 128, sample_rate: int = 80, gain: float = 1, offset: float = 0, ft232h_ser_num: Optional[str] = None) → None[source]¶
Checks the arguments validity.
- Parameters
backend (
str
) –The backend for communicating with the NAU7802. Should be one of:
'Pi4', 'ft232h'
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 port.gain_hardware (
int
, optional) –The gain to be used by the programmable gain amplifier. Setting a high gain allows to read 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
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=None, samplerate=100, nsamples=None)[source]¶
Opens National Instrument devices using the NiDAQmx driver (Windows only).
This class can open ai, ao and dio channels on NI devices. It supports continuous streaming on ai channels only. Streaming is not supported with ai channels of different types.
It uses the
nidaqmx
python module by NI.- __init__(channels=None, samplerate=100, nsamples=None)[source]¶
Builds the different channels lists.
- Parameters
channels (
list
, optional) –A
list
ofdict
describing all the channels to open. See the note below for details.Note
For dio, use DevX/d[i/o] Y to select port(Y//8)/line(Y%8) on DevX.
samplerate (
float
, optional) – If using stream mode, the samplerate of the stream.nsamples (
int
, optional) – If using stream mode, the stream array will be returned after readingnsamples
samples. Defaults tosamplerate // 5
.
Note
Open DaQ¶
Opsens¶
PiJuice¶
- class crappy.inout.piJuice.Pijuice(i2c_port: int = 1, address: int = 20)[source]¶
Block getting the status (plugged or unplugged) and the actual level of charge of the piJuice power platform.
Warning
Only available on Raspberry Pi !
Spectrum¶
- class crappy.inout.spectrum.Spectrum(device=b'/dev/spcm0', channels=None, ranges=None, samplerate=100000, buff_size=67108864, notify_size=65536, split_chan=False)[source]¶
Acquire data from a Spectrum device.
- __init__(device=b'/dev/spcm0', channels=None, ranges=None, samplerate=100000, buff_size=67108864, notify_size=65536, split_chan=False)[source]¶
Sets the args and the instance attributes.
- Parameters
(str (device) – “/dev/spcm0”): The address of the device to use.
default – “/dev/spcm0”): The address of the device to use.
(list (ranges) – [0]): The channels to open. See doc for the allowed combinations!
default – [0]): The channels to open. See doc for the allowed combinations!
(list – [10000]): The ranges of the channels in mV.
default – [10000]): The ranges of the channels in mV.
(int (notify_size) – 100000): The samplerate for all channels in Hz.
default – 100000): The samplerate for all channels in Hz.
(int – 2**16 (64MB)): The size of the memory allocated as a rolling buffer to copy the data from the card
default – 2**16 (64MB)): The size of the memory allocated as a rolling buffer to copy the data from the card
(int – 2**16 (64kB)): The size of the chunks of data to copy from the card.
default – 2**16 (64kB)): The size of the chunks of data to copy from the card.
split_chan – split_chan (bool, default: False): If False, it will return a single 2D array, else each chan will be a 1D array.
T7 Streamer¶
- class crappy.inout.t7Streamer.T7_streamer(device='ANY', connection='ANY', identifier='ANY', channels=None, scan_rate=100000, scan_per_read=10000, resolution=1)[source]¶
Class to use stream mode with Labjack T7 devices.
Note
For single modes, see Labjack T7.
You can use IOBlock with
streamer=True
to read data at high frequency from the Labjack. Streamer mode makes the Labjack unavailable for any other operation (single acquisition, DAC or DIO).You can specify each channel as a
dict
, allowing to set channel-specific settings such as gain, offset (computed on the host machine as this feature is not available on board with streamer mode), range and ability to zero the reading at startup.- __init__(device='ANY', connection='ANY', identifier='ANY', channels=None, scan_rate=100000, scan_per_read=10000, resolution=1)[source]¶
Sets the args and initializes the parent class.
- Parameters
device (
str
, optional) –The type of the device to open. Possible values include:
'ANY', 'T7', 'T4', 'DIGIT'
Only tested with ‘T7’ in Crappy.
connection (
str
, optional) –How is the Labjack connected ? Possible values include:
'ANY', 'TCP', 'USB', 'ETHERNET', 'WIFI'
identifier (
str
, optional) – Something to identify the Labjack. It can be a serial number, an IP address, or a device name.channels (
list
, optional) – Channels to use and their settings. It must be alist
ofdict
.scan_rate (
int
, optional) – The acquisition frequency in Hz for the 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 (
int
, optional) – The number of points to read during each loop.resolution (
int
, optional) – The resolution index for all channels. The bigger this value the better the resolution, but the lower the speed. The possible range is either 1 to 8 or to 12 according to the model.
Note
channels
keys:name (
str
): The name of the channel according to Labjack’s naming convention. Ex: ‘AIN0’. This will be used to define the direction (in/out) and the available settings.- It can be:
AINx: An analog input, if gain and/or offset is given, the integrated slope mechanism will be used with the extended features registers. It can also be used for thermocouples (see below). You can use any EF by using the
write_at_open
andto_read
keys if necessary.(T)DACx: An analog output, you can specify gain and/or offset.
(E/F/C/M IOx): Digital in/outputs. You can specify the direction.
gain (
float
, default: 1): A numeric value that will multiply the given value for inputs and outputs.offset (
float
, default: 0): Will be added to the value.For inputs:
returned_value = gain * measured_value + offset
For outputs:
set_value = gain * given_value + offset.
Where measured_value and set_values are in Volts.
make_zero (
bool
): AIN only, ifTrue
the input value will be evaluated at startup and the offset will be adjusted to return 0 (or the offset if any).range (
float
, default: 10): 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