Go Down

Topic: mpu6050 help  (Read 1 time) previous topic - next topic

Bying

i am using mpu6050 to interfacing with thearduino, but serial monitor come out with angle(analog) instead of digital output(bit).we already searched some codes from web but their output all come out angle, not bit.how could i change my angle to bit? then i need to use this formula
 digital output*3.3/2^16 to get voltage

PaulS

An MPU6050 measures acceleration in 3 axes, and has a gyroscope on 3 axes. Converting the information from the sensor to angles (yaw, pitch, and roll) makes sense.

I have no idea what you mean by "digital output(bit)". You need to explain that in (a lot) more detail.

CrossRoads

I think OP means binary output.
Not sure where the analog info is coming from unless the sketch being used is converting it already.  The data output from the MPU6050 is via I2C or SPI, there is no analog output.

@Bying, can you post your code (use the code tags button, </>), or Reply and use the Attach feature below the response box?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Bying

#include"math.h"
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif

MPU6050 mpu;

// MPU control/status vars
bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
//VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
//VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high

void setup()
{
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
        TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

    Serial.begin(115200);
    while (!Serial); // wait for Leonardo enumeration, others continue immediately

    Serial.println(F("Initializing I2C devices..."));
    mpu.initialize();

    Serial.println(F("Testing device connections..."));
    Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
    Serial.println(F("Initializing DMP..."));
    devStatus = mpu.dmpInitialize();

    //mpu.setXGyroOffset(61);
    //mpu.setYGyroOffset(65);
    mpu.setZGyroOffset(-36);
    mpu.setZAccelOffset(1169); // 1688 factory default for my test chip
    if (devStatus == 0) {
        // turn on the DMP, now that it's ready
        Serial.println(F("Enabling DMP..."));
        mpu.setDMPEnabled(true);
        Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
        attachInterrupt(0, dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();
        Serial.println(F("DMP ready! Waiting for first interrupt..."));
        dmpReady = true;
        packetSize = mpu.dmpGetFIFOPacketSize();
    } else {
        Serial.print(F("DMP Initialization failed (code "));
        Serial.print(devStatus);
        Serial.println(F(")"));
    }
}


void loop()
{
    if (!dmpReady) return;
    mpuInterrupt = false;
    mpuIntStatus = mpu.getIntStatus();
    fifoCount = mpu.getFIFOCount();
    if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
        mpu.resetFIFO();
        Serial.println(F("FIFO overflow!"));

    } else if (mpuIntStatus & 0x02) {
        while (fifoCount < packetSize) {
          fifoCount = mpu.getFIFOCount();
        }
        mpu.getFIFOBytes(fifoBuffer, packetSize);       
        fifoCount -= packetSize;
    //to chaange type of output
      mpu.dmpGetQuaternion(&q, fifoBuffer);
        mpu.dmpGetGravity(&gravity, &q);
        mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
        flashLEDs(ypr[2] * 180/M_PI);
    }
}

void flashLEDs(float roll)
{   
 
    Serial.print(yaw);Serial.print(","); 
    Serial.print(roll);Serial.print("\n"); 
    Serial.println(roll);   
   
}

void dmpDataReady()
{
    mpuInterrupt = true;
}

Bying

thanks for reply. my lecturer keep asking me to calculate voltage from angle. A graph of voltage against angle hv to be plotted for calibration. what we obtained from this code is angle, we cant find any formula regarding to voltage and angle.This is our main problem.

since serial monitor come out with angle,we want to convert it back to raw value in bit,then can only apply this formula 3.3/2^16 *digital output. but i really hv no idea to do it

Bying

I have no idea what is the digital output of mpu6050

PaulS

Quote
thanks for reply. my lecturer keep asking me to calculate voltage from angle.
Voltage of what? It is senseless to talk about the voltage output of an accelerometer. It is senseless to talk about the voltage output of a gyroscope.

Now, if there is a motor being used to move something, and the MPU6050 is attached to that something, and if moving that something requires more, or less, current as the something is moved more, or less, then plotting current vs. angle might make sense.

CrossRoads

I bet that this library
#include "MPU6050_6Axis_MotionApps20.h"
is doing the conversion. You'd have to dig into that library and get the raw data for your own conversion.

Can you post that one?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Bying

Hi, Pauls
My lecturer want me to clearly know how our sensor function. How our sensor come out with angle straight away.. he want us to convert it and find voltage output.

Hi crossroads,
may I know the raw data of mpu6050 is in bit or not (in between 0 to2^16)
I try some code to run raw data , it come out with negative raw value.. but actually should be no negative value.,right? Any code recommended?

Thankyou

CrossRoads

Have you and your instructor read the datasheet?
http://store.invensense.com/datasheets/invensense/MPU-6050_DataSheet_V3%204.pdf
http://store.invensense.com/Datasheets/invensense/RM-MPU-6000A.pdf

See page 7 of the 2nd document
3B 59 ACCEL_XOUT_H R ACCEL_XOUT[15:8] 
3C 60 ACCEL_XOUT_L R ACCEL_XOUT[7:0] 
3D 61 ACCEL_YOUT_H R ACCEL_YOUT[15:8] 
3E 62 ACCEL_YOUT_L R ACCEL_YOUT[7:0] 
3F 63 ACCEL_ZOUT_H R ACCEL_ZOUT[15:8] 
40 64 ACCEL_ZOUT_L R ACCEL_ZOUT[7:0]

You need those 6 registers which are the raw X/Y/Z information.
You access them thru I2C reads.
Whomever wrote the library I referenced earlier went thru the datasheet, figured out how to set up all the other registers and do the self test & stuff on power up for the information to make sense, and then converted the 6 registers into 3 analog voltages that the sketch outputs.

So: you need to reverse engineer the library to output the register information so you can do the calculation yourself.  I glaze over trying to follow other peoples code, especially when there are lots of functions that bounce the code all over the place, good luck to you if you can do better.

Or: read & understand the datasheet, and write some code to read the registers.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

#12
Dec 07, 2016, 02:12 am Last Edit: Dec 07, 2016, 02:13 am by CrossRoads
Look at  line 1758 of MPU6050.cpp
Code: [Select]

// ACCEL_*OUT_*registers

/** Get raw 9-axis motion sensor readings (accel/gyro/compass).

So there's your raw data.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

PaulS

Quote
My lecturer want me to clearly know how our sensor function.
That makes sense. Although, I suspect it is more that your lecturer wants YOU to clearly know how your sensor converts raw acceleration data to angles.

Quote
How our sensor come out with angle straight away
My point, exactly.

Quote
he want us to convert it and find voltage output.
Either you have completely misunderstood, or he is an idiot.

Go talk to your lecturer. Find out which case we are dealing with.

Go Up