Last active
April 19, 2024 00:50
-
-
Save alexsleat/1372845 to your computer and use it in GitHub Desktop.
ROS Publish/Subscribe Arrays Example - http://alexsleat.co.uk/2011/07/02/ros-publishing-and-subscribing-to-arrays/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
@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
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