asukiaaa / MPU9250_asukiaaa

A library for arduino to read value of MPU9250.
MIT License
89 stars 30 forks source link

Not GETTING magnetometer data! #27

Open andreevnikola opened 1 year ago

andreevnikola commented 1 year ago

Everything is implemented right. I am getting data from every other sensor but the magnetometer is ust stucked at: image

Here is my CODE:

#include "IMU.h"

IMU::IMU()
{
    Wire.begin();

    pinMode(G_LED_PORT, OUTPUT);
    pinMode(BUZZER1_PORT, OUTPUT);

    setWire(&Wire);
    beginAccel();
    beginGyro();
    beginMag();

    accelUpdate();
    gyroUpdate();

    // CALIBRATE SENSORs
    Serial.println("PREPARE FOR CALIBRAION!");
    tone(BUZZER1_PORT, 10000);
    digitalWrite(G_LED_PORT, HIGH);
    delay(500);
    Serial.println("CALIBRATING SENSORS...");
    accelXCalibrationValue = accelX();
    accelYCalibrationValue = accelY();
    accelZCalibrationValue = accelZ();
    gyroXCalibrationValue = gyroX();
    gyroYCalibrationValue = gyroY();
    gyroZCalibrationValue = gyroZ();
    noTone(BUZZER1_PORT);
    digitalWrite(G_LED_PORT, LOW);
    Serial.println("ALL SENSORS CALIBRATED");
}

D3 IMU::calibratedAccelData(){
    D3 accelData(accelX() - accelXCalibrationValue, accelZ() - accelZCalibrationValue, -accelY() + accelYCalibrationValue);
    return accelData;
}

D3 IMU::calibratedGyroData(){
    D3 gyroData(gyroX() - gyroXCalibrationValue, gyroZ() - gyroZCalibrationValue, -gyroY() + gyroYCalibrationValue);
    return gyroData;
}

float IMU::getSpeed() {
    return sqrt(accData.X * accData.X + accData.Y * accData.Y + accData.Z * accData.Z) * 9.8;
}

// float IMU::getPitch() { return pitchKalmanFilter.updateEstimate(atan2((-accData.X), sqrt(accData.Y * accData.Y + accData.Z * accData.Z)) * 57.3); }
// float IMU::getRoll() { return atan2(accData.Y, accData.Z) * 57.3; }
// float IMU::getYaw() { return atan(accData.Z / sqrt(accData.X * accData.X + accData.Z * accData.Z)) * 57.3; }

void IMU::updateSensors() {
    accelUpdate();
    gyroUpdate();

    accData = calibratedAccelData();
    gyroData = calibratedGyroData();
    speed = getSpeed();

    // pitch = getPitch();
    // roll = getRoll();
    // yaw = getYaw();
}

void IMU::output()
{
    Serial.println("    ");
    // Serial.println("Accel:");
    // Serial.println(accData.X);
    // Serial.println(accData.Y);
    // Serial.println(accData.Z);
    // Serial.println("Pitch: " + String(getPitch()) + "°");
    // Serial.println("Yaw: " + String(getYaw()) + "°");
    // Serial.println("Roll: " + String(getRoll()) + "°");
    // Serial.println("SPEED " + String(speed) + "m/s");
    // Serial.println("Gyro:");
    // Serial.println(gyroData.X);
    // Serial.println(gyroData.Y);
    // Serial.println(gyroData.Z);

    magUpdate();
    Serial.println("MAG:");
    Serial.println(magX());
    Serial.println(magY());
    Serial.println(magZ());
    Serial.println(magHorizDirection());
}

This is how i have connected my sensor with the arduino board: image image

Can someone help?

asukiaaa commented 1 year ago

Did you tried example code? (What is IMU.h? Is it orher library?) https://github.com/asukiaaa/MPU9250_asukiaaa/blob/master/examples/GetData/GetData.ino If there is problem with the example, the hardware (IC) or circuit may have problem.