Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,64 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fisheye calibration resolution (protocol V2.10)

The wrist fisheye default moved from 640x480 to 1920x1080. Those are different
sensor crops (4:3 vs 16:9), so intrinsics cannot be rescaled from one to the
other. Leader firmware 1.2.8 / follower 1.2.18 (command set V2.10) store the
calibration resolution next to the 8 params, via two new ops on
`CameraFisheyeCal` (0x2B): `0x02` sized read and `0x03` sized write. Ops
`0x00`/`0x01` are unchanged on the wire.

#### Added

- **`Calibration::read_fisheye_record()` / `write_fisheye_sized(cal, w, h)` /
`supports_fisheye_size()`**, plus a `FisheyeCalRecord` (`calibration`,
`width`, `height`, `size_known()`; `0/0` = unknown). The capability probe is
the sized read (old firmware rejects it with `InvalidParam`), and its answer
is cached per `Calibration`. On old firmware the record reads back with an
unknown size, and `write_fisheye_sized()` accepts only 640x480 (through the
legacy write). Any other size raises and asks for a firmware upgrade.
Implausible values are refused before anything is sent.
- **`fisheye_cal_size_plausible()`** (the firmware's sized-write rule) and
**`infer_fisheye_cal_size()`**, which handles records without a size. The
candidates are exactly 640x480 and 1920x1080. A candidate matches when the
principal point is within 10% of its centre on each axis, and a size is
returned only when exactly one candidate matches. The PC calibration tools
use the same rule.
- **`FisheyeUndistorter(cal, calibrated_size, frame_size, balance)`**, which
requires the two sizes to be equal. In Python this is the `calibrated_size=(w, h)`
keyword.
- **`ResolvedFisheyeCal.calibrated_size` / `.size_inferred`**. In Python,
`resolve_fisheye()` now returns a `ResolvedFisheyeCal` object. It still
unpacks as the old `(calibration, is_reference, reason)` tuple.
- Protocol mirror: `CAMERA_FISHEYE_CAL_OP_READ_SIZED` / `_OP_WRITE_SIZED` /
`_SIZED_SIZE`, `CameraFisheyeCalSized`, and
`encode_camera_fisheye_cal_{read,write}_sized()` /
`decode_camera_fisheye_cal_sized()`. `scripts/check_protocol_drift.py`
now also compares these wire constants (and the legacy 0x2B ones) against
the firmware headers.

#### Changed

- `resolve_fisheye()` uses the recorded size. When the record has no size, it
uses the inferred one and logs a warning. When the size cannot be inferred
either, it falls back to the reference calibration ("the calibration
resolution is unknown and could not be inferred"). The reference is always
640x480.
- The wrist undistorter that grippers install (`undistort_wrist`) is built for
the calibration's own size, so it now also works on a camera configured at
1920x1080 with a 1080p calibration. A size mismatch still throws.
- `fisheye_cal.py set-fisheye` takes `--width/--height` (required) and stores
them with `write_fisheye_sized()`. `show` prints the size and whether it was
recorded or inferred. `read_intrinsics.py` reports `image_size` and
`image_size_source` (`assumed_image_size` is kept). `wrist_camera.py` and the
rerun viewer open the camera at the calibration's size.
- `write_fisheye()` is unchanged, but on V2.10 firmware it leaves the size
unknown, so prefer `write_fisheye_sized()`. The legacy
`FisheyeUndistorter(cal, size, balance)` still treats the params as a
640x480 calibration.

### High-rate IMU (candidate, not yet versioned; on top of 0.4.6)

Requires leader firmware 1.2.8 / follower 1.2.18 (candidates; previous
Expand Down
60 changes: 56 additions & 4 deletions cpp/include/taccap/components/calibration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
//
// - Fisheye camera intrinsics + distortion (Cmd::CameraFisheyeCal 0x2B,
// command set >= V2.0) — supported on BOTH leader and follower.
// Command set V2.10 (leader 1.2.8 / follower 1.2.18) also records the
// capture resolution the params belong to (read_fisheye_record /
// write_fisheye_sized); older firmware leaves it unknown.
// - Leader encoder max travel angle (Cmd::EncoderMaxCal 0x2C,
// command set >= V2.1, i.e. leader >= 1.2.0) — LEADER ONLY; the follower
// has no MT6816 encoder and NACKs with ErrorCode::InvalidCmd.
Expand All @@ -28,6 +31,7 @@
#include <taccap/components/fisheye_undistorter.hpp>
#include <taccap/protocol/payloads.hpp>

#include <atomic>
#include <chrono>
#include <optional>

Expand All @@ -43,9 +47,6 @@ class Calibration {
std::optional<protocol::CameraFisheyeCal> read_fisheye(
std::chrono::milliseconds timeout = std::chrono::milliseconds{200});

// Persists to MCU flash (survives power cycles). Throws ProtocolError if
// any value is NaN/Inf (firmware returns InvalidParam) or the flash write
// fails (SysBusy).
// Ask for this unit's wrist calibration, falling back to the SDK's
// reference values when it cannot supply one.
//
Expand All @@ -57,15 +58,53 @@ class Calibration {
// themselves get the same decision the SDK makes internally rather than
// re-deriving it.
//
// The calibration resolution comes from read_fisheye_record(). A record
// without one (written by write_fisheye() or by pre-V2.10 firmware) gets
// infer_fisheye_cal_size()'s answer, with size_inferred set and a warning
// logged; a record whose size cannot be inferred either is treated as
// unusable and the reference calibration (640x480) stands in.
//
// Never throws for a missing or unusable calibration — that is a fallback,
// reported through ResolvedFisheyeCal::reason. A transport failure still
// propagates.
ResolvedFisheyeCal resolve_fisheye(
std::chrono::milliseconds timeout = std::chrono::milliseconds(200));

// Persists to MCU flash (survives power cycles). Throws ProtocolError if
// any value is NaN/Inf (firmware returns InvalidParam) or the flash write
// fails (SysBusy).
//
// Writes the 8 params only (CalOp::Write). On V2.10 firmware this marks the
// calibration resolution UNKNOWN; use write_fisheye_sized() to record it.
void write_fisheye(const protocol::CameraFisheyeCal& cal,
std::chrono::milliseconds timeout = std::chrono::milliseconds{500});

// ---- Fisheye calibration resolution (V2.10: leader 1.2.8 / follower 1.2.18)
// True when the firmware stores the calibration resolution (answers the
// sized read, op 0x02, with Ok or CalNotSet); false when it rejects that op
// as InvalidParam, which is how older firmware answers an unknown op. The
// answer is cached for the life of this object; a transport failure
// propagates and is not cached, and so is any other NACK.
bool supports_fisheye_size(
std::chrono::milliseconds timeout = std::chrono::milliseconds{200});

// The params plus the resolution they belong to. Uses the sized read when
// the firmware supports it, else the legacy read with width/height 0/0
// (unknown). Returns std::nullopt when never calibrated (CalNotSet).
std::optional<FisheyeCalRecord> read_fisheye_record(
std::chrono::milliseconds timeout = std::chrono::milliseconds{200});

// Persist the params together with the capture resolution they were
// measured at. Throws Error before sending anything when the values fail
// fisheye_cal_size_plausible() (the firmware's own rule). On V2.10 firmware
// this is the sized write (op 0x03); a NACK throws ProtocolError, as
// write_fisheye() does. Older firmware cannot record a size: a 640x480
// calibration then falls back to the legacy write_fisheye(), and any other
// resolution throws Error asking for a firmware upgrade.
void write_fisheye_sized(const protocol::CameraFisheyeCal& cal,
int width, int height,
std::chrono::milliseconds timeout = std::chrono::milliseconds{500});

// ---- Leader encoder max travel angle (leader only) ---------------------
// The shaft angle (rad) at which the gripper is fully open, measured from
// the encoder zero (fully closed). Returns std::nullopt when never
Expand All @@ -78,7 +117,20 @@ class Calibration {
std::chrono::milliseconds timeout = std::chrono::milliseconds{500});

private:
bus::Transport& t_;
// Sends the sized read (op 0x02). Returns false when the firmware rejects
// the op as InvalidParam (pre-V2.10); otherwise fills `out`, with
// std::nullopt for CalNotSet. Any other NACK throws ProtocolError.
bool read_fisheye_sized_(std::chrono::milliseconds timeout,
std::optional<FisheyeCalRecord>& out);

// Records whether the firmware answered the sized read: -1 not yet asked,
// 0 unsupported (pre-V2.10), 1 supported. Set only from a real answer.
void note_fisheye_size_support_(bool supported) noexcept {
fisheye_size_support_.store(supported ? 1 : 0);
}

bus::Transport& t_;
std::atomic<int> fisheye_size_support_{-1};
};

} // namespace xense::taccap
82 changes: 71 additions & 11 deletions cpp/include/taccap/components/fisheye_undistorter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,71 @@
#include <opencv2/core.hpp>

#include <cmath>
#include <cstdint>
#include <optional>
#include <string>

namespace xense::taccap {

// The wrist camera is calibrated at 640x480 and the firmware record carries
// ONLY the 8 intrinsic/distortion floats — no image size. Rather than guess a
// scale factor for a differently-sized frame (which would silently produce a
// wrong rectification), the constructor rejects any other size outright. If a
// second calibration resolution is ever needed, the right fix is to record the
// size in the firmware payload, not to infer it here.
// The 8 intrinsic/distortion floats only hold at the capture resolution they
// were measured at. 640x480 (4:3) and 1920x1080 (16:9) are DIFFERENT sensor
// crops, not one image at two scales, so intrinsics cannot be rescaled between
// them — nor to any other size. Firmware V2.10 (leader 1.2.8 / follower 1.2.18)
// therefore records the calibration resolution next to the params
// (Calibration::read_fisheye_record / write_fisheye_sized), and the undistorter
// refuses any frame that is not at that exact resolution.
//
// 640x480 remains the resolution of the SDK's reference calibration and of the
// legacy constructor below, which predates the recorded size.
inline constexpr int FISHEYE_CALIB_WIDTH = 640;
inline constexpr int FISHEYE_CALIB_HEIGHT = 480;

// A fisheye record as the firmware holds it: the params plus the capture
// resolution they belong to. width/height 0/0 means the size is UNKNOWN — the
// record was written without one (CalOp::Write, or firmware older than V2.10)
// — and must not be assumed to be 640x480; see infer_fisheye_cal_size().
struct FisheyeCalRecord {
protocol::CameraFisheyeCal calibration{};
uint16_t width = 0;
uint16_t height = 0;

bool size_known() const noexcept { return width != 0 && height != 0; }
};

// The plausibility rule the firmware applies to a write-sized (op 0x03), so a
// write can be refused host-side with a useful message rather than a bare
// InvalidParam: every value finite, fx > 0, fy > 0, 1 <= width, height <= 4096,
// and the principal point strictly inside the frame (0 < cx < width,
// 0 < cy < height).
bool fisheye_cal_size_plausible(const protocol::CameraFisheyeCal& cal,
int width, int height);

// Best-effort calibration resolution for a record whose size is unknown.
//
// The rule, shared verbatim with the PC calibration tools so every consumer
// infers the same answer:
// - candidates are exactly 640x480 and 1920x1080;
// - a candidate W x H matches when the calibration is usable
// (is_usable_fisheye_cal) AND |cx - W/2| <= 0.10 * W AND
// |cy - H/2| <= 0.10 * H, i.e. the principal point sits within 10% of the
// frame centre on each axis;
// - the size is returned only when EXACTLY one candidate matches; zero or
// several matches return std::nullopt.
// A principal point far from both centres is not "probably 640x480": it means
// the record cannot be placed, and the caller should ask for a recalibration.
std::optional<cv::Size> infer_fisheye_cal_size(const protocol::CameraFisheyeCal& cal);

// `balance` interpolates the output focal length between these two, matching
// the PC tool's fisheye_focal_scale_for_balance()
// (tc-gu-01-pc/camera_calibration/calibration/workflow.py). balance = 0 keeps
// the calibrated focal length — a natural, reference-like view; balance = 1
// shortens it to 0.70x for the widest field of view, at the cost of more
// black border around the edges.
// A reference calibration for the TC-GU-01 wrist lens, measured on a sample
// unit. It exists so a gripper whose firmware was never calibrated can still
// deliver an approximately rectified stream instead of raw fisheye — every unit
// carries the same lens and the same 640x480 sensor, so the shared numbers are
// far closer to correct than no rectification at all.
// unit at 640x480. It exists so a gripper whose firmware was never calibrated
// can still deliver an approximately rectified stream instead of raw fisheye —
// every unit carries the same lens and sensor, so the shared numbers are far
// closer to correct than no rectification at all. They hold at 640x480 ONLY.
//
// It is NOT a substitute for calibrating a unit. Lens placement varies between
// assemblies, so the principal point in particular drifts per unit; anything
Expand All @@ -70,10 +111,17 @@ inline constexpr protocol::CameraFisheyeCal FISHEYE_FALLBACK_CAL{
// UVC device, and callers that own the device themselves (the common case, since
// Config::open_cameras defaults to false) get the identical answer instead of
// re-deriving it and drifting.
//
// `calibrated_size` is the capture resolution `calibration` belongs to: the
// size the firmware recorded, or — for a record written without one —
// infer_fisheye_cal_size()'s answer, flagged by `size_inferred`. The reference
// calibration is always 640x480 with size_inferred false.
struct ResolvedFisheyeCal {
protocol::CameraFisheyeCal calibration;
bool is_reference;
std::string reason;
cv::Size calibrated_size{FISHEYE_CALIB_WIDTH, FISHEYE_CALIB_HEIGHT};
bool size_inferred = false;
};

inline bool is_usable_fisheye_cal(const protocol::CameraFisheyeCal& c) {
Expand All @@ -85,9 +133,21 @@ inline constexpr double FISHEYE_WIDE_FOCAL_SCALE = 0.70;

class FisheyeUndistorter {
public:
// Throws Error when `size` is not the calibrated 640x480. `balance` is
// `calibrated_size` is the resolution `cal` was measured at (from
// FisheyeCalRecord / ResolvedFisheyeCal::calibrated_size); `frame_size` is
// what the camera actually delivers. They must be EQUAL — throws Error
// otherwise, because intrinsics do not carry over to another resolution or
// aspect ratio; recalibrate at the capture resolution instead. `balance` is
// clamped to [0,1] rather than rejected — it is a taste knob, not a
// correctness one.
FisheyeUndistorter(const protocol::CameraFisheyeCal& cal,
cv::Size calibrated_size,
cv::Size frame_size,
float balance = 0.0f);

// Legacy form, from before the firmware recorded a calibration size: `cal`
// is taken to be a 640x480 calibration, so this throws Error unless `size`
// is 640x480. Prefer the form above with the record's own size.
FisheyeUndistorter(const protocol::CameraFisheyeCal& cal,
cv::Size size,
float balance = 0.0f);
Expand Down
5 changes: 3 additions & 2 deletions cpp/include/taccap/follower_gripper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ class FollowerGripper {
//
// Degrades to raw frames with a warning when the firmware holds no
// calibration or is too old to answer. Throws when the camera is not
// configured at the calibrated 640x480 — the firmware record stores no
// image size, so rescaling the intrinsics would be a guess.
// configured at the calibration's own resolution (recorded by V2.10
// firmware, inferred for an older record, 640x480 for the reference) —
// intrinsics are never rescaled to another resolution.
bool undistort_wrist = false;
// 0 = the calibrated focal length (natural view, matches the PC tool's
// default); 1 = 0.70x focal length for the widest field of view, with
Expand Down
5 changes: 3 additions & 2 deletions cpp/include/taccap/leader_gripper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ class LeaderGripper {
//
// Degrades to raw frames with a warning when the firmware holds no
// calibration or is too old to answer. Throws when the camera is not
// configured at the calibrated 640x480 — the firmware record stores no
// image size, so rescaling the intrinsics would be a guess.
// configured at the calibration's own resolution (recorded by V2.10
// firmware, inferred for an older record, 640x480 for the reference) —
// intrinsics are never rescaled to another resolution.
bool undistort_wrist = false;
// 0 = the calibrated focal length (natural view, matches the PC tool's
// default); 1 = 0.70x focal length for the widest field of view, with
Expand Down
8 changes: 8 additions & 0 deletions cpp/include/taccap/protocol/codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ std::vector<uint8_t> encode(const CalSetAllPayload&);
// instead of an encode() overload (the same struct maps to two wire shapes).
std::vector<uint8_t> encode_camera_fisheye_cal_read(); // 1 byte
std::vector<uint8_t> encode_camera_fisheye_cal_write(const CameraFisheyeCal&); // 33 bytes
// V2.10 — fisheye params together with their calibration resolution.
std::vector<uint8_t> encode_camera_fisheye_cal_read_sized(); // 1 byte
std::vector<uint8_t> encode_camera_fisheye_cal_write_sized(const CameraFisheyeCal&,
uint16_t width,
uint16_t height); // 37 bytes
std::vector<uint8_t> encode_encoder_max_cal_read(); // 1 byte
std::vector<uint8_t> encode_encoder_max_cal_write(float max_rad); // 5 bytes

Expand Down Expand Up @@ -141,6 +146,9 @@ CalGetResponse decode_cal_get(const uint8_t* data, std::size_t len);
// V2.0/V2.1 — read responses lead with the CalOp byte echoed back (Read), not
// an error byte; these decoders verify that byte and strip it.
CameraFisheyeCal decode_camera_fisheye_cal(const uint8_t* data, std::size_t len);
// V2.10 — read-sized response; verifies the echoed op byte (0x02) and strips it.
// width/height 0/0 are passed through as-is (size unknown).
CameraFisheyeCalSized decode_camera_fisheye_cal_sized(const uint8_t* data, std::size_t len);
float decode_encoder_max_cal(const uint8_t* data, std::size_t len);
// V1.6+
SensorErrorReport decode_sensor_error(const uint8_t* data, std::size_t len);
Expand Down
Loading
Loading