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

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

edit retag flag offensive close merge delete

Comments

2

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
0

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

Stats

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

Seen: 265 times

Last updated: May 03 '16