Reading data from GPS devices

Invisible Pacemaker

Once I have the YAML data, I can easily write applications in Perl and other languages that interpret the stored GPS data. One feature I really miss with my Garmin 10 (and that my previously used Garmin Forerunner 101 had) is the so-called Virtual Partner.

With this feature, runners can program in the speed of a virtual pacesetter, who runs at constant speed and crosses the finish line at precisely the expected time by definition. During the run, the GPS device indicates how far the virtual runner's legs have taken them. If the pacemaker is 100 yards ahead, I need to speed up, if it drops back, I can slow down, and if we are jogging together, I am sure to finish on time.

Listing 3 implements a function to compare two runs in a similar way for my new GPS device. It uses two FIT files converted into YAML format, reads the distance and timestamp entries, and draws a graph with the distances covered by the two runners at specific times. The x-axis shows the elapsed running time in seconds, and the y-axis visualizes the distance covered by the runners in meters.

Listing 3

vrunner

001 #!/usr/bin/perl
002 use strict;
003 use warnings;
004 use Imager;
005 use Imager::Plot;
006 use YAML qw( LoadFile );
007
008 my $plot =
009   Imager::Plot->new(
010  Width  => 600,
011  Height => 400,
012  GlobalFont =>
013    '/usr/share/fonts/' .
014    'truetype/freefont/' .
015    'FreeSans.ttf'
016   );
017
018 my ($first, $second) = @ARGV;
019 die "usage: $0 1.yaml 2.yaml"
020   if !defined $second;
021
022 my @first_pairs =
023   data_extract($first);
024 my @second_pairs =
025   data_extract($second);
026
027 data_set_add($plot,
028  \@first_pairs, "green");
029 data_set_add($plot,
030  \@second_pairs, "red");
031
032 my $img = Imager->new(
033  xsize => 650,
034  ysize => 450
035 );
036
037 $img->box(
038  filled => 1,
039  color  => 'white'
040 );
041
042 # Add text
043 $plot->{'Ylabel'} =
044   'Distance';
045 $plot->{'Xlabel'} = 'Time';
046 $plot->{'Title'} =
047   'Fast Pace vs. Slow';
048
049 $plot->Render(
050  Image => $img,
051  Xoff  => 40,
052  Yoff  => 420
053 );
054
055 $img->write(
056  file => "vrunner.png");
057
058 #############################
059 sub data_extract {
060 #############################
061  my ($file) = @_;
062
063  my $base;
064  my @pairs = ();
065
066  my $data = LoadFile($file);
067  for my $record (@$data) {
068   my $time =
069     $record->{timestamp};
070   my $dist =
071     $record->{distance};
072   $base = $time
073     if !defined $base;
074   $dist =~ s/\D+$//;
075   push @pairs,
076     [ $time - $base, $dist ];
077  }
078
079  return @pairs;
080 }
081
082 #############################
083 sub data_set_add {
084 #############################
085  my ($plot, $data, $color) =
086    @_;
087
088  $plot->AddDataSet(
089   X => [
090    map { $_->[0] } @$data
091   ],
092   Y => [
093    map { $_->[1] } @$data
094   ],
095   style => {
096    marker => {
097     size   => 2,
098     symbol => "circle",
099     color =>
100       Imager::Color->new(
101      $color),
102    }
103   }
104  );
105 }

Both FIT files were created during real runs. After my sporting excursion (Figure 2), I completed another training run on the same route. I wanted to find out whether a slightly slower pace in the first few miles would leave me feeling stronger for the final sprint and leave enough breathing room to manage a few hills in between (elevation difference about 300 feet).

The CPAN Imager::Plot module plots the data passed in as an array on the coordinate system and labels the axes nicely. The data_extract() function in line 59 expects a YAML file with the FIT data and returns an array. It contains x/y value pairs of stored combinations of timestamps and distances traveled.

Line 74 removes the unit, m for "meters," from the value for distance, leaving only the numeric value. The variable $base is set to the timestamp of the first entry, so that vrunner can later only add timestamps relative to this base point and append them to the resulting array.

Clockwork Horse

The green graph in Figure 6 shows the first, fast run; the red graph was created after the second, slower run. The comparison shows that I was unable to translate the power reserves gained by the slower starting pace into a faster second half. Once I'm in motion, I seem to run like clockwork – without anyone dangling a carrot in front of my nose, I seem to run no faster than necessary.

Figure 6: The laid-back jogger in red is steadily losing against the runner in flattering green, who is apparently pushing himself to the limit.

The Author

Mike Schilli works as a software engineer with Yahoo! in Sunnyvale, California. He can be contacted at mailto:mschilli@perlmeister.com. Mike's homepage can be found at http://perlmeister.com.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: Plotting GPS Data

    Perl hackers take to the hills with a navigation system that provides a graphical rendering of a hiking tour.

  • Perl: Automating Color Correction

    If you have grown tired of manually correcting color-casted images (as described in last month's Perl column), you might appreciate a script that automates this procedure.

  • Perl: Jawbone UP Data

    The Jawbone UP electronic bracelet measures the wearer's daily activity and nocturnal sleep patterns. If you are bored by the official smartphone app, you can create your own Perl scripts and access your data via the unofficial web API.

  • GPS Tools

    Almost all manufacturers of GPS devices use proprietary formats to save routes, tracks, and waypoints. Vendors unfortunately rarely offer Linux software for uploading and downloading or processing the data. Four GPS editors keep Linux users on the right track.

  • Orca

    Monitor and troubleshoot Linux system performance with the free and powerful Orca.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News

njobs Europe
What:
Where:
Country:
Njobs Netherlands Njobs Deutschland Njobs United Kingdom Njobs Italia Njobs France Njobs Espana Njobs Poland
Njobs Austria Njobs Denmark Njobs Belgium Njobs Czech Republic Njobs Mexico Njobs India Njobs Colombia