Can I inherit the MPU6050 class from the i2cdev external library and add methods of my own?

Im not that great at OOP so i needed some help. I wanted to take the logic that happens in this example and condense it to my own OOP where this logic occurs in methods such as setupMPU() and setRollPitchYaw()

So i thought of inheriting the MPU6050 class and adding all those variables to it and then adding these methods that call other methods which were in the original external library.
Basically Ill have mpu.h and mpu.cpp and in the header can I do this

#ifndef MPU_H
#define MPU_H

#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"
#include <Arduino.h>


class MPU : public MPU6050 {
  private:
    // MPU control/status vars
    bool dmpReady = false;  // set true if DMP init was successful
    uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
    uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
    uint16_t fifoCount;     // count of all bytes currently in FIFO
    uint8_t fifoBuffer[64]; // FIFO storage buffer
    
    // orientation/motion vars
    Quaternion q;           // [w, x, y, z]         quaternion container
    VectorFloat gravity;    // [x, y, z]            gravity vector
    float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
    

  public:
    void setupMPU();  // Wakes up MPU and gets it ready for reading data.
    void setData();
};


#endif

And what will my sketch have, should i include only the mpu.h or should I also include the external library?
And are there other ways to go about this and or will this work

You should research a bit; then go to experimenting.

"Arduino" "OOP" - Search (bing.com)

As you include the external header in your header file you don't have to include it in the sketch again.

We cannot tell you if this will work because we have only seen a header file. You even didn't explain your intentions in detail so we don't know what the task is you try to solve.
You can inherit from this class and use all methods and member variables that are declared protected (and public of course) in the derived class.

Thanks, I didnt really think i needed to explain further than this cuz the only thing i wanted to make sure of is if i can inherit from a class inherited an external library.

I only asked this cuz i saw it as an answer in another post that posted something similar to what im doing, This post

I didn't try but i hoped that this bug got fixed in the meantime. You have to include the original library's header if you do your own library but if the source files are part of your project files I thought that the IDE recognize that it has to link the library to the project. But I might be wrong with this.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.