Simvik

Distance from two-way ranging

Compute distance from single- and double-sided two-way ranging timestamps. A medium embedded C problem (Ultra-wideband (UWB)), run and graded in the browser.

medium · Ultra-wideband (UWB) · uwb · ranging · timestamps

UWB radios timestamp packets in ticks of about 15.65 ps (1 / (128 × 499.2 MHz)). Ranging measures the time of flight (ToF) between two devices:

  • single-sided (one round trip): ToF = (Tround − Treply) / 2, where Tround is measured by the initiator and Treply is the responder's turnaround time,
  • double-sided (two round trips, cancelling clock drift): ToF = (Tround1 × Tround2 − Treply1 × Treply2) / (Tround1 + Tround2 + Treply1 + Treply2).

Distance = ToF × speed of light. With ticks, that is ToF_ticks × 4.6917 mm (0.29979 m/ns × 15.65 ps).

Write ss_twr_mm(tround, treply) and ds_twr_mm(tround1, treply1, tround2, treply2), returning millimetres (rounded down; 0 if the result would be negative). Use 64-bit integers; the products are large. Use 46917 / 10000 mm per tick.

Starter code

int64_t ss_twr_mm(int64_t tround, int64_t treply) {
  return (tround - treply) * 46917 / 10000;
}

int64_t ds_twr_mm(int64_t tround1, int64_t treply1, int64_t tround2, int64_t treply2) {
  return ss_twr_mm(tround1, treply1) + ss_twr_mm(tround2, treply2);
}

Example tests

Ideal clocks, 5 m

show(12779520 + 2132, 12779520, 12779520 + 2132, 12779520);

Expected output:

single: 5001 mm, double: 5001 mm

Learn it first