Skip to content

Instantly share code, notes, and snippets.

@alexsleat
Last active April 19, 2024 00:50
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save alexsleat/1372845 to your computer and use it in GitHub Desktop.
Save alexsleat/1372845 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include "ros/ros.h"
#include "std_msgs/MultiArrayLayout.h"
#include "std_msgs/MultiArrayDimension.h"
#include "std_msgs/Int32MultiArray.h"
int main(int argc, char **argv)
{
ros::init(argc, argv, "arrayPublisher");
ros::NodeHandle n;
ros::Publisher pub = n.advertise<std_msgs::Int32MultiArray>("array", 100);
while (ros::ok())
{
std_msgs::Int32MultiArray array;
//Clear array
array.data.clear();
//for loop, pushing data in the size of the array
for (int i = 0; i < 90; i++)
{
//assign array a random number between 0 and 255.
array.data.push_back(rand() % 255);
}
//Publish array
pub.publish(array);
//Let the world know
ROS_INFO("I published something!");
//Do this.
ros::spinOnce();
//Added a delay so not to spam
sleep(2);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include "ros/ros.h"
#include "std_msgs/MultiArrayLayout.h"
#include "std_msgs/MultiArrayDimension.h"
#include "std_msgs/Int32MultiArray.h"
int Arr[90];
void arrayCallback(const std_msgs::Int32MultiArray::ConstPtr& array);
int main(int argc, char **argv)
{
ros::init(argc, argv, "arraySubscriber");
ros::NodeHandle n;
ros::Subscriber sub3 = n.subscribe("array", 100, arrayCallback);
ros::spinOnce();
for(j = 1; j < 90; j++)
{
printf("%d, ", Arr[j]);
}
printf("\n");
return 0;
}
void arrayCallback(const std_msgs::Int32MultiArray::ConstPtr& array)
{
int i = 0;
// print all the remaining numbers
for(std::vector<int>::const_iterator it = array->data.begin(); it != array->data.end(); ++it)
{
Arr[i] = *it;
i++;
}
return;
}
@cipri-tom
Copy link

Hi,

Thank you for the tutorial! Would you mind explaining what is the difference between an array and a multiArray ? When is one preferred over the other?

Also, what do you think would be a good way of publishing an array of geometry_msgs::Point ? 😄
Many thanks!

@merium
Copy link

merium commented Apr 28, 2016

Hi,

Just wanted to add one thing, the code wasn't working for me until I changed ros::spinOnce(); to ros::spin for Subscribe.cpp.

Thanks!

@sami-s-hajjaj
Copy link

@cipri-tom .. the way i understand it is ... an array is a one dimensional list of items ... say from 0 to 10, and each item can be found using Arr[i] ... but a multiArray is a multidimensional array ... something like this

MultiArray= 0 | 1 | 3 | 4 | 5
---------------------
6 | 7 | 8 | 9 | 10

and so an item in this is array is found using Arr[i,j] coordinates .. so Arr[0,0] = 0, Arr[0,1] = 6, and so on .... i hope i got it right

@rdockterjr
Copy link

Line 33 of publisher.cpp should be
pub.publish(array);

@rahuldeo2047
Copy link

rahuldeo2047 commented Apr 25, 2018

In case I am running only the subscriber.
Please help me with the command line ros tool rostopic

$rostopic pub /bot/test/massrange std_msgs/Float32MultiArray "layout:
  dim:
  - label: min_step_max_time
    size: 4
    stride: 1
  data_offset: 0
data: [0.0 1.0 2.0 3.0]" 

Aiming to send
float var[4] = {0.0, 0.1, 0.2, 0.3};

getting following warning:

[WARN] [1524667914.429192]: Inbound TCP/IP connection failed: <class 'struct.error'>: 'required argument is not a float' when writing 'label: "min_step_max_time"
size: 4
stride: 1'

@prabinrath
Copy link

Hi,

Just wanted to add one thing, the code wasn't working for me until I changed ros::spinOnce(); to ros::spin for Subscribe.cpp.

Thanks!

Of course spinOnce is used inside an infinite loop.

@ChunJyeBehBeh
Copy link

@rahuldeo2047 Have you found out how to subscibe to that topic and get the array data?

@emfhasan
Copy link

I would like to know how the message std_msgs/Int32MultiArray is defined. I mean how is the data is formatted or structured in such as Int32MultiArray.msg.

@Gbouna
Copy link

Gbouna commented Aug 10, 2020

Hi,
Thanks for this, its helping me a lot
I'm new to ROS and learning to interface it with Arduino. When I run the Publish.cpp code, I encounter an error in line 19, this line of code
ros::Publisher pub = n.advertise<std_msgs::Int32MultiArray>("array", 100);`

This is the error message
expected primary-expression before '>' token
I will appreciate help from anyone who has an idea.
Thanks

@diane-eng
Copy link

@Gbouna looks likes it's been a while, but did you happen to find a solution to this problem above? Currently stuck on the same thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment