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

How to program Ros Arduino with Multiple publishers and subscribers ?

asked 2021-10-08 10:20:22 -0600

Rakee003 gravatar image

updated 2021-10-09 03:17:27 -0600

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!

edit retag flag offensive close merge delete

Comments

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.

osilva gravatar image osilva  ( 2021-10-08 13:56:14 -0600 )edit

I updated my question can you help me @osilva

Rakee003 gravatar image Rakee003  ( 2021-10-09 03:18:06 -0600 )edit

need a .ino arduino program which can able to subscribe all the 12 topics

The tutorial Blink (example subscriber) shows how to create a subscriber. Also see #q10645

abhishek47 gravatar image abhishek47  ( 2021-10-09 05:09:14 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-10-09 05:21:04 -0600

Dragonslayer gravatar image

Hi, here are some links to rosserial_arduino usecases. collection servo example

And here rosserial_python for the PC side. rosserial_python

However, rosserial in my experience is slow, Iam not sure how many Hz you will get with 12 uint16 topics. Also it depends on what "Arduino", not all can handle it. Also the baud rates are low and can give you trouble.

Have a try, good Luck.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-10-08 10:20:22 -0600

Seen: 664 times

Last updated: Oct 09 '21