Go Down

Topic: MPU6050 + nRF24L01 (Read 764 times) previous topic - next topic

bakito

Hello!

I'm trying to send the signal of MPU6050 (Accelerometer + Gyr) through the nRF24L01 (RF Radio). For both, I'm using librarys, and each sensor alone with its library it works. My aim to send the Euler Angles that the Jeff Rowberg library calculates.

Code: [Select]
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
//RF
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"

MPU6050 mpu;

#define OUTPUT_READABLE_EULER

#define LED_PIN 13 // LED PIN IT'S WORKING
bool blinkState = false;

// 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

Quaternion q;           // [w, x, y, z]         quaternion container
float euler[3];         // [psi, theta, phi]    Euler angle container
float euler_send[3];

uint8_t teapotPacket[14] = { '$', 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' };

// Interrupt
    volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
    void dmpDataReady() {
    mpuInterrupt = true;
}

// RF
RF24 radio(9,10);

const uint64_t pipe = 0xE8E8F0F0E1LL;

// INITIAL SETUP

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();
   
    Serial.begin(9600);
   
    mpu.initialize(); // Initalize I2c device
   
//    Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
   
    while (Serial.available() && Serial.read()); // empty buffer again
    devStatus = mpu.dmpInitialize();
   
     if (devStatus == 0) {
        // turn on the DMP, now that it's ready
//        Serial.println(F("Enabling DMP..."));
        mpu.setDMPEnabled(true);

        // enable Arduino interrupt detection
//        Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
        attachInterrupt(0, dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();

        // set our DMP Ready flag so the main loop() function knows it's okay to use it
//        Serial.println(F("DMP ready! Waiting for first interrupt..."));
        dmpReady = true;

        // get expected DMP packet size for later comparison
        packetSize = mpu.dmpGetFIFOPacketSize();
     }
   
   
    pinMode(LED_PIN, OUTPUT); // Configure LED for output
   
    //RF
   
   radio.begin();
   radio.openWritingPipe(pipe);
}
   
   

void loop() {
 
   if (!dmpReady) return; // If programming failed, don't try to do anything
   
   mpuInterrupt = false;
   mpuIntStatus = mpu.getIntStatus();
   
   fifoCount = mpu.getFIFOCount();
   
   while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
   
   mpu.getFIFOBytes(fifoBuffer, packetSize); // Read a packet from FIFO
   
   fifoCount -= packetSize; //

   // display Euler angles in degrees
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetEuler(euler, &q);
            Serial.print("euler\t");
            Serial.print(euler[0] * 180/M_PI);
            Serial.print("\t");
            Serial.print(euler[1] * 180/M_PI);
            Serial.print("\t");
            Serial.println(euler[2] * 180/M_PI);
     
     //RF
     euler_send[0]= (euler[0] * 180/M_PI);
     radio.write( &euler_send[0], sizeof(euler_send) );
   
      blinkState = !blinkState;
      digitalWrite(LED_PIN, blinkState);
}


But, in the first moment it looks like works, but, a bit later it doesn't work. Below a little example that happen.

euler   -0.13   3.06   0.77
euler   -0.15   3.03   0.77
euler   -55.25   nan   34.44
euler   87.35   nan   46.15
euler   -137.02   nan   23.93


Any idea? Thanks so much

Go Up