hi I'm using a MKR GPS (has a SAM M8Q) and an IMU. I would like to use the data from both sensors together and then acquire them at the same speed. I was wondering how to write code to acquire GPS at the highest possible speed. I currently use this code but I notice that the GPS values arrive randomly every 10 seconds or 30 seconds, how do I solve it? this is the only library I've managed to get working.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <math.h>
#include <SPI.h>
#include <SD.h>
#include "FS.h"
#include <TinyGPS++.h>
#define GPS_BAUDRATE 9600 // The default baudrate of NEO-6M is 9600
TinyGPSPlus gps; // the TinyGPS++ object
// definition of the time constants for sampling and the gyroscope address: first external imu then internal imu
uint16_t BNO055_SAMPLERATE_DELAY_MS = 100;
float BNO055_SAMPLERATE_DELAY = 0.01;
Adafruit_BNO055 bno = Adafruit_BNO055(-1, 0x28, &Wire);
// Calibration data
int16_t accel_offset_x = 76;
int16_t accel_offset_y = -87;
int16_t accel_offset_z = 28;
int16_t gyro_offset_x = -1;
int16_t gyro_offset_y = 0;
int16_t gyro_offset_z = -3;
int16_t mag_offset_x = -1095;
int16_t mag_offset_y = -514;
int16_t mag_offset_z = 677;
int16_t accel_radius = 1000;
int16_t mag_radius = 633;
// initialization of the variables present in the code
float roll, pitch, yaw, roll_d, pitch_d, yaw_d; // direct measurement
void setup() {
Serial.begin(115200);
Serial2.begin(GPS_BAUDRATE);
Serial.println(F("ESP32 - GPS module"));
delay(1000);
bno.begin(OPERATION_MODE_NDOF);
// Set calibration data
adafruit_bno055_offsets_t calibrationData;
calibrationData.accel_offset_x = accel_offset_x;
calibrationData.accel_offset_y = accel_offset_y;
calibrationData.accel_offset_z = accel_offset_z;
calibrationData.gyro_offset_x = gyro_offset_x;
calibrationData.gyro_offset_y = gyro_offset_y;
calibrationData.gyro_offset_z = gyro_offset_z;
calibrationData.mag_offset_x = mag_offset_x;
calibrationData.mag_offset_y = mag_offset_y;
calibrationData.mag_offset_z = mag_offset_z;
calibrationData.accel_radius = accel_radius;
calibrationData.mag_radius = mag_radius;
bno.setSensorOffsets(calibrationData);
if (!SD.begin(15)) {
Serial.println("Card failed, or not present");
while (1);
}
// store in the SD card
File log = SD.open("/datalog.txt", FILE_APPEND);
if (log) {
log.println("Avvio test");
log.println("£ , ANNO , MESE , GIORNO , ORA , MINUTO , SECONDI , ROLL , PITCH , YAW , ROLL_D , PITCH_D , YAW_D , LAT , LONG , ALT , SPEED , $");
log.close();
}else {
Serial.println("Errore apertura file in scrittura");
}
}
void loop() {
if (Serial2.available() > 0) {
if (gps.encode(Serial2.read())) {
if (gps.location.isValid()) {
Serial.print(F("- latitude: "));
Serial.println(gps.location.lat());
Serial.print(F("- longitude: "));
Serial.println(gps.location.lng());
Serial.print(F("- altitude: "));
if (gps.altitude.isValid())
Serial.println(gps.altitude.meters());
else
Serial.println(F("INVALID"));
} else {
Serial.println(F("- location: INVALID"));
}
Serial.print(F("- speed: "));
if (gps.speed.isValid()) {
Serial.print(gps.speed.kmph());
Serial.println(F(" km/h"));
} else {
Serial.println(F("INVALID"));
}
Serial.print(F("- GPS date&time: "));
if (gps.date.isValid() && gps.time.isValid()) {
Serial.print(gps.date.year());
Serial.print(F("-"));
Serial.print(gps.date.month());
Serial.print(F("-"));
Serial.print(gps.date.day());
Serial.print(F(" "));
Serial.print(gps.time.hour());
Serial.print(F(":"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
Serial.println(gps.time.second());
} else {
Serial.println(F("INVALID"));
}
}
}
if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
yaw = euler.x();
roll = euler.y();
pitch = -euler.z();
// gyroscope data acquisition
imu::Vector<3> gyro = bno.getVector(Adafruit_BNO055::VECTOR_GYROSCOPE);
yaw_d = gyro.z();
roll_d = gyro.y();
pitch_d = gyro.x();
// Print sensor data Roll (R) Pitch (P) Yaw (Y)
Serial.print("£ , ");
Serial.print(roll);
Serial.print(" , ");
Serial.print(pitch);
Serial.print(" , ");
Serial.print(yaw);
Serial.print(" , ");
Serial.print(roll_d);
Serial.print(" , ");
Serial.print(pitch_d);
Serial.print(" , ");
Serial.print(yaw_d);
Serial.println(" , $");
// store in the SD card
File log = SD.open("/datalog.txt", FILE_APPEND);
if (log) {
log.print("£ , ");
//-----------------------------------
log.print(gps.date.year());
log.print(" , ");
log.print(gps.date.month());
log.print(" , ");
log.print(gps.date.day());
log.print(" , ");
log.print(gps.time.hour());
log.print(" , ");
log.print(gps.time.minute());
log.print(" , ");
log.print(gps.time.second());
log.print(" , ");
//-----------------------------------
log.print(roll);
log.print(" , ");
log.print(pitch);
log.print(" , ");
log.print(yaw);
log.print(" , ");
log.print(roll_d);
log.print(" , ");
log.print(pitch_d);
log.print(" , ");
log.print(yaw_d);
log.print(" , ");
//-----------------------------------
log.print(gps.location.lat());
log.print(" , ");
log.print(gps.location.lng());
log.print(" , ");
log.print(gps.altitude.meters());
log.print(" , ");
log.print(gps.speed.kmph());
//-----------------------------------
log.println(" , $");
log.close();
}else {
Serial.println("Errore apertura file in scrittura");
}
delay(BNO055_SAMPLERATE_DELAY_MS);
}