Migrating from the previous objects
The NIDAQmx object of hardware/national_instruments/daqmxni.py (one object, one task, channels described by
mutable classes with a source attribute, update_task with several if on the type) is replaced by the API of
this section. There is no compatibility layer: the old names disappear with the old module, once the plugins of the
package use the new API.
Two lines of releases exist:
5.3.x(this branch):nidaqmxand the new API.pydaqmx(branch and tagpydaqmx-final, also the 1.1.x releases): thePyDAQmxobjects, frozen. Code that uses them (laboratory projects, for instance) keeps working by staying on that line. Moving to this API is a rewrite of the few places where the task is configured, as shown below.
Before and after
# before
from pymodaq_plugins_daqmx.hardware.national_instruments.daqmxni import (
NIDAQmx, AIChannel, ClockSettings, ChannelType, UsageTypeAI, TerminalConfiguration)
controller = NIDAQmx()
channel = AIChannel(name='Dev1/ai0', source=ChannelType.ANALOG_INPUT, analog_type=UsageTypeAI.VOLTAGE,
value_min=-5., value_max=5., termination=TerminalConfiguration.DIFF)
controller.update_task([channel], ClockSettings(frequency=1000., Nsamples=100, repetition=False))
controller.task.start()
raw = controller.task.read(number_of_samples_per_channel=100) # list or list of lists
controller.task.stop()
controller.close()
# after
from nidaqmx.constants import TerminalConfiguration
from pymodaq_plugins_daqmx.hardware.national_instruments.api import (
AIVoltage, AnalogInputTask, ClockSettings, TaskConfig)
config = TaskConfig(
channels=[AIVoltage('Dev1/ai0', min_val=-5., max_val=5., terminal_config=TerminalConfiguration.DIFF)],
timing=ClockSettings(rate=1000., samples=100))
with AnalogInputTask(config) as task:
task.start()
data = task.read_analog() # float64 array shaped (1, 100)
task.stop()
What replaces what
Before |
After |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
the same names on every task, which is also a context manager |
Without equivalent for now
isTaskDoneandwaitTaskDone: useon_done.getAICurrentRangeandgetAOCurrentRange(current ranges),DAQmxGetErrorString(usenidaqmx.constants/DAQmxErrorswithDaqError.error_code) andreadCounter(useread_counter).configuration_sequenceand theconfig_daqmx.tomlfile: they belong to the generic plugins, not to the API, and are ported with them.
Things that behave differently
Mistakes in a description are a
ValueErrorwhen you create it, not anassertor an error swallowed later.Driver errors are
nidaqmx.DaqErrorwith anerror_code; nothing is turned into anIOError.Several plugins do not share a controller any more: each one builds its own task. A multi-axes actuator that needs to share something (the clock of the ramp output) shares its own object.
A configuration never changes: to change a channel, a rate or a trigger, close the task and build another one. A stopped task keeps its channels reserved (driver error -50103), so a second task on the same channels cannot be created before the first is closed.
One task is one subsystem (analog in, analog out, digital, counters). The old object let you try to mix them and failed in the driver; the typed tasks make it impossible. Mixing is done with several tasks synchronised by a shared clock or trigger, and
TaskGroupto start and stop them in the right order.read_analogon a continuous task is an error: data comes through the listeners.Driver callbacks run on threads of the driver, in no guaranteed order.
Porting your own code, step by step
Describe each channel with the class of its kind (table above). Put the arguments that were set after creation (
channel.value_min = ...) in the constructor.Describe the timing (
ClockSettings,ChangeDetectionSettingsorImplicitTiming) and the trigger (Noneif there is none), and gather everything in aTaskConfig.Create the typed task from the config (
with AnalogInputTask(config) as task:closes it for you). Replace everycontroller.task.<something>by the verb of the task (read_analog…). Thenidaqmx.Taskis no longer part of the interface.Replace the callbacks by a
TaskListener. Do not touch widgets from it: it is called from a driver thread.Replace
update_taskon a live object by closing the task and building the new one. Code that changed a setting when the user edited it (commit_settings) rebuilds the config and the task.Test with the
driverfixtures of this repository (tests/nidaqmx_double.py) or a simulated device of NI MAX, then on hardware.
In a PyMoDAQ plugin
The controller of the plugin is the task (or a
TaskGroup, or a small object that holds several tasks), not aNIDAQmx.Create a
QtTaskBridgeinini_detector, register it withadd_listenerand connect itssamples_readyanddonesignals to slots of the plugin. Close it before the task, inclose.A viewer that streams (
live_mode_available = True) uses a continuous task (ClockSettings(rate)withoutsamples) apart from the task used for single grabs: the AI plugins of this package are an example.A plugin used as a slave receives the task from the master (
is_masterisFalse): it must not close it.
State of the package
The AI, counter and ramp-output plugins already use the new API. The generic plugins (NIDAQmx viewers and
actuator, which read config_daqmx.toml) still use the old module daqmxni.py and are ported next, together with
the removal of the old module: until then, both may be found in the same checkout, but nothing new should be built on
daqmxni.py.