watecd
A snapshot daemon for the Watec IP camera that watches the sample at BM01.
The camera keeps a still JPEG at http://<camera>/jpg/, 1920x1080, about 120 kB per
frame, behind HTTP digest authentication. It is a scarce shared resource: every frame
costs ~19 ms of preparation plus the transfer, which caps it at about 19-24 frames per
second, and parallel readers divide that ceiling instead of multiplying it. watecd
polls the camera once and fans the very same frame out to every client, so two Pylatus
instances see the full frame rate rather than half of it each.
It also keeps the camera credentials in one place, out of every Pylatus configuration.
Running
watecd --user USER --password PASSWORD [OPTIONS] CAMERA
The camera, the user and the password are required, the camera answers nothing without
them. Run watecd --help for the rest of the options. The daemon stays in the foreground and is
supervised by the init system: an OpenRC service and its configuration are in
scripts/openrc.
Protocol
Both directions send a 12 byte little endian header followed by size payload bytes:
magic uint32 0xa7ecd00d
version uint16 6
type uint16 Image = 1, Ready = 2, Geometry = 3, SetGeometry = 4,
Calibration = 5, SetCalibration = 6,
Goniometer = 7, SetGoniometer = 8
size uint32 payload length
A client asks for the next frame with a Ready header and no payload, and receives an
Image whose payload is the JPEG exactly as the camera sent it, decoded by nobody on
the way. This is a pull protocol on purpose: a client that stops asking stops costing
anything, and when no client is waiting the daemon does not touch the camera at all.
Frames are never cached: a Ready is answered with the next frame that arrives, never
with the previous one, because the click that centres a sample must land on an image
taken after the goniometer stopped. A client that reads too slowly misses frames rather
than delaying the others.
The same frame is never sent twice, and the camera is never asked back to back. It cannot encode while it answers: measured on the beamline, one request every 50 ms is answered with a new frame every single time, while one every 40 ms starves it down to a single new frame in eight seconds -- and asked without a pause, which is what happens as soon as two clients wait since one of them is always waiting, it repeats the JPEG it already has for tens of requests in a row. So watecd stays quiet for 15 ms after every frame, which with a ~37 ms request settles at about 20 frames per second, all of them new. A repeat, should one still arrive, is dropped and doubles the silence up to 200 ms until the camera has something to give. The frame rate a client sees is therefore the rate of the camera rather than the rate of the wire.
Geometry
The cross everything is aligned to -- the zoom, the axis of rotation and the beam are
all brought into that one point, and the sample is then centred in it -- belongs to the
camera and not to whoever looks at it. Two Pylatus instances, one in the hutch and one
in the cabin, watch the same camera and must not be aligned twice, so watecd keeps the
cross and hands it to everybody.
Geometry and SetGeometry carry the same 24 byte payload of three little endian
doubles:
x the centre of the cross, in pixels of the frame
y
size the length of both lines of the cross, in pixels
A client receives a Geometry as soon as it connects, and again whenever anybody
changes it. A SetGeometry is not answered: the daemon stores the new cross and pushes
it to every client, the sender included, so the server stays the single source of truth
and the last writer wins. A geometry equal to the one already held changes nothing and
is not echoed at all.
Calibration
How many micrometres of the sample fit into a pixel depends on the zoom of the lens, so the calibration is a table rather than a number: two vertical um/px per position of the zoom motor, one for each of the two motors of the goniometer head. It belongs to the camera for the same reason the cross does, and travels the same way.
Calibration and SetCalibration carry a payload of 40 byte entries, as many as the
table has, five little endian doubles each:
zoom the position of the zoom motor, a whole number of its own units
um_per_px_at0 micrometres per pixel of the motor vertical at the reference omega
um_per_px_at90 the same for the motor vertical ninety degrees away from it
cross_x the centre of the cross at this zoom, in pixels of the frame
cross_y
Each number carries its sign: it is measured by moving the motor where it stands vertical in the frame and dividing its displacement by the one the image made, so a negative value means the image goes up while the motor grows. Which way that is depends on how the head is mounted, and the client needs it to turn a click into a move. The second motor is as good to measure at plus ninety degrees as at minus ninety, where the image mirrors and the client flips the sign back before it sends the table.
The zoom is mechanically fragile and does not always bring the image back where it was,
so a zoom position may carry a cross of its own, set right there with one click instead
of aligning everything again. It overrides the global cross of Geometry at that zoom
only, and the global one stays for every position that has none. The length of the
lines is always the global one: it is how the cross is drawn, not where it stands.
A number is zero when nobody has measured that motor at that zoom yet, and both coordinates of the cross are NaN when nobody has set one there. An entry with both numbers zero and no cross carries nothing at all, and half a cross is no cross: the message that brings either is refused whole and the client that sent it is dropped, as with any other nonsense on the wire.
A client sends the whole table at once and receives the whole table back, exactly as with the cross, so a zoom position is forgotten by sending a table without its entry. At most 64 entries are accepted.
Goniometer
The calibration means something only together with the goniometer it was measured on: which motor of the head stands vertical at the reference omega, which one ninety degrees away, and what that reference is. There is one goniometer under one camera, so which motor plays which part is kept here as well, together with the rest of what belongs to the goniometer rather than to whoever looks at it. A goniometer swapped in the hutch is then set up once, not once per Pylatus.
Goniometer and SetGoniometer carry four little endian doubles, one byte and six
names, each name being one byte of length followed by that many bytes of UTF-8:
reference_omega the omega where the motor of the head called at0 is vertical
foreshortening the share of a move of the vertical goniometer motor that the
camera sees, next to the same move of the head; zero leaves
that motor alone
window_from the omega range an automatic centering may turn the axis in
window_to
window 1 when that range is used, 0 when the limits of omega are
omega the motor that rotates the sample
sample_at0 the motor of the head vertical at the reference omega
sample_at90 the motor of the head vertical ninety degrees away from it
prver the motor that moves the whole goniometer vertically
prhor the motor that moves the whole goniometer along the axis
zoom the motor of the lens
An empty name is a motor nobody has chosen yet. The doubles must be finite and a name cannot be longer than 255 bytes, anything else is refused and drops the client. It travels exactly as the cross does: sent on connection, pushed to every client on a change, not echoed when nothing changed.
Storage
The cross, the goniometer and the calibration are stored in
$XDG_CONFIG_HOME/watecd/<camera>.toml on every change and read back at startup, so a
restart of the daemon or of the machine keeps the alignment. The file is named after the camera: a second camera gets a second
daemon, a cross, a goniometer and a calibration of its own.