Ask Your Question
0

Does function type of callback work in class?

asked 2013-11-15 09:45:08 -0500

AdrianPeng gravatar image

Hi everyone,

I am wondering if I can use function type of callback in a class?

This is my function type callback signature:

double current_joint_position[7];
double current_joint_velocity[7];
void controllerStateCB(const pr2_controllers_msgs::JointTrajectoryControllerState::ConstPtr& msg)
{
    for (int joint_index = 0; joint_index < 7; joint_index++)
    {
        current_joint_position[joint_index]=msg->actual.positions[joint_index];
        current_joint_velocity[joint_index]=msg->actual.velocities[joint_index];
    }
}

This is where I define the subscriber:

void move_group::MoveGroupMoveAction::executeMoveCallback_PlanOnly(const moveit_msgs::MoveGroupGoalConstPtr& goal, moveit_msgs::MoveGroupResult &action_res)
{
  ...
  ...
  ...
  ros::NodeHandle handle;
  ros::Subscriber sub = handle.subscribe("r_arm_controller/state", 1000, &controllerStateCB);
  ...
  ...
  ...
}

The function signature is in the same cpp file of the class.

However, it seems that the callback function is never called even though there is message in "r_arm_controller/state" topic.

Thanks for any help in advance!

Quan

edit retag flag offensive close merge delete

Comments

Thank you tbh, I just miss spin in my code.

AdrianPeng gravatar image AdrianPeng  ( 2013-11-17 07:13:14 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted
2

answered 2013-11-15 10:10:11 -0500

tbh gravatar image

Where is your spin? Callbacks won't happen until you reach a spin. So if your subscriber goes out of scope and is deleted before a spin happens, it won't do anything.

edit flag offensive delete link more
1

answered 2013-11-15 09:57:05 -0500

You can certainly have callbacks be class methods, but the subscriber instantiator requires an additional argument. This page on subscribers and publishers explains in more detail.

edit flag offensive delete link more
1

answered 2013-11-15 10:49:54 -0500

lindzey gravatar image

updated 2013-11-15 10:56:46 -0500

Yes, you can call a non-class callback from a subscriber that's within another class. As a minimal example, something like this works:

#include "ros/ros.h"
#include "std_msgs/Empty.h"

void emptyCallback(const std_msgs::Empty::ConstPtr& msg) {
  ROS_INFO("Got empty message!");
}

class Listener {
  ros::NodeHandle nh_;
  ros::Subscriber sub_;
public:
  Listener(): nh_("") {
    sub_ = nh_.subscribe("empty_msg", 1000, &emptyCallback);
    ROS_INFO("done constructing Listener!");
  };
  ~Listener() {};
};

int main(int argc, char ** argv) {
  ros::init(argc, argv, "test_cb_node");
  Listener my_listener = Listener();
  ros::spin();
  return 0;
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-11-15 09:45:08 -0500

Seen: 143 times

Last updated: Nov 15 '13