Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. void setupGPS() {
  2.   //Turning off all GPS NMEA strings apart on the uBlox module
  3.   // Taken from Project Swift (rather than the old way of sending ascii text)
  4.   uint8_t setNMEAoff[] = {
  5.     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          };
  6.   sendUBX(setNMEAoff, sizeof(setNMEAoff)/sizeof(uint8_t));
  7.   wait(500);
  8.   setGPS_DynamicModel6();
  9.   wait(500);
  10.   setGps_MaxPerformanceMode();
  11.   wait(500);
  12. }
  13. void sendUBX(uint8_t *MSG, uint8_t len) {
  14.   Serial.flush();
  15.   Serial.write(0xFF);
  16.   wait(100);
  17.   for(int i=0; i<len; i++) {
  18.     Serial.write(MSG[i]);
  19.   }
  20. }
  21. void setGPS_DynamicModel6()
  22. {
  23.   int gps_set_sucess=0;
  24.   uint8_t setdm6[] = {
  25.     0xB5, 0x62, 0x06, 0x24, 0x24, 0x00, 0xFF, 0xFF, 0x06,
  26.     0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00,
  27.     0x05, 0x00, 0xFA, 0x00, 0xFA, 0x00, 0x64, 0x00, 0x2C,
  28.     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0xDC                                                             };
  30.   while(!gps_set_sucess)
  31.   {
  32.     sendUBX(setdm6, sizeof(setdm6)/sizeof(uint8_t));
  33.     gps_set_sucess=getUBX_ACK(setdm6);
  34.   }
  35. }
  36.  
  37. void setGPS_DynamicModel3()
  38. {
  39.   int gps_set_sucess=0;
  40.   uint8_t setdm3[] = {
  41.     0xB5, 0x62, 0x06, 0x24, 0x24, 0x00, 0xFF, 0xFF, 0x03,
  42.     0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00,
  43.     0x05, 0x00, 0xFA, 0x00, 0xFA, 0x00, 0x64, 0x00, 0x2C,
  44.     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x76                                                             };
  46.   while(!gps_set_sucess)
  47.   {
  48.     sendUBX(setdm3, sizeof(setdm3)/sizeof(uint8_t));
  49.     gps_set_sucess=getUBX_ACK(setdm3);
  50.   }
  51. }
  52. void sendUBX(uint8_t *MSG, uint8_t len) {
  53.   Serial.flush();
  54.   Serial.write(0xFF);
  55.   wait(100);
  56.   for(int i=0; i<len; i++) {
  57.     Serial.write(MSG[i]);
  58.   }
  59. }
  60.  
  61. boolean getUBX_ACK(uint8_t *MSG) {
  62.   uint8_t b;
  63.   uint8_t ackByteID = 0;
  64.   uint8_t ackPacket[10];
  65.   unsigned long startTime = millis();
  66.  
  67.   // Construct the expected ACK packet    
  68.   ackPacket[0] = 0xB5;  // header
  69.   ackPacket[1] = 0x62;  // header
  70.   ackPacket[2] = 0x05;  // class
  71.   ackPacket[3] = 0x01;  // id
  72.   ackPacket[4] = 0x02;  // length
  73.   ackPacket[5] = 0x00;
  74.   ackPacket[6] = MSG[2];    // ACK class
  75.   ackPacket[7] = MSG[3];    // ACK id
  76.   ackPacket[8] = 0;     // CK_A
  77.   ackPacket[9] = 0;     // CK_B
  78.  
  79.   // Calculate the checksums
  80.   for (uint8_t ubxi=2; ubxi<8; ubxi++) {
  81.     ackPacket[8] = ackPacket[8] + ackPacket[ubxi];
  82.     ackPacket[9] = ackPacket[9] + ackPacket[8];
  83.   }
  84.  
  85.   while (1) {
  86.  
  87.     // Test for success
  88.     if (ackByteID > 9) {
  89.       // All packets in order!
  90.       return true;
  91.     }
  92.  
  93.     // Timeout if no valid response in 3 seconds
  94.     if (millis() - startTime > 3000) {
  95.       return false;
  96.     }
  97.  
  98.     // Make sure data is available to read
  99.     if (Serial.available()) {
  100.       b = Serial.read();
  101.  
  102.       // Check that bytes arrive in sequence as per expected ACK packet
  103.       if (b == ackPacket[ackByteID]) {
  104.         ackByteID++;
  105.       }
  106.       else {
  107.         ackByteID = 0;  // Reset and look again, invalid order
  108.       }
  109.  
  110.     }
  111.   }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement