OptrisOptrisDocumentation

Documentation/OTC SDK/OTC SDK — C++ API referencev12.0

optris::sdk::pif::PifController

PIF (Process Interface) controller for industrial I/O.

Threading: the I/O calls are safe against the SDK's frame: Threading: the I/O calls are safe against the SDK's frame thread; the four that redescribe the interface are not A PIF on a real camera is driven by the SDK's own frame thread as well as by the application: the inputs a camera reports arrive with each frame, and the controller decodes them, publishes them to the signal bus and writes the internal-temperature outputs from there. A lock of your own serialises your threads against each other and cannot serialise them against that one, which is why this note exists at all.

Safe to call while the camera is streaming, and safe from several application threads at once:

  • Configure() and GetConfiguration();
  • every per-channel read and write -- ReadDigitalInput(), ReadAnalogInput(), ReadRawAnalogInput(), WriteDigitalOutput(), WriteAnalogOutput(), ReadDigitalOutput(), ReadAnalogOutput();
  • ReadAll(), Poll(), SetOutputsBlocked(), AreOutputsBlocked();
  • GetExternalProbeTemperature(), GetFailSafeHealth();
  • OnDecodedInput(), which is additionally fenced against its own delivery -- it returns only once no handler is still running, which is what makes it safe to free what the handler captured afterwards;
  • IsFailSafeArmed() through the SDK's internal face, which is one atomic the fail-safe watchdog on the frame thread depends on.

The channel table and the frame's input counts are each published as a whole, behind an atomic pointer, so a frame pass reads one consistent picture for its whole length and an application thread never has to wait for one. A table applied by Configure() becomes visible to the frame pass on the next frame rather than part-way through the one in flight -- the alternative is a frame built from half of one table and half of another, which is a reading no configuration asked for. Everything else above is behind a mutex the controller never holds across a call that leaves it.

Still NOT safe against the frame thread, so serialise these yourself or call them before Start():

  • SetIdentity(), RedetectIdentity() and AdoptIdentity(), which redescribe the interface -- its kind, its pin counts -- under a frame pass that is reading those counts;
  • SetAnalogCalibration(), which replaces the device calibration;
  • SetFailSafe(), which the watchdog on the frame thread reads (only its enabled flag is fenced, by an atomic).

Those four are declaration-time calls rather than per-frame ones, so the restriction is narrow -- but it is real, and this list is deliberately the whole of what is covered. Do not read it as wider.

Public types

InputHandler

std::function< void(uint8_t index, PifChannelKind kind, float value)> InputHandler

A configured input yielded a new value in its own unit -- emissivity, ambient temperature, or a named uncommitted value. An ambient temperature is in the SDK's temperature unit (Celsius unless SetTemperatureUnit() chose Fahrenheit); an uncommitted value keeps its own named unit and is not converted.

This exists because those three feed the measurement pipeline, which is a data path rather than an alarm. Anything that wants to react to a PIF value writes a Rule instead.

Observing only. Subscribing does not take the value away from the measurement: the SDK's own feed into the pipeline is separate and runs first, so by the time this is called the radiometry already carries the value being handed over. Clearing the handler stops the notifications and nothing else.

A handler installed while the camera is running is called once with each configured input's current value, rather than staying silent until something changes.

Public functions

~PifController

~PifController()

PifController

PifController(PifController &&) noexcept

operator=

PifController & operator=(PifController &&) noexcept

PifController

PifController(const PifController &)=delete

operator=

PifController & operator=(const PifController &)=delete

GetIdentity

auto GetIdentity() const -> const PifIdentity &

Who is attached, and whether that was detected or declared.

GetHardwareInfo

auto GetHardwareInfo() const -> const PifHardwareInfo &

IsConnected

auto IsConnected() const -> bool

SetIdentity

void SetIdentity(PifDeviceKind kind, uint8_t device_count=1)

State what is attached, when the camera cannot tell.

Required on the PI 1 family, for a proprietary PIF, for an external temperature probe, and on an autonomous camera whose type switch disagrees with what is physically there -- see PifDetection. On a camera that can detect its PIF this overrides what it reported.

This is not how a PIF connected or powered up after the camera is brought into use. RedetectIdentity() is, on every camera that can detect its PIF at all -- it asks the camera again, and a PIF that was not there at open is found. Declaring is for the kinds a camera cannot see. On a PI camera other than the PI 1 the type comes from the PIF's own EEPROM, and a Standard or Industrial declaration is refused in every state, attached or not; GetSupportedDeviceKinds() does not offer them, so an application that builds its choices from that list never reaches the refusal. The PI 1 family is the other way round: it cannot read the PIF EEPROM at all, so re-detection never finds anything and declaring is the only route.

Applying it selects the type on the device first, then re-derives the capabilities from what was actually applied and clears the channel table: a table configured against one PIF does not carry over to a different one. The declared PIF's channels are usable straight away -- no reopen in between.

All or nothing. A camera that refuses the declaration leaves this controller exactly as it was: the same identity, the same capabilities, the same channel table, the same outputs and the same alarm rules holding them. A later Configure() is unaffected by the refusal.

Parameters

Name Description
kind what is attached.
device_count physical devices, for a stackable PIF.

Exceptions

Name Description
PifException with code() PifError::kUnsupported if kind is not in GetSupportedDeviceKinds(): this camera never takes it, and nothing is sent.
PifException with code() PifError::kInvalidState if the camera declines because of what is on the connector right now, most often another PIF the firmware can see, which has to come off before a Proprietary interface or an external probe can be declared in its place. The message names what is in the way.

RedetectIdentity

void RedetectIdentity()

Ask the camera again what is attached.

Two uses, and nothing checks which one you are in: this runs from any state.

The first is a PIF connected or powered up after the camera was opened. From PifDetection::kUnknown with PifDeviceKind::kNone -- an empty socket at open -- this re-runs discovery and finds it, and on a camera that reads its PIF from an EEPROM it is the only route: SetIdentity() refuses the kinds such a camera can see for itself. Offer it as "detect again" on a card that is reporting no interface, not only on one reporting a declared one. Finding nothing is an answer rather than an error, so it is safe to offer unconditionally.

The second is the way out of a declaration. SetIdentity() overrides detection and nothing used to undo it: an application that declared Standard to try something had to close and reopen the camera, restarting the stream and re-applying every region and rule, to get back to what was actually attached. This asks the camera again -- the PIF EEPROM on a PI, the on-board PIF information on an autonomous camera -- and rebuilds the identity from the answer.

GetIdentity().detection afterwards is PifDetection::kDetected when the camera named a kind and PifDetection::kUnknown when it named none. Never kDeclared: the SDK's previous belief is discarded before the question is asked, so whatever comes back came from the camera.

That is worth reading literally on an autonomous camera, where a declaration is written into the camera rather than held here. The camera then reports the declared kind as its own, and a re-detection confirms it rather than undoing it -- correctly, and it is still kDetected, because the camera is what answered. To undo such a declaration, declare PifDeviceKind::kNone or reopen the camera, which does not carry the declaration over.

Otherwise identical to a declaration in what it touches: the capabilities are re-derived and the channel table is cleared, because a table configured against one PIF does not carry over to another. All or nothing, as SetIdentity() is -- a re-detection that fails leaves this controller exactly as it was.

Exceptions

Name Description
PifException with code() PifError::kUnsupported on a backend that cannot be asked again. A camera that can be asked and sees nothing is not an error: it answers kNone / kUnknown, honestly.

GetSupportedDeviceKinds

auto GetSupportedDeviceKinds() const -> std::vector< PifDeviceKind >

The PIF kinds that can be declared on this camera.

A user interface should offer exactly these, plus a way back to detection (RedetectIdentity()). Every one of them is a kind SetIdentity() will attempt; a kind outside the list is refused here without reaching the camera.

It is not the list of kinds that can be attached. On a PI camera a Standard or Industrial PIF is read from its EEPROM and can never be declared -- there is nothing an application could usefully tell the camera that the EEPROM has not already told it -- so those kinds are absent here while being perfectly normal values of GetIdentity().kind. Render the current kind from the identity; offer this list as the choice. Conflating the two offers Standard on cameras that refuse it in every state.

A family property, so it does not move when a declaration or a re-detection does. Whether a listed kind is declarable at this moment is a separate question, answered by attempting it: a camera with a PIF already on the connector declines Proprietary with PifError::kInvalidState and a sentence naming what is in the way.

Configure

void Configure(std::vector< PifChannelConfig > channels)

Apply a whole channel table, replacing whatever was configured.

The table is validated in full before anything is written, so a rejected configuration leaves the device exactly as it was and the error names the specific channel and problem. Every configured output is then re-asserted, even one whose settings did not change -- changing a channel's levels has to reach the pin.

Exceptions

Name Description
PifException on a table the device will not take. The message names the specific channel and problem, and code() distinguishes a request that does not make sense (PifError::kInvalidParameter) from a channel index beyond the hardware's count (PifError::kOutOfRange). On a Stackable PIF, PifError::kUnsupported refuses Frame Sync on an analog output, which that box does not pulse, and Region of Interest or Internal Temperature on an analog output while the fail-safe beats on the dedicated terminals (see SetFailSafe()).

GetConfiguration

auto GetConfiguration() const -> std::vector< PifChannelConfig >

The channel table in force, as a copy.

By value, and that is the fix rather than an inefficiency. It used to return a reference straight into the controller's own vector, and the SDK's frame thread holds that reference across a loop -- ProcessInterfaceNative::onFrameMetadata walks it to write the internal-temperature outputs and calls WriteAnalogOutput() inside the loop. Configure() replaces the vector wholesale from an application thread (channels = std::move(channels)), so the buffer the loop was walking had been returned to the allocator: a heap-use-after-free, observed on 6 of 20 ThreadSanitizer runs of PifControllerThreads.ReconfiguringWhileFramesAreIngested.

No lock inside this accessor could have fixed that -- the reference outlives the hold, which is the whole point of a reference. A copy is also the more correct answer for the caller that found it: the frame pass wants the table as the frame arrived, not a table that may change under it half way down.

Source-compatible with the reference it replaces: both for (const auto &c : GetConfiguration()) and const auto &table = GetConfiguration(); keep working, because the temporary's lifetime is extended to the reference's. A caller that wants to avoid the copy on a hot path should take it once and reuse it rather than calling this per channel.

SetAnalogCalibration

void SetAnalogCalibration(const PifAnalogCalibration &calibration)

Set the device-wide analog calibration. Pushed down from the layer that owns calibration data; the PIF never reads a calibration file.

Exceptions

Name Description
PifException with code() PifError::kInvalidParameter for a calibration the hardware cannot be given.

GetAnalogCalibration

auto GetAnalogCalibration() const -> const PifAnalogCalibration &

SetFailSafe

void SetFailSafe(const PifFailSafeConfig &config)

Arm or disarm the fail-safe line.

Exceptions

Name Description
PifException with code() PifError::kOutOfRange if period exceeds PifHardwareInfo::fail_safe_max_period. Refused rather than clamped: the caller would otherwise read a different value back from GetFailSafe() than it wrote, and a period the interface's relay cannot hold produces precisely the fault this line exists to report.
PifException with code() PifError::kUnsupported for on_alarm or on_flag_timeout, which were never implemented.
PifException with code() PifError::kUnsupported for the dedicated line on a Stackable PIF while an analog output is in Region of Interest or Internal Temperature. On that box the host's per-frame analog writes were measured to move the heartbeat off the fail-safe relay and onto the analog output.

Arming parks the line at its inactive level first, so the first heartbeat is an edge; moving the line elsewhere or disabling it parks the line it leaves -- a relay open, an analog output at the bottom of its level pair.

GetFailSafe

auto GetFailSafe() const -> PifFailSafeConfig

The fail-safe configuration in force, as a copy.

By value, for the reason GetConfiguration() is. It returned a reference into the controller's own storage, and a reference outlives any lock taken inside the accessor -- so a caller reading fields off it while another thread called SetFailSafe() read a struct being rewritten under it, and no amount of locking inside this function could have covered that. const auto &c = pif.GetFailSafe(); keeps compiling and keeps meaning the same thing.

GetFailSafeHealth

auto GetFailSafeHealth() const -> PifFailSafeStatus

What the line is actually doing, measured rather than asserted.

Computed at the moment of the call, from the time since the last edge -- so a heartbeat that has stopped is reported as stopped, which a status updated only when an edge arrives could never do.

ReadDigitalInput

auto ReadDigitalInput(uint8_t index) -> DigitalState

Read a digital input.

Most PIFs cannot be asked on demand: their inputs arrive with a frame. Until the first frame has brought one, this returns DigitalState::Unknown -- a reading, not a failure, because a caller polling at the frame rate should not pay for an error per frame while the stream starts.

That is also why converting this surface from a Result to an exception costs a poll loop nothing: "no reading yet" was never the error arm. It throws when the request is wrong: an index the hardware does not have, or a PIF that is not there, which is a wiring fault rather than a per-frame outcome.

Exceptions

Name Description
PifException with code() PifError::kOutOfRange for an index beyond the hardware's channel count, kNoDevice when no PIF is attached, or kNotConnected when one is attached and unreachable.

WriteDigitalOutput

void WriteDigitalOutput(uint8_t index, DigitalState state)

Drive a digital output.

Exceptions

Name Description
PifException as ReadDigitalInput(), plus code() PifError::kInvalidParameter when the channel at index is not a digital output.

ReadDigitalOutput

auto ReadDigitalOutput(uint8_t index) -> DigitalState

What this SDK last drove onto a digital output.

DigitalState::Unknown when nothing has driven it since the PIF was attached or last configured: a reading, not a failure, and the same answer IoSnapshot::digital_outputs gives for that pin on a camera. An output's level is never read off an input. Up to 12.0.28 an undriven output answered with the level on the digital input of the same index on a simulated PIF, and threw PifError::kUnsupported on a camera.

Exceptions

Name Description
PifException as ReadDigitalInput().

ReadAnalogInput

auto ReadAnalogInput(uint8_t index) -> float

Read an analog input in the channel's configured unit.

On a channel whose own unit is a temperature (see PifChannelConfig), that unit is the SDK's temperature unit (Celsius unless SetTemperatureUnit() chose Fahrenheit).

NaN until the first frame has brought a reading, on a PIF whose inputs arrive only with frames. See ReadDigitalInput, including why that costs a poll loop nothing.

Exceptions

Name Description
PifException as ReadDigitalInput().

ReadRawAnalogInput

auto ReadRawAnalogInput(uint8_t index) -> float

One analog input as it is on the wire, in volts, before any channel decode.

The single-read counterpart of IoSnapshot::raw_analog_inputs. ReadAnalogInput() answers in the channel's own unit, so on a configured channel the wire reading was unobtainable: with gain 0.1 and 4.0 V on the pin, ReadAnalogInput() answers 40.0 and no call answered 4.0.

Always volts, whether or not a channel is configured over the pin. NaN before the first reading has arrived, like ReadAnalogInput().

A handler registered with OnDecodedInput() may call this for the index it was handed: the decoded value it received and the reading this returns come from the same poll.

Exceptions

Name Description
PifException as ReadAnalogInput().

GetExternalProbeTemperature

auto GetExternalProbeTemperature() const -> std::optional< float >

The external temperature probe's reading, in the SDK's temperature unit (Celsius unless SetTemperatureUnit() chose Fahrenheit), or nullopt if no frame has carried one.

The probe is its own measurement, not a channel: it has no index, no gain and no direction, and version 11 hid it in the first slot of the PIF analog output array. Declaring PifDeviceKind::kTemperatureProbe made the identity read back and nothing returned a temperature -- the frame path decoded the probe and the controller kept it, reachable only from inside the SDK.

nullopt on a camera with no probe fitted, and until the first frame that carries a reading. It is also published as SignalField::kProbeTemp on the device domain, so an interlock on the reference probe is an ordinary alarm rule.

Only while the interface is a temperature probe: declare it with SetIdentity(PifDeviceKind::kTemperatureProbe), which is the only way a probe is ever known -- no camera can detect one. On any other identity this is nullopt and the signal is not published, whatever the frame carries. The telemetry bytes a probe reading arrives in carry other values on cameras without one: 10.125 C was read from a PI 640 with an Industrial (mA) PIF, and 34.1 C from a PI 4x0i with nothing on the connector, up to 12.0.20. PIX Connect decides it the same way, from the user's choice of the probe as the reference source.

Whether a frame carries a reading is decided at the frame decode, per camera family, by the probe's I2C bus answering validly: a read nothing acknowledged (the bus's all-zero or all-ones non-answer) counts as no probe, never as a temperature, so an unpopulated connector reads nullopt rather than a full-scale 511.9375 C.

nullopt again the moment a frame stops carrying a reading, and the signal is invalidated with it, so a rule on the probe goes AlarmState::kOrphaned and a Compare::kInvalid watchdog on it fires. This used to hold the last value a frame had carried, presented as current, for the life of the Imager -- and a stale temperature that looks live is worse than an error, because it gets used. There is no hold-off: the first frame without a reading is the one that clears it.

Note: What a camera's firmware writes once a probe stops answering has not been measured on any camera here. This reports the loss faithfully when the telemetry reports it; a firmware that latched the last value instead would be indistinguishable from a steady probe, and no SDK change can close that.

WriteAnalogOutput

void WriteAnalogOutput(uint8_t index, float value)

Drive an analog output, in the channel's configured unit.

On a channel whose own unit is a temperature (see PifChannelConfig), that unit is the SDK's temperature unit (Celsius unless SetTemperatureUnit() chose Fahrenheit), and the channel's gain and offset turn it into the level on the wire. ReadAnalogOutput() reads back that level, not the temperature.

The unit is the channel's output_range own -- volts in 0-10 V, milliamperes in 0-20 mA and 4-20 mA, never amperes. That is the only unit any analog value above the wire is in, on every path: this call, both of an alarm output's levels, the shutter's three levels, the frame-sync pulse height, the fail-safe heartbeat and the level a blocked output holds. The converter is the one place it changes, and it says so in its name (rangeUnitToDacCode).

Version 11 settled the same question the other way, in amperes (11.4.13), because its ranges carried no unit to agree with; v12's AnalogRange carries a unit label and this is it. A value in amperes therefore reads as a thousandth of what was meant, and in 4-20 mA used to come out at full scale whatever it was.

The value is clamped into the range, and a non-finite value is refused rather than clamped -- see the implementation note.

Exceptions

Name Description
PifException with code() PifError::kInvalidParameter for a non-finite value, or for a channel at index that is not an analog output; kOutOfRange for an index beyond the hardware's channel count.

ReadAnalogOutput

auto ReadAnalogOutput(uint8_t index) -> float

What this SDK last drove onto an analog output, as it is on the wire: in the unit of the channel's output_range (volts in 0-10 V, milliamperes in 0-20 mA and 4-20 mA), and in volts on an output with no channel configured.

The same number in Celsius and in Fahrenheit. A wire value is never a temperature, so this does not follow the SDK's temperature unit. On a channel whose own unit is a temperature it is therefore not the number WriteAnalogOutput() took: 50 C written at 0.1 V per degree and 0.5 V at zero (122 F in Fahrenheit) reads back 5.5, in either unit. Up to 12.0.27 Fahrenheit converted the 5.5 as if it were a temperature and answered 41.9. A temperature channel's value goes in through WriteAnalogOutput(); the reading back is for checking the wire.

Answered from the shadow where the port cannot read a pin back, which no PIF can today, so this is "what was asked for" rather than a measurement of the wire.

NaN when nothing has driven it since the PIF was attached or last configured, the analog spelling of DigitalState::Unknown, which cannot be mistaken for a level the way 0.0 can. Never the reading of an input: up to 12.0.28 an undriven output answered with the analog input of the same index on a simulated PIF, and threw PifError::kUnsupported on a camera.

Exceptions

Name Description
PifException as WriteAnalogOutput().

ReadAll

auto ReadAll() -> IoSnapshot

Read every channel in one transaction, so the values cannot skew against each other.

Before the first frame on a PIF whose inputs are frame-bound, the snapshot comes back with inputs_known false and its inputs Unknown / NaN. The outputs are valid throughout, so, as with ReadDigitalInput(), the frame-bound case is a reading rather than the error arm and a poll loop pays nothing for the conversion.

Exceptions

Name Description
PifException with code() PifError::kNoDevice when no PIF is attached, or kNotConnected when one is attached and unreachable.

SetOutputsBlocked

void SetOutputsBlocked(bool blocked)

Hold every PIF output at its inactive level without losing the configuration or the alarm state underneath. Used while a process is knowingly disturbed; the outputs resume when released.

AreOutputsBlocked

auto AreOutputsBlocked() const -> bool

OnDecodedInput

void OnDecodedInput(InputHandler handler)

Poll

void Poll()

Advance the controller: read inputs, decode them, publish signals and dispatch handlers.

Call it as often as the process needs. When a stream is running, per-frame inputs additionally arrive bound to their frame, which is the authoritative pairing.

Warning: Poll() does not advance the fail-safe line, and calling it is not what keeps the heartbeat alive. It used to be: the line alternated for as long as the application kept calling Poll(), consulting no health condition at all, so a disconnected camera or a stalled pipeline still left a PLC reading "healthy". The line is driven from the fail-safe watchdog and from nothing else, and the watchdog reaches it only on a health check that passed. See the implementation note on Poll() and PifAccess::StepWatchdog.

Exceptions

Name Description
PifException with code() PifError::kNoDevice when no PIF is attached, or kNotConnected when one is attached and unreachable. A poll that simply found nothing new is success, not a refusal, so a loop over a working PIF never throws.