Warning

Warning: Keep out of reach of small children and smart-alec teenagers. Keep printed material away from open flames or excessive heat. Studying these documents may cause drowsiness. Do not read while driving heavy equipment or machinery. If nervousness, sleeplessness or irritability occur, discontinue use and seek professional help. Excessive and prolonged use may cause career burnout and has been known to cause cancer in laboratory rats. Isolated cases of schizophrenia exhibiting delusions of grandeur have been reported. Do not use if you are being treated for high blood pressure or have problems urinating due to excessive consumption of caffinated drinks.

Friday, October 18, 2013

Pi Lab 4: GPS on the Pi







OBJECTIVE:
The objective of this lab is to connect a GPS receiver to the Pi and use it to determine location and time.

REQUIRED MATERIALS:
  • Raspberry Pi Micro-Controller
  • 8GB SDCard with the Operating System NOOBS (Raspian – Debian Linux derivative) pre-installed.
  • Power Supply, AC-DC Converter, External Plug in, Micro USB connector, 5W, 5V, 1A output.
  • USB Keyboard.
  • USB Mouse.
  • USB Hub
  • HDMI Monitor.
  • HDMI Cable.
  • HDMI to DVI Adpater (if your monitor only has a DVI and not a HDMI connector).
  • One Ethernet cable
  • Adafruit Ultimate 66 GPS Breakout with 9 pin Header and Coin Cell Battery Holder
  • SMA to uFL/u.FL/IPX/IPEX RF Adapter Cable
  • Adafruit External GPS Antenna, 3-5Volt,  28dB gain, 5 Meter cable with SMA Connector
  • USB to TTL Serial Cable - Debug / Console Cable
  • CR1220 Coin Cell Battery
  • Soldering iron with solder
  • Electronics cleaning solvent (available at electronics and automotive stores)
PRE-REQUISITES:
  • An operating system must be installed onto the Raspberry Pi (see Pi Lab – Initial Setup)
  • The Pi must have Internet connectivity.
A basic understanding of:
  • Ethernet networking.
  • Connector types such as HDMI, DVI, USB, RJ-45 (Ethernet), etc…
  • The Linux Operating System
REFERENCES:
Raspberry Pi Help
  • http://www.raspberrypi.org/phpBB3
  • http://learn.adafruit.com/adafruit-ultimate-gps-on-the-raspberry-pi
  • http://blog.retep.org/2012/06/18/getting-gps-to-work-on-a-raspberry-pi/
GPS
  • http://gpsd.berlios.de/gpsd.html
  • http://catb.org/gpsd/
  • http://gpsd.berlios.de/client-howto.html
PROCEDURE:
Parts Assembly   
1.    Place the battery holder onto the back side of the circuit board using the outline that is printed on the circuit board as a guide. Solder the ears of the battery holder onto the solder pads of the circuit board.
2.    The header that came with my GPS Breakout module had more than 9 pins on it. If yours does too, then break off the extra pins so that you header has 9 pins.
3.    Insert the short pins into the holes of the GPS Breakout circuit board and solder them into the circuit board. The longer pins should be pointing away from the circuit board. Whether they are on the top of the circuit board or the bottom, it doesn’t really matter. I soldered mine with the pins pointing down so that I can stick them into a breadboard with the circuitry facing up if I want to at some future date.
4.    Clean the flux residue off of the circuit board using a brush and circuit board cleaner.
5.    Insert the battery into the clip on the backside of the circuit board with the positive terminal facing away from the board.
6.    Screw the smaller connector of the white antenna adapter cable to the connector on the circuit board as shown below.
7.    Screw the GPS SMA connector on the black antenna cable onto the larger (SMA) connector on the white adapter cable.
8.    Take the four wires of the USB to TTL adapter and connect them to the header pins on the GPS circuit board as follows:
  • The white wire goes to the header pin labeled “TX”.
  • The black wire goes to “GND”.
  • The red wire goes to “VIN”.
  • The green wire goes to “RX”.
9.    Because there are only two USB ports on the Pi, and three USB connectors that you need to plug into the Pi, you will need to connect the USB hub to the Pi and then connect the keyboard, mouse and GPS Breakout board to the hub.
10.    Power on the switch and connect the Pi to the Ethernet switch with the straight through Ethernet cable.
11.    Place the GPS antenna (the big black square block) on a window sill.
12.    Connect the monitor to the Pi and turn it on.
13.    Apply power to the Pi.


Installing the “gpsd” Program On the Pi
1.    Assuming you have booted up the Pi while being connected to the Internet, your Pi should have the current UTC time. Execute the “date” command to verify (remember that your local time might be several hours different than UTC time) or if you booted to the GUI, the time is displayed in the bottom right corner of the screen. Move your mouse over the time to view the date.

pi@raspberrypi ~ $ date
Wed Oct 16 16:38:38 CDT 2013


2.    Verify that the GPS module connected to the USB port is being recognized. The Adafruit GPS module is highlighted in yellow. Enter the following commands:

pi@raspberrypi ~ $ ls /dev/ttyUSB*
/dev/ttyUSB0
pi@raspberrypi ~ $ sudo lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 058f:9254 Alcor Micro Corp. Hub
Bus 001 Device 005: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 001 Device 006: ID 04d9:1702 Holtek Semiconductor, Inc.
Bus 001 Device 007: ID 093a:2510 Pixart Imaging, Inc. Optical Mouse


3. Verify that the GPS module is sending data to the Pi. Enter the following command and watch the streaming data scroll down your screen. Press “ctrl-c” to exit the data flow and return to the command prompt.

pi@raspberrypi ~ $ sudo cat /dev/ttyUSB0
$GPGGA,221016.000,3531.7468,N,09735.5017,W,2,8,1.02,382.5,M,-25.2,M,0000,0000*66
$GPGSA,A,3,18,02,05,25,29,21,26,15,,,,,1.83,1.02,1.52*0C
$GPRMC,221016.000,A,3531.7468,N,09735.5017,W,0.02,332.89,161013,,,D*78
$GPVTG,332.89,T,,M,0.02,N,0.05,K,D*3C
$GPGGA,221016.000,3531.7468,N,09735.5017,W,2,8,1.02,382.5,M,-25.2,M,0000,0000*66
<output omitted>


4. Install the gps daemon. Enter the command:

pi@raspberrypi ~ $ sudo apt-get install gpsd gpsd-clients python-gps
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed:
  libgps20 python-cairo python-gobject-2 python-gtk2
Suggested packages:
  python-gobject-2-dbg python-gtk2-doc
The following NEW packages will be installed:
  gpsd gpsd-clients libgps20 python-cairo python-gobject-2 python-gps
  python-gtk2
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,605 kB of archives.
After this operation, 8,113 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libgps20 armhf 3.6-4+deb7u1 [233 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ wheezy/main gpsd armhf 3.6-4+deb7u1 [110 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ wheezy/main python-gps armhf 3.6-4+deb7u1 [124 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ wheezy/main python-cairo armhf 1.8.8-1 [68.6 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ wheezy/main python-gobject-2 armhf 2.28.6-10 [475 kB]
Get:6 http://mirrordirector.raspbian.org/raspbian/ wheezy/main python-gtk2 armhf 2.24.0-3 [1,450 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ wheezy/main gpsd-clients armhf 3.6-4+deb7u1 [144 kB]
Fetched 2,605 kB in 22s (116 kB/s)                                            
Preconfiguring packages ...
Selecting previously unselected package libgps20:armhf.
(Reading database ... 62275 files and directories currently installed.)
Unpacking libgps20:armhf (from .../libgps20_3.6-4+deb7u1_armhf.deb) ...
Selecting previously unselected package gpsd.
Unpacking gpsd (from .../gpsd_3.6-4+deb7u1_armhf.deb) ...
Selecting previously unselected package python-gps.
Unpacking python-gps (from .../python-gps_3.6-4+deb7u1_armhf.deb) ...
Selecting previously unselected package python-cairo.
Unpacking python-cairo (from .../python-cairo_1.8.8-1_armhf.deb) ...
Selecting previously unselected package python-gobject-2.
Unpacking python-gobject-2 (from .../python-gobject-2_2.28.6-10_armhf.deb) ...
Selecting previously unselected package python-gtk2.
Unpacking python-gtk2 (from .../python-gtk2_2.24.0-3_armhf.deb) ...
Selecting previously unselected package gpsd-clients.
Unpacking gpsd-clients (from .../gpsd-clients_3.6-4+deb7u1_armhf.deb) ...
Processing triggers for man-db ...
Processing triggers for menu ...
Setting up libgps20:armhf (3.6-4+deb7u1) ...
Setting up gpsd (3.6-4+deb7u1) ...
Setting up python-gps (3.6-4+deb7u1) ...
Setting up python-cairo (1.8.8-1) ...
Setting up python-gobject-2 (2.28.6-10) ...
Setting up python-gtk2 (2.24.0-3) ...
Setting up gpsd-clients (3.6-4+deb7u1) ...
Processing triggers for python-support ...
Processing triggers for menu ...
pi@raspberrypi ~ $


5. Now I will start up the gps daemon and instruct it to acquire its data from the Adafruit Ultimate GPS device that is connected to the the USB to TTY adapter cable. The Adafruit procedure omits the “-n” switch, but I am going to use it.

pi@raspberrypi ~ $ sudo gpsd /dev/ttyUSB0 -n -F /var/run/gpsd.sock

If the Adafruit is locked onto some satellites and tracking them, and the gpsd has opened up the appropriate socket (it may require you to wait a minute or so), you should be able to display the resulting stream of GPS data that we saw earlier. To see if “gpsd” is working properly, execute the command:

pi@raspberrypi ~ $ cgps -s


Press “ctrl-c” to exit and return to the command prompt. If you desire a fancier graphical display, enter the command:

pi@raspberrypi ~ $ xcgps

 

Your display should have a valid number for latitude and longitude. I scrambled the latitude and longitude in the pictures so you won’t come hunt me down and kill me if your gps doesn’t work like mine.

TROUBLESHOOTING:
1. Something that I encountered after I powered off my Pi for the night and then powered it back on the next day was that for some reason “cgps” and “xgps” did not work anymore!  No data was displayed! So I opened up "/var/log/syslog" with the Leafpad text editor and found the following output:

Oct 16 23:15:20 raspberrypi gpsd[1988]: gpsd:ERROR: can't bind to IPv4 port gpsd, Address already in use
Oct 16 23:15:20 raspberrypi gpsd[1988]: gpsd:ERROR: maybe gpsd is already running!
Oct 16 23:15:20 raspberrypi gpsd[1988]: gpsd:ERROR: can't create IPv6 socket


2. Apparently multiple instances of gpsd are trying to access the same resources. So I terminated all instances of gpsd running by executing the command:

pi@raspberrypi ~ $ sudo killall gpsd

3. Then I restarted gpsd:

pi@raspberrypi ~ $ sudo gpsd /dev/ttyUSB0 -n -F /var/run/gps.sock

4. Then I tried “cgps -s” again, and lo and behold, it was working properly.

Change the gpsd Startup Parameters
1. So, what I did next is change the startup parameters of gpsd using the “dpkg-reconfigure” program so that the next time the Pi reboots, it won't try to load gpsd multiple times.

pi@raspberrypi ~ $ sudo dpkg-reconfigure gpsd

2. Select YES to start gpsd automatically.

3. Do NOT let gpsd handle attached USB GPS receivers automatically. Select NO!



4. Enter "/dev/ttyUSB0" if isn't entered already, then select "Ok".


5. Enter the "-n" switch and select "Ok".


6. "/var/run/gpsd.sock" should be entered. If not, enter and then select "Ok". The program should exit at this point and return you to the command prompt.


7. Now that you have reconfigured the startup parameters of gpsd, I reboot your Pi, wait for a few minutes for the GPS to acquire a satellite fix and then try to run “cgps -s”. It should work every time you boot up the Pi now.


No comments:

Post a Comment