You are on page 1of 3

MIT Subject 2.

017
Lab 4 Worksheet – Serial Communications and the GPS Receiver

Goals:
Understand and implement serial communications on the TT8.
Parse strings from a GPS receiver into numerical data.
Make quantitative calculations on GPS data.

1. Hook up the GPS to the mobile board 12-pin ribbon cable, and a 9V battery to the
appropriate plug. Attach the GPS serial output line to one of your PC’s ports, and
view the GPS output in Crosscut. You see that
a. there is about one update per second,
b. the satellite readings (ending with N for north, and E for east) are bogus
because you are inside a building, and
c. the strings always have the same number of characters, in exactly the same
places.
(You don’t need to use the serial output again to do the rest of the lab, but it is
always available to help.)

2. Using the oscilloscope on the output of the LM324N op-amp, confirm that the
digital signal is normally high, and that the two states are about 5V and 0.5V. Do
not proceed to Step 2 until you have verified this! – The TT8 serial port takes the
opposite polarity as is provided by the GPS’s NEMA output, and this is the
purpose of the op-amp circuit. Also, in this lab you should check the battery
voltage several times per hour, because the GPS is a power hog, and you will
start getting get strange symptoms if the voltage sags.

3. Hook up the TT8 to the mobile power supply plug, and attach the output line of
the op-amp to TPU0/B10. You can communicate with the TT8 when it is
connected to the mobile board, through the regular serial port plug.

4. Now write code to read in ten GPS strings. The first code fragment on the next
page is a model (for the hhmmss string).

5. Now extract the hhmmss as an integer number of seconds, and the latitude and
longitude numbers as decimal-degree doubles from the string. The second code
fragment is a model (for the hhmmss string).

6. Now set up your program so that it logs the seconds, the latitude, and the
longitude to a file. You can afford to write to the card every time step, since the
GPS updates only once per second; so you don’t need to use big number arrays.

7. Set up your system to go for a walk – this means attaching the antenna to the
GPS, starting logging, and pulling the serial plug to the PC. Go out to the Charles
River and stand still for five minutes (to make sure initialization is done), walk
toward the Science Museum for five minutes, stand still for a few minutes, and
then come back.

8. Process your data. Make an annotated plot of your latitude versus your longitude.
About how far did you go? What is the approximate heading of the sidewalk you
surveyed? What is the scatter in the data from the periods when you were
standing still?

Your lab notebook should contain:


Lab worksheet on the first page.
Code listing (or listings).
Notes on the code and what you observed from the system.
Data plots, answering the questions in the last item above.

FIRST CODE FRAGMENT


#define SERCHAN 0 // TPU channel for serial input comms
#define TSBUFSIZ 128 // buffer size for TPU serial channel, a factor of 2!
……
void main( ) {
int i ;
char c[TSBUFSIZ] ;
……
printf("Serial input port opened: %d ( check: should be %d ) \n",
TSerOpen( SERCHAN, // channel
MiddlePrior, // priority
INP, // configured as input
malloc(TSBUFSIZ+TSER_MIN_MEM), // allocate the buffer
TSBUFSIZ, // size of the buffer
9600, 'N', 8, 1 ), // comm. protocol
tsOK ) ; // (second item printed: “OK”)

for (i=0;i<TSBUFSIZ;i++) // get the chars from buffer as fast as you can!
c[i] = TSerGetByte(SERCHAN) ;

for(i=0;i<TSBUFSIZ;i++) // now print them out


printf("%c",c[i]) ;
printf("\n") ;
TSerClose(SERCHAN);
……
}

Simple rules about this algorithm: the buffer is


• TSBUFSIZ long; it may not match the length of the incoming string
• first-in, first-out (FIFO); oldest is c[0], latest is c[TSBUFSIZ-1]
• recycled after the last spot is filled, whether you read it or not!
SECOND CODE FRAGMENT (augments First Code Fragment)
// These parameters are specific to the GARMIN GP25-HV operating in
// '$GPRMC' mode; the target is the hhmmss number (a six digit integer).
#define CUE_CHAR '$' // cueing character to look for in serial string
#define TARGET_OFFSET 7 // how many characters after CUE_CHAR does the
// target string begin
#define TARGET_LENGTH 6 // how long is the target string
……
int istart ;
long int ic ; // have to use long because an int can only be +/-32000 (2^16)
char d[TARGET_LENGTH] ; // target string
……
// find the location in c[] of the cue character (‘$’); the index is cycled backward, starting at
// the last location from which we could hope to get a full target string out
for(i=TSBUFSIZ – 1 - TARGET_OFFSET - TARGET_LENGTH + 1 ; i>-1 ; i--){
if (c[i] == CUE_CHAR) {
istart = i+TARGET_OFFSET ;
break ;
}
}

// make a fresh set of the target characters, indexed from zero


for (i=0;i<TARGET_LENGTH;i++)
d[i] = c[istart+i] ;

// now turn d[] into a real number; it is known to be a positive integer in this case
ic = 0 ;
ic += (long int) (d[0]-‘0’)*3600*10 ; // tens of hours
ic += (d[1]-‘0’)*3600 ; // hours
ic += (d[2]-‘0’)*60*10 ; // tens of minutes
ic += (d[3]-‘0’)*60 ; // minutes
ic += (d[4]-‘0’)*10 ; // tens of seconds
ic += (d[5]-‘0’) ; // seconds
printf("Target value: %ld seconds.\n", ic) ;

You might also like