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.
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 flightdevice 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.
---
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 commissionerin 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:
now we can take the average to offset the errors:
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.
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
first, we can get the numerator, which lets us cancel out the clock error:
| piece | value | what happens to it |
|---|---|---|
| 500,000 × 300,000 | 150,000,000,000 | removed by subtracting Da × Db |
| 32.6 × 300,000 | 9,780,000 | off by +3,000,000 |
| 500,000 × 16.6 | 8,300,000 | off by −3,000,000 |
| 32.6 × 16.6 | 541.16 | tiny, stays |
now, steps 2 and 3 can cancel the clock error out:
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:
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.