NI analog write task
Write command channel values to the analog outputs of an NI device.
An NI analog write task writes the values of command channels to the analog outputs of
one NI device. For example, a setpoint on a schematic can drive a proportional valve
from ao0 of a USB-6343.
Each output has a command channel and a state channel. Each command writes the last value of every output of the task in one call.
Before you start, you need a configured device.
Create an NI analog write task
Console
Python
TypeScript
- In the devices toolbar, right-click the device and select Create analog write task.
- Set State update rate.
- Click Add channel. In Source, select the Port.
- In Signal, select the Channel type, then set its fields. See the channel reference.
- Click (Start).
The Console creates the channels. For a device with the identifier dev1, port 0 gives
the command channel dev1_ao_0_cmd with the index dev1_ao_0_cmd_time, and the state
channel dev1_ao_0_state with the index dev1_ao_state_time.
Python does not create channels. Create them first.
import synnax as sy
from synnax import ni
dev = client.devices.retrieve(key="<device key>")
state_time = client.channels.create(
name="dev1_ao_state_time", is_index=True, data_type=sy.DataType.TIMESTAMP
)
state = client.channels.create(
name="valve_setpoint_state",
index=state_time.key,
data_type=sy.DataType.FLOAT32,
)
cmd_time = client.channels.create(
name="valve_setpoint_cmd_time", is_index=True, data_type=sy.DataType.TIMESTAMP
)
cmd = client.channels.create(
name="valve_setpoint_cmd", index=cmd_time.key, data_type=sy.DataType.FLOAT32
)
task = ni.AnalogWriteTask(
name="Valve setpoint",
device=dev.key,
channels=[
ni.AOVoltageChannel(
port=0,
min_val=0,
max_val=10,
cmd_channel=cmd.key,
state_channel=state.key,
)
],
)
client.tasks.configure(task)
task.start()Python creates the task on the embedded Driver of the Core. For a device on another Driver, use the Console or TypeScript.
TypeScript does not create channels. Create them first, as in the Python example.
import { ni } from "@synnaxlabs/client";
const dev = await client.devices.retrieve({ key: "<device key>" });
const task = await client.tasks.create({
name: "Valve setpoint",
rack: dev.rack,
type: "ni_analog_write",
config: ni.analogWriteConfigZ.parse({
device: dev.key,
channels: [
{
type: "ao_voltage",
port: 0,
minVal: 0,
maxVal: 10,
cmdChannel: cmd.key,
stateChannel: state.key,
},
],
}),
});
await task.start();Task options
Channel reference
Every channel has a port (Port), a cmd_channel, and a state_channel.
Each table shows the Console label of a setting, with its Python field under it. Voltage and current channels also have Custom scaling.
Custom scaling
Channels with Custom scaling convert the raw value before the Driver writes it. In
the Console, the options are in the Scale section of the channel. In code, set
custom_scale to one of these scales:
Every scale except None also has Prescaled units (pre_scaled_units, Volts)
and Scaled units (scaled_units).
Troubleshooting
- The outputs stop changing after a command.
- Cause
A command was outside the Range of its channel. NI-DAQmx rejects it with no message, and because each command writes every output, all later commands fail too. The state channels still show the commands that were not written.
- Fix
Send a value inside the range to that channel. The task then writes again.
- Cause
Two channels use the same port.
- Fix
Change the port of one of them.
For other problems, see Configure an NI device and Troubleshoot hardware.