0

I'm coding a plugin for XPLANE10 which gets a MSG from ROS. My IDE is QTcreator 4.1.0 based QT 5.7.0 for Ubuntu 64 Bit. I would like to use C++11 Standards

My code explained

The main initializes ROS and creates a map -> container. ROS spins in a loop till my GUI sends a MSG where my AirPlane should fly.

The MSG contains 3 floats(phi, theta, psi) where "phi" is the AirPlane ID, theta contains the ID for my ETA(Estimated Time of Arrival) and psi contains the ID for my pose All of the IDs are saved in the ParameterServer(lookuptable). So at the beginning i look up the activeAirplanes which returns a vector . I would like to store them in a map where the key is the AirCraft ID and the second param is an instance of the Object.

So i have initialized the for example(looked in container while debugging):

[0] first = 1 // Airplane ID1
[0] second = new CObject(freq)
[1] first = 2 // Airplane ID2
[1] second = new CObject(freq)

If i get a MSG from GUI

phi = 1
theta=2
psi=3

, ROS will callback

MSG(....std::map<i32, CObject> &container)

// if phi is 1 so use the mapkey 1 and trigger the method do_stuff from CObject

do_stuff(phi, theta, psi,freq)

I would like to call the in a function from main

int getPlanes(std::map<i32,CObject>& container)
{
...
getActiveAirplanesFromServer(activePlanes);
}

First Question: How do i pass the container to my callback?

Second Question: How do i parallelize do_stuff() so my callback will return to main and i'm able to command more aircrafts while the others are calculated?

Third Question: How would be the correct syntax for getPlanes to pass the container by reference so getPlanes() can edit it?

Fourth Question: Is there a difference between

std::map<i32,CObject*> map 
std::map<i32,CObject>* map

and

std::map<i32,CObject*>::iterator it=container->begin();
  std::map<i32,CObject*>::iterator* it=container->begin();

If yes, what do i want ? #4Solved

// I have to edit stuff 'cause of some restrictions in my company. 
#include "Header.h"
int main()

{
  f64 freq = 10;
  std::map<i32, CObject>* container;
  std::map<i32,CObject>::iterator* it=container->begin();


  // 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=new ros::NodeHandle;
  ros::AsyncSpinner spinner(2);
  ParameterServer * ptrParam= new ParameterServer(mainNodeHandle);
  ros::Subscriber airSub=mainNodeHandle->subscribe<own_msgs::ownStruct>("/MSG",
                                                                             1000,
                                                                             boost::bind(MSG,
                                                                                         _1,
                                                                                         freq,
                                                                                         container));

  std::vector<i32> activePlanes;
  i32 retVal=0;
  retVal += ptrParam->  ParameterServer::getActiveAirplanesFromServer(activePlanes);
  if (retVal == 0 && activePlanes.size()>0)
    {

      for (u32 j =0; j <activePlanes.size(); j++)
        {

          container->insert (std::pair<i32,CObject> (activePlanes[j] , new CObject(freq)));


        }
    }

  while (ros::ok())
    {
      spinner.start(); //spinnt sehr viel :-)
      ros::waitForShutdown ();
    }
  std::cout<<"ENDE"<<std::endl;
  int retval = 1;
  return retval;
}

void MSG(const own_msgs::ownStruct<std::allocator<void> >::ConstPtr &guiMSG,
                                     f64 freq,
                                     std::map<i32, CObject> &container)
{



  if ((guiMSG->phi != 0) && (guiMSG->theta != 0) && (guiMSG->psi != 0))
    {
      std::string alpha = std::to_string(guiMSG->phi)+std::to_string(guiMSG->theta)+to_string(guiMSG->psi);    
    container.at(guiMSG->phi) -> do_stuff(guiMSG->phi,guiMSG->theta,guiMSG->psi, freq);
    }
  else
    {
      std::cout<<" Did not receive anything\n"<<endl;

    }

}


void do_stuff(...)
{
//copy the IDs to private Member of this single Object 
//setROS() for this single Object 
//callback the current AC pose via ID from XPLANE
//callback the wished AC pose via ID from ParamServer
// do some calculations for optimum flight path
// publish the Route to XPlane

}

EDIT:: Problem is i get it to compile now and if debug it and set a breakpoint at :

void MSG(const own_msgs::ownStruct<std::allocator<void> >::ConstPtr &guiMSG,f64 freq,std::map<i32, CObject*> &container) 
{
..
/*->*/ container.at(guiMSG->)... 
}

The Container remains empty.

  • 4
    First of all I suggest you [find a beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) because you have many beginners errors in your code. Reading a book will teach you the difference between `std::map` and `std::map*`. – Some programmer dude Nov 22 '16 at 10:27
  • Why do you need a `std::map*`? – CinCout Nov 22 '16 at 10:28
  • I tried something. Didnt get the pass-by-reference with the map working. And iam not allowed to declare the map global. – MightyMirko Nov 22 '16 at 10:37
  • 1
    @Someprogrammerdude I started to code some weeks ago. Could you point out my errors? The pointer to map is unnessecary. I turned on my brain and solved the question on my own :-) – MightyMirko Nov 22 '16 at 11:56

1 Answers1

0

So i read some stuff about pointers and i saw my errors..

I confused * and & if i want to pass the adress of a variable i have to write like

int main()
{
int a = 0;
AddTwo(&a)
cout<<a<<endl; // Output: 2 
}
void AddTwo(int* a)
{
a+=2;
}
  • 1
    This increases your pointer by 2 - which means that the address to which the pointer points changes by two, not the content of the current address. Not that it matters, since the copy of the pointer dies with the function call. Either go with dereferencing, *a += 2, or go with a call by reference, AddTwo(int& a). Seems to me that you should start with the basics, like http://www.cplusplus.com/doc/tutorial/pointers/ . That said, the usual standard with C++ is to avoid pointers whenever possible. Rather create containers which handle the pointers than to use them directly. – Aziuth Nov 23 '16 at 09:51
  • 1
    To elaborate on "the pointer dies", see, int* a is an address that points to a place where an integer is stored. Let's say the address is 1337. What you now do is to give the function AddTwo the value 1337 to do something with it. It takes this value and adds 2, which is 1339. At this point, we have no idea what at position 1339 in the memory is stored, could be anything, and if you try to access it, it will most likely result in an error (only valid situation is when we work with arrays). Then the function ends and the local variable a that stores 1339 is deleted. – Aziuth Nov 23 '16 at 09:57