Go Down

Topic: Clean, straight-forward MPU-6050 code (Read 15410 times) previous topic - next topic


I was wondering if there were any simple, straight-forward examples to access just the temperature, yaw/pitch/roll, and X/Y/Z acceleration.  Most of the demos are several hundred lines long (with or without comments) and for me, the i2cdev demo just halts at the "DMP ready! Waiting for first interrupt..." stage.

I never understood the point of people who create tutorials and examples that are so needlessly complicated.  It'd be easier for everyone (including the tutors) if there were separate files for each core feature rather than cram most (but not all) features into 1 file and leave the user to spend hours filtering out whats needed and what isn't. Also with such long codes, it's effortless to break the entire thing while attempting to clean up everything you don't need.  There have been many times I could take a 200 line example code and shrink it to about 1/4 the size with the exact same results and more descriptive details of what every single line is trying to accomplish.  But in this situation, the examples are so long and over-complicated that it drew me here.

Considering the MPU-6050 is a DMP on an I2C bus, I would think my request should be relatively simple.  The code could be as short as maybe 75 lines (minus comment lines) to get the results I want, where you initialize the libraries, prepare the device, request the DMP data, parse the DMP data, and send it over serial.  That being said, technically the mpu6050 library by i2cdev shouldn't be needed, but that library does help put human-readable names to all the addresses.

I managed to get the ADXL345 to work in less than 50 lines of code, no libraries, and convert the raw data into degrees.  I don't see why the same can't be done with this.  Does anyone have any suggestions?


I tried to do just that: http://playground.arduino.cc/Main/MPU-6050
But it doesn't use the DMP.



The alternative is to write the code you believe is needed for one device and share with the great unwashed.  Your example would show the world what it has been missing in easy and simple code and everyone would join the movement.



I noticed that code but it too is ridiculously long.  I'm sure 90% of the code is unneeded but its hard to determine what MUST be there.

The problem is I'm not really sure where to begin.  The MPU-6050 has so much stuff to it, most of which I could care less about.  I wouldn't mind submitting my own cleaned-up code but I need something reasonable to work with.  I don't have time to spend days upon days of research.



Im trying to compile the code you provided and get the following errors:

sketch_jan31a.ino: In function 'int MPU6050_read(int, uint8_t*, int)':
sketch_jan31a:802: error: 'class TwoWire' has no member named 'write'
sketch_jan31a:806: error: no matching function for call to 'TwoWire::endTransmission(int)'
C:\Documents and Settings\Gary W\My Documents\Arduino\libraries\Z/Wire.h:51: note: candidates are: uint8_t TwoWire::endTransmission()
sketch_jan31a:811: error: no matching function for call to 'TwoWire::requestFrom(int, int&, int)'
C:\Documents and Settings\Gary W\My Documents\Arduino\libraries\Z/Wire.h:52: note: candidates are: uint8_t TwoWire::requestFrom(uint8_t, uint8_t)
C:\Documents and Settings\Gary W\My Documents\Arduino\libraries\Z/Wire.h:53: note:                 uint8_t TwoWire::requestFrom(int, int)
sketch_jan31a:815: error: 'class TwoWire' has no member named 'read'
sketch_jan31a.ino: In function 'int MPU6050_write(int, const uint8_t*, int)':
sketch_jan31a:848: error: 'class TwoWire' has no member named 'write'
sketch_jan31a:852: error: 'class TwoWire' has no member named 'write'
sketch_jan31a:856: error: no matching function for call to 'TwoWire::endTransmission(int)'
C:\Documents and Settings\Gary W\My Documents\Arduino\libraries\Z/Wire.h:51: note: candidates are: uint8_t TwoWire::endTransmission()

I thought most of the error were due to Wire Library not being available but its in the Library folder.
I'm using a DUO with 1.0.3 version of the IDE

Any assistance would be appreciated.



Are you sure you are using version 1.0.3 ?
I seems you have a version older than 1.0.
Perhaps you can install it again.

Which board are you using, "Due" or "Uno" or "Duemilanove".

I don't have a "Due" myself, but I could at least make the compiler run.
I have tested 1.0.3 with the "Uno" once again, and it compiles but has a lot warnings. I have to fix that someday.


it too is ridiculously long.

That particular piece of code is actually well written. It's well structured and well commented. In particular, the use of all the defines helps make the code more portable and more readable / maintainable. That's how one is supposed to have written code pieces in C - I would have put it in a .c/.h file for that's for another discussion.

The actual code being executed are towards the bottom, if you wish to live with magic numbers - a very bad way of programming.

The device is i2c, so it can be easily coded as such. A 15 minute job,  max, if you want to roll your own.


I wanted just one single sketch to get started as quick as possible. A seperate *.h file is better programming.

Do you mean the error codes with the "magic numbers" ?
I can live with that, but perhaps I should make them defines also.



Sorry I misspoke, I'm using a UNO and as I understand it the 1.0.3 IDE, that's what it says at the top of the IDE application.  I've provided a screenshot.  The compile error I'm getting is when I Verify the sketch.

Again thanks for your help, this is confusing


I can't replicate that.
Did you change the default libraries ? Or install an other "Wire" library ?
Did you change the sketch ?

I still think you have the libraries of an old version.

Perhaps you should delete all the Arduino software (not your sketches, not your own libraries).
Can you download the Arduino software again, and place it in a new folder, and start it from that new folder.


I didn't change the code, just cut and paste from the webpage.  I did add in other libraries after downloading 1.0.3, I'll try a new install without moving in my libraries.


Go Up