OOP Problem and Kalman Filtering

Hi all. I am quite new to OOP and I have a little knowledge on Arduino coding. Lately, I have been trying to execute a program for my project which uses Kalman filter. I have made a separate C++ header to implement the filter. I succeeded to some extent when I encountered a small issue. I am trying to simplify the problem I am facing by giving the following code (Just like my C++ header for Kalman filter). I have made a Test class and it has two methods as you can see below. The problem is in my case, I have to use variable of one function in the other [just like I am using bias_X in the method get_Y_Angle() ]. Moreover, I have made two objects and I am calling the functions from loop() like this:

double Angle_X = Kal_X.get_X_Angle();
double Angle_Y = Kal_Y.get_Y_Angle();

where Kal_X and Kal_Y are the two objects created. Now if I do something like the below code, I always get Angle_X = 1, 2, 3.... etc and Angle_Y = 0 always. I know that this is happening because of two objects created. If I use only one object then I get answers (Well there are other problems with this which I am not mentioning now !!!). My question is pretty straight forward:

How can I use variable of one method in the other one when I am calling both the methods using two different objects ?

class Test {
public:
Test() {

bias_X = 0;
bias_Y = 0;
};

public : double get_X_Angle() {

bias_X++;
return bias_X;
};

public : double get_Y_Angle() {

bias_Y += bias_X;
return bias_Y;
};

public:

double bias_X;
double bias_Y;

};

I am not understanding your question.

First, why are bias_X and bias_Y public? The whole point of encapsulation is to not do this.

Anyway, if you create two objects of type Test, they each have their own bias_X and bias_Y.

Sorry for the mistake. In my actual code for the filter, bias_x and bias_y are private. I was trying out something and posted it like that !

Anyways, if i want to use bias_x in get_Y_Angle() method, what should be done ? I understand that I am making two objects of type Test, but in practice, the value of bias_x is updated in get_X_Angle() but I want to use this value in get_Y_Angle() method.

As I mentioned initially, I am not quite familiar with OOP !!!

If OO and arduino interests you, have a look at my page on arduino OO programming.

If you have a class that needs to talk to some other instance, then supply it with a reference:

class Kal {
public:
  Kal &otherKal;
};

Kal kal_a, kal_b;

void setup() {
  kal_a.otherKal = kal_b;
  kal_b.otherKal = kal_a;
}

Alternatively, you could have a class 'kal pair" whose job it is to manage this stuff. It might make sense to have Kal as an inner class of this pair:

class KalPair {

  class Kal {
    Kal &getOtherKal() {
      getOtherKal(this);
    }
  }
  kal_a, kal_b;

  Kal &getOtherKal(Kal &k) {
    if(k == kal_a) 
      return kal_b;
    else
      return kal_a;
  }
};

There'll be a lot of mucking about with forward declarations, of course.

If you dont want to use an inner class, you could make a class whose job it is to manage a group (in this case two) Kals. It supplies methods that allow a kal to ask 'give me some other kal in the group that has a particular role with respect to me".

class KalCoordinator {
 Kal &kal_a, &kal_b, &special;

 KalCoordinator(Kal &kal_a, Kal &kal_b, Kal &special) :
   kal_a(kal_a), kal_b(kal_b), special(&special) {
   kal_a.coordinator = this;
   kal_b.coordinator = this;
   special.coordinator = this;
 }

 Kal &getOtherKal(Kal &k) {
   if(k == kal_a) 
     return kal_b;
   else if(k == kal_b) 
     return kal_a;
   else
     return k;
 }

 Kal getSpecialKal() {
   return special;
 }
};

class Kal {
public: // or use a friend declaration
 KalCoordinator coordinator;
};


Kal a,b,special;
KalCoordinator(a,b,special);

Again, much fooling about with forward declarations etc until you can make the compiler understand what you are trying to do.

But there's a couple of techniques you can use. You have to decide what's best.

invisible_13:
How can I use variable of one method in the other one when I am calling both the methods using two different objects ?

I am not entirely sure what you are asking but. You can share a class variable between all instances of the same class by declaring it as a static class member. In OOP parlance, it is sometimes called a, 'shared property'.

You can read my own aide-memoir article on my knowledge base, which may or may not make sense to you.

However, I would recommend getting your head around simple class instances before progressing to static members.