Skip to content

Commit

Permalink
Merge pull request #86 from xpeqex/master
Browse files Browse the repository at this point in the history
Updating the examples
  • Loading branch information
sabas1080 authored Oct 3, 2024
2 parents 0281cd4 + 27b0bd5 commit be83d6a
Show file tree
Hide file tree
Showing 15 changed files with 1,542 additions and 4,802 deletions.
582 changes: 257 additions & 325 deletions examples/IMU_Zero/IMU_Zero.ino

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions examples/IMU_Zero/Makefile

This file was deleted.

164 changes: 80 additions & 84 deletions examples/MCU_Powercycle_DMP6/MCU_Powercycle_DMP6.ino
Original file line number Diff line number Diff line change
@@ -1,118 +1,113 @@
// This sample code shows how to use dmpSetFIFOPacketSize() so as not to waste time
// initializaing the MPU6050 when the MCU has been reset or powercycled but the MPU6050 has not.
// Instead we just need to set the FIFOPacketSize, since this is the only setting needed
// that was stored in the MCU RAM.
/*
MCU Powercycle DMP6
// This example code was written for the LGT8F328. In this example the LGT8F328 goes into
// a deep sleep for 1 second which erases its ram and thus causes the MCU to restart on wakeup.
// The code is based off a simplication of example code "MPU6050_DMP6".
Understand the use of function dmpSetFIFOPacketSize() so the MPU6050 initiates faster
when the MCU has been reset or powercycled but MPU6050 has not.
// Code and comments related to demonstrating how to use dmpSetFIFOPacketSize() are preceded
// by a line of asterisks (***************************************************************).
This example is designed for the LGT8F328. The MCU will enter deep sleep for 1 second, erasing
the RAM, causing the MCU to restart on wakeup.
Find the full MPU6050 library documentation here:
https://github.com/ElectronicCats/mpu6050/wiki
created 19 Sep 2023
by John Harrison
*/

#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include <lgt_LowPower.h>

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
#include <lgt_LowPower.h> //Library needed to enter sleep mode, use the one for your board

/* MPU6050 default I2C address is 0x68*/
MPU6050 mpu;

// MPU control/status vars
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint8_t fifoBuffer[42]; // FIFO storage buffer

// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container
VectorFloat gravity; // [x, y, z] gravity vector
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
//MPU6050 mpu(0x69); //Use for AD0 high
//MPU6050 mpu(0x68, &Wire1); //Use for AD0 low, but 2nd Wire (TWI/I2C) object.

/*---MPU6050 Control/Status Variables---*/
uint8_t devStatus; // Return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // Expected DMP packet size (default is 42 bytes)
uint8_t FIFOBuffer[42]; // FIFO storage buffer

/*---Orientation/Motion Variables---*/
Quaternion q; // [w, x, y, z] Quaternion container
VectorFloat gravity; // [x, y, z] Gravity vector
float ypr[3]; // [yaw, pitch, roll] Yaw/Pitch/Roll container and gravity vector
uint32_t startTime;

// ================================================================
// === INITIAL SETUP ===
// ================================================================

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

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment on this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
Serial.begin(115200);
Serial.println(F("Starting setup"));

// ***************************************************************
// if MotionDetectionDuration is not the default value (0) but is instead a value we have set previously (1)
// then we know that the mpu6050 has not been reset or powercycled only the MCU has been, so we don't need
// to waste time reininitializing the mpu6050. Instead we just have to set the MCU to know the packet size
// since this value was only retained previously in the MCU RAM:
/*
If MotionDetectionDuration is not 0 (default value) and instead a value we defined (1), then is know
that the mpu6050 has not been reset or power-cycled only the MCU has been.
We just have to set the MCU to know the packet size since this value was only retained previously in
the MCU RAM.
*/
if (mpu.getMotionDetectionDuration() == 1) {
Serial.println("Skipping MPU6050 initialization");
mpu.dmpSetFIFOPacketSize(42);
return;
}

// ***************************************************************
// if we got here then the MPU6050 has been power cycled or reset so we have to do a full initialization
/* Full initialization if the MPU6050 was powercycled */
Serial.println(F("Initializing I2C devices..."));
mpu.initialize();
pinMode(INTERRUPT_PIN, INPUT);

// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
/*Verify connection*/
Serial.println(F("Testing MPU6050 connection..."));
if(mpu.testConnection() == false){
Serial.println("MPU6050 connection failed");
while(true);
}
else {
Serial.println("MPU6050 connection successful");
}

// load and configure the DMP
/* Initializate and configure the DMP*/
Serial.println(F("Initializing DMP..."));
devStatus = mpu.dmpInitialize();

// make sure it worked (returns 0 if so)
/* Making sure it worked (returns 0 if so) */
if (devStatus == 0) {
// Calibration Time: generate offsets and calibrate our MPU6050
mpu.CalibrateAccel(6);
mpu.CalibrateAccel(6); // Calibration Time: generate offsets and calibrate our MPU6050
mpu.CalibrateGyro(6);
Serial.println(F("Enabling DMP..."));
Serial.println(F("Enabling DMP...")); //Turning ON DMP
mpu.setDMPEnabled(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 "));
packetSize = mpu.dmpGetFIFOPacketSize(); // Get expected DMP packet size for later comparison
}
else {
Serial.print(F("DMP Initialization failed (code ")); //Print the error code
Serial.print(devStatus);
Serial.println(F(")"));
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
}

// ***************************************************************
// set MotionDetectionDuration to a value other than the default (0).
// Note that we could set any value other than default in the MPU6050
// and test for that instead of MotionDetectionDuration. We chose
// MotionDetectionDuration to set in this example only as an example
// of how to test for reset/powerup on the MPU6050.
/*
Setting MotionDetectionDuration to a value other than the default (0).
Note that we could set any value other than default and test for that
instead of MotionDetectionDuration. We chose MotionDetectionDuration
to set in this example only as an example of how to test for reset/powerup
on the MPU6050.
*/
mpu.setMotionDetectionDuration(1);

startTime = millis();
}



// ================================================================
// === MAIN PROGRAM LOOP ===
// ================================================================

void loop() {
if ((millis() - startTime) < 5000) { // show YPR values for 5 seconds
if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) { // Get the Latest packet
// display Euler angles in degrees
mpu.dmpGetQuaternion(&q, fifoBuffer);
if ((millis() - startTime) < 5000) { // Print YPR values for 5 seconds
if (mpu.dmpGetCurrentFIFOPacket(FIFOBuffer)) { // Get the Latest packet
/* Display Euler angles in degrees */
mpu.dmpGetQuaternion(&q, FIFOBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
Serial.print("ypr\t");
Expand All @@ -122,13 +117,14 @@ void loop() {
Serial.print("\t");
Serial.println(ypr[2] * 180 / M_PI);
}
} else {
// ***************************************************************
// 5 seconds have passed
// put the MCU into a deep sleep for 1 second. All RAM is lost
// however the MPU6050 will continue to operate.
Serial.println("going to sleep for 1 Sec.");
delay(100); // give the MCU a chance to print the above message
LowPower.deepSleep2(SLEEP_1S);
}
else {
/*
5 seconds have passed, put the MCU into a deep sleep for 1 second.
All RAM is erased however the MPU6050 will continue to operate.
*/
Serial.println("Going to sleep for 1 Sec.");
delay(100); // Give the MCU a chance to print the above message
LowPower.deepSleep2(SLEEP_1S); //Function from low power library to enter deep sleep
}
}
Loading

0 comments on commit be83d6a

Please sign in to comment.