@@ -36,53 +36,10 @@ THE SOFTWARE.
3636#define MPU6050_INCLUDE_DMP_MOTIONAPPS20
3737
3838#include " MPU6050_6Axis_MotionApps20.h"
39-
40- // Tom Carpenter's conditional PROGMEM code
41- // http://forum.arduino.cc/index.php?topic=129407.0
42- #ifdef __AVR__
43- #include < avr/pgmspace.h>
44- #elif defined(ESP32)
45- #include < pgmspace.h>
46- #else
47- // Teensy 3.0 library conditional PROGMEM code from Paul Stoffregen
48- #ifndef __PGMSPACE_H_
49- #define __PGMSPACE_H_ 1
50- #include < inttypes.h>
51-
52- #define PROGMEM
53- #define PGM_P const char *
54- #define PSTR (str ) (str)
55- #define F (x ) x
56-
57- typedef void prog_void;
58- typedef char prog_char;
59- typedef unsigned char prog_uchar;
60- typedef int8_t prog_int8_t ;
61- typedef uint8_t prog_uint8_t ;
62- typedef int16_t prog_int16_t ;
63- typedef uint16_t prog_uint16_t ;
64- typedef int32_t prog_int32_t ;
65- typedef uint32_t prog_uint32_t ;
66-
67- #define strcpy_P (dest, src ) strcpy((dest), (src))
68- #define strcat_P (dest, src ) strcat((dest), (src))
69- #define strcmp_P (a, b ) strcmp((a), (b))
70-
71- #define pgm_read_byte (addr ) (*(const unsigned char *)(addr))
72- #define pgm_read_word (addr ) (*(const unsigned short *)(addr))
73- #define pgm_read_dword (addr ) (*(const unsigned long *)(addr))
74- #define pgm_read_float (addr ) (*(const float *)(addr))
75-
76- #define pgm_read_byte_near (addr ) pgm_read_byte(addr)
77- #define pgm_read_word_near (addr ) pgm_read_word(addr)
78- #define pgm_read_dword_near (addr ) pgm_read_dword(addr)
79- #define pgm_read_float_near (addr ) pgm_read_float(addr)
80- #define pgm_read_byte_far (addr ) pgm_read_byte(addr)
81- #define pgm_read_word_far (addr ) pgm_read_word(addr)
82- #define pgm_read_dword_far (addr ) pgm_read_dword(addr)
83- #define pgm_read_float_far (addr ) pgm_read_float(addr)
84- #endif
85- #endif
39+ #include < stdio.h>
40+ #include < unistd.h>
41+ #include < string.h>
42+ #include < math.h>
8643
8744/* Source is from the InvenSense MotionApps v2 demo code. Original source is
8845 * unavailable, unless you happen to be amazing as decompiling binary by
@@ -100,15 +57,9 @@ THE SOFTWARE.
10057
10158// #define DEBUG
10259#ifdef DEBUG
103- #define DEBUG_PRINT (x ) Serial.print(x)
104- #define DEBUG_PRINTF (x, y ) Serial.print(x, y)
105- #define DEBUG_PRINTLN (x ) Serial.println(x)
106- #define DEBUG_PRINTLNF (x, y ) Serial.println(x, y)
60+ #define DEBUG_PRINTF (...) fprintf(stderr, __VA_ARGS__)
10761#else
108- #define DEBUG_PRINT (x )
109- #define DEBUG_PRINTF (x, y )
110- #define DEBUG_PRINTLN (x )
111- #define DEBUG_PRINTLNF (x, y )
62+ #define DEBUG_PRINTF (...)
11263#endif
11364
11465#define MPU6050_DMP_CODE_SIZE 1929 // dmpMemory[]
@@ -131,7 +82,7 @@ THE SOFTWARE.
13182
13283// I Only Changed this by applying all the configuration data and capturing it before startup:
13384// *** this is a capture of the DMP Firmware after all the messy changes were made so we can just load it
134- static const unsigned char dmpMemory[MPU6050_DMP_CODE_SIZE ] PROGMEM = {
85+ static const unsigned char dmpMemory[MPU6050_DMP_CODE_SIZE ] = {
13586 /* bank # 0 */
13687 0xFB , 0x00 , 0x00 , 0x3E , 0x00 , 0x0B , 0x00 , 0x36 , 0x00 , 0x01 , 0x00 , 0x02 , 0x00 , 0x03 , 0x00 , 0x00 ,
13788 0x00 , 0x65 , 0x00 , 0x54 , 0xFF , 0xEF , 0x00 , 0x00 , 0xFA , 0x80 , 0x00 , 0x0B , 0x12 , 0x82 , 0x00 , 0x01 ,
@@ -271,14 +222,14 @@ static const unsigned char dmpMemory[MPU6050_DMP_CODE_SIZE] PROGMEM = {
271222// I Simplified this:
272223uint8_t MPU6050_6Axis_MotionApps20::dmpInitialize () {
273224 // reset device
274- DEBUG_PRINTLN ( F ( " \n\ n Resetting MPU6050..." ) );
225+ DEBUG_PRINTF ( " \ n Resetting MPU6050...\n " );
275226 reset ();
276- delay ( 30 ); // wait after reset
227+ usleep ( 30000 ); // wait after reset
277228
278229 // enable sleep mode and wake cycle
279- /* Serial.println(F( "Enabling sleep mode...") );
230+ /* Serial.println("Enabling sleep mode...");
280231 setSleepEnabled(true);
281- Serial.println(F( "Enabling wake cycle...") );
232+ Serial.println("Enabling wake cycle...");
282233 setWakeCycleEnabled(true);*/
283234
284235 // disable sleep mode
@@ -287,51 +238,47 @@ uint8_t MPU6050_6Axis_MotionApps20::dmpInitialize() {
287238 // get MPU hardware revision
288239 setMemoryBank (0x10 , true , true );
289240 setMemoryStartAddress (0x06 );
290- DEBUG_PRINTLN (F (" Checking hardware revision..." ));
291- DEBUG_PRINT (F (" Revision @ user[16][6] = " ));
292- DEBUG_PRINTLN (readMemoryByte ());
293- DEBUG_PRINTLN (F (" Resetting memory bank selection to 0..." ));
241+ DEBUG_PRINTF (" Checking hardware revision...\n " );
242+ DEBUG_PRINTF (" Revision @ user[16][6] = %hhx\n " , readMemoryByte ());
243+ DEBUG_PRINTF (" Resetting memory bank selection to 0...\n " );
294244 setMemoryBank (0 , false , false );
295245
296246 // check OTP bank valid
297- DEBUG_PRINTLN (F (" Reading OTP bank valid flag..." ));
298- DEBUG_PRINT (F (" OTP bank is " ));
299- DEBUG_PRINTLN (getOTPBankValid () ? F (" valid!" ) : F (" invalid!" ));
247+ DEBUG_PRINTF (" Reading OTP bank valid flag...\n " );
248+ DEBUG_PRINTF (" OTP bank is %s\n " , getOTPBankValid () ? " valid!" : " invalid!" );
300249
301250 // setup weird slave stuff (?)
302- DEBUG_PRINTLN ( F ( " Setting slave 0 address to 0x7F..." ) );
251+ DEBUG_PRINTF ( " Setting slave 0 address to 0x7F...\n " );
303252 setSlaveAddress (0 , 0x7F );
304- DEBUG_PRINTLN ( F ( " Disabling I2C Master mode..." ) );
253+ DEBUG_PRINTF ( " Disabling I2C Master mode...\n " );
305254 setI2CMasterModeEnabled (false );
306- DEBUG_PRINTLN ( F ( " Setting slave 0 address to 0x68 (self)..." ) );
255+ DEBUG_PRINTF ( " Setting slave 0 address to 0x68 (self)...\n " );
307256 setSlaveAddress (0 , 0x68 );
308- DEBUG_PRINTLN ( F ( " Resetting I2C Master control..." ) );
257+ DEBUG_PRINTF ( " Resetting I2C Master control...\n " );
309258 resetI2CMaster ();
310- delay ( 20 );
311- DEBUG_PRINTLN ( F ( " Setting clock source to Z Gyro..." ) );
259+ usleep ( 20000 );
260+ DEBUG_PRINTF ( " Setting clock source to Z Gyro...\n " );
312261 setClockSource (MPU6050_CLOCK_PLL_ZGYRO );
313262
314- DEBUG_PRINTLN ( F ( " Setting DMP and FIFO_OFLOW interrupts enabled..." ) );
263+ DEBUG_PRINTF ( " Setting DMP and FIFO_OFLOW interrupts enabled...\n " );
315264 setIntEnabled (1 <<MPU6050_INTERRUPT_FIFO_OFLOW_BIT |1 <<MPU6050_INTERRUPT_DMP_INT_BIT );
316265
317- DEBUG_PRINTLN ( F ( " Setting sample rate to 200Hz..." ) );
266+ DEBUG_PRINTF ( " Setting sample rate to 200Hz...\n " );
318267 setRate (4 ); // 1khz / (1 + 4) = 200 Hz
319268
320- DEBUG_PRINTLN ( F ( " Setting external frame sync to TEMP_OUT_L[0]..." ) );
269+ DEBUG_PRINTF ( " Setting external frame sync to TEMP_OUT_L[0]...\n " );
321270 setExternalFrameSync (MPU6050_EXT_SYNC_TEMP_OUT_L );
322271
323- DEBUG_PRINTLN ( F ( " Setting DLPF bandwidth to 42Hz..." ) );
272+ DEBUG_PRINTF ( " Setting DLPF bandwidth to 42Hz...\n " );
324273 setDLPFMode (MPU6050_DLPF_BW_42 );
325274
326- DEBUG_PRINTLN ( F ( " Setting gyro sensitivity to +/- 2000 deg/sec..." ) );
275+ DEBUG_PRINTF ( " Setting gyro sensitivity to +/- 2000 deg/sec...\n " );
327276 setFullScaleGyroRange (MPU6050_GYRO_FS_2000 );
328277
329278 // load DMP code into memory banks
330- DEBUG_PRINT (F (" Writing DMP code to MPU memory banks (" ));
331- DEBUG_PRINT (MPU6050_DMP_CODE_SIZE );
332- DEBUG_PRINTLN (F (" bytes)" ));
279+ DEBUG_PRINTF (" Writing DMP code to MPU memory banks (%u bytes)\n " , MPU6050_DMP_CODE_SIZE );
333280 if (!writeProgMemoryBlock (dmpMemory, MPU6050_DMP_CODE_SIZE )) return 1 ; // Failed
334- DEBUG_PRINTLN ( F ( " Success! DMP code written and verified." ) );
281+ DEBUG_PRINTF ( " Success! DMP code written and verified.\n " );
335282
336283 // Set the FIFO Rate Divisor int the DMP Firmware Memory
337284 unsigned char dmpUpdate[] = {0x00 , MPU6050_DMP_FIFO_RATE_DIVISOR };
@@ -342,35 +289,35 @@ uint8_t MPU6050_6Axis_MotionApps20::dmpInitialize() {
342289 // write start address LSB into register
343290 setDMPConfig2 (0x00 );
344291
345- DEBUG_PRINTLN ( F ( " Clearing OTP Bank flag..." ) );
292+ DEBUG_PRINTF ( " Clearing OTP Bank flag...\n " );
346293 setOTPBankValid (false );
347294
348- DEBUG_PRINTLN ( F ( " Setting motion detection threshold to 2..." ) );
295+ DEBUG_PRINTF ( " Setting motion detection threshold to 2...\n " );
349296 setMotionDetectionThreshold (2 );
350297
351- DEBUG_PRINTLN ( F ( " Setting zero-motion detection threshold to 156..." ) );
298+ DEBUG_PRINTF ( " Setting zero-motion detection threshold to 156...\n " );
352299 setZeroMotionDetectionThreshold (156 );
353300
354- DEBUG_PRINTLN ( F ( " Setting motion detection duration to 80..." ) );
301+ DEBUG_PRINTF ( " Setting motion detection duration to 80...\n " );
355302 setMotionDetectionDuration (80 );
356303
357- DEBUG_PRINTLN ( F ( " Setting zero-motion detection duration to 0..." ) );
304+ DEBUG_PRINTF ( " Setting zero-motion detection duration to 0...\n " );
358305 setZeroMotionDetectionDuration (0 );
359- DEBUG_PRINTLN ( F ( " Enabling FIFO..." ) );
306+ DEBUG_PRINTF ( " Enabling FIFO...\n " );
360307 setFIFOEnabled (true );
361308
362- DEBUG_PRINTLN ( F ( " Resetting DMP..." ) );
309+ DEBUG_PRINTF ( " Resetting DMP...\n " );
363310 resetDMP ();
364311
365- DEBUG_PRINTLN ( F ( " DMP is good to go! Finally." ) );
312+ DEBUG_PRINTF ( " DMP is good to go! Finally.\n " );
366313
367- DEBUG_PRINTLN ( F ( " Disabling DMP (you turn it on later)..." ) );
314+ DEBUG_PRINTF ( " Disabling DMP (you turn it on later)...\n " );
368315 setDMPEnabled (false );
369316
370- DEBUG_PRINTLN ( F ( " Setting up internal 42-byte (default) DMP packet buffer..." ) );
317+ DEBUG_PRINTF ( " Setting up internal 42-byte (default) DMP packet buffer...\n " );
371318 dmpPacketSize = 42 ;
372319
373- DEBUG_PRINTLN ( F ( " Resetting FIFO and clearing INT status one last time..." ) );
320+ DEBUG_PRINTF ( " Resetting FIFO and clearing INT status one last time...\n " );
374321 resetFIFO ();
375322 getIntStatus ();
376323
@@ -557,9 +504,9 @@ uint8_t MPU6050_6Axis_MotionApps20::dmpGetYawPitchRoll(float *data, Quaternion *
557504 data[2 ] = atan2 (gravity -> y , gravity -> z);
558505 if (gravity -> z < 0 ) {
559506 if (data[1 ] > 0 ) {
560- data[1 ] = PI - data[1 ];
507+ data[1 ] = M_PI - data[1 ];
561508 } else {
562- data[1 ] = -PI - data[1 ];
509+ data[1 ] = -M_PI - data[1 ];
563510 }
564511 }
565512 return 0 ;
0 commit comments