ahmedthabet
Members-
Posts
2 -
Joined
-
Last visited
Everything posted by ahmedthabet
-
DMP output stopped working
ahmedthabet replied to ahmedthabet's topic in MPU-6050 6-axis accelerometer/gyroscope (InvenSense)
This is my integrated code. #include "Wire.h" // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files // for both classes must be in the include path of your project #include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h" //#include "MPU6050.h" // not necessary if using MotionApps include file // class default I2C address is 0x68 // specific I2C addresses may be passed as a parameter here // AD0 low = 0x68 (default for SparkFun breakout and InvenSense evaluation board) // AD0 high = 0x69 MPU6050 mpu; //New Includes #include <Servo.h> //#include <I2C_Anything.h> #include "I2C_Anything.h" //#include <FastSerial.h> //FastSerialPort0(Serial); //End New Includes //Declerations const byte MY_ADDRESS = 42; volatile boolean haveData = false; volatile float X; volatile float Y; volatile float Z; // motors Servo myservo1; Servo myservo2; Servo myservo3; Servo myservo4; //PWM Modified int Duty_1=1000; int Duty_2=1200; int Duty_3=1260; int Duty_4=1110; int motor_1=0; int motor_2=0; int motor_3=0; int motor_4=0; //pwm char ff = '1'; // pwm double integ_1, deriv_1,integ_2 , deriv_2 ; double dt = 0; // sampling time float kp_1 = 2.79, ki_1 = 0, kd_1 =20 ; //pitch //Enter PID parameter float kp_2 = 2.79, ki_2 = 0, kd_2 =20 ; //roll float set_point =0 ; float time_new = 0; float time_old = 0; float old_error_1 = 0; float new_error_1 = 0; float old_error_2 = 0; float new_error_2 = 0; long PWM_1 ; long PWM_2 ; long PWM_4 ; long PWM_3; //End of Declerations #define OUTPUT_READABLE_YAWPITCHROLL #define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6) 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 // 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 // packet structure for InvenSense teapot demo uint8_t teapotPacket[14] = { '$', 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' }; volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high void dmpDataReady() { mpuInterrupt = true; } void receiveEvent (int howMany) { if (howMany >= (sizeof X) + (sizeof Y) + (sizeof Z)) { I2C_readAnything (X); I2C_readAnything (Y); I2C_readAnything (Z); haveData = true; } } void setup() { // join I2C bus (I2Cdev library doesn't do this automatically) //Wire.begin(); Wire.begin (MY_ADDRESS); Wire.onReceive (receiveEvent); //Serial.begin (38400); Serial.begin(115200); Serial.println("Initializing..."); //attach servo myservo1.attach(3); myservo1.writeMicroseconds(1000); myservo2.attach(4); myservo2.writeMicroseconds(1000); myservo3.attach(5); myservo3.writeMicroseconds(1000); myservo4.attach(6); myservo4.writeMicroseconds(1000); // initialize serial communication // (115200 chosen because it is required for Teapot Demo output, but it's // really up to you depending on your project) //Serial.begin(57600); while (!Serial); // wait for Leonardo enumeration, others continue immediately // NOTE: 8MHz or slower host processors, like the Teensy @ 3.3v or Ardunio // Pro Mini running at 3.3v, cannot handle this baud rate reliably due to // the baud timing being too misaligned with processor ticks. You must use // 38400 or slower in these cases, or use some kind of external separate // crystal solution for the UART timer. // initialize device Serial.println(F("Initializing I2C devices...")); mpu.initialize(); // verify connection Serial.println(F("Testing device connections...")); Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed")); // wait for ready Serial.println(F("\nSend any character to begin DMP programming and demo: ")); while (Serial.available() && Serial.read()); // empty buffer while (!Serial.available()); // wait for data while (Serial.available() && Serial.read()); // empty buffer again // load and configure the DMP Serial.println(F("Initializing DMP...")); devStatus = mpu.dmpInitialize(); // 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...")); 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(); } 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(")")); } // configure LED for output pinMode(LED_PIN, OUTPUT); } // ================================================================ // === MAIN PROGRAM LOOP === // ================================================================ void loop() { // if programming failed, don't try to do anything if (Serial.available() > 0) { ff = Serial.read(); if(ff == '0'){ if (haveData) { haveData = false; } time_new = millis(); dt = (time_new - time_old) / 1000.0; time_old = time_new; //calculating the derivatives float input_1 = Y ; float input_2 = X ; //calculate error new_error_1=(set_point - input_1) ; //pitch new_error_2=(set_point - input_2) ; //roll float delta_error_1=new_error_1 - old_error_1 ; float delta_error_2=new_error_2 - old_error_2 ; old_error_1 = new_error_1; old_error_2 = new_error_2; integ_1=delta_error_1*dt; deriv_1=delta_error_1/dt ; integ_2=delta_error_2*dt; deriv_2=delta_error_2/dt ; double RPS_roll=((kp_2*new_error_2)+(kd_2*deriv_2)+(ki_2*integ_2)) ; double RPS_pitch=((kp_1*new_error_1)+(kd_1*deriv_1)+(ki_1*integ_1)) ; //double NOMINAL =sqrt ((3.120*9.81)/(0.000013*8)) ;// double RPS_1=400 +(RPS_roll/4) ; //ROLL double RPS_2=400 -(RPS_pitch/4) ; // PITCH double RPS_3=400 -(RPS_pitch/4) ; //PITCH double RPS_4=400 -(RPS_roll/4) ; //ROLL if(RPS_1>13741 || RPS_2>13720 || RPS_3>10632 || RPS_4>13000 ) { RPS_1=13741; RPS_2=13720; RPS_3=10632; RPS_4=13000; } //end if if(4611< RPS_1<13741 || 4549< RPS_2<13720 || 3036< RPS_3<10632 || 3039< RPS_4<13000 ) { RPS_1=RPS_1 ; RPS_2=RPS_2 ; RPS_3=RPS_3 ; RPS_4=RPS_4 ; } // pwm for all motors Duty_1 =(((0.0000002*RPS_1*RPS_1)+(0.0008*RPS_1)+(15.908))+1000) ; Duty_2 =(((0.0000002*RPS_2*RPS_2)+(0.0002*RPS_2)+(20.006))+1000) ; Duty_3 =(((0.0000004*RPS_4*RPS_4)+(0.0006*RPS_4)+(28.272))+1000) ; Duty_4 =(((0.0000003*RPS_3*RPS_3)+(0.0006*RPS_3)+(10.127))+1000) ; // off motors myservo1.writeMicroseconds(Duty_1); myservo2.writeMicroseconds(Duty_2); myservo3.writeMicroseconds(Duty_3); myservo4.writeMicroseconds(Duty_4); //PWM_1=PWM_1 ; PWM_2=PWM_2 ; PWM_3=PWM_3 ; PWM_4=PWM_4; Serial.print("Pitch="); Serial.print(Y); Serial.print("Roll="); Serial.print(X); Serial.print(" , RPS_roll="); Serial.print(RPS_roll);Serial.print(" , RPS_pitch="); Serial.println(RPS_pitch); Serial.print(" , Duty_1="); Serial.println(Duty_1); Serial.print(" , Duty_2="); Serial.println(Duty_2); Serial.print(" , Duty_3="); Serial.println(Duty_3); Serial.print(" , Duty_4="); Serial.println(Duty_4); ff='0'; } if (ff =='1') { //PWM_1=1000 ; PWM_2=1000 ; PWM_3=1000 ; PWM_4=1000 ; Duty_1 = Duty_1 + 50; Duty_2 = Duty_2 + 38; Duty_3 = Duty_3 + 52; Duty_4 = Duty_4 + 43; Serial.println(ff); Serial.println(Duty_1); Serial.println(Duty_2); Serial.println(Duty_3); Serial.println(Duty_4); myservo1.writeMicroseconds(Duty_1); myservo2.writeMicroseconds(Duty_2); myservo3.writeMicroseconds(Duty_3); myservo4.writeMicroseconds(Duty_4); } if(ff=='2') { Duty_1 = Duty_1 - 50; Duty_2 = Duty_2 - 38; Duty_3 = Duty_3 - 52; Duty_4 = Duty_4 - 43; Serial.println(ff); Serial.println(Duty_1); Serial.println(Duty_2); Serial.println(Duty_3); Serial.println(Duty_4); myservo1.writeMicroseconds(Duty_1); myservo2.writeMicroseconds(Duty_2); myservo3.writeMicroseconds(Duty_3); myservo4.writeMicroseconds(Duty_4); } if(ff=='3') { Duty_1=1000; Duty_2=1200; Duty_3=1260; Duty_4=1110; Serial.println(ff); Serial.println(Duty_1); Serial.println(Duty_2); Serial.println(Duty_3); Serial.println(Duty_4); myservo1.writeMicroseconds(Duty_1); myservo2.writeMicroseconds(Duty_2); myservo3.writeMicroseconds(Duty_3); myservo4.writeMicroseconds(Duty_4); } if (!dmpReady) return; // wait for MPU interrupt or extra packet(s) available while (!mpuInterrupt && fifoCount < packetSize) { // other program behavior stuff here // . // . // . // if you are really paranoid you can frequently test in between other // stuff to see if mpuInterrupt is true, and if so, "break;" from the // while() loop to immediately process the MPU data // . // . // . } // reset interrupt flag and get INT_STATUS byte mpuInterrupt = false; mpuIntStatus = mpu.getIntStatus(); // get current FIFO count fifoCount = mpu.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 mpu.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 = mpu.getFIFOCount(); // read a packet from FIFO mpu.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; #ifdef OUTPUT_READABLE_QUATERNION // display quaternion values in easy matrix form: w x y z mpu.dmpGetQuaternion(&q, fifoBuffer); Serial.print("quat\t"); Serial.print(q.w); Serial.print("\t"); Serial.print(q.x); Serial.print("\t"); Serial.print(q.y); Serial.print("\t"); Serial.println(q.z); // Serial.print("WAIT _1_"); #endif #ifdef OUTPUT_READABLE_EULER // 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); // Serial.print("WAIT _2_"); #endif #ifdef OUTPUT_READABLE_YAWPITCHROLL // display Euler angles in degrees mpu.dmpGetQuaternion(&q, fifoBuffer); mpu.dmpGetGravity(&gravity, &q); mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); Serial.print("ypr\t"); Serial.print(ypr[0] * 180/M_PI); Serial.print("\t"); Serial.print(ypr[1] * 180/M_PI); Serial.print("\t"); Serial.println(ypr[2] * 180/M_PI); //Serial.print("WAIT _3_"); #endif #ifdef OUTPUT_READABLE_REALACCEL // display real acceleration, adjusted to remove gravity mpu.dmpGetQuaternion(&q, fifoBuffer); mpu.dmpGetAccel(&aa, fifoBuffer); mpu.dmpGetGravity(&gravity, &q); mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity); Serial.print("areal\t"); Serial.print(aaReal.x); Serial.print("\t"); Serial.print(aaReal.y); Serial.print("\t"); Serial.println(aaReal.z); // Serial.print("WAIT _4_"); #endif #ifdef OUTPUT_READABLE_WORLDACCEL // display initial world-frame acceleration, adjusted to remove gravity // and rotated based on known orientation from quaternion mpu.dmpGetQuaternion(&q, fifoBuffer); mpu.dmpGetAccel(&aa, fifoBuffer); mpu.dmpGetGravity(&gravity, &q); mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity); mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q); Serial.print("aworld\t"); Serial.print(aaWorld.x); Serial.print("\t"); Serial.print(aaWorld.y); Serial.print("\t"); Serial.println(aaWorld.z); //Serial.print("WAIT _5_"); #endif #ifdef OUTPUT_TEAPOT // display quaternion values in InvenSense Teapot demo format: teapotPacket[2] = fifoBuffer[0]; teapotPacket[3] = fifoBuffer[1]; teapotPacket[4] = fifoBuffer[4]; teapotPacket[5] = fifoBuffer[5]; teapotPacket[6] = fifoBuffer[8]; teapotPacket[7] = fifoBuffer[9]; teapotPacket[8] = fifoBuffer[12]; teapotPacket[9] = fifoBuffer[13]; Serial.write(teapotPacket, 14); teapotPacket[11]++; // packetCount, loops at 0xFF on purpose // Serial.print("WAIT _6_"); #endif // blink LED to indicate activity blinkState = !blinkState; digitalWrite(LED_PIN, blinkState); } } } -
My name is ahmed thabet, Im a mechatronics student in the american university in cairo. My thesis is a doing a quadcopter. I have been using the IMU mpu 6050 for a semester now doing tests and such. I have been to integrate your code of the MPU and my code of motors in order to get a final code that would read the readings of the MPU raw ,pitch , roll) and use them to stabilize the quadcopter, since yesterday everything was working fine until the code stopped giving me readings and started giving me the same output of : "Enabling DMP" "Enabling interrupt detection (Arduino external interrupt 0)...")) DMP ready! Waiting for first interrupt..." This didn't happen before, before the code simply outputed the readings of the raw pitch roll while moving the quad, so I went back to the original code which is yours and I ran it , and it gave me the same thing, so I had to ask you why? It always gave me readings, I have a MEGA 2560 Arduino board. By my understanding I need to connect the #pin2 to the Int pin of the MPU but I didnt do this before and it used to give me readings. So is it because of something in the connections because when I ran the MPU_raw it , it starting outputing readings automatically but not raw pitch roll, rather acceleration and gyro readings like the one's in the upper post, which I dont need tho. A fast reply would be appreciated. thank you.