Tuesday, June 18, 2013

RTKLIB on the Raspberry Pi

I've got a workmate. He wants to build an autonomous, electric, rotary hoe. Anyway, he needs to locate  the hoe to within a few centimetres.

I'd been contemplating DGPS for a while as I've found GPS tracks are a bit wandery when logging tracks for OpenStreetMap. I thought it'd be a good capability to have, so I've stepped up to the task of tracking Robohoe.


Background Information

This is basically a link dump of what I've found so far:

RTKLIB : DGPS software
  This is the bees knees for cheap arse DGPS. It can take raw GPS input from 5 or 6 different types of GPS, as well as most variants of the RTCM GPS correction stream. It can combine two data streams, from a reference station and a rover, to produce a high-precision position for the rover, or it can take a single stream from a reference station and produce an RTCM output for other stations.

  It's also got a number of other super handy programs, that do things like convert RTKLIB output into a Google Earth KML file, serve serial input out to multiple processes via Unix sockets etc.

The RTKLIB manual is here.

LEA-4T : GPS hardware
  The U-Blox 'T' series of modules (the LEA-4T, LEA-5T, and NEO-6T) produce data suitable for input to RTKLIB. The link above is the cheapest source I've found for these. I've bought 5 so far, and they've all been OK. Interestingly, this guy sells them on EBay for about three times the price.

 The pinout for this module is (from here):
antenna - SMB male connector
2 4 6 8
1 3 5 7
bottom

1 antenna vcc
2 vcc
3 tx
4
5 rx
6 timepulse
7
8 gnd

Note that Antenna Vcc and Vcc are both 5V, and aren't connected on board, so 5V is needed on both pins. Tx and Rx are both serial at 3.3V levels.

EBay : GPS antenna
  Generic powered GPS antenna with SMB plug, ~$7.00. Works.

EBay : USB serial unit
  Generic FTDI USB serial dongle. Used for GPS configuration on a Windows machine. In final use, the module will send data to the Pi via the 3.3V serial port on the GPIO.

u-center : GPS configuration software
  This is a whizzy Windows application which can be used to configure the modules to output the correct messages for RTKLIB. It can also be used to have a peek at the contents of all the mysterious binary packets that come out of the module.

Geosciences Australia : DGPS correction data
   GA runs a network of reference stations around Australia, and you can get non real-time correction data from their FTP site. This will be handy for post-processing data from my static installation to derive a precise location. I can then use the static station for real-time DGPS.

RNXCMP : file conversion software
  The GA data is in an odd format called the Hatanaka format. This program converts it into a more usable form.


Up To Now

What I've done so far is:

  • By default, Raspbian on the Pi runs a debug console on the GPIO serial port. I've modified the install to not do this and leave the serial port free.
  • Made up an adaptor to power the module from the GPIO, and send and receive data to the GPIO serial port.
  • Installed and (mostly) compiled RTKLIB.
  • Configured the module using u-center on a Windows machine.
  • Installed the module on the Pi, and used str2str (part of the RTKLIB suite) to send the module's serial data over the network.
  • Used the Windows version of RTKLIB to receive the data stream, and convert the raw stream to a position.


Current Status

The current status is:
  • My static antenna is fixed to point under the Laserlite in the porch.
  • Raw data is being collected from the module using the static antenna (~48 hours so far)


The Future

Future plans include:
  • Work out conversion of the GA correction files.
  • Work out how to produce a precise position from the logged static antenna data, and the GA correction data.
  • Work out how to produce real time correction data from the static station and RTKLIB.


Monday, March 4, 2013

Silliness on a bike


A POCSAG -> email gateway

EDITED 26-Nov-2013: Minor bugfixes on commands, moved script to Google Drive, added more explanation.

I put this script together a while ago. It examines the output from Multimon-NG, and forwards selected POCSAG messages via email. Note that it uses SMTP via TLS, which may not work for your email provider. Gmail works fine.

First off, copy the script to the home directory of your Pi.

Then:

sudo apt-get install libnet-smtp-tls-perl libdatetime-perl
chmod +x pocsag_fwd.pl

Next, configure it. Lines 31-33 need to be updated to include your SMTP server and login details.
Also, the definition of the @stream variable (lines 11-26) need to be modified to include the POCSAG address ID's and email addresses you want. Hopefully the syntax and meaning of the fields will be self-explanatory. You might want to examine the output from multimon-ng for a while to find the addresses you're interested in.

To use it:

rtl_fm -N -o 4 -l 10 -A fast -r22050 -s88200 -C -D -f 148.7125M - |multimon-ng  -t raw -a POCSAG512 -f alpha /dev/stdin|./pocsag_fwd.pl

Putting rtl_fm and Multimon-NG together

Unix pipes are awesome.

To decode POCSAG on the Pi, this is all you need:


pi@raspberrypi ~ $ rtl_fm -N -o 4 -l 10 -A fast -r22050 -s88200 -C -D -f 148.7125M - |multimon-ng -t raw -a POCSAG512 -f alpha /dev/stdin
Found 1 device(s):
  0:  Realtek, RTL2838UHIDIR, SN: 

Using device 0: ezcap USB 2.0 DVB-T/DAB/FM dongle
multimonNG  (C) 1996/1997 by Tom Sailer HB9JNX/AE4WA
            (C) 2012 by Elias Oenal
available demodulators: POCSAG512 POCSAG1200 POCSAG2400 EAS UFSK1200 CLIPFSK AFSK1200 AFSK2400 AFSK2400_2 AFSK2400_3 HAPN4800 FSK9600 DTMF ZVEI SCOPE
Enabled demodulators: POCSAG512
Found Elonics E4000 tuner
Oversampling input by: 3x.
Oversampling output by: 4x.
Buffer size: 7.74ms
Tuned to 148977100 Hz.
Sampling at 1058400 Hz.
Output at 22050 Hz.
Exact sample rate is: 1058400.010094 Hz
Tuner gain set to automatic.
POCSAG512-: Address:   12935  Function: 2
POCSAG512-: Numeric: 4-23U7]47978140-0[93-]]338.50.2.U7 [62795]0.659560U9- 660.6182836.7.-]560180482-]43683818 .569696926]281-04-201.6 920-01U1405 2647438.9169[81-0.85]277U410000
POCSAG512-: Blah blah blah - content redacted by me<NUL><NUL>
POCSAG512-: Address: 1720997  Function: 0
POCSAG512-: Numeric: U-U1  5 .4.3161395]..0. 2 270.9]3U.45428].5 925143 024U802960-97 228]U352280733.9-46 20-538  562-9740155.1 1]2950.9255.[ 5619502U1869-40915.[ 62868[ 1]-05.5 2]2950.924U85 059]358  2637. 2014U84-2.49502U1 2U5]0U 14-46100
POCSAG512-: Alpha: THIS IS A TEXT MESSAGE, IT WAS ANOTHER TEXT MESSAGE, BUT NOW IT INCRIMINATES NO PERSON<NUL>

And it's going. You can now construct a veritable daisy chain of processes to do all sorts of things with the output.

Compiling Multimon-NG for the Raspberry Pi

EDITED 26-Nov-2013: Minor bugfixes on commands, added more explanation.

Got my mouth all ready to do some POCSAG decoding on the Pi, running Raspbian Wheezy.

Enter Multimon-NG. I tried compiling it as per this page. Had no problems. In summary:

sudo su -
apt-get install libpulse-dev libx11-dev qt4-qmake
apt-get install git cmake build-essential
git clone https://github.com/EliasOenal/multimonNG.git
cd multimonNG/
mkdir build
cd build
qmake ../multimon-ng.pro
make
make install

A previous version of this post listed lots of problems, mainly due to the lack of qmake. I later found that it's included in the qt4-qmake package. I used the latest (2013-09-25) version of Raspbian, which may have also helped.

Compiling rtl_fm on the Raspberry Pi

EDITED 13-Sep-2013: Minor bugfixes on commands, added more explanation.

I got myself an RTL2832 DVB-T dongle from the bestest provider of cheap stuff in the whole Internets.

Getting it going on Raspbian Wheezy on the Pi turned out to be easy (ref):

sudo su -
apt-get update
apt-get install git
git clone git://git.osmocom.org/rtl-sdr.git
apt-get install libusb-dev libusb-1.0 libtool cmake
apt-get install build-essential

cd rtl-sdr/
mkdir build
cd build
cmake ../
make
make install
ldconfig

I found that the instructions about running cmake ../ -DINSTALL_UDEV_RULES=ON  to allow non-root access didn't work, so I copied the udev rule over manually to the correct directory:

cd ..
cp rtl-sdr.rules /etc/udev/rules.d/


Proof of the pudding, reboot, plug in the dongle, test it as a regular user.


pi@raspberrypi ~ $ rtl_test -t
Found 1 device(s):
  0:  ezcap USB 2.0 DVB-T/DAB/FM dongle

Using device 0: ezcap USB 2.0 DVB-T/DAB/FM dongle
Found Elonics E4000 tuner
Supported gain values (14): -1.0 1.5 4.0 6.5 9.0 11.5 14.0 16.5 19.0 21.5 24.0 29.0 34.0 42.0 
Benchmarking E4000 PLL...
[E4K] PLL not locked for 51000000 Hz!
[E4K] PLL not locked for 2217000000 Hz!
[E4K] PLL not locked for 1109000000 Hz!
[E4K] PLL not locked for 1243000000 Hz!
E4K range: 52 to 2216 MHz
E4K L-band gap: 1109 to 1243 MHz

And we're all done!

You'll notice the first command I entered is sudo su - .This gives root access, saving you from having to type Simon Says sudo for every command. Once you're done, exit brings you back to the unprivileged user.