Is it possible to change 'object' halfway?

I am not sure what to call this problem, sorry for the unclear title. But I will make it clear by making an example the situation I am facing now.

#include "motor.h"

motor brushless(8, 9);

void setup()
{

}

void loop()
{

}

WHAT IF I want to change the numbers ‘8’ and ‘9’ to different numbers at certain period of time. I have tried ‘motor brushless(variable1, variable2);’ this kind of things but I am getting error.

What happens depends on how that motor library is written.

You could create a dynamic motor object using new() and then delete() this, creating a new one with your new pin numbers. Whether the library behaves properly (ie, they have implemented the class destructor properly for the resources they are using in the library) is a different question.

The answer lies in the implementation of the motor class.

In other words...

I am sorry I couldn't post the complete codes because it's strictly for a new research under an institution, else I have already posted it, and of course, it's not as simple as controlling motor. I just want to know how for similar situation. Anyway, I will describe more about what kind of things are inside the motor.h library. It's not the exact codes to grasp but the concept behind the problem.

//motor.h file

class motor
{
    public:
        motor(int speed, int direction);
    private:
        int _speed, _direction;
        void otherFunction();
};
//motor.cpp file

#include "motor.h"

motor::motor(int speed, int direction)
{
    _speed = speed;
    _direction = direction;
}

void motor::otherFunction()
{
}

So what should I do with the cpp and header files? Examples?

Accessor methods...

class motor
{
    public:
        motor(int speed, int direction);
    private:
        int _speed, _direction;
        void otherFunction();
    public:
        void set_speed( int value ) { _speed = value; }
        void set_direction( int value ) { _direction = value; }
};

I suspect you will have to add significantly more code to set_speed and set_direction.

I also suspect what you really need is a level of indirection...

motor brushlessA(8, 9);
motor brushlessB(10, 11);
motor * current_brushless;

void setup( void )
{
  current_brushless = & brushlessA;
}

void loop( void )
{
...
  if ( switch_to_B )
  {
    switch_to_B = false;

    if ( current_brushless != & brushlessB )
    {
      current_brushless->stop();
      current_brushless = & brushlessB;
      current_brushless->prepare_to_go();
    }
  }
...
}

Could you not create 2 instances of the motor object, each with their own pin definitions, and selectively use the required instance ? An array of motor objects would make this fairly easy.

Knowing why you want to change the pin allocations at different times would be helpful in providing advice.

Thanks for taking time to reply. Again, I am sorry that I couldn’t post the original code. And I think the examples I’ve given in my previous replies explain badly.

main.ino, no problem with this.

#include "motor.h"

motor alpha(3, 4, 5, 6); //different device use different pins

void setup()
{

}

void loop()
{
}

motor.h

#include <Arduino.h>
#include "sensor.h"

class motor
{
    public:
        motor(uint8_t a, uint8_t b, uint8_t c, uint8_t d);
        void init();
        void write();
    private:
        int pinA, pinB, pinC, pinD;
}

motor.cpp

#include "motor.h"

sensor encoder1(pinA, pinB); //here's the problem
sensor encoder2(pinC, pinD);  //here's the problem

motor::motor(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
{
    pinA = a;
    pinB = b;
    pinC = c;
    pinD = d; 
}

motor::write()
{
    encoder1.read(0xff); //here's the problem too. This read function is from sensor.h
    encoder2.read(0xfe); //here's the problem too. This read function is from sensor.h
}

For the “sensor.h” library, its another library with class, same as the “motor.h”. I know I can combine both libraries into one, but it will become more complicated. But if at the end no choice, then I will. But I to try for a simpler way first.

Roughly like this… Thank you and again, I apologize. I know it will be solved immediately if I posted the code, but I can’t. Apologies.

ElegantBlack:
Again, I am sorry that I couldn't post the original code.

This is an Open Source forum where people give their time for free.

If you are not prepared to be open it does not seem fair to seek advice here. Pay a professional and get him/her to sign an NDA.

...R

Robin2, I know and I appreciate. Again, if you understand my situation, you will know how much I wish too, but I can't. It's not merely just me and myself, it's a research developed by many seniors researchers for years and I got responsibility for them. Thank you.

Why does motor need to know about pinA through pinD? What does a motor instance do with that information?

Would giving motor references to sensors (encoders) make more sense?

Thank you for reply.

The motor.h needs to get the value of pinA and pass it to a. Because, the motor.h library is what I create, whereas the sensor.h is the library created by others.

giving references? Like how?

ElegantBlack:
it's a research developed by many seniors researchers for years and I got responsibility for them.

So ask their permission.

...R

@ElegantBlack

Apparently you cannot publish the code as you are not the (sole) owner (my interpretation)

Besides the solutions proposed allready (ask permission, go for pro & NDA, ...) you can create a new class that is called e.g. motor2 - that does not implement anything except the part that is problemantic.

That makes it easier to give advice and you can translate the solution back to the original problem class.

Is that an option for you?

@robtillaart

So means that I will need another library named motor2.h? Hmm, this doesn't solve the problem. My question is like, why is it not possible to declare object in .cpp file? Please refers motor.cpp at previous post.

Thank you.

ElegantBlack:
@robtillaart

So means that I will need another library named motor2.h? Hmm, this doesn't solve the problem. My question is like, why is it not possible to declare object in .cpp file? Please refers motor.cpp at previous post.

Thank you.

no, just create an example of your issue but with a basic set of classes not defined as in your proprietary code.

OP, so far there have been at least 4 solutions offered to you:

  1. Dynamically delete and allocate new motor objects as necessary.

  2. Statically create multiple motor objects and dynamically switch between them as necessary using pointers.

  3. Create your senor objects external to the motor objects and pass in that information via pointer or references.

  4. Pay someone to fix your code under NDA.

You have yet to respond as to why these possibilities will or will not work for you.