AlexGodbehere Posted March 5, 2015 Report Share Posted March 5, 2015 Hi all. I'm using an MPU6050 to obtain orientation information for a quadcopter, however when the function that is attached to the interrupt pin is ran (to obtain the orientation data from the FIFO buffer), the sketch crashes. Sometimes, the console prints the first few characters of "Getting Sensor Data!", which is called in the routine. Am I doing anything seriously wrong? Main Sketch: Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)...")); attachInterrupt(0, AP_getSensorData, RISING); // Actually pin 2-(5?) AP_Sensors.cpp: // Library header #include "Sensors_.h" #include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h" #include "Serial_.h" #include <SFE_BMP180.h> #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE #include "Wire.h" #endif /************************************** * LED DEFINITIONS *************************************/ #define LED_GREEN 9 #define LED_YELLOW 10 #define LED_YELLOW2 11 #define LED_YELLOW3 12 #define LED_RED 13 // Code MPU6050 onboardIMU; extern bool systemError; int ultrasonicAlt; // 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 byte error, address; int nDevices; // ================================================================ // === INTERRUPT DETECTION ROUTINE === // ================================================================ volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high void dmpDataReady() { mpuInterrupt = true; } bool AP_initSensors() { // join I2C bus (I2Cdev library doesn't do this automatically) #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 /************************************** * Scan for I2C Devices *************************************/ Serial.println("Scanning I2C Bus..."); nDevices = 0; for (address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address < 16) Serial.print("0"); Serial.print(address, HEX); Serial.println(" !"); nDevices++; } else if (error == 4) { Serial.print("Unknow error at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("Completed I2C Bus Scan...\n"); Serial.println("Initalising I2C Devices..."); onboardIMU.setSleepEnabled(false); onboardIMU.initialize(); // Try to connect to IMU Devices... Serial.println("Connecting to I2C Devices..."); if (onboardIMU.testConnection()) { Serial.println("IMU Connected!:"); // load and configure the DMP Serial.println(F("Initializing DMP...")); devStatus = onboardIMU.dmpInitialize(); // supply your own gyro offsets here, scaled for min sensitivity onboardIMU.setXGyroOffset(220); onboardIMU.setYGyroOffset(76); onboardIMU.setZGyroOffset(-85); onboardIMU.setZAccelOffset(1788); // 1688 factory default for my test chip // make sure it worked (returns 0 if so) if (devStatus == 0) { // turn on the DMP, now that it's ready Serial.println(F("Enabling DMP...")); onboardIMU.setDMPEnabled(true); // enable Arduino interrupt detection // Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)...")); // attachInterrupt(0, dmpDataReady, RISING); mpuIntStatus = onboardIMU.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 = onboardIMU.dmpGetFIFOPacketSize(); } else { // ERROR! // 1 = initial memory load failed // 2 = DMP configuration updates failed // (if it's going to break, usually the code will be 1) Serial.print(F("DMP Initialization failed (code ")); Serial.print(devStatus); Serial.println(F(")")); } } else { Serial.println("Unable to find any I2C Devices..."); } Serial.println("Initialising Barometric Sensor..."); AP_initBarometricSensor(); } void AP_getSensorData() { Serial.println("Getting Sensor Data!"); // reset interrupt flag and get INT_STATUS byte mpuInterrupt = false; mpuIntStatus = onboardIMU.getIntStatus(); // get current FIFO count fifoCount = onboardIMU.getFIFOCount(); // check for overflow (this should never happen unless our code is too inefficient) if ((mpuIntStatus & 0x10) || fifoCount == 1024) { // reset so we can continue cleanly onboardIMU.resetFIFO(); Serial.println(F("FIFO overflow!")); // otherwise, check for DMP data ready interrupt (this should happen frequently) } else if (mpuIntStatus & 0x02) { // wait for correct available data length, should be a VERY short wait while (fifoCount < packetSize) fifoCount = onboardIMU.getFIFOCount(); // read a packet from FIFO onboardIMU.getFIFOBytes(fifoBuffer, packetSize); // track FIFO count here in case there is > 1 packet available // (this lets us immediately read more without waiting for an interrupt) fifoCount -= packetSize; onboardIMU.dmpGetQuaternion(&q, fifoBuffer); onboardIMU.dmpGetGravity(&gravity, &q); onboardIMU.dmpGetYawPitchRoll(ypr, &q, &gravity); } } Ekbergdub and Johnnydofs 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.