Push vector into `MultiArray`-Message and publish it
I simply try to push multiple vector<double>
into the message format, but can not figure it out also I looked at this and this.
std_msgs::Float64MultiArray msg;
vector<double> vec1 = { 1.1, 2., 3.1};
vector<double> vec2 = { 3., 4.1, 2., 3.1};
vector<double>::const_iterator itr, end(vec.end());
for(itr = vec.begin(); itr!= end; ++itr) {
// how to use the pointer *itr here? to push vec1 and vec2 into msg ?
}
//I also want to specify the layout and tried to use some of these lines:
msg.data.clear();
msg.layout.dim.push_back(std_msgs::MultiArrayDimension());
msg.layout.dim[0].size = *da;
msg.layout.dim[0].stride = 1;
msg.layout.dim[0].label = "typ i";
Edit: Solution
So as said below one cannot push multiple arrays(vector is basically 1D-array) into one message as originally thought. So it does not behave like a container object for multiple variables but only holds one i-D array.
To fix this I could sum up all vectors into one 2-D array and push it into the msg
:
The title MultiArray
is quite misleading then, but once known its ok.
(I could not get to work with data.extend(..)
sorry for the typo. updated my answer with the correct field name.
I confused the python
extend()
with the C++ vector'sinsert()
. Answer updated and should work now. Note that insert() is faster than an element-by-element push, because it only expands the size of the vector once.Explanations on
insert()
can be found here, furthermore this leads back to pushing multiple vectors inside the array: make all have samevec.size()
and specify it in the.stride
. So each row represents one vector.