Skip to content

Commit 2f190ae

Browse files
committedAug 27, 2010
Don't connect to gpsd directly, but rather use gpspipe(1), which knows how to talk to various versions of gpsd
1 parent a2c6df1 commit 2f190ae

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed
 

‎bacchusmapper/tools/bacchusmapper-gpsd.pl

+37-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/perl
2+
# vim:set ts=4 sw=4 ai:
23

34
# seconds
45
my $FREQ = 30;
@@ -8,7 +9,6 @@
89

910
exit main();
1011

11-
1212
# this only works with specially tagged KMLs... 'cause I don't wanna parse XML.
1313
# write out both a point and append to the polyline
1414
sub addCoordToKML {
@@ -51,19 +51,6 @@ sub createBlankKML {
5151
return;
5252
}
5353

54-
sub connectToGPS {
55-
use IO::Handle;
56-
use Socket;
57-
58-
my $sock;
59-
60-
socket($sock, PF_INET, SOCK_STREAM, (getprotobyname('tcp'))[2]) || die "socket() failed";
61-
connect($sock, sockaddr_in(2947, inet_aton("127.0.0.1"))) || die "unable to connect to gpsd";
62-
$sock->autoflush(1);
63-
64-
return $sock;
65-
}
66-
6754
sub parsedict {
6855
my $ind = shift;
6956
my %dict;
@@ -87,28 +74,46 @@ sub main {
8774

8875
createBlankKML($OUTFN) if not -e $OUTFN;
8976

90-
my $gpsd = connectToGPS();
91-
92-
my $initdone = 0;
77+
open my $gpsd, "gpspipe -r |" or die "fork: $!";
9378
my $lastts = 0;
79+
9480
while (my $line = <$gpsd>) {
95-
$line =~ tr/\r//d; $line =~ tr/\n//d;
96-
print STDERR "in: '$line'\n";
97-
my $d = parsedict($line);
98-
die "no class in packet from gpsd" unless defined $d->{'class'};
99-
if (!$initdone && $d->{'class'} eq "VERSION") {
100-
print $gpsd "?WATCH={\"enable\":true,\"json\":true}\r\n";
101-
$initdone = 1;
102-
} elsif (($d->{'class'} eq 'TPV') && ($d->{'tag'} eq 'GLL')) {
103-
# {"class":"TPV","tag":"GLL","device":"/dev/tty.usbserial","time":1280012402.000,"ept":0.005,"lat":37.762366667,"lon":-122.419180000,"alt":25.000,"epx":27.271,"epy":24.113,"epv":23.000,"track":0.0000,"speed":0.000,"climb":0.000,"mode":3}
104-
print "" . $d->{'time'} . ": (" . $d->{'lat'} . ", " . $d->{'lon'} . ", " . $d->{'alt'} . ") @ " . $d->{'speed'} . "\n";
105-
next unless defined $d->{'time'} && defined $d->{'lat'} && defined $d->{'lon'} && defined $d->{'alt'} && defined $d->{'speed'};
106-
next unless (time - $lastts >= $FREQ);
107-
addCoordToKML($OUTFN, $d->{'time'}, $d->{'lat'}, $d->{'lon'}, $d->{'alt'}, $d->{'speed'});
108-
$lastts = time + 0;
109-
}
81+
$line =~ s/\r?\n$//;
82+
warn "in: '$line'\n";
11083

84+
# $GPGGA,172134.84,3747.0852,N,12223.3278,W,1,03,22.4,00008,M,,,,*04
85+
# $GPGGA,210948.48,3748.9736,N,12107.6101,W,1,08,1.1,00014,M,,,,*34
86+
87+
if ($line =~ /^\$GPGGA,(\d{2})(\d{2})(\d{2}).\d{2},([\d\.]+),N,([\d\.]+),W,(\d+),(\d+),([\d.]+),([\d\.]+),M,/) {
88+
my ($hour, $min, $sec, $long, $lat, $fixtype, $num_sats, $horizontal_dilution, $alt) =
89+
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
90+
91+
my $fraction;
92+
$lat *= -1 / 100;
93+
$long /= 100;
94+
95+
$fraction = $lat - int $lat;
96+
$lat = int($lat) + $fraction/60 * 100;
97+
98+
$fraction = $long - int $long;
99+
$long = int($long) + $fraction/60 * 100;
100+
101+
$alt = 0 if ($alt <= 0);
102+
my $feet = $alt * 3.2808399;
103+
my $speed = 0;
104+
105+
unless (time - $lastts >= $FREQ) {
106+
warn "Position: $lat $long $feet $speed (too soon)\n";
107+
next;
108+
} else {
109+
warn "Position: $lat $long $feet $speed\n";
110+
}
111+
112+
addCoordToKML($OUTFN, scalar(localtime), $long, $lat, $feet, $speed);
113+
$lastts = time;
114+
}
111115
}
116+
warn "EOF\n";
112117
close $gpsd;
113118
return 0;
114119
}

0 commit comments

Comments
 (0)
Please sign in to comment.