Skip to content

Conditionally adding class members #230

@eadf

Description

@eadf

I found this piece of code in MPU6050.h (Arduino):

#ifdef MPU6050_INCLUDE_DMP_MOTIONAPPS20
            uint8_t *dmpPacketBuffer;
            uint16_t dmpPacketSize;
....

It is conditionally adding member variables to the MPU6050 class. The problem is that when MPU6050.cpp is compiled the #define is not active.
Member variables are accessed by index at runtime (and not by name) so any random bug can occur.

I can fix this in the 'develop' branch, but I thought it would be better to discuss how it should be solved first.

The options I can think of right now are:

  • delete dmpPacketBuffer, permanently add dmpPacketSize.
  • move the added variables so that they are only appended to the class. This should prevent the .cpp code from being misaligned (but that depends on how the compiler is assigning member variable indices, and that is something we should not know nor assume anything about).
  • Make MPU6050 a 'only .h' class ala boost. (i.e move the MPU6050.cpp code into MPU6050.h)

P.S. dmpPacketBuffer is never assigned a value, it's just a random pointer of doom.

Edit: Just to test this I added this code to MPU6050.cpp:

void MPU6050::initialize() {
    ....
    Serial.println(); Serial.println("MPU6050::initialize()");
    Serial.print("this=");Serial.println((uint32_t)this, HEX);
    Serial.print("&devAddr=");Serial.println((uint32_t)&devAddr, HEX);
    Serial.print("&buffer=");Serial.println((uint32_t)&buffer, HEX);
    Serial.println();
    ...
}

and this to MPU6050_6Axis_MotionApps20.h:

uint8_t MPU6050::dmpInitialize() {
    ...
    Serial.println(); Serial.println("MPU6050::dmpInitialize()");
    Serial.print("this=");Serial.println((uint32_t)this, HEX);
    Serial.print("&devAddr=");Serial.println((uint32_t)&devAddr, HEX);
    Serial.print("&buffer=");Serial.println((uint32_t)&buffer, HEX);
    Serial.print("&dmpPacketBuffer=");Serial.println((uint32_t)&dmpPacketBuffer, HEX);
    Serial.print("&dmpPacketSize=");Serial.println((uint32_t)&dmpPacketSize, HEX);
    Serial.println();

Here are the results:

MPU6050::initialize()
this=1DC
&devAddr=1D6
&buffer=1D7
....

MPU6050::dmpInitialize()
this=1DC
&devAddr=1DA
&buffer=1DB
&dmpPacketBuffer=1D6
&dmpPacketSize=1D8

So that's why dmpPacketBuffer is never assigned a value, it is overwriting MPU6050.cpp's devAddr

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions