DAQ and FPGA boards

A DAQ card is a subtype of DAQ. Its interface creates a task on a named channel (createtask), writes or reads a voltage through that task (setvoltage, readvoltage) and deletes it (deletetask); showdevices and showchannels list what the driver finds. NIdaq is the National Instruments driver, through DAQmx.jl, and NIDAQModulator drives a light's analog input from one of its outputs.

The Opal Kelly XEM boards are not DAQs and have no abstract type: XEM controls an FPGA board whose exposure timing is selected through NI DAQ outputs, and XEM_dac uses one as a four-channel DAC.

DAQ interface

MicroscopeControl.HardwareInterfaces.DAQInterface.addchannel! — Method
addchannel!(daq::DAQ,t::Any,tasktype::String, channel::String)

Add a channel to a task.

Arguments

  • daq::DAQ: A DAQ type.
  • t::Any: A Any type.
  • tasktype::String: A task type. Options are "AI","AO","DI","DO".
  • channel::String: A channel name, obtained from showchannels.
source
MicroscopeControl.HardwareInterfaces.DAQInterface.createtask — Method
createtask(daq::DAQ,tasktype::String,channel::String; min_val=nothing, max_val=nothing)

Create a task of a given task type and channel.

Arguments

  • daq::DAQ: A DAQ type.
  • tasktype::String: A task type. Options are "AI","AO","DI","DO".
  • channel::String: A channel name, obtained from showchannels.
  • min_val, max_val: for "AI" and "AO", the voltage range the channel is created with, both or neither. With neither, the driver picks the device's own range. With both, the device refuses a range it lacks (a USB-6008 has no 0-10 V output range) instead of the driver silently using another.

Returns

  • t::Any: A Any type.
source
MicroscopeControl.HardwareInterfaces.DAQInterface.setvoltage — Method
setvoltage(daq::DAQ,t::Any,voltage::Float64)

Set the voltage of a output task.

Arguments

  • daq::DAQ: A DAQ type.
  • t::Any: A Any type.
  • voltage::Float64: The voltage to set the task to, unit: volt. If tasktype is "DO", the voltage should be 0 or 1.

Returns

  • ret::Int: The number of samples written to the task.
source
MicroscopeControl.HardwareInterfaces.DAQInterface.showchannels — Method
showchannels(daq::DAQ,channeltype::String,device::String)

List available channels of a given channel type and device.

Arguments

  • daq::DAQ: A DAQ type.
  • channeltype::String: A channel type. Options are "AI","AO","DI","DO","CI","CO".
  • device::String: A device name, obtained from showdevices.
source

National Instruments DAQ

MicroscopeControl.HardwareImplementations.NIDAQcard.NIDAQModulator — Type
NIDAQModulator(; channel, v_range, per_volt, daq = NIdaq(), write_timeout = 0.5)

An AbstractModulationControl that drives a light's analogue input from one NI-DAQmx analogue output channel ("Dev1/ao1"). v_range = (v_min, v_max) is the range the output is created with, passed to DAQmx through createtask(daq, "AO", channel; min_val, max_val) so DAQmx refuses a range the card lacks (a USB-6008 has no 0-10 V output range: error -200077); it is also the reach of setpower. per_volt is required and positive, in the light's unit per volt (mW/V in closed loop, mA/V in open loop): measure it as in the TCube CALIBRATION.md, "MOD IN scale".

Construction is pure. initialize makes ONE analogue-output task and writes 0 V; shutdown writes 0 V, then deletes the task; a failed initialize deletes the task it made. Pass it to a light as modulator = ...; the light opens and closes it.

setpower(mc, p) writes p / per_volt volts, and throws ArgumentError, before any write, when that is not finite or lies outside v_range.

write_timeout (seconds, default 0.5) is the DAQmx timeout of every analogue write this modulator makes, instead of DAQmx's 10 s default. A software-timed scalar write to an NI USB DAQ takes about 0.5 ms (measured, PR #83, 2026-10-13), so 0.5 s is three orders of magnitude of margin and bounds a stall. [limitation] DAQmx's scalar write is a synchronous native call: with ONE Julia thread, a write stalled on its transport blocks the whole thread, so a light_off issued from another task cannot run until the write returns or times out, up to write_timeout. Issue light_off from another thread (julia -t 2, Threads.@spawn), as 0.2.7 does for the DCAM4 frame wait, to be independent of it.

source
MicroscopeControl.HardwareInterfaces.DAQInterface.addchannel! — Method
addchannel!(daq::NIdaq, t::DAQmx.Task, tasktype::String, channel::String)

Add a channel to an existing task.

Arguments

  • daq::NIdaq: A NIdaq type.
  • t::DAQmx.Task: A DAQmx Task.
  • tasktype::String: A task type. Options are "AI","AO","DI","DO".
  • channel::String: A channel name, obtained from showchannels.
source
MicroscopeControl.HardwareInterfaces.DAQInterface.createtask — Method
createtask(daq::NIdaq,tasktype::String,channel::String; min_val=nothing, max_val=nothing)

Create a task of a given task type and channel.

Arguments

  • daq::NIdaq: A NIdaq type.
  • tasktype::String: A task type. Options are "AI","AO","DI","DO".
  • channel::String: A channel name, obtained from showchannels.
  • min_val, max_val: for "AI" and "AO", the voltage range to create the channel with, both or neither (ArgumentError otherwise, and for "DI"/"DO"). They go to DAQmx as given, so DAQmx refuses a range the device lacks (error -200077 for 0-10 V on a USB-6008) rather than the driver choosing another. With neither, the driver queries the device: the widest range for "AI", the first for "AO".

Returns

  • t::DAQmx.Task: A DAQmx Task type.
source
MicroscopeControl.HardwareInterfaces.DAQInterface.setvoltage — Method
setvoltage(daq::NIdaq,t::DAQmx.AOTask,voltage::Array{Float64})

Set the voltage of an analog output task with an array of values.

Arguments

  • daq::NIdaq: A NIdaq type.
  • t::DAQmx.AOTask: A DAQmx AOTask type.
  • voltage::Array{Float64}: The voltages to set, unit: volt.

Returns

  • ret::Int: The number of samples written to the task.
source
MicroscopeControl.HardwareInterfaces.DAQInterface.setvoltage — Method
setvoltage(daq::NIdaq,t::DAQmx.AOTask,voltage::Float64)

Set the voltage of an analog output task.

Arguments

  • daq::NIdaq: A NIdaq type.
  • t::DAQmx.AOTask: A DAQmx AOTask type.
  • voltage::Float64: The voltage to set the task to, unit: volt.

Returns

  • ret::Int: The number of samples written to the task.
source
MicroscopeControl.HardwareInterfaces.DAQInterface.setvoltage — Method
setvoltage(daq::NIdaq,t::DAQmx.DOTask,voltage::Array{Float64})

Set the voltage of a digital output task with an array of values.

Arguments

  • daq::NIdaq: A NIdaq type.
  • t::DAQmx.DOTask: A DAQmx DOTask type.
  • voltage::Array{Float64}: The voltages to set (0 or 1).

Returns

  • ret::Int: The number of samples written to the task.
source
MicroscopeControl.HardwareInterfaces.DAQInterface.setvoltage — Method
setvoltage(daq::NIdaq,t::DAQmx.DOTask,voltage::Float64)

Set the state of a digital output task. For a single-line task voltage is a level, 0 or 1; for a port-wide task it is the port word. See do_port_word, which explains why those differ.

Arguments

  • daq::NIdaq: A NIdaq type.
  • t::DAQmx.DOTask: A DAQmx DOTask type.
  • voltage::Float64: 0 or 1 for a line task; the port word for a port task.

Returns

  • ret::Int: The number of samples written to the task.
source
MicroscopeControl.HardwareInterfaces.DAQInterface.showchannels — Method
showchannels(daq::NIdaq,channeltype::String,device::String)

List available channels of a given channel type and device.

Arguments

  • daq::NIdaq: A NIdaq type.
  • channeltype::String: A channel type. Options are "AI","AO","DI","DO","CI","CO".
  • device::String: A device name, obtained from showdevices.
source
MicroscopeControl.initialize — Method
initialize(daq::NIdaq)

No-op. DAQmx tasks are created and deleted per operation via createtask and deletetask, so NIdaq has no persistent hardware connection to open.

source
MicroscopeControl.shutdown — Method
shutdown(daq::NIdaq)

No-op. DAQmx tasks are created and deleted per operation via createtask and deletetask, so NIdaq has no persistent hardware connection to close.

source

Opal Kelly XEM FPGA

MicroscopeControl.HardwareImplementations.OK_XEM.XEM — Type
XEM(; fpgalabel="", serialnumber="", devicehandle=0, exposure_bit=2.56, exposure_time=30e3,
    enable=0.0, task_trigger=0, task_enable=0, bitfile="", volt_bit="0000", daq=NIdaq()) -> XEM

Opal Kelly XEM FPGA board, driven through the FrontPanel DLL (okFrontPanel.dll), with the camera trigger and enable lines carried by a National Instruments digital-output task (daq). The keyword constructor only fills the struct; it opens nothing. initialize creates the FrontPanel handle, opens the first board listed (index 0), configures it with the bitfile at bitfile, and sets fpgalabel (model) and serialnumber. shutdown destroys the handle. [limitation] The DLL path is C:\Program Files\Opal Kelly\FrontPanelUSB\API\lib\x64\okFrontPanel.dll, so the driver works only on Windows with FrontPanel installed. initialize reports a missing board or a failed open or configure with println and returns without throwing, so a returned call does not mean the board is configured. It also passes the serial string to the DLL as a byte vector with the zero bytes removed, so the string handed over has no terminating NUL.

Fields: exposure_bit (us per exposure step, default 2.56), exposure_time (us, the last rounded exposure setexposure was asked for, stored before its DAQ write, default 30000), enable (default 0.0; not updated by enable), volt_bit (the four exposure code bits of the last setexposure(fpga, exposure_time) request that could be encoded, as a string, even if its DAQ write then failed; a request outside the encodable range throws before changing it, and the form that takes the digits directly updates neither field), task_trigger and task_enable (NI tasks created by setupIO), devicehandle, bitfile and daq.

source
MicroscopeControl.HardwareImplementations.OK_XEM.activetriggerin — Method
activetriggerin(fpga::XEM, epaddr::UInt8, bitmask::UInt32) -> nothing

Fire one trigger-in bit on FrontPanel endpoint epaddr (ActivateTriggerIn). Despite the argument's name, bitmask is passed as the bit number, as the FrontPanel API takes it, converted to a C int (a value above typemax(Cint) throws InexactError) and otherwise unchecked (which bits exist depends on the board's trigger endpoint): 4 fires bit 4, not the bit that the mask 0x04 would select. [limitation] A failing call is reported with println and the function returns nothing as on success; nothing is thrown.

source
MicroscopeControl.HardwareImplementations.OK_XEM.enable — Method
enable(fpga::XEM, enable::Float64) -> nothing

Write enable to the enable task fpga.task_enable (see setupIO) through NIDAQcard.setvoltage; fpga.enable is not updated. The unexported constants are TRIGGER_OFF (0.0, the struct's enable default) and TRIGGER_ON (16.0). NIDAQcard.setvoltage accepts only 0 or 1 for a single-line digital task and the port word for a port task, so TRIGGER_ON is accepted only when the task channel is a port.

source
MicroscopeControl.HardwareImplementations.OK_XEM.setexposure — Method
setexposure(fpga::XEM, exposure_time::Float64) -> Matrix{Float64}
setexposure(fpga::XEM, volt_digit::Array{Float64}) -> nothing

First form: set the exposure exposure_time (us) on the FPGA through the four trigger lines. With div = ceil(exposure_time / fpga.exposure_bit), it finds i in 0:15 with 2^i <= div < 2^(i+1), stores the rounded exposure, 2^i * fpga.exposure_bit us, in fpga.exposure_time (printed) before the DAQ write, so the field holds the requested rounded value even if the write then throws (there is no readback from the FPGA), keeps the four low bits of i as the string fpga.volt_bit, and writes the digits [8, 4, 2, 1] (each kept where its bit is 1, else 0) to fpga.task_trigger. It returns the 1 by 4 digit matrix. So the exposure is rounded to a power-of-two multiple of exposure_bit, not the requested value. [limitation] A request with div below 1 or at or above 65536 matches no i and the call fails with an error before anything is written.

Second form: write volt_digit to fpga.task_trigger as is, without touching fpga.exposure_time or fpga.volt_bit. Requires setupIO first.

source
MicroscopeControl.HardwareImplementations.OK_XEM.setupIO — Method
setupIO(fpga::XEM) -> nothing

Create the two NI digital-output tasks the FPGA board is driven through. With devs the DAQ devices found by showdevices, showchannels(daq, "DO", devs[1]) lists the digital-output channels of the first device: the exposure-trigger task takes channels 4, 3, 2 and 1 of that list (added in that order) and the enable task takes channel 5. The tasks are stored in fpga.task_trigger and fpga.task_enable. Needs NI-DAQmx; nothing is sent to the FPGA.

source
MicroscopeControl.HardwareImplementations.OK_XEM.setwirein — Method
setwirein(fpga::XEM, epaddr::UInt8, value::UInt32) -> nothing

Set FrontPanel wire-in endpoint epaddr to value and apply it to the FPGA (SetWireInValue with mask 0xFFFF, so only the low 16 bits of value are written, then UpdateWireIns). [limitation] A failing FrontPanel call is reported with println and the function returns nothing as on success; nothing is thrown.

source

Opal Kelly XEM as a DAC

MicroscopeControl.HardwareImplementations.XEM_DAC.XEM_dac — Type
XEM_dac(; xem=XEM()) -> XEM_dac

Four-channel DAC (DAC8814 per the source comment; output range -10 to 10 V) on an Opal Kelly FPGA board, set through FrontPanel wire-in endpoints. The one field, xem, is the XEM that carries the connection; the keyword constructor wraps the given XEM (default XEM()) and opens nothing. The board is opened and configured with initialize(dac.xem) (set dac.xem.bitfile first); there is no initialize or shutdown method for XEM_dac itself. Channels A to D are wire-in endpoints 1 to 4 and start and stop write endpoint 5.

source
MicroscopeControl.HardwareImplementations.XEM_DAC.setvoltageA — Method
setvoltageA(fpga::XEM_dac, va::Float64)

Set the output voltage for channel A of the DAC.

Arguments

  • fpga::XEM_dac: An instance of the XEM_dac hardware implementation.
  • va::Float64: The desired output voltage for channel A (in volts).

Throws an ErrorException if va is outside -10 to 10 V. A failed FPGA write is printed, not thrown. Returns nothing.

source
MicroscopeControl.HardwareImplementations.XEM_DAC.setvoltageAll — Method
setvoltageAll(fpga::XEM_dac, va::Float64, vb::Float64, vc::Float64, vd::Float64)

Set the output voltages for all four channels of the DAC.

Arguments

  • fpga::XEM_dac: An instance of the XEM_dac hardware implementation.
  • va::Float64: The desired output voltage for channel A (in volts).
  • vb::Float64: The desired output voltage for channel B (in volts).
  • vc::Float64: The desired output voltage for channel C (in volts).
  • vd::Float64: The desired output voltage for channel D (in volts).

Throws an ErrorException if any voltage is outside -10 to 10 V; all four are converted before any is written. A failed FPGA write is printed, not thrown. Returns nothing.

source
MicroscopeControl.HardwareImplementations.XEM_DAC.setvoltageB — Method
setvoltageB(fpga::XEM_dac, vb::Float64)

Set the output voltage for channel B of the DAC.

Arguments

  • fpga::XEM_dac: An instance of the XEM_dac hardware implementation.
  • vb::Float64: The desired output voltage for channel B (in volts).

Throws an ErrorException if vb is outside -10 to 10 V. A failed FPGA write is printed, not thrown. Returns nothing.

source
MicroscopeControl.HardwareImplementations.XEM_DAC.setvoltageC — Method
setvoltageC(fpga::XEM_dac, vc::Float64)

Set the output voltage for channel C of the DAC.

Arguments

  • fpga::XEM_dac: An instance of the XEM_dac hardware implementation.
  • vc::Float64: The desired output voltage for channel C (in volts).

Throws an ErrorException if vc is outside -10 to 10 V. A failed FPGA write is printed, not thrown. Returns nothing.

source
MicroscopeControl.HardwareImplementations.XEM_DAC.setvoltageD — Method
setvoltageD(fpga::XEM_dac, vd::Float64)

Set the output voltage for channel D of the DAC.

Arguments

  • fpga::XEM_dac: An instance of the XEM_dac hardware implementation.
  • vd::Float64: The desired output voltage for channel D (in volts).

Throws an ErrorException if vd is outside -10 to 10 V. A failed FPGA write is printed, not thrown. Returns nothing.

source

DAQ internals

MicroscopeControl.HardwareInterfaces.DAQInterface.UnreleasedTask — Type
UnreleasedTask(task, cause)

Thrown by a createtask whose channel setup failed AND whose cleanup of the allocated task failed too: task is the handle that is still allocated and must be deleted later (deletetask), cause the setup error. A caller that owns the output keeps task so a later shutdown can retry. Public, not exported.

source
MicroscopeControl.HardwareImplementations.NIDAQcard.do_port_word — Method
do_port_word(channels::Vector{String}, value::Float64) -> UInt32

The UInt32 to hand DAQmxWriteDigitalScalarU32 for a digital output task holding channels, given a requested value.

Digital scalar writes are port format: bit n is line n of the port, even when the task holds a single line. So a task on Dev1/port0/line1 written with 1 drives nothing — bit 0 is line 0, which is not in the task. Only line 0 ever worked. Confirmed on hardware (NI-DAQmx 23.5, USB-6008): a TTL shutter on port0/line1 did not respond to 1, and did respond to 2.

So the word depends on what the task actually holds:

  • one line channel (.../portN/lineK) — the value is a level, and the word is UInt32(value != 0) << K. Any value other than 0 or 1 is rejected: it is almost certainly a caller pre-shifting to work around this very defect, and shifting it again would drive a different line.
  • one port channel (.../portN) — the value is already a port word and is passed through. The old behaviour was correct for this case.
  • more than one channel — rejected. A single scalar across several lines is ambiguous, and guessing is what produced the defect.

This is split out from setvoltage so it can be tested without a DAQ.

source
MicroscopeControl.HardwareImplementations.XEM_DAC.volts_to_code — Method
volts_to_code(v::Float64; vmin::Float64=-10.0, vmax::Float64=10.0)

Convert a voltage value to hex code for the DAC8814.

Arguments

  • v::Float64: The voltage value to convert (in volts).
  • vmin::Float64: The minimum voltage the DAC can output (default: -10.0 V).
  • vmax::Float64: The maximum voltage the DAC can output (default: 10.0 V).

Returns

  • UInt32: The corresponding hex code.
source