// // Exported functions // void gps_setup() { int gps_success = 0; strcpy(gps_time, "000000"); strcpy(gps_aprs_lat, "0000.00N"); strcpy(gps_aprs_lon, "00000.00E"); #ifdef GPS_USING_UBLOX // Setup for the uBLOX MAX-6/7/8 // Check to see if we need to power up the GPS #ifdef GPS_POWER_PIN pinMode(GPS_POWER_PIN, OUTPUT); pin_write(GPS_POWER_PIN, LOW); #endif #ifdef GPS_POWER_SLEEP_TIME // Add a bit of a delay here to allow the GPS a chance to wake up // (even if we aren't powering it via a digital pin) delay(GPS_POWER_SLEEP_TIME); #endif #ifdef GPS_LED_PIN // LED for GPS Status pinMode(GPS_LED_PIN, OUTPUT); pin_write(GPS_LED_PIN, LOW); #endif // Set MAX-6 to flight mode int setup_attempts_remaining = 120; while (!gps_success && (setup_attempts_remaining--) >= 0) { sendUBX(setNav, sizeof(setNav)/sizeof(uint8_t)); gps_success = getUBX_ACK(setNav); if (!gps_success) { // Should find a better way to indicate a problem? #ifdef GPS_LED_PIN pin_write(GPS_LED_PIN, HIGH); #endif delay(500); } } #ifdef GPS_LED_PIN pin_write(GPS_LED_PIN, LOW); #endif // Finally turn off any NMEA sentences we don't need (in this case it's // everything except GGA and RMC) sendUBX(setGLL, sizeof(setGLL)/sizeof(uint8_t)); // Disable GGL sendUBX(setGSA, sizeof(setGSA)/sizeof(uint8_t)); // Disable GSA sendUBX(setGSV, sizeof(setGSV)/sizeof(uint8_t)); // Disable GSV sendUBX(setVTG, sizeof(setVTG)/sizeof(uint8_t)); // Disable VTG #endif }
void setupGPS(){ mrtDelay(5000); //Turning off all GPS NMEA strings apart on the uBlox module // Taken from Project Swift (rather than the old way of sending ascii text) // Had problems with memcpy (or the lack of) - solution here: http://www.utasker.com/forum/index.php?topic=1750.0;wap2 static uint8_t setNMEAoff[] = {0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x80, 0x25, 0x00, 0x00, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0xA9}; sendUBX(setNMEAoff, 28); mrtDelay(1000); // Check and set the navigation mode (Airborne, 1G) static uint8_t setNav[] = {0xB5, 0x62, 0x06, 0x24, 0x24, 0x00, 0xFF, 0xFF, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x05, 0x00, 0xFA, 0x00, 0xFA, 0x00, 0x64, 0x00, 0x2C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0xDC}; sendUBX(setNav, 44); }
bool setNavPosLLH(){ uint8_t buf[]={0x06, 0x01, 0x08, 0x00, 0x01, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}; CK_A = 0x00, CK_B = 0x00; for(int x=0;x<12;x++) { CK_A = CK_A + buf[x]; CK_B = CK_B + CK_A; } uint8_t setport[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0x01, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, CK_A, CK_B};//don't forget to calculate checksum (page84) then add that to end of array - done unsigned long starttime = millis(); while(!gps_set_success) { /* if (millis() - starttime > gps_timeout){ gps_set_success=0; return false; }*/ sendUBX(setport, sizeof(setport)/sizeof(uint8_t)); gps_set_success=getUBX_ACK(setport); } gps_set_success=0; return true; }
/** * Verify that the uBlox 6 GPS receiver is set to the <1g airborne * navigaion mode. */ uint8_t gps_check_nav(void) { GPSerror = 0; static uint8_t request[8] = {0xB5, 0x62, 0x06, 0x24, 0x00, 0x00, 0x2A, 0x84}; sendUBX(request, 8); // Get the message back from the GPS gps_get_data(); // Verify sync and header bytes if( gps_buf[0] != 0xB5 || gps_buf[1] != 0x62 ){ GPSerror = 41; } if( gps_buf[2] != 0x06 || gps_buf[3] != 0x24 ){ GPSerror = 42; } // Check 40 bytes of message checksum if( !_gps_verify_checksum(&gps_buf[2], 40) ) { GPSerror = 43; } // Return the navigation mode and let the caller analyse it if (GPSerror == 0){ return gps_buf[8]; } else { return -1; } }
uint8_t gps_get_time(uint8_t* hour, uint8_t* minute, uint8_t* second) { // Send a NAV-TIMEUTC message to the receiver uint8_t request[8] = {0xB5, 0x62, 0x01, 0x21, 0x00, 0x00, 0x22, 0x67}; sendUBX(request, 8); while(Serial.available()) Serial.read(); Serial.flush(); uint8_t buf[28]; if (!getBytes(28,buf)) return 1; // Verify the sync and header bits if( buf[0] != 0xB5 || buf[1] != 0x62 ) return 2; if( buf[2] != 0x01 || buf[3] != 0x21 ) return 3; *hour = buf[22]; *minute = buf[23]; *second = buf[24]; return 0; }
uint8_t gps_check_lock(uint8_t* lock, uint8_t* sats) { // Construct the request to the GPS uint8_t request[8] = {0xB5, 0x62, 0x01, 0x06, 0x00, 0x00, 0x07, 0x16}; sendUBX(request, 8); while(Serial.available()) Serial.read(); Serial.flush(); uint8_t buf[60]; if (!getBytes(60,buf)) return 1; // Verify the sync and header bits if( buf[0] != 0xB5 || buf[1] != 0x62 ) return 2; if( buf[2] != 0x01 || buf[3] != 0x06 ) return 3; // Return the value if GPSfixOK is set in 'flags' if( buf[17] & 0x01 ) *lock = buf[16]; else *lock = 0; *sats = buf[53]; return 0; }
// Set UART1 to 115200 baud // Relies on ubx_cfg_prt[1] being set to 115200 bool MAXM8interface::changeBaudRate() { unsigned int tries = 0; int ackReturn = -5; do { sendUBX(g::ubx_cfg_prt[1], 24); Serial3.begin(115200); sendUBX(g::ubx_cfg_prt[1], 24); do { delay(10); ackReturn = getUbxAck(g::ubx_cfg_prt[1]); } while (ackReturn == -1); if (ackReturn == 1) return true; else if (ackReturn == 0) return false; Serial3.begin(9600); tries++; } while (tries < 5); return false; }
bool MAXM8interface::sendUntilAck(const uint8_t message[], const int messageLength) { unsigned int tries = 0; int ackReturn = -5; do { sendUBX(message, messageLength); do { delay(10); ackReturn = getUbxAck(message); } while (ackReturn == -1); if (ackReturn == 1) return true; else if (ackReturn == 0) return false; tries++; } while (tries < 5); return false; }
bool setOutputUBX(){ uint8_t setUBX[] = {0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x80, 0x25, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9C, 0x89};//don't forget to calculate checksum (page84) then add that to end of array - done unsigned long starttime = millis(); // while(!gps_set_success) // { /*if (millis() - starttime > gps_timeout){ gps_set_success=0; return false; }*/ sendUBX(setUBX, sizeof(setUBX)/sizeof(uint8_t)); // gps_set_success=getUBX_ACK(setUBX); // } gps_set_success=0; Serial.println("UBX mode set."); return true; }
uint8_t getLocation(int32_t* lat, int32_t* lon, int32_t* alt) { // Request a NAV-POSLLH message from the GPS uint8_t request[8] = {0xB5, 0x62, 0x01, 0x02, 0x00, 0x00, 0x03, 0x0A}; sendUBX(request, 8); while(Serial.available()) Serial.read(); Serial.flush(); uint8_t buf[36]; if (!getBytes(36,buf)) return 1; //for (int i =0; i < 36; i++){ // Serial.print(buf[i],HEX); // Serial.print(", "); //} // Verify the sync and header bits if( buf[0] != 0xB5 || buf[1] != 0x62 ) return 2; if( buf[2] != 0x01 || buf[3] != 0x02 ) return 3; // 4 bytes of longitude (1e-7) *lon = (int32_t)buf[10] | (int32_t)buf[11] << 8 | (int32_t)buf[12] << 16 | (int32_t)buf[13] << 24; // 4 bytes of latitude (1e-7) *lat = (int32_t)buf[14] | (int32_t)buf[15] << 8 | (int32_t)buf[16] << 16 | (int32_t)buf[17] << 24; // 4 bytes of altitude above MSL (mm) *alt = (int32_t)buf[22] | (int32_t)buf[23] << 8 | (int32_t)buf[24] << 16 | (int32_t)buf[25] << 24; return 0; }
/** * Poll the GPS for a position message then extract the useful * information from it - POSLLH. */ void gps_get_position() { GPSerror = 0; // Request a NAV-POSLLH message from the GPS static uint8_t request[8] = {0xB5, 0x62, 0x01, 0x02, 0x00, 0x00, 0x03, 0x0A}; sendUBX(request, 8); // Get the message back from the GPS gps_get_data(); // Verify the sync and header bits if( gps_buf[0] != 0xB5 || gps_buf[1] != 0x62 ) GPSerror = 21; if( gps_buf[2] != 0x01 || gps_buf[3] != 0x02 ) GPSerror = 22; if( !_gps_verify_checksum(&gps_buf[2], 32) ) { GPSerror = 23; } if(GPSerror == 0) { // 4 bytes of longitude (1e-7) lon = (int32_t)gps_buf[10] | (int32_t)gps_buf[11] << 8 | (int32_t)gps_buf[12] << 16 | (int32_t)gps_buf[13] << 24; lon /= 1000; // 4 bytes of latitude (1e-7) lat = (int32_t)gps_buf[14] | (int32_t)gps_buf[15] << 8 | (int32_t)gps_buf[16] << 16 | (int32_t)gps_buf[17] << 24; lat /= 1000; // 4 bytes of altitude above MSL (mm) alt = (int32_t)gps_buf[22] | (int32_t)gps_buf[23] << 8 | (int32_t)gps_buf[24] << 16 | (int32_t)gps_buf[25] << 24; alt /= 1000; } }
bool setSeaMode(){ uint8_t B[]={0x06, 0x24, 0x24, 0x00, 0xFF, 0xFF, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x05, 0x00, 0xFA, 0x00, 0xFA, 0x00, 0x64, 0x00, 0x2C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; CK_A = 0x00, CK_B = 0x00; for(int I=0;I<40;I++) { CK_A = CK_A + B[I]; CK_B = CK_B + CK_A; } uint8_t setNav[] = {0xB5, 0x62, 0x06, 0x24, 0x24, 0x00, 0xFF, 0xFF, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x05, 0x00, 0xFA, 0x00, 0xFA, 0x00, 0x64, 0x00, 0x2C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, CK_A, CK_B }; unsigned long starttime = millis(); while(!gps_set_success) { /*if (millis() - starttime > gps_timeout){ gps_set_success=0; return false; }*/ sendUBX(setNav, sizeof(setNav)/sizeof(uint8_t)); gps_set_success=getUBX_ACK(setNav); } gps_set_success=0; Serial.println("Sea mode set."); return true; }
/** * Check the navigation status to determine the quality of the * fix currently held by the receiver with a NAV-STATUS message. */ void gps_check_lock() { GPSerror = 0; // Construct the request to the GPS static uint8_t request[8] = {0xB5, 0x62, 0x01, 0x06, 0x00, 0x00, 0x07, 0x16}; sendUBX(request, 8); // Get the message back from the GPS gps_get_data(); // Verify the sync and header bits if( gps_buf[0] != 0xB5 || gps_buf[1] != 0x62 ) { GPSerror = 11; } if( gps_buf[2] != 0x01 || gps_buf[3] != 0x06 ) { GPSerror = 12; } // Check 60 bytes minus SYNC and CHECKSUM (4 bytes) if( !_gps_verify_checksum(&gps_buf[2], 56) ) { GPSerror = 13; } if(GPSerror == 0){ // Return the value if GPSfixOK is set in 'flags' if( gps_buf[17] & 0x01 ) lock = gps_buf[16]; else lock = 0; sats = gps_buf[53]; } else { lock = 0; } }
void resetGPS() { // Cold Boot GPS sendUBX(set_reset, sizeof(set_reset)/sizeof(uint8_t)); }
void setGps_MaxPerformanceMode() { //Set GPS for Max Performance Mode sendUBX(setMax, sizeof(setMax)/sizeof(uint8_t)); }
void setGPS_PowerSaveMode() { // Power Save Mode sendUBX(setPSM, sizeof(setPSM)/sizeof(uint8_t)); }
void gps_sleep(){ uint8_t GPSoff[] = {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00,0x08, 0x00, 0x16, 0x74}; sendUBX(GPSoff, sizeof(GPSoff)/sizeof(uint8_t)); }
void gps_wake(){ uint8_t GPSon[] = {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00,0x09, 0x00, 0x17, 0x76}; sendUBX(GPSon, sizeof(GPSon)/sizeof(uint8_t)); }