bool Gyro_init(void) { Wire.begin(); mpu.initialize(); if (mpu.testConnection() == false) { return false; } devStatus = mpu.dmpInitialize(); if (devStatus == 0) { mpu.setXGyroOffset(X_GYRO_OFFSET); mpu.setYGyroOffset(Y_GYRO_OFFSET); mpu.setZGyroOffset(Z_GYRO_OFFSET); mpu.setXAccelOffset(X_ACCEL_OFFSET); mpu.setYAccelOffset(Y_ACCEL_OFFSET); mpu.setZAccelOffset(Z_ACCEL_OFFSET); mpu.setDMPEnabled(true); dmpReady = true; attachInterrupt(0, dmp_data_ready, RISING); mpuIntStatus = mpu.getIntStatus(); packetSize = mpu.dmpGetFIFOPacketSize(); return true; } else { return false; } }
void setup() { // join I2C bus (I2Cdev library doesn't do this automatically) #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE Wire.begin(); //Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE Fastwire::setup(400, true); #endif // initialize serial communication Serial.begin(38400); while (!Serial); mpu.initialize(); pinMode(INTERRUPT_PIN, INPUT); send_status(MPU_INITIALIZE, STATUS_OK); // verify connection send_status(MPU_CONNECTION, mpu.testConnection() ? STATUS_OK : STATUS_FAIL); // load and configure the DMP // 0 = DMP OK // 1 = initial memory load failed // 2 = DMP configuration updates failed ua_dev_status = mpu.dmpInitialize(); send_status(DMP_INITIALIZE, ua_dev_status); // supply your own gyro offsets here, scaled for min sensitivity mpu.setXGyroOffset(120); mpu.setYGyroOffset(76); mpu.setZGyroOffset(-185); mpu.setZAccelOffset(1688); // 1688 factory default for my test chip // make sure it worked (returns 0 if so) if (ua_dev_status == 0) { // turn on the DMP, now that it's ready mpu.setDMPEnabled(true); // enable Arduino interrupt detection attachPinChangeInterrupt(INTERRUPT_PIN, dmpDataReady, RISING); ua_mpu_interrupt_status = mpu.getIntStatus(); send_status(DMP_INTERRUPT, ua_mpu_interrupt_status); b_dmp_ready = true; // get expected DMP packet size for later comparison uh_packet_size = mpu.dmpGetFIFOPacketSize(); } // configure LED for output pinMode(LED_PIN, OUTPUT); }
void setup() { mpu.initialize(); // trace_printf(mpu.testConnection() ? ("MPU6050 connection successful\n") : ("MPU6050 connection failed\n")); // load and configure the DMP // trace_printf("Initializing DMP...\n"); devStatus = mpu.dmpInitialize(); // supply your own gyro offsets here, scaled for min sensitivity mpu.setXGyroOffset(220); mpu.setYGyroOffset(76); mpu.setZGyroOffset(-85); mpu.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 // trace_printf("Enabling DMP...\n"); mpu.setDMPEnabled(true); mpuIntStatus = mpu.getIntStatus(); // set our DMP Ready flag so the main loop() function knows it's okay to use it // trace_printf("DMP ready! Waiting for first interrupt...\n"); // trace_printf("System is running!\n"); 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) // trace_printf("DMP Initialization failed (code \n"); // trace_printf("%d\n", devStatus); } }
void calibrate_gyros(){ byte i=0; while (i < ITERATIONS){ //hope that offsets converge in 6 iterations accelgyro.getRotation(&gx, &gy, &gz); if (count == SAMPLE_COUNT){ xoff += int(gxm/-3); yoff += int(gym/-3); zoff += int(gzm/-3); accelgyro.setXGyroOffset(xoff); accelgyro.setYGyroOffset(yoff); accelgyro.setZGyroOffset(zoff); #ifdef CAL_DEBUG Serial.print(gxm); Serial.print(" "); Serial.print(gym); Serial.print(" "); Serial.println(gzm); Serial.print(xoff); Serial.print(" "); Serial.print(yoff); Serial.print(" "); Serial.println(zoff); Serial.println("*********************"); #endif count = 0; i++; //iteration++ #ifdef CAL_DEBUG Serial.print("."); #endif } else{ gxm = (gxm*count + gx)/(count+1.0); gym = (gym*count + gy)/(count+1.0); gzm = (gzm*count + gz)/(count+1.0); count++; } } #ifdef CAL_DEBUG Serial.println(" Done."); #endif }
void setup() { Spark.variable("quaternionW", &quaternionW, DOUBLE); // join I2C bus (I2Cdev library doesn't do this automatically) Wire.begin(); //TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz) // 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(115200); while (!Serial.available()) ; // 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("Initializing I2C devices..."); mpu.initialize(); // verify connection Serial.println("Testing device connections..."); Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); // wait for ready Serial.println("\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("Initializing DMP..."); devStatus = mpu.dmpInitialize(); // supply your own gyro offsets here, scaled for min sensitivity mpu.setXGyroOffset(220); mpu.setYGyroOffset(76); mpu.setZGyroOffset(-85); mpu.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("Enabling DMP..."); mpu.setDMPEnabled(true); // enable Arduino interrupt detection Serial.println("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("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("DMP Initialization failed (code "); Serial.print(devStatus); Serial.println(")"); } // configure LED for output pinMode(LED_PIN, OUTPUT); }
//PROGRAM FUNCTIONS void setup_mpu6050(){ clear_i2c(); Wire.begin(); SERIAL_OUT.println("Initializing gyro..."); accelgyro.initialize(); //accelgyro.reset(); accelgyro.setSleepEnabled(false); // thanks to Jack Elston for pointing this one out! // verify connection SERIAL_OUT.println("Testing device connections..."); SERIAL_OUT.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); SERIAL_OUT.println(F("Setting clock source to Z Gyro...")); accelgyro.setClockSource(MPU6050_CLOCK_PLL_ZGYRO); //SERIAL_OUT.println(accelgyro.getClockSource(MPU6050_CLOCK_PLL_ZGYRO); SERIAL_OUT.println(F("Setting sample rate to 200Hz...")); accelgyro.setRate(0); // 1khz / (1 + 4) = 200 Hz // * | ACCELEROMETER | GYROSCOPE // * DLPF_CFG | Bandwidth | Delay | Bandwidth | Delay | Sample Rate // * ---------+-----------+--------+-----------+--------+------------- // * 0 | 260Hz | 0ms | 256Hz | 0.98ms | 8kHz // * 1 | 184Hz | 2.0ms | 188Hz | 1.9ms | 1kHz // * 2 | 94Hz | 3.0ms | 98Hz | 2.8ms | 1kHz // * 3 | 44Hz | 4.9ms | 42Hz | 4.8ms | 1kHz // * 4 | 21Hz | 8.5ms | 20Hz | 8.3ms | 1kHz // * 5 | 10Hz | 13.8ms | 10Hz | 13.4ms | 1kHz // * 6 | 5Hz | 19.0ms | 5Hz | 18.6ms | 1kHz // * 7 | -- Reserved -- | -- Reserved -- | Reserved SERIAL_OUT.println(F("Setting DLPF bandwidth")); accelgyro.setDLPFMode(MPU6050_DLPF_BW_42); SERIAL_OUT.println(F("Setting gyro sensitivity to +/- 250 deg/sec...")); accelgyro.setFullScaleGyroRange(0); //accelgyro.setFullScaleGyroRange(MPU6050_GYRO_FS_250); //accelgyro.setFullScaleGyroRange(0); // 0=250, 1=500, 2=1000, 3=2000 deg/sec //SERIAL_OUT.println(F("Resetting FIFO...")); //accelgyro.resetFIFO(); // use the code below to change accel/gyro offset values accelgyro.setXGyroOffset(XGYROOFFSET); accelgyro.setYGyroOffset(YGYROOFFSET); accelgyro.setZGyroOffset(ZGYROOFFSET); SERIAL_OUT.print(accelgyro.getXAccelOffset()); SERIAL_OUT.print("\t"); // SERIAL_OUT.print(accelgyro.getYAccelOffset()); SERIAL_OUT.print("\t"); // SERIAL_OUT.print(accelgyro.getZAccelOffset()); SERIAL_OUT.print("\t"); // SERIAL_OUT.print(accelgyro.getXGyroOffset()); SERIAL_OUT.print("\t"); // SERIAL_OUT.print(accelgyro.getYGyroOffset()); SERIAL_OUT.print("\t"); // SERIAL_OUT.print(accelgyro.getZGyroOffset()); SERIAL_OUT.print("\t"); // SERIAL_OUT.print("\n"); SERIAL_OUT.println(F("Enabling FIFO...")); accelgyro.setFIFOEnabled(true); accelgyro.setZGyroFIFOEnabled(true); accelgyro.setXGyroFIFOEnabled(false); accelgyro.setYGyroFIFOEnabled(false); accelgyro.setAccelFIFOEnabled(false); SERIAL_OUT.print("Z axis enabled?\t"); SERIAL_OUT.println(accelgyro.getZGyroFIFOEnabled()); SERIAL_OUT.print("x axis enabled?\t"); SERIAL_OUT.println(accelgyro.getXGyroFIFOEnabled()); SERIAL_OUT.print("y axis enabled?\t"); SERIAL_OUT.println(accelgyro.getYGyroFIFOEnabled()); SERIAL_OUT.print("accel enabled?\t"); SERIAL_OUT.println(accelgyro.getAccelFIFOEnabled()); accelgyro.resetFIFO(); return ; }
void imu_init() { uint8_t count = 10; // initialize device #ifdef __BOARD_YUN__ Console.println(F("Initializing I2C devices...")); #else Serial.println(F("Initializing I2C devices...")); #endif mpu.initialize(); // verify connection #ifdef __BOARD_YUN__ Console.println(F("Testing device connections...")); Console.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed")); #else Serial.println(F("Testing device connections...")); Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed")); #endif /* // 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 #ifdef __BOARD_YUN__ Console.println(F("Initializing DMP...")); #else Serial.println(F("Initializing DMP...")); #endif do { devStatus = mpu.dmpInitialize(); // Set some offset to the MEMS mpu.setXGyroOffset(220); mpu.setYGyroOffset(76); mpu.setZGyroOffset(-85); mpu.setZAccelOffset(1788); // make sure it worked (returns 0 if so) if (devStatus == 0) { count = 10; // turn on the DMP, now that it's ready #ifdef __BOARD_YUN__ Console.println(F("Enabling DMP...")); #else Serial.println(F("Enabling DMP...")); #endif mpu.setDMPEnabled(true); mpuIntStatus = mpu.getIntStatus(); // set our DMP Ready flag so the main loop() function knows it's okay to use it #ifdef __BOARD_YUN__ Console.println(F("DMP ready! Waiting for first interrupt...")); #else Serial.println(F("DMP ready! Waiting for first interrupt...")); #endif dmpReady = true; // get expected DMP packet size for later comparison packetSize = mpu.dmpGetFIFOPacketSize(); return; } else { // ERROR! // 1 = initial memory load failed // 2 = DMP configuration updates failed // (if it's going to break, usually the code will be 1) #ifdef __BOARD_YUN__ Console.print(F("DMP Initialization failed (code")); Console.print(devStatus); Console.println(F(")")); // New attempt message Console.println(F("Trying again")); #else Serial.print(F("DMP Initialization failed (code ")); Serial.print(devStatus); Serial.println(F(")")); // New attempt message Serial.println(F("Trying again")); #endif } } while (--count); // configure LED for output pinMode(SOL_LED, OUTPUT); // Check if the configuration has failed // if (!count) { #ifdef __BOARD_YUN__ Console.println(F("DMP initializaion failed")); #else Serial.println(F("DMP initialization failed")); #endif while (true) { // Locks in infinite loop digitalWrite(SOL_LED, HIGH); delay(300); digitalWrite(SOL_LED, LOW); delay(300); } } }