Measure Bike Speed with PocketCHIP and GPS Receiver

I won a #FreePocketCHIP last Christmas. It is a "pocketable" Linux computer running Debian, and has one USB 2.0 host port. Apart from adding a speaker and playing PICO-8 games, PocketCHIP's form factor and hackability make it suitable for many other projects. Today, I'm going to find out how fast I am riding a bike, with the PocketC.H.I.P and VK-172 GPS receiver.

PocketCHIP and VK-172 GPS receiver

Meet the GPS Receiver

The GPS receiver I have is the VK-172 G-mouse USB GPS receiver. It has a small form factor, and connects to the PocketCHIP via USB. When connected, it identifies itself as a USB device with ID 1546:01a7, and shows up as a serial port:

chip@chip-c:~$ lsusb
Bus 002 Device 008: ID 1546:01a7 U-Blox AG 
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
chip@chip-c:~$ ls -l /dev/ttyACM0 
crw-rw---- 1 root dialout 166, 0 Jul 27 19:16 /dev/ttyACM0

After getting a serial port, almost all GPS receivers work the same: they will receive signals from the GPS satellites, compute the receiver's location and other parameters, and write the results in a text format, called "NMEA sentences", to the serial port. sudo screen /dev/ttyACM0 115200 command reads from the serial port, and gives me a scrolling window of NMEA sentences:

NMEA sentences read from GPS receiver

These NMEA sentences contain a variety of information received, computed, or inferred by the GPS receiver. I can easily identity longitude and latitude, date, and UTC time fields. However, semantics of other fields are less apparent without reading a specification.

Install gpsd and Clients

A better way to work with GPS receivers is through gpsd. gpsd is a service daemon that monitors GPS receivers attached to a computer, and make data on the location/course/velocity of the sensors available for queries. gpsd understands a wide variety of GPS receivers and protocols, so that I do not have to deal with all the complexity.

To install gpsd on PocketCHIP, execute:

sudo apt install gpsd gpsd-clients python-gps

The gpsd-clients package installs a few "example" client programs that can display GPS data, such as xgps, which visualizes the information nicely:

xgps graphical user interface

However, my goal is to find out how fast I am riding a bike, and I can't be watch xgps interface while I am on the bike! I need to log this information into a file for later analysis.

Flipping through all the client programs that came with gpsd, there are two programs that can write GPS data to a file: gpxlogger and gpspipe. gpxlogger collects GPS data and prints the measurements in XML format, which works exactly like GPS Logger for Android. Its output looks like:

<gpx version="1.1" creator="GPSD 3.11 -"
  <src>GPSD 3.11</src>
   <trkpt lat="32.220033" lon="-110.940000">
    <src>GPSD tag=""</src>
   <trkpt lat="32.220035" lon="-110.940001">
    <src>GPSD tag=""</src>
   <trkpt lat="32.220042" lon="-110.940002">
    <src>GPSD tag=""</src>

Unfortunately, this output does not contain the speed.

gpspipe uses a JSON output format, prodiving richer information. gpspipe -w output looks like (the actual output is line-based where every line is a complete JSON object; snippet has been re-formatted):


 "subtype":"1.00 (59842)","activated":"2017-07-28T04:12:05.076Z","flags":1,"native":0,






In this output, I can find my speed in "class":"TPV" records under "speed" property.

Go for a Bike Ride

A GPS receiver must have good reception of satellite signals to work correctly and accurately. If the skyview were obscured, the GPS receiver would complain "no fix" and does not produce any meaningful readings. Thus, it is critical to keep the GPS receiver exposed.

GPS receiver directly plugged into PocketCHIP

My test indicates that plugging the GPS receiver directly on PocketCHIP's USB port causes bad reception. Most likely, the big printed circuit board is blocking satellite signals. Thus, a USB extension cord is necessary for optimal GPS reception performance.

bike ride with PocketCHIP and GPS receiver

I place the GPS receiver in a small pocket on the outside of my backpack, connected via an USB extension cord to the PocketCHIP located in a larger compartment of the backpack. When I'm ready to ride, I open the console and type gpspipe -w | tee gps.log, to collect GPS data into a file. While gpspipe is running, I turn off the screen by selecting Sleep option in PocketHome, so that the battery can last longer. At the end of my ride, I go back into the console, and press CTRL+C to end GPS data collection.

Regarding battery consumption: with PocketCHIP screen in sleep mode and VK-172 GPS receiver running, a 3-hour bike ride recording session consumes 30~35% from a fully charged battery. Thus, I estimate the PocketCHIP battery can last about 7 hours for this application.

Find Top Speed through Data Analysis

Although gpspipe -w writes JSON output, every record is on a separate line, so it's easy enough to parse the log with awk.

#!/usr/bin/awk -f
  FS = ","
$0 ~ /"class":"TPV"/ && $0 ~ /"tag":"GLL"/ && $0 ~ /"mode":3/ {
  for (i = 1; i <= NF; ++i) {
    if ($i ~ /"speed":/) {
      print substr($i, 9, 9999)

This script parses gps.log and prints all speed records. Speed unit is meters per second (m/s).

To obtain my top speed, I can simply run:

chip@chip-c:~$ ./gps.awk gps.log | sort -n | tail -1

So here's the answer! I am riding the bike at a top speed of 9.641 m/s, or 21.56 mph.

Plot Speed Over Time

I can visualize how my riding speed changes over time with Gnuplot.

After installing Gnuplot sudo apt install gnuplot, I can plot the speeds with:

chip@chip-c:~$ ./gps.awk gps.log > speed.txt 
chip@chip-c:~$ gnuplot

    G N U P L O T
    Version 4.6 patchlevel 6    last modified September 2014
    Build System: Linux armv7l

    Copyright (C) 1986-1993, 1998, 2004, 2007-2014
    Thomas Williams, Colin Kelley and many others

    gnuplot home:
    faq, bugs, etc:   type "help FAQ"
    immediate help:   type "help"  (plot window: hit 'h')

Terminal type set to 'qt'
gnuplot> plot 'speed.txt' with lines notitle

The chart appears: (press Q to close the chart)

gnuplot lines chart of bike speed over time

I can see that my riding speed starts at more than 5 m/s, but it drops to 4 m/s when I'm into the third hour and my sore legs aren't helping.


This post demonstrates how to use a PocketC.H.I.P and a VK-172 USB GPS receiver to record an activity such as a bike ride using gpsd and gpspipe programs, and then find out the maximum moving speed during the activity, as well as create a chart of speed over time using gnuplot program.