ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Class member with subscriptor does not update

asked 2016-04-29 12:28:16 -0500

pgarcia gravatar image

updated 2016-04-29 13:56:52 -0500

ahendrix gravatar image

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 {
      ros::NodeHandle n;
      ros::Subscriber sub;
      float x;

      void callBack_B(const type_1 msg){

      B() {
        sub=n.subscribe("/topic_B", 1, &B::callBack_B, this);

class A {
      ros::NodeHandle n;
      ros::Subscriber sub;
      ros::Publisher pub;
      float y;
      type_2 z;
      B B_member;

      void main_process_A(const type_3 msg){
        y = = B_member.x + y;

      A() {
        sub=n.subscribe("/topic_A", 1, &A::main_process_A, this);

int main(int argc, char **argv)

  ros::init(argc, argv, "my_node");

  A A_member;

  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!!

edit retag flag offensive close merge delete



That looks pretty reasonable and should probably work. Are you sure callBack_B is actually being called?

ahendrix gravatar image ahendrix  ( 2016-04-29 13:56:27 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-05-03 06:52:10 -0500

pgarcia gravatar image

Hello Ahendrix

Thank you very much for your answer. The code above actually works. The problem was in another part of the code: the topic /topic_B was not actually being published continuously. That is why B_member.x was not updated.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-04-29 12:28:16 -0500

Seen: 285 times

Last updated: May 03 '16