Skip to content

Instantly share code, notes, and snippets.

@dougtoppin
Last active December 15, 2015 09:19
Show Gist options
  • Save dougtoppin/5237425 to your computer and use it in GitHub Desktop.
Save dougtoppin/5237425 to your computer and use it in GitHub Desktop.
ForeFlight is an excellent iOS application for all aspects of aviation (planning, filing, monitoring, situational awareness, browsing, weather). It uses GPS (internal or external via bluetooth) for position data. It also supports external position data which means you can use ForeFlight to display data from something other than where you are. Th…
#!/usr/bin/perl
# ForeFlight.pl
# Simple perl script to send position data via udp to a running instance
# of the iOS ForeFlight aviation app.
# A description can be found at: http://foreflight.com/support/network-gps
# FF expects the following:
# - updates once a second
# - udp protocol
# - port 49002
# - string format message (comma separated fields):
# - XGPS
# - name of simulator
# - longitude
# - latitude
# - alitude in meters MSL
# - track-along-ground from true north
# - groundspeed in meters/sec
# - example string: 'XGPSMy Sim,-80.11,34.55,1200.1,359.05,55.6'
# When you run this you must go the ForeFlight menu More->Devices
# and you should see the device name you have set below.
# Select that device name and set the slider to 'on' (you should
# only have to do this once for the name that you pick).
# FF should detect your device within 5 seconds once you start
# sending the data.
# If you stop the script FF will detect it in about 5 seconds
# and fall back to the devices own GPS data source.
use IO::Socket;
use strict;
use Math::Trig qw(great_circle_direction deg2rad rad2deg);
# you can send directly to the FF iOS device IP or the broadcast ip for your wifi network
my $ffip = '192.168.1.141';
my $ffport = 49002;
# This will just loop through the following locations
# position data in long, lat
my @positions = (
"-80.12,34.40",
"-80.22,34.51",
"-80.31,34.63",
"-80.35,34.74",
"-80.28,34.85",
"-80.19,34.95",
"-80.09,35.03",
"-79.96,34.99",
"-79.87,34.95",
"-79.76,34.89",
"-79.84,34.81",
"-79.96,34.71",
"-79.84,34.67",
"-79.73,34.63",
"-79.81,34.55",
"-79.89,34.45",
"-79.99,34.43",
);
# altitude in meters MSL
my $alt = 500.1;
# track-along-ground from true north
my $track = 359.05;
# groundspeed in meters/sec
my $gndspeed = 55.6;
# create the outbound socket
my $sock = IO::Socket::INET->new(
Proto => 'udp',
PeerPort => $ffport,
PeerAddr => $ffip
) or die "Could not create socket: $; \n";
my $index = 0;
my $data = '';
while (1) {
# calculate the heading data (which way the plane is pointing)
# using great circle trig using the current and next
# sets of position data
# get the current position data
my @values = split(',',$positions[$index]);
my $loncurrent = $values[0];
my $latcurrent = $values[1];
# determine the array index for the next set of position data
my $nextindex;
if ($index > ($#positions - 1)) {
$nextindex = 0;
} else {
$nextindex = $index + 1;
}
# get the next position data
my @values = split(',',$positions[$nextindex]);
my $lonnext = $values[0];
my $latnext = $values[1];
# radian conversion
sub NESW { deg2rad($_[0]), deg2rad(90 - $_[1]) }
my @current = NESW($loncurrent, $latcurrent);
my @next = NESW($lonnext, $latnext);
my $rad = great_circle_direction(@current, @next);
# now determine the heading data
my $degrees = rad2deg($rad);
# round it to one decimal place
my $degreesStr = sprintf("%.1f", $degrees);
$data = 'XGPSForeFlight Sim' . ',' .
$positions[$index] . ',' .
$alt . ',' .
$degreesStr . ',' .
$gndspeed
;
print "Sending $data\n";
$sock->send($data);
# move to the next set of position data
$index = $nextindex;
# FF expects position data once a second
sleep 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment