ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Publishing and subscribing to an array and store it (in the right order)

asked 2014-09-28 17:31:32 -0600

Andromeda gravatar image

updated 2014-09-29 13:30:08 -0600

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

edit retag flag offensive close merge delete

Comments

Can you edit your question to add the main function?

mateus03 gravatar image mateus03  ( 2014-09-29 13:01:22 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-09-30 04:42:28 -0600

mateus03 gravatar image

updated 2014-09-30 08:19:52 -0600

@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.

edit flag offensive delete link more

Comments

Ok I got it now.

Andromeda gravatar image Andromeda  ( 2014-09-30 15:02:20 -0600 )edit
1

answered 2014-09-29 03:53:12 -0600

BennyRe gravatar image

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.

edit flag offensive delete link more

Comments

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?

Andromeda gravatar image Andromeda  ( 2014-09-29 12:06:15 -0600 )edit
1

Arrays in messages are implemented with std::vector. Just google how to copy a vector.

BennyRe gravatar image BennyRe  ( 2014-09-30 00:24:48 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-09-28 17:31:32 -0600

Seen: 1,449 times

Last updated: Sep 30 '14