Detect GPS Crossing a Boundary Line

Crossing Finish Line

Here is some mixed code and pseudo code that represents my 2nd attempt to detect when a GPS crosses a boundary line. The boundary line in my case is the start/finish line of a race course. I want the code to be lean and fast, but it must be bullet-proof reliable too.

Attempt #2:

//pseudo code to detect crossing the start/finish line  
if (Heading is within 30 of Initial_Startline_Heading) {		
  if (Distance to Startline < 50’) {
    if (CheckXing(StartPt, CurrentPt)) {
	//crossed start/finish
    }
  }
}

#define UNDEFINED 0
#define NEGATIVE  1
#define POSITIVE  2
#define SIGN_BIT  0xF000
#define TRUE      1
#define FALSE     0

uint8_t x_sign, y_sign;

struct Point {
	uint32_t x, y;
} StartPt, CurrentPt;

//check crossing start/finish
bool CheckXing(Point s, Point p) {
  long x, y;

  x = (p.x – s.x);
  y = (p.y – s.y);

  if (x_sign == UNDEFINED) {
    if (x&SIGN_BIT)
      x_sign = NEGATIVE;
    else
      x_sign = POSITIVE;
    if (y&SIGN_BIT)
      y_sign = NEGATIVE;
    else
      y_sign = POSITIVE;
  
    return FALSE;
  }
  
  if (x_sign) {
    if (x&SIGN_BIT && (x_sign == POSITIVE))
      goto Xed;
    if (!x&SIGN_BIT && (x_sign == NEGATIVE))
      goto Xed;
    if (y&SIGN_BIT && (y_sign == POSITIVE))
      goto Xed;
    if (!y&SIGN_BIT && (y_sign == NEGATIVE))
      goto Xed;

    return FALSE;

Xed:
    x_sign = UNDEFINED;
    return TRUE;
  }
}

My first attempt utilized this pseudo code:

GETTING_NEARER 0x01
CURRENT_SAMPLE 0x01
PREVIOUS_SAMPLE 0x02

GettingCloser = (GettingCloser<<1);
if (NewDistanceToStart < PreviousDistanceToStart)
  GettingCloser |= GETTING_NEARER;
if ((GettingCloser&PREVIOUS_SAMPLE) && !(GettingCloser&CURRENT_SAMPLE) && 
    (PreviousDistanceToStart < 10’) && (Heading is within 30)) {
  //crossed start/finish
}

Both have significant issues. I may need to resort to the standard line intersection algorithm

Advertisements

About Jim Eli

µC experimenter
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s