Interfacing Teensy 3.x to MPU6050/DMP using Jeff Roberg's I2CDev

Status
Not open for further replies.

paynterf

Well-known member
Hi,

As part of an effort to troubleshoot problems with motor noise causing my MPU6U050-enabled robot to crash intermittently, I decided to change controllers from my current UNO-based system to the Teensy 3.2. However, my current system uses Jeff Rowberg's I2CDev library to utilize the MPU6050's DMP and I couldn't find any good examples of doing this in the Teensy world. So, after just finishing the not-so-straightforward process of getting a Teensy 3.2 interfaced to a MPU6050 (GY-521) breakout module using Jeff Rowberg's I2CDev & MPU6050 libraries and also interfacing to the Adafruit DRV8871 motor driver, I thought I would post the results here to maybe help others.

I had a huge problem trying to track down compile issues with the I2CDev libraries, but in the end it came down to figuring out a way to get I2CDev to use i2c_t3.h instead of Wire.h. I solved this by copying I2CDev.h/cpp from my Libraries folder to my local project/solution folder, and editing the code to define the I2C implementation as I2CDEV_ARDUINO_WIRE and then replacing "#include <Wire.h>" with "#include <i2c_t3.h> in the appropriate section as shown below. I'm sure there are more elegant ways of doing this (maybe adding a 'I2CDEV_TEENSY_3.X' section at the top?)



HTML:
// -----------------------------------------------------------------------------
// I2C interface implementation setting
// -----------------------------------------------------------------------------
#ifndef I2CDEV_IMPLEMENTATION
#define I2CDEV_IMPLEMENTATION       I2CDEV_ARDUINO_WIRE
//#define I2CDEV_IMPLEMENTATION       I2CDEV_BUILTIN_SBWIRE //09/29/19 gfp enabled this to avoid I2C hangups
//#define I2CDEV_IMPLEMENTATION       I2CDEV_BUILTIN_FASTWIRE
#endif // I2CDEV_IMPLEMENTATION

// comment this out if you are using a non-optimal IDE/implementation setting
// but want the compiler to shut up about it
#define I2CDEV_IMPLEMENTATION_WARNINGS

// -----------------------------------------------------------------------------
// I2C interface implementation options
// -----------------------------------------------------------------------------
#define I2CDEV_ARDUINO_WIRE         1 // Wire object from Arduino
#define I2CDEV_BUILTIN_NBWIRE       2 // Tweaked Wire object from Gene Knight's NBWire project
                                      // ^^^ NBWire implementation is still buggy w/some interrupts!
#define I2CDEV_BUILTIN_FASTWIRE     3 // FastWire object from Francesco Ferrara's project
#define I2CDEV_I2CMASTER_LIBRARY    4 // I2C object from DSSCircuits I2C-Master Library at https://github.com/DSSCircuits/I2C-Master-Library
#define I2CDEV_BUILTIN_SBWIRE	    5 // I2C object from Shuning (Steve) Bian's SBWire Library at https://github.com/freespace/SBWire 

// -----------------------------------------------------------------------------
// Arduino-style "Serial.print" debug constant (uncomment to enable)
// -----------------------------------------------------------------------------
//#define I2CDEV_SERIAL_DEBUG

#ifdef ARDUINO
    #if ARDUINO < 100
        #include "WProgram.h"
    #else
        #include "Arduino.h"
    #endif
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        //#include <Wire.h>
        #include <i2c_t3.h>
#endif
    #if I2CDEV_IMPLEMENTATION == I2CDEV_I2CMASTER_LIBRARY
        #include <I2C.h>
    #endif
	#if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE
		#include "SBWire.h"
	#endif
#endif

After making the above change, the project started compiling OK, and I was able to connect to the MPU6050 and pull off yaw values using the DMP.

The next problem occurred when I tried to run a test program with the Adafruit DRV8871 drivers, which use two lines per driver, where both lines alternate between used as digital outputs (for direction control) and analog/PWM outputs (for speed control). It turns out that once a Teensy 3.2 line has been written to with 'analogWrite()', it can no longer be used as a digital line without first running 'pinMode([pin],OUTPUT)' on it. This particular little 'gotcha' appears to have been there since around 2016, but got lost in the shuffle as it is still there in the latest TeensyDuino libraries.

After fixing that problem, I was successful in both getting yaw values from the MPU6050 using Jeff's I2CDev libraries, and in driving my Pololu D20 metal-geared motors via the Adafruit DRV8871 motor driver modules.

I have included a ZIP file containing my entire VS2019 project folder, in case anyone else is interested. You can follow my other efforts to combat a persistent EMI/RFI problem with the MPU6050 on my blog site 'fpaynter.com'.

Frank
 

Attachments

  • Teensy_MPU6050_DMP6_V2.vcxproj.zip
    69.2 KB · Views: 72
I also went back and figured out how to create a pull request for the modification to i2cdev.h, so (assuming the pull request is accepted), the only thing required to use I2CDev for Teensy would be to edit i2cdev.h in the Arduino folder to change

#define I2CDEV_IMPLEMENTATION I2CDEV_ARDUINO_WIRE
//#define I2CDEV_IMPLEMENTATION I2CDEV_TEENSY_3X_WIRE

to

//#define I2CDEV_IMPLEMENTATION I2CDEV_ARDUINO_WIRE
#define I2CDEV_IMPLEMENTATION I2CDEV_TEENSY_3X_WIRE


Frank
 
I also went back and figured out how to create a pull request for the modification to i2cdev.h, so (assuming the pull request is accepted), the only thing required to use I2CDev for Teensy would be to edit i2cdev.h in the Arduino folder to change

#define I2CDEV_IMPLEMENTATION I2CDEV_ARDUINO_WIRE
//#define I2CDEV_IMPLEMENTATION I2CDEV_TEENSY_3X_WIRE

to

//#define I2CDEV_IMPLEMENTATION I2CDEV_ARDUINO_WIRE
#define I2CDEV_IMPLEMENTATION I2CDEV_TEENSY_3X_WIRE


Frank
Note, that Teensy 4.0 does not support t3_i2c.h, ony the Teensy LC and 3.x currently support it.
 
Wonderful - outmoded before I even get started! ;-). FWIW, I figured out how to do a pull request to add the "I2CDEV_IMPLEMENTATION I2CDEV_TEENSY_3X_WIRE" section to i2cdev.h, and I just got notified that it has already been merged with the master distro.

Frank
 
Status
Not open for further replies.
Back
Top