Multiple objects (with multiple node handles?)

asked 2016-07-12 16:45:15 -0600

Pototo gravatar image

updated 2017-08-25 14:26:25 -0600

130s gravatar image

Folks,

I am using openni_tracker obtain the number of users inside my Kinect view. From another node, subscribed to openni_tracker, I want to create a user object. When I create the object, it initializes its own node handle, and it's own subscribers/publishers and call back functions etc. But when a user disappears, I want to destroy the respective object. Spawning a new ROS node per new person is not fast enough because I need to do other things like gesture and face recognition, etc., which make the spawning/shutdown of a node slow. 1) Can I create several objects, each with it own node handle? I was thinking of using a callback subscribe to openni_tracker, from within my "people detected" callback function, and append a new object to a list or vector of people, sort of like this:

// a UserClass object has it's own node handles and subscribers/publishers
vector<UserClass> users;

void chatterCallback(const openni::People::ConstPtr& msg)
{
   // check if a user disappear, and erase from vector/list
         //....write code

   // check if new user came in
   if (msg->new_user)
  {
     users.push_back(UserClass(msg->new_user_id));    // or something similar
  }
}

int main(int argc, char** argv)
{ 
  ros::init(argc, argv, "v_recognize_gesture");
  ros::NodeHandle n;
  ros::Rate loop_rate(2);
  ros::Subscriber sub = n.subscribe("people", 1000, chatterCallback);
  while (ros::ok())
  {
    ros::spinOnce();
    loop_rate.sleep;
  }
  return 0;
}

2) Is it better to pass a global my main function's node handle into each UserClass object? 3) Would it be good to create a boost::thread pointer that can some how create a new object every time?

Hope my question is not too confusing

edit retag flag offensive close merge delete