Skip to content

Commit

Permalink
MPU6050 init and error handling
Browse files Browse the repository at this point in the history
SysModPins
- show proper Wire.begin debug info

UserModMPU6050:
- add mtReady checkbox
- improve init and error handling
- rename dmpReady to motionTrackingReady and make public var.
  • Loading branch information
ewoudwijma committed Jun 21, 2024
1 parent a43bfe0 commit d9f6479
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ lib_deps =
build_flags =
-D APP=StarBase
-D PIOENV=$PIOENV
-D VERSION=24060410 ; Date and time (GMT!), update at every commit!!
-D VERSION=24062015 ; Date and time (GMT!), update at every commit!!
-D CONFIG_ASYNC_TCP_USE_WDT=0
-D LFS_THREADSAFE ; enables use of semaphores in LittleFS driver
-D STARBASE_DEVMODE
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModPins.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class SysModPins:public SysModule {
allocatePin(21, "Pins", "I2S SDA");
allocatePin(22, "Pins", "I2S SCL");
bool success = Wire.begin(21,22);
ppf("initI2S Wire begin ...\n", success?"success":"failure");
ppf("initI2S Wire begin %s\n", success?"success":"failure");
return success;
}
};
Expand Down
64 changes: 39 additions & 25 deletions src/User/UserModMPU6050.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@license For non GPL-v3 usage, commercial licenses must be purchased. Contact [email protected]
*/

#pragma once

#include "../Sys/SysModUI.h" //why needed here and not in other sysmods?
#include "../Sys/SysModPins.h"

Expand All @@ -20,6 +22,8 @@ class UserModMPU6050: public SysModule {

public:

bool motionTrackingReady = false; // set true if DMP init was successful

Coord3D gyro; // in degrees (not radians)
Coord3D accell;

Expand All @@ -31,6 +35,13 @@ class UserModMPU6050: public SysModule {
SysModule::setup();
parentVar = ui->initUserMod(parentVar, name, 6305);

ui->initCheckBox(parentVar, "mtReady", &motionTrackingReady, true, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun:
ui->setLabel(var, "tracking ready");
return true;
default: return false;
}});

ui->initCoord3D(parentVar, "gyro", &gyro, 0, UINT16_MAX, true, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun:
ui->setComment(var, "in degrees");
Expand All @@ -49,31 +60,35 @@ class UserModMPU6050: public SysModule {
mpu.initialize();

// verify connection
ppf("Testing device connections %s\n", mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

ppf("Initializing DMP...\n");
uint8_t devStatus = mpu.dmpInitialize();

if (devStatus == 0) {
// // Calibration Time: generate offsets and calibrate our MPU6050
mpu.CalibrateAccel(6);
mpu.CalibrateGyro(6);
// mpu.PrintActiveOffsets();

mpu.setDMPEnabled(true); //mandatory

// mpuIntStatus = mpu.getIntStatus();

dmpReady = true;
}
else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
ppf("DMP Initialization failed (code %d)\n", devStatus);
if (mpu.testConnection()) {
ppf("MPU6050 connection successful Initializing DMP...\n");
uint8_t devStatus = mpu.dmpInitialize();

if (devStatus == 0) {
// // Calibration Time: generate offsets and calibrate our MPU6050
mpu.CalibrateAccel(6);
mpu.CalibrateGyro(6);
// mpu.PrintActiveOffsets();
mpu.setDMPEnabled(true); //mandatory

// mpuIntStatus = mpu.getIntStatus();

motionTrackingReady = true;
}
else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
ppf("DMP Initialization failed (code %d)\n", devStatus);
}
}
else
ppf("Testing device connections MPU6050 connection failed\n");
}

mdl->setValue("mtReady", motionTrackingReady);
}

void loop() {
Expand All @@ -82,7 +97,7 @@ class UserModMPU6050: public SysModule {
// ppf("mpu6050 %d,%d,%d %d,%d,%d\n", accell.x, accell.y, accell.z, gyro.x, gyro.y, gyro.z);

// if programming failed, don't try to do anything
if (!dmpReady) return;
if (!motionTrackingReady) return;
// read a packet from FIFO
if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) { // Get the Latest packet
mpu.dmpGetQuaternion(&q, fifoBuffer);
Expand Down Expand Up @@ -120,7 +135,6 @@ class UserModMPU6050: public SysModule {
MPU6050 mpu;

// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint8_t fifoBuffer[64]; // FIFO storage buffer

Expand Down

0 comments on commit d9f6479

Please sign in to comment.