Back Original

two way ranging - how it works

two way ranging is a protocol used for ultra-wideband radio to measure the distance between two devices.

each device sends a pulse to the other, and we divide by 2 to get the average time of flight, which lets us estimate distance.

simple diagram

the simplest version is a just one exchange:

---
config:
  sequence:
    mirrorActors: false
---
sequenceDiagram
  D1->>D2: pulse 1
  Note over D2: wait 500 µs
  D2->>D1: pulse 2
  Note over D1: compute time of flight

device 1 sends the first pulse to device 2. device 2 then waits 500 us, and sends a pulse back to device 1. device 1 recorded the total time the round trip took and since it knows that device 2 waited a fixed time, it can subtract 500us from the total time, to get the time for both pulses. then we divide by 2 to get the time of flight (ToF).

this looks great, but theres a problem here.

device 2's clock may run slower than device 1's, or vice versa. this means when we subtract the wait time of 500us, it could have actually been 500.001us. every single nanosecond counts here.

to fix this, we send an additional pulse back and forth.

real diagram

---
config:
  sequence:
    mirrorActors: false
---
sequenceDiagram
  D1->>D2: pulse 1
  Note over D2: wait 500 µs
  D2->>D1: pulse 2
  Note over D1: wait 500 µs
  D1->>D2: pulse 3
  D1->>D2: data: D1's round trip + wait
  Note over D2: compute time of flight
  D2->>D1: result report: distance
  Note over D1: reports distance to the commissioner

in this example, D2's clock runs slower than D1.

we are able to still get an accurate measurement because we combine the two rounds to get average.

the true distance is 3.4 m, so the true ToF is 11.3 ns. D2's slow clock counts "500 µs" when 500.010 µs (500,010 ns) have really passed.

heres how it works:

  • round 1: D1 measures 500,032.6 ns between sending pulse 1 and receiving pulse 2. then we subtract D2's wait time (500,000 ns).
    • ToF1 = 32.6 ns / 2 = 16.3 ns
    • (technically this is too high since D2 really waited 10 ns longer)
  • round 2: D2 measures 500,012.6 ns between sending pulse 2 and receiving pulse 3
    • then we subtract D1's wait time (500,000 ns)
    • ToF2 = 12.6 ns / 2 = 6.3 ns
    • (technically this is too low, D2's slow clock reads 10 ns short of the real 500,022.6 ns)

now we can take the average to offset the errors:

  • (16.3 + 6.3) / 2 = 11.3 ns

11.3 ns × 0.3 m/ns ≈ 3.4 m

this method works because the wait times for D1 and D2 are the same. we are essentially cancelling out the low read of round 2, with the high read of round 1.

but, this method isn't always sufficient. if we no longer adhere to this fixed slot, or different firmware is used, then we need a formula that can handle these cases.

the real formula

now lets imagine this is twr from the qorvo chip to an iphone, and the wait times are different.

D1 waits 300 µs, D2 still waits 500 µs:

ToF = (Ra × Rb − Da × Db) / (Ra + Rb + Da + Db)

  • Ra = D1's round trip = 500,032.6 ns

  • Rb = D2's round trip = 300,016.6 ns

  • Da = D1's wait = 300,000 ns

  • Db = D2's wait = 500,000 ns

    ToF = (500,032.6 × 300,016.6 − 300,000 × 500,000) / (500,032.6 + 300,016.6 + 300,000 + 500,000) = 18,080,541.16 / 1,600,049.2 = 11.29999 ns ≈ 3.4 m

(full math here)

first, we can get the numerator, which lets us cancel out the clock error:

piecevaluewhat happens to it
500,000 × 300,000150,000,000,000removed by subtracting Da × Db
32.6 × 300,0009,780,000off by +3,000,000
500,000 × 16.68,300,000off by −3,000,000
32.6 × 16.6541.16tiny, stays

now, steps 2 and 3 can cancel the clock error out:

  • D1's round trip is so when we multiply by 300k ns, its 3mil too large.
  • D2's round trip is 6ns too short, so when we multiply by 500k ns, its 3mil too small.

this combines the separate flawed "there and back" measurements into one unified one.

now the numerator is 18,080,541, representing the true time of flight for one round trip(22.6 ns) times the length the entire exchange (800,032.6 ns).

to get the average time of flight for one pulse, we need to divide by the length of the two exchanges. this is because right now we are measuring for a full "there and back" round trip:

22.6 ns×800,032.6 ns2×800,032.6 ns=11.3 ns\frac{22.6 \text{ ns} \times 800{,}032.6 \text{ ns}}{2 \times 800{,}032.6 \text{ ns}} = 11.3 \text{ ns}

then when we take 11.3ns, compute time of flight and get 3.4m.

engineers typically use this formula by default since it covers all the use cases.