I have adapted a code that works with a Pololu using the compass.read(), could someone enlighten me on what is the equivalent with the unified lib.
static int n = 10; // number of samples
for (int i = 0; i < n; i++) {
// compass.read();// this works with a Pololu, so after an equivalent for the Adafruit NXP Precision 9 DOF IMU---------------------------------------------------------------
// accelmag.getEvent(&accel_event, &mag_event);// this of course didn't, have tried others things without luck!!
Complete code below
Thanks for any help
Kevin
Code: Select all
#include <Wire.h>
#include <Adafruit_Sensor.h>
#define ST_LSM303DLHC_L3GD20 (0)
#define ST_LSM9DS1 (1)
#define NXP_FXOS8700_FXAS21002 (2)
#define AHRS_VARIANT NXP_FXOS8700_FXAS21002
#if AHRS_VARIANT == ST_LSM303DLHC_L3GD20
#include <Adafruit_L3GD20_U.h>
#include <Adafruit_LSM303_U.h>
#elif AHRS_VARIANT == ST_LSM9DS1
// ToDo!
#elif AHRS_VARIANT == NXP_FXOS8700_FXAS21002
#include <Adafruit_FXAS21002C.h>
#include <Adafruit_FXOS8700.h>
#else
#error "AHRS_VARIANT undefined! Please select a target sensor combination!"
#endif
// Create sensor instances.
#if AHRS_VARIANT == ST_LSM303DLHC_L3GD20
Adafruit_L3GD20_Unified gyro(20);
Adafruit_LSM303_Accel_Unified accel(30301);
Adafruit_LSM303_Mag_Unified mag(30302);
#elif AHRS_VARIANT == ST_LSM9DS1
// ToDo!
#elif AHRS_VARIANT == NXP_FXOS8700_FXAS21002
Adafruit_FXAS21002C gyro = Adafruit_FXAS21002C(0x0021002C);
Adafruit_FXOS8700 accelmag = Adafruit_FXOS8700(0x8700A, 0x8700B);
#endif
float AxAvg, AyAvg, AzAvg, MxAvg, MyAvg, MzAvg;
int HDG, ALT ;
int delta_HDG = 30;
int delta_ALT = 30;
char report[80];
char InChar;
void setup()
{
Serial.begin(115200);
Serial.println(F("Adafruit AHRS Fusion Example")); Serial.println("");
// Initialize the sensors.
if (!gyro.begin())
{
/* There was a problem detecting the gyro ... check your connections */
Serial.println("Ooops, no gyro detected ... Check your wiring!");
while (1);
}
#if AHRS_VARIANT == NXP_FXOS8700_FXAS21002
if (!accelmag.begin(ACCEL_RANGE_4G))
{
Serial.println("Ooops, no FXOS8700 detected ... Check your wiring!");
while (1);
}
#else
if (!accel.begin())
{
/* There was a problem detecting the accel ... check your connections */
Serial.println("Ooops, no accel detected ... Check your wiring!");
while (1);
}
if (!mag.begin())
{
/* There was a problem detecting the mag ... check your connections */
Serial.println("Ooops, no mag detected ... Check your wiring!");
while (1);
}
#endif
Serial.println(" Code waits until 3 is typed in Serial monitor (top line) the prints a N sample average of Acc and Mag ");
Serial.println( " AVERAGES");
Serial.println("ALT, HDG, AX, AY, AZ, MX, MY, MZ");
Serial.print(ALT); Serial.print(" ");
Serial.print(HDG); Serial.print(" "); // this will print next HDG
}
void loop(void)
{
sensors_event_t gyro_event;
sensors_event_t accel_event;
sensors_event_t mag_event;
// Get new data samples
gyro.getEvent(&gyro_event);
#if AHRS_VARIANT == NXP_FXOS8700_FXAS21002
accelmag.getEvent(&accel_event, &mag_event);
#else
accel.getEvent(&accel_event);
mag.getEvent(&mag_event);
#endif
float mx = mag_event.magnetic.x;
float my = mag_event.magnetic.y;
float mz = mag_event.magnetic.z ;
float ax = accel_event.acceleration.x;
float ay = accel_event.acceleration.y;
float az = accel_event.acceleration.z;
// Serial.print("ax,ay,az RAW "), Serial.print (" "), Serial.print (ax), Serial.print (" "), Serial.print (ay), Serial.print (" "), Serial.println (az);
while (Serial.available() > 0 ) {
InChar = Serial.read();
//Serial.print(InChar);
}
if (InChar == '3')
{
static int n = 10; // number of samples
for (int i = 0; i < n; i++) {
// compass.read();
accelmag.getEvent(&accel_event, &mag_event);
AxAvg += accel_event.acceleration.x;
AyAvg += accel_event.acceleration.y;
AzAvg += accel_event.acceleration.z;
MxAvg += mag_event.magnetic.x;
MyAvg += mag_event.magnetic.y;
MzAvg += mag_event.magnetic.z;
delay(100);
}
AxAvg = AxAvg / n;
AyAvg = AyAvg / n;
AzAvg = AzAvg / n;
MxAvg = MxAvg / n;
MyAvg = MyAvg / n;
MzAvg = MzAvg / n;
Serial.print(AxAvg); Serial.print(" ");
Serial.print(AyAvg); Serial.print(" ");
Serial.print(AzAvg); Serial.print(" ");
Serial.print(MxAvg); Serial.print(" ");
Serial.print(MyAvg); Serial.print(" ");
Serial.println(MzAvg);
InChar = 0;
AxAvg = 0;
AyAvg = 0;
AzAvg = 0;
MxAvg = 0;
MyAvg = 0;
MzAvg = 0;
ALT = ALT + delta_ALT;
if (ALT > 270)
{
ALT = 0;
HDG = HDG + delta_HDG;
}
Serial.print(ALT); Serial.print(" ");
Serial.print(HDG); Serial.print(" "); // this will print next HDG
}
}