0
$\begingroup$

Rosanswers logo

Hi Guys, the question is not so easy as it could sound.

In my program I have a class which has the following member function:

/* class #1 */
void Waypoint::updateWaypoints( const ros::TimerEvent& event ) {

/* publish the markers */
marker_pub.publish( marker_array );
ros::spinOnce();
}

where marker_array is a defined Array of markers and is going to be published at a given frequency (to make them visible on RViz), repeatedly.

In a separate class I have defined another function (see next function) which should read the same published array from the first element to the last only ONCE!!!. Now: since both classes are started in the main routine I cannot control how to PC will run them. So it could happen that the function defined in the second class could take, for istance, the 2nd value, read until the last and then read it again. This must not happen.)

/* class #2: different from #1 */
...
ros::Subscriber array_sub = subscribe ("marker_topic", 5, &function, this );
...
visualization_msgs::MarkerArray incoming_array;
...
void classname::function( const visualization_msgs::MarkerArray& msg ) {

   if( !done ) {
    incoming_array = msg; // <= ?!?!?!?!? Here I am not really sure
    done = true;                // Do it only once
  }
}

EDIT: Here is the main routine. It calls both classes and let the them to run the code above:

#include "include/header_class1.h"
#include "include/header_class2.h"

int main( int argc, char **argv ) {

ros::init( argc, argv, "main_routine" );

ros::NodeHandle nh;
ros::NodeHandle nh_private( "~" );

/* publish the waypoints */
class1( nh, nh_private );

/* should read the wayoints from the previous class */
class2( nh, nh_private );
ros::spin();
return 0;
}

The problem is that I can do it whit normal vectors or other array but with not with markers. At last I am not really happy with the flag variable done that is going to be set once it has all data from the array. Im sure there are much better implementation of such a function. Any idea?!?

Regards


Originally posted by Andromeda on ROS Answers with karma: 893 on 2014-09-28

Post score: 0


Original comments

Comment by mateus03 on 2014-09-29:
Can you edit your question to add the main function?

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

@Andromeda. BennyRe is right. If you check the documentation for the visualization_msgs::MarkerArray, it contains only one field, Marker[] markers. This field is a vector of markers to store it just declare a std::vector of that type.

/* class #2: different from #1 */
...
ros::Subscriber array_sub = subscribe ("marker_topic", 5, &function, this );
...
std::vector<visualization_msgs::Marker> incoming_array;
...
void classname::function( const visualization_msgs::MarkerArray& msg ) {

if( !done ) {
  incoming_array = msg->markers; 
  done = true; 
 }
}

With that modification it should work.


Originally posted by mateus03 with karma: 73 on 2014-09-30

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Andromeda on 2014-09-30:
Ok I got it now.

$\endgroup$
0
$\begingroup$

Rosanswers logo

I'm not sure if I understood your problem correctly.

But if( done = false ) { is missing a =.

To avoid the done variable use the shutdown() member function of the subscriber class.


Originally posted by BennyRe with karma: 2949 on 2014-09-29

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by Andromeda on 2014-09-29:
jes, thanks...I forgot one = Your idea with the shutdown()) is pretty cool. I didn tknow it. But how to "copy" an array from the first element to the last?

Comment by BennyRe on 2014-09-30:
Arrays in messages are implemented with std::vector. Just google how to copy a vector.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.