Ask Your Question

Revision history [back]

There are a few issues here (some of which you could probably avoid if you go through the ROS tutorials carefully ;) )

  • Advertise and subscribe should be done outside the while loop as otherwise publishers and subscribers get re-initalized every time.
  • When running a while ros::ok() loop, you should call ros::spinOnce() instead of spin() as the latter is a blocking call.
  • The euler_t vector only gets resized in the subscriber callback. As callbacks only get serviced when a spin (or spinOnce, see above) has been called, it is perfectly normal that your code trying to access elements of the vector crashes.
  • Even if you would call spin correctly, there is no guarantee that the callback filling euler_t is called before you try accessing elements of it in the while-loop. For this reason (and many others) it's a good idea to check if the vector really has the correct size before trying to access it's elements directly.
  • Instead of filling vectors in the callbacks you could also store the shared pointer to the last received message and then check if that one is non-zero wherever you try to use the data.