Unable to get GPS fix when using MKR IMU + MKR GPS

I am unable to get a connection to GPS satellites when I use the MKR IMU an MKR GPS code at the same time, even after waiting for a fix for 30 min. When I uncomment the IMU functions it works fine, after a minute or so I am able to connect to satellites.

Hardware setup: MKR GPS using I2C cable with clear sky access, MKR IMU shield on top of MKR WIFI 1010.

Working code:

#include <MKRIMU.h>
#include <Arduino_MKRGPS.h>

float x_acceleration = 0;
float y_acceleration = 0;
float z_acceleration = 0;

float x_gyroscope = 0;
float y_gyroscope = 0;
float z_gyroscope = 0;

float x_euler = 0;
float y_euler = 0;
float z_euler = 0;

float x_magneticfield = 0;
float y_magneticfield = 0;
float z_magneticfield = 0;

float latitude = 0;
float longitude = 0;
float altitude = 0;
float speed = 0;
int satellites = 0;

unsigned long start;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // if (!IMU.begin()) {
  //   Serial.println("Failed to initialize IMU!");
  //   while (1);
  // }

  if (!GPS.begin()) {
    Serial.println("Failed to initialize GPS!");
    while (1);
  }
}

void loop() {

  start = micros();

  // if (IMU.accelerationAvailable()) {
  //   IMU.readAcceleration(x_acceleration, y_acceleration, z_acceleration);
  //   }

  // if (IMU.gyroscopeAvailable()) {
  //   IMU.readGyroscope(x_gyroscope, y_gyroscope, z_gyroscope);
  //   }

  // if (IMU.eulerAnglesAvailable()) {
  //   IMU.readEulerAngles(x_euler, y_euler, z_euler);
  //   }

  // if (IMU.magneticFieldAvailable()) {
  //   IMU.readMagneticField(x_magneticfield, y_magneticfield, z_magneticfield);
  //   }

  if (GPS.available()) {
    latitude   = GPS.latitude();
    longitude  = GPS.longitude();
    altitude   = GPS.altitude();
    speed      = GPS.speed();
    satellites = GPS.satellites();
    }

  Serial.print("freq:");Serial.print(1E6/(micros()-start));Serial.print(",");

  // Serial.print("x_acc:");Serial.print(x_acceleration);Serial.print(",");
  // Serial.print("y_acc:");Serial.print(y_acceleration);Serial.print(",");
  // Serial.print("z_acc:");Serial.print(z_acceleration);Serial.print(",");

  // Serial.print("x_gyro:");Serial.print(x_gyroscope);Serial.print(",");
  // Serial.print("y_gyro:");Serial.print(y_gyroscope);Serial.print(",");
  // Serial.print("z_gyro:");Serial.print(z_gyroscope);Serial.print(",");
  
  // Serial.print("heading:");Serial.print(x_euler);Serial.print(",");
  // Serial.print("roll:");Serial.print(y_euler);Serial.print(",");
  // Serial.print("pitch:");Serial.print(z_euler);Serial.print(",");

  // Serial.print("x_mag:");Serial.print(x_magneticfield);Serial.print(",");
  // Serial.print("y_mag:");Serial.print(y_magneticfield);Serial.print(",");
  // Serial.print("z_mag:");Serial.print(z_magneticfield);Serial.print(",");

  Serial.print("lat:");Serial.print(latitude);Serial.print(",");
  Serial.print("lon:");Serial.print(longitude);Serial.print(",");
  Serial.print("alt:");Serial.print(altitude);Serial.print(",");
  Serial.print("speed:");Serial.print(speed);Serial.print(",");
  Serial.print("sats:");Serial.print(satellites);Serial.println(",");

  delay(1000);
}

Not working code:

#include <MKRIMU.h>
#include <Arduino_MKRGPS.h>

float x_acceleration = 0;
float y_acceleration = 0;
float z_acceleration = 0;

float x_gyroscope = 0;
float y_gyroscope = 0;
float z_gyroscope = 0;

float x_euler = 0;
float y_euler = 0;
float z_euler = 0;

float x_magneticfield = 0;
float y_magneticfield = 0;
float z_magneticfield = 0;

float latitude = 0;
float longitude = 0;
float altitude = 0;
float speed = 0;
int satellites = 0;

unsigned long start;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

  if (!GPS.begin()) {
    Serial.println("Failed to initialize GPS!");
    while (1);
  }
}

void loop() {

  start = micros();

  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(x_acceleration, y_acceleration, z_acceleration);
    }

  if (IMU.gyroscopeAvailable()) {
    IMU.readGyroscope(x_gyroscope, y_gyroscope, z_gyroscope);
    }

  if (IMU.eulerAnglesAvailable()) {
    IMU.readEulerAngles(x_euler, y_euler, z_euler);
    }

  if (IMU.magneticFieldAvailable()) {
    IMU.readMagneticField(x_magneticfield, y_magneticfield, z_magneticfield);
    }

  if (GPS.available()) {
    latitude   = GPS.latitude();
    longitude  = GPS.longitude();
    altitude   = GPS.altitude();
    speed      = GPS.speed();
    satellites = GPS.satellites();
    }

  Serial.print("freq:");Serial.print(1E6/(micros()-start));Serial.print(",");

  Serial.print("x_acc:");Serial.print(x_acceleration);Serial.print(",");
  Serial.print("y_acc:");Serial.print(y_acceleration);Serial.print(",");
  Serial.print("z_acc:");Serial.print(z_acceleration);Serial.print(",");

  Serial.print("x_gyro:");Serial.print(x_gyroscope);Serial.print(",");
  Serial.print("y_gyro:");Serial.print(y_gyroscope);Serial.print(",");
  Serial.print("z_gyro:");Serial.print(z_gyroscope);Serial.print(",");
  
  Serial.print("heading:");Serial.print(x_euler);Serial.print(",");
  Serial.print("roll:");Serial.print(y_euler);Serial.print(",");
  Serial.print("pitch:");Serial.print(z_euler);Serial.print(",");

  Serial.print("x_mag:");Serial.print(x_magneticfield);Serial.print(",");
  Serial.print("y_mag:");Serial.print(y_magneticfield);Serial.print(",");
  Serial.print("z_mag:");Serial.print(z_magneticfield);Serial.print(",");

  Serial.print("lat:");Serial.print(latitude);Serial.print(",");
  Serial.print("lon:");Serial.print(longitude);Serial.print(",");
  Serial.print("alt:");Serial.print(altitude);Serial.print(",");
  Serial.print("speed:");Serial.print(speed);Serial.print(",");
  Serial.print("sats:");Serial.print(satellites);Serial.println(",");

  delay(1000);
}

Hi, I also observed the same phenomenon.
It seems that the frequent queries to the IMU prevents reading GPS data. Since GPS runs at 1Hz and the IMU runs at 100Hz, I enable a maximum of 98 queries to the IMU before disabling them until I get a GPS fix. If I allow for 99 IMU readings then I miss a GPS fix from time to time.

After getting a GPS fix I enable IMU reading again.

Here is code example below.

#include <MKRIMU.h>
#include <Arduino_MKRGPS.h>

float heading, roll, pitch;
float latitude, longitude, altitude, speed;
int nbSatellites;
unsigned long t0, t;


bool IMU_read_enable;
unsigned char IMU_read_count;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

   if (!GPS.begin()) {
    Serial.println("Failed to initialize GPS!");
    while (1);
  }

  t0 = micros();
   
  IMU_read_enable = false;
  IMU_read_count = 0;
}

void loop()
{
  
 
 if (GPS.available()) 
  { 
    latitude   = GPS.latitude();
    longitude  = GPS.longitude();
    altitude   = GPS.altitude();
    speed      = GPS.speed();
    nbSatellites = GPS.satellites();  
    Serial.print("Speed "); 
    Serial.print(speed,4);
    Serial.print("   dt GPS = "); 
    t= micros();
    Serial.println((t-t0)/1000);
    t0 = t; 
    IMU_read_enable = true;
  }
  
  if (IMU_read_enable && IMU.eulerAnglesAvailable())
  {
    IMU.readEulerAngles(heading, roll, pitch);
    Serial.print(heading);
    Serial.print(";");
    IMU_read_count++;
    if (IMU_read_count >= 98)
    {
    IMU_read_enable = false;
    IMU_read_count = 0;
    Serial.println("*");
    }
  }
 
}
1 Like

Thanks a lot for the reply. Yes I also noticed that - I managed to get it to work separately at 100Hz and 1Hz by manually setting the function calls to a specific frequency, which is not optimal but works for my case.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.