Connecting the Raspberry Pi to a USB GPS device

Before you get started, let me give you a brief tutorial on GPS. GPS, which stands for Global Positioning System, is a system of satellites that transmit signals. GPS devices use these signals to calculate the position of an object. There are a total of 24 satellites transmitting signals all around the Earth at any given moment, but your device can only see the signal from a much smaller set of satellites.

Each of these satellites transmits a very accurate time signal that your device can receive and interpret. It receives the time signal from each of these satellites, and then based on the delay—the time it takes the signal to reach the device—it calculates the receiver's position, which is in turn based on a procedure called triangulation. The next two diagrams illustrate how a device uses the difference between the delay data from three satellites to calculate its position. The following is the first diagram, depicting the device at its initial position:

Connecting the Raspberry Pi to a USB GPS device

The GPS device is able to detect the three signals and the time delays associated with receiving them. In the following diagram, the device is at a different location, and the time delays associated with the three signals have changed from those in the previous diagram:

Connecting the Raspberry Pi to a USB GPS device

The time delays of the signals T1, T2, and T3 can provide the GPS with an absolute position using triangulation. Since the positions of the satellites are known, the amount of time that the signal takes to reach the GPS device is also a measure of the distance between that satellite and the GPS device. To simplify this concept, let's see an example in two dimensions. If the GPS device knows its distance from one satellite, based on the amount of time delay, you could draw a circle around the satellite at that distance and know that your GPS device is on the boundary of that sphere, as shown in the following diagram:

Connecting the Raspberry Pi to a USB GPS device

If you have two satellites' signals and know the distance between them, you can draw two circles, as shown in the following diagram:

Connecting the Raspberry Pi to a USB GPS device

However, you know that since you can only be at the points on the boundary of the circle, you must be at one of the two points that are on the boundary of both the circles. Adding an additional satellite would eliminate one of these two points, providing your exact location. You need more satellites if you are going to do this in all three dimensions.

Now it's time to connect the device. First, connect the GPS unit by plugging it into one of the free USB ports on the USB hub. Once it is plugged in and the unit is rebooted, type lsusb and you should see the output shown in the following screenshot:

Connecting the Raspberry Pi to a USB GPS device

The device is shown as Prolific Technology, Inc. PL2303 Serial Port. Your device is now connected to your Raspberry Pi. Now create a simple Python program that will read the value from the GPS device. If you are using the Emacs editor, type emacs measgps.py. A new file will be created called measgps.py. Then type the following:

#!/usr/bin/python 
import serial 
ser = serial.Serial('/dev/ttypUSB0', 4800, timeout = 1) 
x = ser.read(1200) 
print x 

Let's go through the code to see what is happening:

  • #!/usr/bin/python: As discussed earlier, this line simply makes this file available for you to execute from the command line.
  • import serial: This imports the serial library. This will allow you to interface the USB GPS sensor with the GPS system.
  • ser = serial.Serial('/dev/ttyUSB0', 4800, timeout = 1): This command sets up the serial port to use the /dev/ttyUSB0 device, which is your GPS sensor using a baud rate of 4800 and a timeout value of 1 second.
  • x = ser.read(1200): This command then reads a set of values from the USB port. In this case, you read 1200 bytes; this includes a fairly complete set of your GPS data.
  • print x: This command then prints out the value obtained from the previous command.

Once you have created this file, you can run the program and talk to the device. Do this by typing python measgps.py, and the program will run. You should see the output shown in the following screenshot:

Connecting the Raspberry Pi to a USB GPS device

The device returns raw readings to you, which is a good sign. Unfortunately, there isn't much good data here, as the robot is indoors. How do you know this? Look at one of the lines that starts with $GPRMC; this line should tell you your current latitude and longitude values. The GPRS reports the following code:

$GPRMC,001714.037,V,,,,,,,150209,,,N*45 

The preceding line of data should take the form shown in the following table, with each field separated by a comma:

0

1

2

3

4

5

6

7

8

9

10

11

12

$GPRMC

1714.037

V

150209

N

*45

The following table offers an explanation of each of the fields shown in the preceding table:

Field

Value

Explanation

1

220516

Timestamp

2

V

Validity: A (OK) and V (invalid)

3

Empty

The current latitude

4

Empty

North or South

5

Empty

The current longitude

6

Empty

East or West

7

Empty

The speed, in knots, at which you are moving

8

Empty

Course: The angular direction in which you are moving

9

150209

Date-stamp

10

Empty

Magnetic variation: The variation between magnetic and true North

11

N

East or West

12

*45

Checksum

In this case, the second value in the string is reporting V or that the unit cannot find enough satellites to get a position. Take the unit outdoors, and you can get the result shown in the following screenshot from your measgps.py program:

Connecting the Raspberry Pi to a USB GPS device

Note that the $GPRMC line now reads as follows:

$GPRMC,194827.000,A,4349.1418,N,11146.1046,W,0.00,,111213,,,A*64 

Now the values will be as shown in the following table:

Field

Value

Explanation

1

020740.000

Timestamp

2

A

Validity: A (OK) and V (invalid)

3

4349.1426

The current latitude

4

N

North or South

5

11146.1064

The current longitude

6

W

East or West

7

1.82

Speed, in knots, at which you are moving

8

214.11

Course: The angular direction in which you are moving

9

021013

Date-stamp

10

Magnetic variation: The variation between magnetic and true North

11

East or West

12

*7B

Checksum

Now you have some indication of where you are; however, the GPS data is in raw form, which may not mean much. In the next section, you will figure out how to do something with these readings.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset