Callbacks, Multithreading, spinning with complex class

asked 2016-11-18 11:47:28 -0500

MightyMirko gravatar image

updated 2016-11-18 12:15:51 -0500

Hello dear Community,

We do have an event at an Airport(small one) where we try to catch eggs thrown off some rc airplanes( 1 airplane = 6 Eggs). If we catch it we get points, if we catch it with our head we'll get more points. You have 10 minutes to catch all 6 eggs

Since i am studying mechatronics i decided to get a project with drones to win this event ->6 quadrocopter(each has one Egg due to weight ). Those Copter are able to release an egg while flying The copters are controlled by one pc. They fly some fix waypoints and my pc is getting messages from the "target"( we do have an gps-transmitter weared by the persons whom will catch the eggs).

My code works for one release. I would like to be able to release more than one egg via some kind of GUI . This GUI publishes messages on my topic and my code is able to receive them. My problem is in the main and in the callbacks. my main: (iam simulating the stuff in flightgear)

int main() {
    f64 freq = 10;                // setting the frequency
    std::vector<Eggs *> container // container for later purposes
    // ROS
    if (!ros::isInitialized()) 
     {
      int rosargc = 0;
      char **rosargv = NULL;
      ros::init(rosargc, rosargv,
        "MainNode"); //), ros::init_options::AnonymousName);
      }
      else {
        printf("Ros has already been initialized.....\n");
      }

      ros::NodeHandle mainNodeHandle("SurveillanceStarted");
      ros::AsyncSpinner spinner(2); // tried something with this
      ros::Subscriber releaseSub = mainNodeHandle.subscribe<egg_msgs::floatStruct>(
        "/releaseMSG", 100, boost::bind(releaseCallback, _1, freq,
          container)); // callback with arguments
          while (ros::ok())  // waiting for guiMSG
          {
            std::cout << "Spinning\n" << endl;
            ros::spin();
            // spinner.start(); spins a lot
            ros::waitForShutdown();
          }
          std::cout << "END" << std::endl;
          int retval = 1;
          return retval;
        }
      }

so the releaseCallback inits a pointer of my Class Eggs:

releaseCallback(const egg_msgs::floatStruct<std::allocator<void>>::ConstPtr &guiMSG,
        f64 freq, std::vector<Eggs *> container)
{
Eggs *pVecEgg = NULL;
pVecEgg = new Eggs(guiMSG->phi, guiMSG->theta, guiMSG->psi, freq);
pVecEgg->releasetheEgg; // here i would like to process this release in a other thread. I tried to do it with a vector}

Since the Target and the Egg has GPS Transmitter(in the sim) i can calculate the drop. Every Egg thrown should be a Object of Eggs and since the class has its own node, every egg will have its own node. To do this i implemented two callbacks

targetcallback(&rosmsg){copyMSG for calc and Visualization}
eggPoseCallback(&rosmsg){copyMSG for calc and Visualization }


Eggs::releasetheEgg 
{
while(ros::ok())
{
ros::spinOnce // target and EggPoseCallback
//Multispinner.spin ();
      if ( time_ <1.0)
        {
          do
            {
              initLaunch();
              time_+=1.0/frequency_; // private member stuff to control the object

            }while (targetConfirmed_==false);
          std::cout <<"Egg <<XXX<< prepare to be launched\t"<< endl;
        }   // if we have a target and my time_ >1.0 s release the stuff and do the math
      else
        {          
updateEgg(pose); // my publisher, which works. 
        }

}

If it hits the ground the C++Object is destroyed. The Class controls itself and has some bools to do this.

My problem now is: I ... (more)

edit retag flag offensive close merge delete