Skip to content

Andor SDK3 camera driver: binding, lifecycle and single frame - #100

Merged
kalidke merged 13 commits into
mainfrom
andor-sdk3-core
Dec 3, 2026
Merged

kalidke merged 13 commits into
mainfrom
andor-sdk3-core

Conversation

@kalidke

@kalidke kalidke commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

The first of five pull requests that add Andor camera drivers, written from Andor's headers and manuals: this one adds
AndorSDK3Camera, for cameras that use Andor's SDK3 library (the Zyla family), up to a single-frame capture.
Sequences, streaming and live view are the next pull request; the SDK2 driver (the Luca) follows.

Not breaking: everything is new. The version stays 0.2.7-DEV.

What it adds

  • AndorSDK3Camera <: Camera, built by keyword with a pure constructor; the camera is opened by serial number in
    initialize, never by index (an index can select one of SDK3's simulated cameras).
  • The binding: atcore.jl holds one @ccall per SDK3 function the driver uses (27), bound against atcore.h, and
    checked.jl one function per call that throws AndorSDK3Error, naming the call and its code, unless the library
    answered success. Every call takes an SDK3Binding value as its first argument; that value is the test seam, so the
    tests use a recording fake and no method is replaced. dev/andor/check_atcore_signatures.jl compares every binding
    with the header's declaration (27 bound, 0 differ).
  • Lifecycle: initialize sends the driver's defaults, the caller's ordered list of features (read back) and the
    three settings; a failure at any step closes what was opened and gives back the library count, and the error thrown is
    the first one. shutdown stops if acquiring and closes the handle, the close attempted even when the stop failed,
    and gives back the library count only after a close that succeeded; it throws the first failure. When the close succeeded the camera is :closed with its
    handle cleared; when it failed the camera keeps its handle, its record of the open index and its count of the library
    and stays open, so a second shutdown retries the close and discovery never reopens that index.
  • What the driver guarantees, and what it leaves alone. It calls the library correctly, checks every return code,
    throws a clear error, and cleans up with ordinary try/finally. The library is counted for the process under one
    lock (AT_InitialiseLibrary for the first user, AT_FinaliseLibrary when the last gives it back), with a plain list
    of the indexes this process has open, so a listing or a serial search never opens an index that a camera in the
    process holds (a held index is reported from its record, including after a failed close). Each camera has one lock around each group of library calls, released between polls, so a GUI's abort
    on another task runs while a capture waits; the order is the camera's lock, then the library's. Buffers queued to
    the library stay referenced from the camera until a flush or a close succeeds, and a buffer whose flush or close
    failed stays referenced for the life of the process. By design the package leaves
    stops and Ctrl-C to the system around it: a Ctrl-C may leave the camera in any state, and nothing here defers it,
    confirms a stop, or refuses to start after a failed one.
  • The stop follows Andor's manual: AcquisitionStop, then AT_Flush. capture runs it after the frame is read
    (a failed stop throws) and after any failure or the deadline (the first error is the one thrown). abort always
    stops an acquisition the driver started, even one whose one-frame cycle has ended; on an idle camera it sends the
    stop only when CameraAcquiring reads true, then flushes, so it does not throw there. shutdown from another task while capture waits replaces the acquisition, so the waiter throws and
    never calls the closed camera.
  • No task and a bounded wait: capture polls AT_WaitBuffer with a timeout of zero and sleeps in Julia; the
    deadline is fixed before the start on a monotonic clock and includes the cycle time, and a deadline that comes out
    infinite is refused. (AT_Command, the start and the stop, blocks until the camera has done it, as the manual says.)
  • Frames are (height, width), converted once with rowmajor_to_yx, row padding dropped (an image larger than
    its rows, with padding at the end, decodes from the rows); Mono16 and Mono12 are decoded, any other encoding
    throws.
  • The request is read once: capture reads the exposure, region, trigger mode and timeout once at its start, under
    the camera's lock, and checks and sends that one set. A model with AOIBinning and not the AOIHBin/AOIVBin pair
    (Neo) is set to 1x1 at initialize. setfeature! and capture refresh the reported exposure, readout time and
    pixel encoding after their last write.
  • The library is Libraries.libatcore (libatcore.so on Linux, found through the loader's path; the SDK3 folder
    on Windows), overridden by MICROSCOPECONTROL_LIBATCORE; a 32-bit Julia is refused before the library is loaded.
    When no camera is found on Linux (and none is a real camera that opened, or one that another program holds) the error
    names the two usual causes, a missing libusb-1.0-0-dev package and a low usbfs_memory_mb.
  • Andor's /tmp scratch files: on Linux, SDK3 keeps scratch files in /tmp (names beginning atdebug, atdev,
    patternout or andor_pat_temp_, read off the strings of the archived 3.15.30175 libraries), and when one belongs
    to another account (fs.protected_regular=2) it crashes the process inside AT_Open without reporting an error.
    Before every operation that may open an index, and before the library is loaded, the driver lists the folder and
    checks the owner of each matching entry (a link counts by its target's owner; an entry gone in between is skipped; an
    entry it cannot read throws) and throws, naming each file and its owner.
  • abort, setexposuretime!, setroi!, settriggermode!, export_state, and the module functions
    AndorSDK3.list_cameras, AndorSDK3.sensor_temperature, AndorSDK3.getfeature and AndorSDK3.setfeature! (not
    exported). The features the driver sets itself are refused by the constructor, initialize and setfeature! alike,
    under the library's own matching (spaces ignored, case ignored: "AOI HBin" is AOIHBin).
    live, sequence, getdata and getlastframe throw in this version, because the camera contract test requires the
    methods on every camera type.

Tests

test/andor_sdk3.jl, against a recording fake of atcore (test/andor_sdk3_fake.jl) with two camera profiles: a failure
code injected at every library call of initialize, capture and list_cameras, shutdown with each step failing alone
and together, a camera that wants a finished acquisition stopped and one that refuses the stop of a finished one, buffers
kept after a failed flush, abort and shutdown from a second task during a wait, concurrent opens, two cameras sharing
the library, frame decoding on non-square and padded frames, export_state through HDF5, and the /tmp check with a uid
that does not own the files. Source scan: no raw library name is called outside the binding. No test loads an Andor
library or touches a camera.

Checked: the full local suite and the docs build on Julia 1.13; the Andor testset on Julia 1.11.9.

Owed at the instrument

A check on a real camera, and these, none of which a fake can settle:

  • the first Julia dlopen of libatcore.so (Julia's default flags include RTLD_DEEPBIND, and the library is C++);
  • that AcquisitionStop on a camera that ended its fixed cycle by itself answers success or an error, since capture
    now throws when the stop after a read frame fails, and that a second AcquisitionStart needs the stop (the driver
    sends it after every acquisition it started, abort included; only abort on an idle camera asks CameraAcquiring
    first; Andor's acquisition/main.cpp and SingleScan.py examples send it after a finished fixed cycle and check the code);
  • that a second AT_Open of an index this process holds answers an error, not the same handle (the driver never
    reopens an index a camera object holds, even after that camera's close failed; it can reopen an index whose probe,
    opened by a listing or a serial search only to read it, failed to close, and then throws what the library answers);
  • that a second AT_Close after a failed one can succeed (a second shutdown retries the close);
  • re-initializing the library within one process (AT_InitialiseLibrary, AT_FinaliseLibrary, AT_InitialiseLibrary,
    as a listing followed by initialize makes), which the manual describes once per application;
  • whether ExposureTime stays writable under External Exposure mode (the driver sends it at every start);
  • ImageSizeBytes against AOIStride times AOIHeight at full frame and at a narrow AOI (the driver decodes the rows
    and accepts a larger image).

🤖 Generated with Claude Code

kalidke and others added 13 commits December 1, 2026 21:12
A driver for Andor cameras through the SDK3 library (the Zyla family),
tested against a recording fake of atcore; no test loads an Andor library.

- atcore.jl: one @CCall per vendor function the driver uses, bound from
  atcore.h; checked.jl: one function per call that throws AndorSDK3Error
  unless the library answered success. The binding takes an SDK3Binding as
  its first argument, which is the test seam.
- AndorSDK3Camera: pure constructor; initialize opens the camera by serial
  number, sends the driver's defaults, the caller's feature list (read
  back) and the three settings; shutdown stops, closes and gives back the
  library count, attempting every step. Library count and the cameras held
  are process-level records, so list_cameras and several cameras share one
  library.
- capture polls with a deadline and never blocks in the library; any
  failure or interrupt runs one confirmed stop, leaving the camera idle or
  unknown. Frames are (height, width), converted once with
  rowmajor_to_yx; Mono16 and Mono12 are decoded.
- abort, setexposuretime!, setroi!, settriggermode!, export_state,
  sensor_temperature, getfeature, setfeature!; live, sequence, getdata and
  getlastframe throw in this version.
- Libraries.libatcore (MICROSCOPECONTROL_LIBATCORE overrides it).
- dev/andor/check_atcore_signatures.jl compares each binding with the
  header's declaration.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…he library is used

On Linux, SDK3 3.15.30175 keeps scratch files under fixed names in /tmp.
When one exists but belongs to another account, fs.protected_regular=2
stops the library reopening it, the library does not check, and the process
dies with SIGSEGV inside AT_Open. Before the first library call (before
AT_InitialiseLibrary), with the real binding on Linux, initialize and
list_cameras now throw, naming each such file and its owner and saying to
remove them or run from the owning account.

The check is two small functions, foreign_scratch_files(paths, uid) and
require_own_scratch_files, so a test exercises them with a different user
id and no second account. A fake binding never reaches it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Julia 1.11: the lock, the two checks and the poll of capture's wait are a function of their own (poll_once); inline,
Julia 1.11.9 compiled a failing first poll into "Unreachable reached". The wait uses the monotonic clock, and the
frame deadline is checked and fixed before the start, with the cycle time (1 / FrameRate) in it.

The stop follows Andor's manual: AcquisitionStop, AT_Flush, then CameraAcquiring must read false. It is confirmed by
the flush and the last read, not by the stop command's answer. abort runs it on any open camera; a failed or
inconclusive read leaves :unknown. shutdown advances the acquisition id, stop! makes no call on a closed camera, and
capture's two cleanup paths check the acquisition's identity under the lock, so a waiter cannot stop a replacement or
touch a closed camera. Buffers are kept after a flush that failed.

Ownership follows the library. The library count, the library's initialized state and every open handle (a camera's
or a probe's) are process-level records under one lock, held across probing in initialize and list_cameras. Each
transfer (the call and its record) runs with Ctrl-C deferred. A failed close keeps the handle and the library; a failed
finalize leaves the library up for the next release, shutdown (even of a closed camera) or holder. One helper runs
cleanup steps, keeps an interrupt over every other failure, and logs the rest.

Checks before the library is used: a 32-bit Julia is refused (the Windows library is stdcall); the /tmp scratch files
are matched by name prefix read off the archived libraries and checked before every operation that may open an index,
through the binding, so a test points it at a temporary folder. The hint about libusb and usbfs_memory_mb is shown
when only simulated cameras opened.

The features the driver sets itself are refused in the constructor, initialize and setfeature! by one rule; setfeature!
updates what export_state reports; an explicit invalid roi is refused at initialize; the fixed cycle is read back;
exposure_time_actual is read after the last setting; last_error clears when a later stop succeeds.

Tests: a rule per test, with the fake extended to answer a stop on an idle camera either way, to want a stop before the
next start, to return a short buffer or odd rows, and to run a hook after the effect of the four lifecycle calls. A
child Julia process sends real SIGINTs there. The public source no longer names an instrument computer or its run.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ratch check, interrupts, half-open)

Handle records. Every function that changes the record of open handles (forget_handle!, open_probe!, close_probes!,
close_held!) takes REGISTRY_LOCK itself, and shutdown and the undoing of a failed initialize take it before they defer
Ctrl-C, so the library's reuse of a closed handle's number cannot erase another camera's record, and no lock wait sits
inside a deferral. A camera whose initialize failed and whose close failed too is :half_open: it was never set up and
accepts only shutdown (abort and capture refuse it, naming shutdown). A probe that could not be closed is warned about
by its own index, even when the search finds the camera, and listed in the error when it does not; a stranded probe that
holds the requested serial says so. A finalize answered "not initialised" counts as down; a holder count that would go
below zero throws.

Names and checks. The driver-owned feature names are compared as the library reads them (spaces removed, case
ignored). The /tmp scratch check stops the operation when the folder cannot be listed or an entry cannot be inspected
(only a missing folder, a vanished entry or a link to nothing mean no file), judges a link by its target's owner too,
and no longer reports a vanished entry as root's.

Interrupts and the stop. A real interrupt delivered when a deferral ends no longer skips the failure report of shutdown
or of the undoing of initialize; in discovery an SDK error is logged with an interrupt in the probe close. The stop
records its state before anything is logged, and its diagnostic is made outside the stop. A derived frame deadline that
overflows is refused before the start. The frame's checks, decoding and stop run in the lock hold that proved the
acquisition is still the camera's (poll_once, not inlined), which removes the window between them; the wait reads the
binding's monotonic clock. A shutdown that completes every step clears last_error, in one place, on a closed camera too.

Tests: the fake can reuse handle numbers, record the library load, and supply the clock; each rule above has a test that
fails without it, and the child process now also sends a real SIGINT at the end of shutdown's and of rollback's deferral.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ailed initialize

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…deferral form, phase from the first step of initialize)

A Ctrl-C while the undoing of a failed initialize waits for the registry left a closed camera with an open handle and a
library count. The camera is now :half_open from the first step of initialize until it completes, and shutdown closes a
handle and gives back a count it holds whatever the phase says.

Three rules, applied to every site and each tested by a mutant that breaks it:
- No logger runs while a lock of the driver is held or inside a deferral: diagnostics are kept (diagnose) and said when
  the outermost operation has released its locks (with_diagnostics).
- One deferral form (deferred!, deferred) keeps a step's own error and an interrupt delivered when the deferral ends, and
  reports both; it replaces the bare disable_sigint in settle_library!, close_probes!, the stop and the others.
- The stop runs under the camera's lock alone; the registry lock covers the close, its record and the release of the
  library, and is taken before the deferral.

Also: each failed probe close, retries included, reaches the report once per index, and the search's message is read off
the final record; a listing marks a probe that could not be closed; elapsed time subtracts unsigned readings; feature
values are keyed by the normalized name; the scratch check names the folder at fault; tests for the single hold of the
frame's stop, the shipped clock, last_error with an interrupt, and the helpers' own locking.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… failures travel in the exception)

Round 3's machinery for writing log messages after the locks (with_diagnostics, diagnose, report_stranded) produced
four of round 4's nine Codex findings. It is deleted, with every @warn, @error, @info and @debug of the driver:
- a failure that comes with a thrown error travels in Julia's exception stack beneath it (throw_over), and an interrupt
  that arrives with a library error is thrown while the error is being handled; shutdown's several failures stay one
  AndorSDK3ShutdownError, thrown under the interrupt;
- a probe that could not be closed stays in the registry's record with every distinct error its close gave, and is read
  off the record when the operation ends: list_cameras' rows and the serial search's message are one entry for each
  index observed with its final ownership, and export_state lists them (probes_not_closed);
- the rule that after an interrupt is recorded an operation waits for no lock but the one it needs to finish what it
  owns is added to the other three.

Also: a failed stop publishes :unknown and last_error before it keeps its buffers, and the buffers are kept under a spin
lock (no wait a task interrupt can land in); the scratch check judges an entry that stopped being a link as what it now
is, and refuses one that keeps changing; deferred! is the only call of Base.disable_sigint (a source scan asserts it) and
its comment says why; the interrupt tests are deterministic (a holder on a second thread, no clock) and cover
AT_InitialiseLibrary, AT_Open and the stop of a frame in capture with a library error and a real SIGINT on one call.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…d stop-confirmation machinery)

The driver calls the library, checks every return code, throws an error that names the call, and cleans up with
ordinary try/finally. Removed: every Ctrl-C deferral and its helpers, the :unknown and :half_open phases, the
refusal to start after a failed stop, last_error, the CameraAcquiring confirmation read, the stranded-probe
records and their retries, the process-wide leaked-buffer list, AndorSDK3ShutdownError, and the scratch check's
race hardening. Kept: the bindings and checked calls, one library count and a plain list of open indexes under one
lock, one lock per camera released between polls, polling waits on a monotonic deadline, buffers referenced from the
camera until a flush or close succeeds, shutdown that attempts every step and throws the first failure, and the /tmp
scratch check as one readdir and an owner check per entry. Tests of the removed machinery, including the SIGINT
child-process suite, are removed; the rest run against the fake.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…e request snapshot, Neo binning, abort on an idle camera)

A buffer whose flush or close failed stays referenced for the life of the
process. A failed close keeps the handle, the record of the open index and the
library count, and the camera stays open, so shutdown retries it. capture reads
the exposure, region, trigger mode and timeout once and checks and sends that
set. A model with AOIBinning and not AOIHBin/AOIVBin is set to 1x1. Discovery
names the call that failed, and list_cameras throws the first failure.
setfeature! and capture refresh the reported exposure, readout time and pixel
encoding. An image larger than its rows decodes from the rows. abort sends
AcquisitionStop only when CameraAcquiring reads true. The capture docstring no
longer mentions an interrupt; the phase check in the poll is deleted.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…, packed rows without AOIStride, first discovery failure kept)

- abort always sends AcquisitionStop for an acquisition the driver started, even when its one-frame cycle has
  ended by itself; only on an idle camera does it read CameraAcquiring first.
- A camera that does not report AOIStride has packed rows (two bytes a pixel), so end padding no longer
  stretches the inferred row length.
- A probe whose read failed and whose close failed too throws the read failure; a listing whose library release
  fails after an index failed throws that index's failure.
- Tests: abort after the cycle ended on a library that wants the stop, an odd number of trailing bytes, end
  padding without AOIStride, a failed close alone keeping its buffer, both discovery cleanup cases.
- The capture and shutdown docstrings and the CHANGELOG say what the code does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s failure)

- list_cameras gives the library back inside its one try, and when it cannot return its rows it throws the first
  index's failure, also when a later index's probe fails to close.
- The row-length error names the row length, not AOIStride, which a simulated camera does not have.
- A shutdown test title says what it checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… Andor driver's)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kalidke
kalidke merged commit 2eb4ae1 into main Dec 3, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant