Mpu6050和Adafruit Ultimate Gps在Arduino Due上没有合作

时间:2017-10-23 15:32:55

标签: gps adafruit arduino-due mpu6050

我有mpu6050和adafruit终极gps突破v3的代码,他们在arduino上分别正常工作但是当我尝试将两个代码组合时,gps没有得到修复。有人可以帮帮我吗? mpu6050的代码如下所示

#include <Adafruit_GPS.h>
#define mySerial Serial1
Adafruit_GPS GPS(&mySerial);
#define GPSECHO  true
   boolean usingInterrupt = false;
    void useInterrupt(boolean); // Func prototype keeps Arduino 0023  happy


    void setup()  
    {


      Serial.begin(9600);
      GPS.begin(9600);
      mySerial.begin(9600);
      GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
      GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);  
      GPS.sendCommand(PGCMD_ANTENNA);
     #ifdef __arm__
  usingInterrupt = false;  
#else
  useInterrupt(true);
  #endif

  delay(1000);

}

#ifdef __AVR__
SIGNAL(TIMER0_COMPA_vect) {
  char c = GPS.read();
#ifdef UDR0
  if (GPSECHO)
    if (c) UDR0 = c;  
    // writing direct to UDR0 is much much faster than Serial.print 
    // but only one character can be written at a time. 
#endif
}

void useInterrupt(boolean v) {
  if (v) {

    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}
#endif //#ifdef__AVR__

uint32_t timer = millis();
void loop()                     
{

  if (! usingInterrupt) {
    char c = GPS.read();
  }

   // if a sentence is received, we can check the checksum, parse it...
   if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences! 
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false

    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }

  // if millis() or timer wraps around, we'll just reset it
   if (timer > millis())  timer = millis();

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) { 
     timer = millis(); // reset the timer

    Serial.print("\nTime: ");
    Serial.print(GPS.hour, DEC); Serial.print(':');
    Serial.print(GPS.minute, DEC); Serial.print(':');
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); Serial.print((int)GPS.fix);
    Serial.print(" quality: "); Serial.println((int)GPS.fixquality); 
    if (GPS.fix) {
      //Serial.print("Location: ");
      Serial.print(convertDegMinToDecDeg(GPS.latitude)); 
      Serial.print(", "); 
      Serial.println(convertDegMinToDecDeg(GPS.longitude)); 

      //Serial.print("Speed (knots): "); Serial.println(GPS.speed);
      //Serial.print("Angle: "); Serial.println(GPS.angle);
      //Serial.print("Altitude: "); Serial.println(GPS.altitude);
      //Serial.print("Satellites: ");       Serial.println((int)GPS.satellites);
    }
  }
}

下面给出了Adafruit终极Gps突破的代码

<TestComponent />

两个代码都工作正常,但是我无法将它们组合在一起运行。我试图将它们结合起来并且adafruit终极gps突破并不起作用,它什么都没有。我想知道如何将它们组合起来以便在一个代码中工作。提前谢谢。

1 个答案:

答案 0 :(得分:0)

使用NeoGPS代替 - 只需将其添加到IMU草图中:

#include <NMEAGPS.h>
NMEAGPS gps;
#define gpsPort Serial1

   ...

void setup(){
  Wire1.begin();
  Wire1.beginTransmission(MPU_addr);
  Wire1.write(0x6B);  // PWR_MGMT_1 register
  Wire1.write(0);     // set to zero (wakes up the MPU-6050)
  Wire1.endTransmission(true);
  Serial.begin(9600);
  pinMode(led, OUTPUT);

  gpsPort.begin( 9600 );
}

void loop(){
  if (gps.available( gpsPort )) {
    gps_fix fix = gps.read();  // A new GPS update is ready, get all the pieces
    // Print some of the pieces?

    Serial.print( F("Location: ") );
    if (fix.valid.location) {
      Serial.print( fix.latitude(), 6 );
      Serial.print( ',' );
      Serial.print( fix.longitude(), 6 );
    }

    Serial.print( F(", Altitude: ") );
    if (fix.valid.altitude)
      Serial.print( fix.altitude() );

    Serial.println();

    //  Take an IMU sample too.
    Wire1.beginTransmission(MPU_addr);
       ...
    Serial.print(" | GyZ = "); Serial.println(GyZ);
  }
}

这将每秒显示一个GPS更新和一个IMU样本。

此外,您无法使用delay。 Arduino在延迟期间不会做任何其他事情,它将失去GPS角色。请注意,上述循环结构始终在运行,检查GPS数据。当GPS更新最终准备就绪时,它将获取IMU样本并打印所有结果。

您还必须小心printing too much信息。最终,Arduino将花费所有时间等待打印字符。

NeoGPS可从Arduino IDE库管理器中获得,菜单草图 - &gt;包含图书馆 - &gt;管理图书馆。 NeoGPS比所有其他GPS库更快,更小,更可靠,更准确,并且示例结构合理。 其他库&#39;非常常见。修改它们时要打破的例子。即使您没有使用它,NeoGPS安装和故障排除页面上也有很多信息。