?? gps.c
字號(hào):
gpsAquireRequest = utFalse; } AQUIRE_UNLOCK // We are now either NOT in powerSaveMode, or 'gpsAquire(...)' has been called. /* exit now if this thread has been told to quit */ if (!gpsRunThread) { break; } /* open port to GPS */ if (!_gpsOpen()) { // Open failed. Wait, then retry open. // This should be a critical error. if (utcIsTimerExpired(gpsLastOpenErrorTimer,900L)) { gpsPortName = propGetString(PROP_CFG_GPS_PORT, DEFAULT_GPS_PORT); // refresh name gpsLastOpenErrorTimer = utcGetTimer(); if (!comPortIsValidName(gpsPortName)) { logERROR(LOGSRC,"GPS port invalid: %s", gpsPortName); } else { logCRITICAL(LOGSRC,"Unable to open GPS port: %s", gpsPortName); // strerror(errno)); } } threadSleepMS(5000L); continue; } // The HP hw6945 GPS receiver should be powering up from a cold-start at this point. // If the device always keeps the GPS hot, then we will just be able to aquire a // fix much sooner. /* read GPS (forever, or until error) */ // We want to make sure we stay in '_gpsReadGPSFix' long enough to get at least one // GPS fix (assuming that the receiver may be coming up from a cold-start). If the // GPS receiver is already hot, then we'll just get a fix much sooner. UInt32 timeoutMS = 0L; if (powerSaveMode) { // If we are in power-save mode, then 'gpsAquire(...)' was called and is waiting // for a valid GPS fix (hopefully with a reasonable timeout value). We will wait // here for a GPS fix with the same timeout value. timeoutMS = powerSaveTimeoutMS; // from 'gpsAquireTimeoutMS' UInt32 MIN_TIMOUT = 90L * 1000L; // 90 seconds (should be enough for a cold GPS start) if (timeoutMS < MIN_TIMOUT) { timeoutMS = MIN_TIMOUT; } } int gpsErr = _gpsReadGPSFix(timeoutMS); if (gpsErr <= 0) { // (gpsErr < 0) we've received an error, increment restart counter // (gpsErr == 0) powerSaveMode timeout, increment restart counter SAMPLE_LOCK { gpsRestartCount++; logDEBUG(LOGSRC,"Error/Timeout - closing GPS port [%lu]", gpsRestartCount); } SAMPLE_UNLOCK } else { // (gpsErr > 0) powerSaveMode GPS fix received logDEBUG(LOGSRC,"PowerSave - closing GPS port ..."); } /*close GPS port */ _gpsClose(); } //while (gpsRunThread) /* once this thread stops, it isn't starting again */ // The following resources should be released: // - gpsComPort // - gpsMutex // - gpsSampleMutex // - gpsAquireMutex // - gpsAquireCond // - And everything in the supported GPS modules // - ??? logERROR(LOGSRC,"GPS thread is terminating ..."); threadExit(); }/* indicate thread should stop */static void _gpsStopThread(void *arg){ gpsRunThread = utFalse; AQUIRE_LOCK { // nudge thread AQUIRE_NOTIFY } AQUIRE_UNLOCK _gpsClose();}/* start the thread */static utBool _gpsStartThread(){ /* create thread */ gpsRunThread = utTrue; // must set BEFORE we start the thread! if (threadCreate(&gpsThread,&_gpsThreadRunnable,0,"GPS") == 0) { // thread started successfully } else { logCRITICAL(LOGSRC,"Unable to start GPS thread"); gpsRunThread = utFalse; } return gpsRunThread;}#endif // defined(GPS_THREAD)// ----------------------------------------------------------------------------/* GPS module initialization */static utBool _gpsDidInit = utFalse;void gpsInitialize(eventAddFtn_t queueEvent){ /* already initialized? */ if (_gpsDidInit) { return; } _gpsDidInit = utTrue; /* init gps comport */ comPortInitStruct(&gpsComPort); /* clear gps struct */ gpsClear(&gpsFixLast); gpsClear(&gpsFixUnsafe); #if defined(GPS_THREAD) /* create mutex's */ threadMutexInit(&gpsMutex); threadMutexInit(&gpsSampleMutex); threadMutexInit(&gpsAquireMutex); threadConditionInit(&gpsAquireCond);#endif /* GPS module initialization */ gpsModuleInitialize(queueEvent);#if defined(GPS_THREAD) /* start thread */ _gpsStartThread();#endif}// ----------------------------------------------------------------------------/* set flag indicating GPS fix is stale (or not) */void gpsSetFixStale(utBool stale){ // this value is currently only set in 'mainloop.c' gpsIsStale = stale;}/* return true if current GPS fix is stale */utBool gpsIsFixStale(){ // Note: this may be accessed from multiple threads, but since this is not // a critical value, no locking is performed. return gpsIsStale;}// ----------------------------------------------------------------------------/* check minimum values in GPS record */GPS_t *gpsCheckMinimums(GPS_t *gps){ if (gps) { // check minimum speed double minSpeedKPH = propGetDouble(PROP_GPS_MIN_SPEED, 7.0); if (gps->speedKPH < minSpeedKPH) { // the reported speed does not meet our minimum requirement // mark the speed as 'not moving' gps->speedKPH = 0.0; gps->heading = 0.0; // clear the heading when the speed is '0' } } return gps;}// ----------------------------------------------------------------------------/* aquire GPS fix */GPS_t *gpsAquire(GPS_t *gps, UInt32 timeoutMS){ /* null gps pointer specified */ if (!gps) { return (GPS_t*)0; } /* clear gps point */ gpsClear(gps);#if defined(GPS_THREAD) /* no timeout, return last fix */ if (timeoutMS <= 0L) { // no timeout specified, just return the latest fix that we have (if any) // the caller can determine if he wants to use the fix return gpsGetLastGPS(gps, -1); } /* indicate to GPS thread that we want a fix */ // This is only necessary if the gps interval is long and the gps thread is // waiting to be told to make a gps aquisition. AQUIRE_LOCK { gpsAquireTimeoutMS = timeoutMS; // this value is > 0 gpsAquireRequest = utTrue; AQUIRE_NOTIFY } AQUIRE_UNLOCK // at this point, the gps thread should be working on getting a fix /* wait until a fix is available */ UInt32 accumTimeoutMS = 0L; for (;accumTimeoutMS < timeoutMS;) { /* get latest fix */ GPS_t *g = gpsGetLastGPS(gps, -1); if (g && (utcGetTimerAgeSec(g->ageTimer) <= 7L)) { // The latest fix occurred within the last 7 seconds. return g; } /* wait for next fix */ UInt32 tmo = timeoutMS - accumTimeoutMS; if (tmo > 1000L) { tmo = 1000L; } threadSleepMS(tmo); accumTimeoutMS += tmo; } /* no fix (timeout?) */ return (GPS_t*)0;#else /* timeout mode: block while reading GPS fix */ UInt32 tmo = (timeoutMS < 3000L)? 3000L : timeoutMS;#if defined(GPS_DEVICE_SIMULATOR) const char *gpsPortName = propGetString(PROP_CFG_GPS_PORT, DEFAULT_GPS_PORT); gpsSimulator = strEqualsIgnoreCase(gpsPortName, GPS_SIMULATOR_PORT); if (gpsSimulator) { int rtn = _gpsReadGPSFix(tmo); if (rtn > 0) { // valid fix aquired return gpsGetLastGPS(gps, 15); } else { // timeout return (GPS_t*)0; } } else#endif if (comPortIsOpen(&gpsComPort) || _gpsOpen()) { int rtn = _gpsReadGPSFix(tmo); _gpsClose(); // always close the port when runnin in non-thread mode if (rtn > 0) { // valid fix aquired (at least $GPRMC) GPS_t *g = gpsGetLastGPS(gps, 15); return g; } else if (rtn < 0) { // error return (GPS_t*)0; } else { // timeout return (GPS_t*)0; } } else { // unable to open GPS port return (GPS_t*)0; }#endif // defined(GPS_THREAD)}/* get last aquired GPS fix */GPS_t *gpsGetLastGPS(GPS_t *gps, Int16 maxAgeSec){ if (gps) { /* get latest fix */ GPS_LOCK { // gpsFixLast - contains the most recent fix that has all the GPS components // gpsFixUnsafe - contains the fix-in-progress if (gpsFixUnsafe.fixtime < (gpsFixLast.fixtime + GP_EXPIRE)) { // The last fix is within 5 seconds of the new fix-in-process. // During normal GPS aquisition, the fix times will likely be the same. // If not the same, then the fix-in-process may have just started on // the next sample. memcpy(gps, &gpsFixLast, sizeof(GPS_t)); } else if (gpsFixUnsafe.nmea & NMEA0183_GPRMC) { // The last fix ('gpsFixLast') is stale. // The fix-in-process at least has the GPRMC // One of the following may have occured: // - We stopped getting GPGGA records (or don't get them). // - We just started getting valid GPS fixes again and we happen to have been // called when we are between GPRMC/GPGGA records (unlikely, but possible). //logDEBUG(LOGSRC,"We don't have a valid $GPGGA record"); memcpy(gps, &gpsFixUnsafe, sizeof(GPS_t)); } else { // The fix-in-process doesn't have a GPRMC. // The last fix is stale, but return it anyway. memcpy(gps, &gpsFixLast, sizeof(GPS_t)); // fix may still be invalid } } GPS_UNLOCK /* check fix */ if (!gpsIsValid(gps)) { //logINFO(LOGSRC,"Point is invalid ..."); return (GPS_t*)0; // GPSPoint is invalid } else if ((maxAgeSec > 0) && (utcGetTimerAgeSec(gps->ageTimer) > maxAgeSec)) { //logINFO(LOGSRC,"Point is stale ... [%ld]", utcGetTimerAgeSec(gps->ageTimer)); return (GPS_t*)0; // GPSPoint is stale } else { return gpsCheckMinimums(gps); } } else { /* no place to put fix */ return (GPS_t*)0; }}// ----------------------------------------------------------------------------
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -