kriswiner / MPU9250

Arduino sketches for MPU9250 9DoF with AHRS sensor fusion
1.03k stars 472 forks source link

Simple code strange output #133

Open Albercook opened 7 years ago

Albercook commented 7 years ago

First off, thank you for all your examples and the time that you take to answer questions. I'm using the the MPU9255s on the GY9255 boards. I'm using a Teensy-LC. I was not able to get your code to work. I could not find some of the libraries. mbed.h for example. So first question.

Did I miss a folder with all the libraries and .h files?

I switched to the example at http://www.lucidarme.me/?p=5057. Real simple. my speed. But it can't get past the gyro to read the magnetometor. It is not getting a ready signal back.

Finally, the data that I get back it no changing.

Time 1032 Accel -19463 0 0 Gyro 13059 225 4356
Time 1032 Accel -19463 0 0 Gyro 13059 225 4356

If I change the starting register from 0x3B to 0x3D the numbers don't change at all.

Any thoughts?

Much appreciation George Albercook

----- my code---- from http://www.lucidarme.me/?p=5057 --- -----with slight modifications. Comment and print statements mostly.

include

include

define MPU9250_ADDRESS 0x68

define MAG_ADDRESS 0x0C

define GYRO_FULL_SCALE_250_DPS 0x00

define GYRO_FULL_SCALE_500_DPS 0x08

define GYRO_FULL_SCALE_1000_DPS 0x10

define GYRO_FULL_SCALE_2000_DPS 0x18

define ACC_FULL_SCALE_2_G 0x00

define ACC_FULL_SCALE_4_G 0x08

define ACC_FULL_SCALE_8_G 0x10

define ACC_FULL_SCALE_16_G 0x18

// This function read Nbytes bytes from I2C device at address Address. // Put read bytes starting at register Register in the Data array. void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data) { // Set register address Wire.beginTransmission(Address); Wire.write(Register); Wire.endTransmission();

// Read Nbytes Wire.requestFrom(Address, Nbytes); uint8_t index=0; while (Wire.available()) Data[index++]=Wire.read(); }

// Write a byte (Data) in device (Address) at register (Register) void I2CwriteByte(uint8_t Address, uint8_t Register, uint8_t Data) { // Set register address Wire.beginTransmission(Address); Wire.write(Register); Wire.write(Data); Wire.endTransmission(); }

// Initial time long int ti; volatile bool intFlag=false;

// Initializations void setup() { // Arduino initializations Wire.begin(); Serial.begin(115200);

// Set accelerometers low pass filter at 5Hz I2CwriteByte(MPU9250_ADDRESS,29,0x06); // Set gyroscope low pass filter at 5Hz I2CwriteByte(MPU9250_ADDRESS,26,0x06);

// Configure gyroscope range I2CwriteByte(MPU9250_ADDRESS,27,GYRO_FULL_SCALE_1000_DPS); // Configure accelerometers range I2CwriteByte(MPU9250_ADDRESS,28,ACC_FULL_SCALE_4_G); // Set by pass mode for the magnetometers I2CwriteByte(MPU9250_ADDRESS,0x37,0x02);

// Request continuous magnetometer measurements in 16 bits I2CwriteByte(MAG_ADDRESS,0x0A,0x16);

pinMode(13, OUTPUT); Timer1.initialize(10000); // initialize timer1, and set a 1/2 second period Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt

// Store initial time ti=millis(); }

// Counter long int cpt=0;

void callback() { intFlag=true; digitalWrite(13, digitalRead(13) ^ 1); }

// Main loop, read and display data void loop() { while (!intFlag); intFlag=false;

// Display time Serial.print("Time "); Serial.print (millis()-ti,DEC); Serial.print ("\t");

// ___ // ::: Counter :::

// Display data counter //Serial.print (cpt++,DEC); //Serial.print ("\t");

// ____ // ::: accelerometer and gyroscope :::

// Read accelerometer and gyroscope uint8_t Buf[14]; I2Cread(MPU9250_ADDRESS,0x3B,14,Buf);

// Create 16 bits values from 8 bits data

// Accelerometer int16_t ax=-(Buf[0]<<8 | Buf[1]); int16_t ay=-(Buf[2]<<8 | Buf[3]); int16_t az=Buf[4]<<8 | Buf[5];

// Gyroscope int16_t gx=-(Buf[8]<<8 | Buf[9]); int16_t gy=-(Buf[10]<<8 | Buf[11]); int16_t gz=Buf[12]<<8 | Buf[13];

// Display values

// Accelerometer Serial.print("Accel "); Serial.print (ax,DEC); Serial.print ("\t"); Serial.print (ay,DEC); Serial.print ("\t"); Serial.print (az,DEC);
Serial.print ("\t");

// Gyroscope Serial.print(" Gyro "); Serial.print (gx,DEC); Serial.print ("\t"); Serial.print (gy,DEC); Serial.print ("\t"); Serial.print (gz,DEC);
Serial.print ("\t");

// _____ // ::: Magnetometer ::: /* Serial.print(" read status "); // Read register Status 1 and wait for the DRDY: Data Ready

uint8_t ST1; do { I2Cread(MAG_ADDRESS,0x02,1,&ST1); } while (!(ST1&0x01)); Serial.print(" status=Ready "); // Read magnetometer data
uint8_t Mag[7];
I2Cread(MAG_ADDRESS,0x03,7,Mag);

// Create 16 bits values from 8 bits data

// Magnetometer int16_t mx=-(Mag[3]<<8 | Mag[2]); int16_t my=-(Mag[1]<<8 | Mag[0]); int16_t mz=-(Mag[5]<<8 | Mag[4]);

// Magnetometer Serial.println(" Mag "); Serial.print (mx+200,DEC); Serial.print ("\t"); Serial.print (my-70,DEC); Serial.print ("\t"); Serial.print (mz-700,DEC);
Serial.print ("\t");

*/

// End of line Serial.println("help"); delay(1000);
}

kriswiner commented 7 years ago

I can't wade through all of this code, but this sketch:

https://github.com/kriswiner/MPU-9250/blob/master/MPU9250_MS5637_AHRS_t3.ino

was written for the Teensy and I have run it on the LC. You might have to change a few things (like the WHO_AM_I = 0x73 for the MPU9255) and if you don;t have a pressure sensor you might have to comment this out but the basic sketch should work. You'll also need to put the quaternionFilter.ino sketch in your sketch folder or comment out the Madgwick call.

On Sat, Apr 1, 2017 at 10:11 PM, George Alber Cook <notifications@github.com

wrote:

First off, thank you for all your examples and the time that you take to answer questions. I'm using the the MPU9255s on the GY9255 boards. I'm using a Teensy-LC. I was not able to get your code to work. I could not find some of the libraries. mbed.h for example. So first question.

Did I miss a folder with all the libraries and .h files?

I switched to the example at http://www.lucidarme.me/?p=5057. Real simple. my speed. But it can't get past the gyro to read the magnetometor. It is not getting a ready signal back.

Finally, the data that I get back it no changing.

Time 1032 Accel -19463 0 0 Gyro 13059 225 4356 Time 1032 Accel -19463 0 0 Gyro 13059 225 4356

If I change the starting register from 0x3B to 0x3D the numbers don't change at all.

Any thoughts?

Much appreciation George Albercook ----- my code---- from http://www.lucidarme.me/?p=5057 --- -----with slight modifications. Comment and print statements mostly.

include

include

define MPU9250_ADDRESS 0x68

define MAG_ADDRESS 0x0C

define GYRO_FULL_SCALE_250_DPS 0x00

define GYRO_FULL_SCALE_500_DPS 0x08

define GYRO_FULL_SCALE_1000_DPS 0x10

define GYRO_FULL_SCALE_2000_DPS 0x18

define ACC_FULL_SCALE_2_G 0x00

define ACC_FULL_SCALE_4_G 0x08

define ACC_FULL_SCALE_8_G 0x10

define ACC_FULL_SCALE_16_G 0x18

// This function read Nbytes bytes from I2C device at address Address. // Put read bytes starting at register Register in the Data array. void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data) { // Set register address Wire.beginTransmission(Address); Wire.write(Register); Wire.endTransmission();

// Read Nbytes Wire.requestFrom(Address, Nbytes); uint8_t index=0; while (Wire.available()) Data[index++]=Wire.read(); }

// Write a byte (Data) in device (Address) at register (Register) void I2CwriteByte(uint8_t Address, uint8_t Register, uint8_t Data) { // Set register address Wire.beginTransmission(Address); Wire.write(Register); Wire.write(Data); Wire.endTransmission(); }

// Initial time long int ti; volatile bool intFlag=false;

// Initializations void setup() { // Arduino initializations Wire.begin(); Serial.begin(115200);

// Set accelerometers low pass filter at 5Hz I2CwriteByte(MPU9250_ADDRESS,29,0x06); // Set gyroscope low pass filter at 5Hz I2CwriteByte(MPU9250_ADDRESS,26,0x06);

// Configure gyroscope range I2CwriteByte(MPU9250_ADDRESS,27,GYRO_FULL_SCALE_1000_DPS); // Configure accelerometers range I2CwriteByte(MPU9250_ADDRESS,28,ACC_FULL_SCALE_4_G); // Set by pass mode for the magnetometers I2CwriteByte(MPU9250_ADDRESS,0x37,0x02);

// Request continuous magnetometer measurements in 16 bits I2CwriteByte(MAG_ADDRESS,0x0A,0x16);

pinMode(13, OUTPUT); Timer1.initialize(10000); // initialize timer1, and set a 1/2 second period Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt

// Store initial time ti=millis(); }

// Counter long int cpt=0;

void callback() { intFlag=true; digitalWrite(13, digitalRead(13) ^ 1); }

// Main loop, read and display data void loop() { while (!intFlag); intFlag=false;

// Display time Serial.print("Time "); Serial.print (millis()-ti,DEC); Serial.print ("\t");

// ___ // ::: Counter :::

// Display data counter //Serial.print (cpt++,DEC); //Serial.print ("\t");

// ____ // ::: accelerometer and gyroscope :::

// Read accelerometer and gyroscope uint8_t Buf[14]; I2Cread(MPU9250_ADDRESS,0x3B,14,Buf);

// Create 16 bits values from 8 bits data

// Accelerometer int16_t ax=-(Buf[0]<<8 | Buf[1]); int16_t ay=-(Buf[2]<<8 | Buf[3]); int16_t az=Buf[4]<<8 | Buf[5];

// Gyroscope int16_t gx=-(Buf[8]<<8 | Buf[9]); int16_t gy=-(Buf[10]<<8 | Buf[11]); int16_t gz=Buf[12]<<8 | Buf[13];

// Display values

// Accelerometer Serial.print("Accel "); Serial.print (ax,DEC); Serial.print ("\t"); Serial.print (ay,DEC); Serial.print ("\t"); Serial.print (az,DEC); Serial.print ("\t");

// Gyroscope Serial.print(" Gyro "); Serial.print (gx,DEC); Serial.print ("\t"); Serial.print (gy,DEC); Serial.print ("\t"); Serial.print (gz,DEC); Serial.print ("\t");

// _____ // ::: Magnetometer ::: /* Serial.print(" read status "); // Read register Status 1 and wait for the DRDY: Data Ready

uint8_t ST1; do { I2Cread(MAG_ADDRESS,0x02,1,&ST1); } while (!(ST1&0x01)); Serial.print(" status=Ready "); // Read magnetometer data uint8_t Mag[7]; I2Cread(MAG_ADDRESS,0x03,7,Mag);

// Create 16 bits values from 8 bits data

// Magnetometer int16_t mx=-(Mag[3]<<8 | Mag[2]); int16_t my=-(Mag[1]<<8 | Mag[0]); int16_t mz=-(Mag[5]<<8 | Mag[4]);

// Magnetometer Serial.println(" Mag "); Serial.print (mx+200,DEC); Serial.print ("\t"); Serial.print (my-70,DEC); Serial.print ("\t"); Serial.print (mz-700,DEC); Serial.print ("\t");

*/

// End of line Serial.println("help"); delay(1000); }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU-9250/issues/133, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qgojLRUizG9sLQy7Kk0cF7FMMWVXks5rry38gaJpZM4Mws_a .

Albercook commented 7 years ago

I misunderstood the following line in the READ.ME

", all require quaternionFilters.ino in the IDE folder also to use the Madgwick and/or Mahony sensor fusion algorithms."

I did not read that as the same folder as the sketch.

Changed the WHO_AM_I Now it works

Thanks, George Albercook