[QUAT W]  [       ]  [QUAT X]   [        ]  [QUAT Y]   [           ]    [QUAT Z]    [              ]    [GYRO X]    [              ]    [GYRO Y]    [          ]
          0   1       2 3         4   5         6 7          8   9      10  11        12  13         14  15          16  17           18  19           20  21        22  23 
                                                                                                 
 [GYRO Z]    [          ]    [ACC X ]    [          ]    [ACC Y ]    [          ]    [ACC Z ]    [          ]    [          ]                        
      24  25      26  27          28  29      30  31       32  33       34  35        36  37       38  39       40  41   
                     

   
     

MPU6050 DMP with ProtonBasic

Is it DMP of MPU6050 or Rocket Science?This is the question I asked myself when I started coding for my stabilized platform project. There are so many references of the DMP within the MPU6050 all over the internet but I found they were not as straight forward as it should be.KISS- Keep It Simple and Stupid is what I try to follow so that others can benefit from my work. As you everybody know INVENSENSE INC didn’t release DMP code to public, and the genius guy Jeff Rowbregdid all the hard work to Hack the Protocol and DMP driver code.Well I am not repeating that story again…. You can find their work on www.i2cdevlib.com. This i2cdevlib works fine on Arduino Platform, which is good for hobbyist. Copy->Paste->Call and it just works. You will find no explanation, step by step which memory address and bank needs to be updated and in what sequence to talk to this beast MPU6050. Moreover there are many confusing functions where you will find written (Function Unknown) as comments. Being C++ it is Object Oriented which makes it modular and fun to use. But porting to another language, say Proton Basic for some raw fun could be a pain in the ass.
This tutorial is thus mainly for Proton Basic users. However anybody else who alreadyknow how to write codes for microcontrollers can use the procedure on his own platform. Let’s fun begin……..


Before everything I will advice u to -try to get Raw Accelerometer and Gyroscope Data from MPU6050. Then try DMP. Remember you must have some knowledge on I2C protocol. At least try to get normal EEPROM Data with I2C beforeJ. There are many syntaxesfor i2c communications in Proton. We will be using two of them.

First one is:
I2COut SDA, SCL, Device_Address, Ragistor_Name, [Data,Data,…..]
I2in   SDA, SCL, Device_Address, Ragistor_Name, [Variable,Variable,….]

Second one is:
HBStart
HBusOut Device_Address
HBusOut Ragistor_Name
For i = 0 To Number
            HBusOut Data
Next i
HBStop

Please refer ProtonBasic user manual for further info on this two syntaxes. If you are using Mikro C or C 18 you can use your own syntaxes. Here is the user access Block of MPU.
 

 


 

 

 

 

User can access offset registers, direct raw data of Gyro/Accel output register, or DMP data through FIFO.
      The MPU6050 chip specifications are as follows:

  • Accelerometer range is Selectable +-2/4/8/16g
  • Gyroscope range is Selectable +-250/500/1000/2000 degrees/s
  • 16 bit output from both sensors
  • Gyroscope linear acceleration sensitivity of 0.1 degrees/s, a vast improvement over the tri axis gyroscopes of other companies.
  • Low noise on both outputs, see the datasheet
  • Data output rate up to 1000Hz, although the built in digital low pass filter (DLPF) has a maximum corner frequency of 256Hz.

If you are ready, follow the steps. There are five steps to work with DMP.
Step 1: Do some initial configuration setting
Step 2: Write DMP Memory Block to the certain Bank address.
Step 3: Write DMP Configuration data to certain Bank address
Step 4: Write Final Configuration settings to configuration register
Step 5: On interrupt read FIFO Bytes and Store it to Variable for further use

Initial configuration settings
The function INIT_SETUP will set some necessary register, RA_PWR_MGMT_1 and RA_PWR_MGMT_2.
This is DMP init requirement.

Write DMP Memory Block

There are 8 Memory Blocks for DMP; each block contains 16x16 Hex codes except the last one. The last memory block has 138 bytes of Data. Each block have to upload on a certain Bank ID. So, we are getting total 0 to 7 Banks. Remember the word BANK for next time useJ. When we upload Memory blocks to the MPU, we must announce the name of Bank Address and the Memory Address each time. Because there are 256 bytes of data in each block (except 7th block), better send 16 bytes each time. For that, once we have to announce the Bank address and memory, then sequentially have to send 16 bytes of data. So, to send first Bank’s first row we have to declare Bank address = 0 and Memory address = 0, to send 2nd row of first bank we have to declare Bank address = 0 and Memory address = 16 and so on. The syntax will be :-
I2COut SDA, SCL, MPU6050_ADDRESS_W, MPU6050_RA_MEM_R_W, [Str Data\16]

But special care has to be taken when 7th Memory block is uploading. The sub function called Update_DMP_Memory is taking care of it. In the world wide internet i didn’t get any explanation about Memory block uploading, I figured it out myself, and spend months of hard work to solve it.    
  

Writing DMP configuration Memory

The trickiest part of whole algorithm is DMP Configuration settings. You will see

  BANK  OFFSET  LNGTH  DATA
  $03,   $7B,   $03,   $4C, $CD, $6C,_           
  $00,   $6C,   $02,   $20, $00,

  ==================================
 Kind of arrangement of code. All codes are not applicable to upload on MPU memory. In each line of configuration code the first part represents Bank Address. Second part represents - from which Memory address code uploading will start. It’s called offset. Third part represents the length of data. This part is ignored from uploading. This part only use in for… next loop to determine data length. And the last part is Data bytes which have to upload DMP Configuration Memory. In the for… next loop when i = 22 an special instruction have to upload. Please check it in the code. The last byte of the configuration setting is $01. This will define the FIFO rate. Setting it $09, which drops the FIFO rate down to 20 Hz,. $07 is 25 Hz, $01 is 100 Hz. DMP Output frequency is calculated easily using this equation: (200Hz / (1 + value)). It is important to make sure the host processor can keep up with reading and processing the FIFO output at the desired rate. Handling FIFO overflow cleanly is also a good idea. So, I have implemented an algorithm on interrupt service routine to check if it is overflow then FIFO automatically resets.

Write Final Configuration settings

The final configuration setting is the last job to activate DMP. This section defines sample rate of gyroscope, DMP interrupt enable, FIFO reset etc. Sub function MPU6050_DMP_CONFIG is to do the job. At the end of final configuration setting we have to reset FIFO and DMP to start up.

Reading FIFO Bytes and Store it to Variable

Now it’s time to read FIFO. There are 42 ~ 44 bytes to read. The diagram of bytes is given Jeff Rowbreg’s website.

 

               

 

 

 

That means every bytes are not necessary. If u need quaternion output only, u just take [QUAT W],[QUAT X],[QUAT Y],[QUAT Z] and leave the rest. But the important thing is whatever bytes you need that doesn’t matter. You must read all bytes. That’s the rule of First in First Out (FIFO).  If your processor takes long time to read, FIFO will overflow and massy garbage value will come. So besides reading FIFO, you must have to check if it is overflow, and reset it again. Register USER_CTRL, Bit 2 is set 1 to reset the FIFO.

Congratulations now you have completed the DMP setup. It’s time to see the result.  We will test quaternion output with famous Teapot Processing sketch.

This is My Self Balancing Platform With DMP Mpu6050

Here is the Code and Schematic

Dmp with Mpu6050 and protonbasic. DMP protonbasic. i2c Dmp Protonbasic. Self balancing platform with protonbasic. protonbasic dmp code. Dmp example of protonbasic