Playing Pong with Ultra Wideband
By: Alex Suzuki
Tags: Indoor Positioning, Mqtt, Makers, Ultra Wideband, Technology

A short introduction to Ultra Wideband and a not so serious application of the technology, using the Decawave UWB development kit.

Last year’s iPhone 11 announcement contained a surprise announcement without any of the usual fanfare. Amid the plethora of improvements in the new iPhone models, one thing was flying under the radar: the U1 chip.

iPhone 11 announcement (image credit: Apple)

People quickly caught on and discovered that Apple is now integrating an ultra wideband chip into its latest phones. For now, it is being used to prioritize devices eligible for AirDrop, or to quote Apple:

Ultra Wideband technology comes to iPhone. The new Apple‑designed U1 chip uses Ultra Wideband technology for spatial awareness — allowing iPhone 11 to understand its precise location relative to other nearby U1‑equipped Apple devices. Think GPS at the scale of your living room. So if you want to share a file with someone using AirDrop, just point your iPhone at theirs and they’ll be first on the list.

Ultra Whatband?

Ultra Wideband (abbreviated as UWB) is a radio technology for short-range, high-bandwidth communication. It is by no means new — it has been around for almost two decades. It is being used mostly in industrial environments for indoor positioning and asset localization. Due to its high data rate and short range, it is usually also touted as being suitable for a wireless peripheral communication, but apparently that hasn’t materialized yet.

Indoor Positioning

UWB is well-suited for indoor positioning applications, as it is much preciser than other RF technologies like WiFi or Bluetooth. Positioning approaches using UWB typically rely on measuring the time of flight (ToF) of signals. The idea is to measure the time it takes for a signal to pass between two nodes. As the speed of light is known and constant, the distance can be derived by multiplying with the elapsed time. Positioning can be achieved by measuring the distance to multiple nodes (usually fixed in place, with known positions) and calculating the position with respect to them. This approach yields far better results than relying on the received signal strength to determine the distance between sender and receiver.

Decawave

One of the best-known vendors of UWB chipsets is Decawave. One of the modules provided is the DWM1001C which features the DWM1000 tranceiver, a Nordic Semiconductor nRF52 chip, and a 3-axis accelerometer. Decawave also provides a development board for prototyping, and an evaluation kit, for evaluating its positioning solution. In addition to the hardware, Decawave provides a stock firmware called PANS (Positioning and Networking Stack).

DWM1001C module (image credit: Decawave)

For the remainder of this blog post, a Decawave evaluation kit is assumed.

Getting Started with Indoor Positioning

Now that we know all this, how do we proceed? Getting started is actually surprisingly easy. Setting up the Decawave evaluation kit is pretty straightforward (see also the official quick start guide):

Mounting the anchors: choose suitable positions for the fixed nodes (in Decawave terminology, these are called anchors). Anchors are usually mounted on walls and positioned near the ceiling (to increase the chance of having line of sight), and should cover most of the room.

Sample anchor placement at corners of room.

Assigning positions to anchors: next, you need to assign positions to the anchors. Decawave provides an Android app to configure the nodes. Anchor positions can be determined for up to four anchors, but for best results, Decawave recommends measuring the anchor positions with centimeter accuracy and then setting the position manually. I used a laser distance meter to determine the distance from the walls.

Sample anchor positions

Adding a tag node: now that the anchors have been placed and their positions are known, it is time to add a mobile node (also called a tag). Once added to the network, the tag will be able to determine its location based on its distance from the anchors. Tag and anchor positions can be displayed in the app, which uses a Bluetooth Low Energy connection to communicate with the modules.

Screenshot from Decawave DRTLS app showing tag and ranged anchors

Profit: congrats, you should now be able to accurately determine the position of a moving object in your room.

Video showing indoor positioning in our office

Making Position Data Accessible

Now that you can position things in your room, it’s time to make those positions available for consumption. Right now you can get the position via a BLE connection to the device, which may or may not be what you want. In any case, it is limiting.

Extending Network with a Gateway Node

Enter gateway nodes. In the Decawave positioning system, gateway nodes can forward UWB traffic to a IP-based network, typically the internet.

A gateway node provides IP backhaul

Position and configuration messages are transmitted via a protocol called MQTT:

MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.

MQTT is really useful, and all major cloud vendors offer it as managed service (AWS IoT, Azure IoT hub, Google Cloud IoT core) under an IoT moniker. You can of course also host your own MQTT broker, even on-premise. The Mosquitto broker is a popular OSS choice, and was used for demo purposes.

Decawave provides gateway functionality through a custom Raspbian image for the RaspberryPi model 3B+. No other host hardware is currently supported. Grab some headers and solder the development board (it’s exactly the same one as used in the anchors and tags) onto the Pi’s GPIO headers and you’re good to go.

Please don’t look too closely at the solder joints, thank you.

The Raspbian gateway image offers a web application for configuration of the nodes and an MQTT broker. So if you want to grab your MQTT messages directly from the RaspberryPi, you can do that. In practice, the MQTT broker on the RaspberryPi will forward the messages to an aggregating broker, especially in multi-room installations, where there will typically be a gateway for each room.

Note that in addition to position data, nodes can also send additional data via the UWB network, which will also get forwarded. In Decawave’s positioning stack, this is called IoT data, and its intended use is sensor readings.

Using the Position Data in Applications

Now that we have the position data available at an easily accessible location, and exposed over a standard protocol, we can really start working with it.

These days, if something is not accessible via a web browser, it might as well not exist. Maybe that’s the reason that MQTT can also be transported over a WebSocket (supported by Mosquitto).

To use the data in our application, we need to do the following:

Open a secure WebSocket connection to our MQTT broker: I used the MQTT.js library and it worked well. Your host requires a valid SSL certificate otherwise your browser might not cooperate.

Subscribe to MQTT topics: MQTT is a publish/subscribe messaging protocol so we need to know which topics are relevant and what kind of messages to expect. You can also subscribe to the blanket topic # which will subscribe to all topics.

Receive messages and act on them: Messages are JSON objects which can be easily used in a browser. Position messages will arrive at the configured rate and contain the position in meters.

Playing Pong

I don’t think I need to explain the game of Pong to the audience of this blog post. It was the first interesting (and to be honest, fun) application that came to my mind and I decided to just go with it.

Image credit: Wikipedia

Modeling the Paddles

In Pong the paddles are moved along a single axis (up/down usually) by the players. To simplify the implementation I therefore decided to only consider one coordinate of the received positions, use that as the y position of the paddle, and render the paddles at fixed x positions.

If UWB pong becomes a thing, we could use the y-coordinate to auto-scale the playing field. I’ll gladly accept a pull request for such a feature. :-)

Receiving Paddle Position Updates

The relevant MQTT topic for us is the the location topic, whose name is dwm/node/<id>/uplink/location . Messages received on location topics have the following format:

{

"position" : {

"x" : 5.2394915,

"y" : 1.4866817,

"z" : 0.64550543,

"quality" : 57

},

"superFrameNumber" : 997

}

As you can see, the message contains the paddle’s X/Y/Z positions, along with a quality indicator and a frame number. For our purposes, we are interested only in position.x . In the game, we calculate the position from a moving average of the last 3 positions to achieve a smoother movement.

Rendering the Paddles

An HTML5 canvas is used to render the playfield, ball and paddles. A simple, non-physically-based collision and reflection logic is used, inspired by this great Stackoverflow answer.

The physical playfield boundaries along the main axis (min X and max X) are used to map the received position to a range between 0 and 1. This normalized position is then mapped to a screen coordinate along the y axis.

I will not go into the details of the implementation, if you’re interested you can have a look at the web app. All the game and rendering logic is contained in a single file. The app is built with Angular 8, just because I’m familiar with it, you could also use plain JS without any framework of course.

The full source code is available under an Apache 2.0 license at: https://github.com/classycodeoss/uwb-pong

Demo

Here’s how this looks in real life, in our office. As you can see, it’s not totally precise, but it’s playable for sure.

Action-packed demo of UWB pong

Conclusion

This post was meant as quick introduction into UWB as an indoor positioning technology, a primer on the Decawave kit, along with a fun application. I am excited to see what the future holds. Will UWB make its way into smartphones and become as ubiquitous as Bluetooth Low Energy? That would pave the way for all kinds of applications that require spatial awareness. Or will it remain a niche technology, inaccessible to a broad audience of users and developers (Apple’s history with NFC on iOS is something to consider here). Only time will tell.

Thank you for your time!