Class member with subscriptor does not update
Hi everybody!
I'm having some problems with a simple program. There is a main class A, which uses a second class B. Both of them are subscribed to different topics. I simplified both of them:
class B {
private:
ros::NodeHandle n;
ros::Subscriber sub;
public:
float x;
void callBack_B(const type_1 msg){
x=msg.data
}
B() {
sub=n.subscribe("/topic_B", 1, &B::callBack_B, this);
x=0;
}
};
class A {
private:
ros::NodeHandle n;
ros::Subscriber sub;
ros::Publisher pub;
public:
float y;
type_2 z;
B B_member;
void main_process_A(const type_3 msg){
y = msg.data
z.data = B_member.x + y;
pub.publish(z);
}
A() {
sub=n.subscribe("/topic_A", 1, &A::main_process_A, this);
y=0;
}
};
int main(int argc, char **argv)
{
ros::init(argc, argv, "my_node");
A A_member;
ros::spin();
return 0;
}
I hope I did not make any mistakes in this code; anyway my code is similar and compiles without any problem.
The thing is that B_member.x does not update! Its value is always the same! What am I doing wrong? I know one solution would be to merge A and B in the same class, but I would like to understand how ROS works...
Thank you very much in advance!!
That looks pretty reasonable and should probably work. Are you sure callBack_B is actually being called?