How to program Ros Arduino with Multiple publishers and subscribers ?
I'm new to write code with ros node. I will explain my problem statement.
I have 12 different topics which is published by various publishers by a node. The topics will publish a servo angle in those 12 topics. Arduino program should have 12 subscribers at its end to recieve those angles. An array is created and all the subscribing values are saved and the servo motors are made to run using those values .
I dont know what data type to use and how to write multiple publisher node and multiple arduino .ino program. Help me in identifing a efficient way to solve this issue.
This is my code
#include "ros/ros.h"
#include "std_msgs/Int16.h"
#include <vector>
#include<iostream>
#include<cstdlib>
using namespace std;
int main(int argc, char **argv)
{
// ROS objects
ros::init(argc, argv, "servo_firmware");
ros::NodeHandle n;
ros::Publisher pub = n.advertise<std_msgs::Int16>("servo_angles1", 20);
ros::Publisher pubb = n.advertise<std_msgs::Int16>("servo_angles2", 20);
ros::Publisher pubbb = n.advertise<std_msgs::Int16>("servo_angles3", 20);
ros::Publisher pubbbb = n.advertise<std_msgs::Int16>("servo_angles4", 20);
ros::Publisher pubbbbb = n.advertise<std_msgs::Int16>("servo_angles5", 20);
ros::Publisher pubbbbbb = n.advertise<std_msgs::Int16>("servo_angles6", 20);
ros::Publisher pubbbbbbb = n.advertise<std_msgs::Int16>("servo_angles7", 20);
ros::Publisher pubbbbbbbb = n.advertise<std_msgs::Int16>("servo_angles8", 20);
ros::Publisher pubbbbbbbbb = n.advertise<std_msgs::Int16>("servo_angles9", 20);
ros::Publisher pubbbbbbbbbb = n.advertise<std_msgs::Int16>("servo_angles10",20);
ros::Publisher pubbbbbbbbbbb = n.advertise<std_msgs::Int16>("servo_angles11",20);
ros::Publisher pubbbbbbbbbbbb = n.advertise<std_msgs::Int16>("servo_angles12", 20);
ros::Rate loop_rate(1);
// the message to be published
std_msgs::Int16 msg;
// loop control
int count = 0;
int a=0;
while (ros::ok())
{
a=(rand() % 180) + 1;
msg.data =int16_t(a);
ROS_INFO("%d", int(msg.data));
pub.publish(msg);
pubbb.publish(msg);
pubbbb.publish(msg);
pubbbbb.publish(msg);
pubbbbbb.publish(msg);
pubbbbbbb.publish(msg);
pubbbbbbbb.publish(msg);
pubbbbbbbbb.publish(msg);
pubbbbbbbbbb.publish(msg);
pubbbbbbbbbbb.publish(msg);
pubbbbbbbbbbbb.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
This the cpp node i created for publishing various topics . I need a .ino arduino program which can able to subscribe all the 12 topics and store those in a array[12].
Thank you!
Hi @Rakee003 respectfully but your question is open ended so difficult to provide help. Please narrow it down to a more specific issue. Do you need help creating publishers? If so what kind of help? It's useful for the person helping to know what you have tried so far, what issues you are trying to resolve, etc.
I updated my question can you help me @osilva
The tutorial Blink (example subscriber) shows how to create a subscriber. Also see #q10645