Rosanswers logo

Hello everybody,

i wanted to know is there any way out by which i can 2 or more servos at a time from a single subscriber. Currently i am running one servo on a single channel of arduino Due. But i require two servos to be run at a same time by giving them different angle values. like;

rostopic pub Servo1 std_msgs/Int16 230; 

this passes 230 degree to the servo attached. But i want to control two at a time like;

rostopic pub Servo1 std_msgs/Int16 230 130 240;

230 for 1st servo; (degrees) 130 for 2nd servo; (degrees) 240 for 3rd servo...(degrees)

I would appreciate if someone puts a light on this issue.


Originally posted by sonny on ROS Answers with karma: 33 on 2014-03-29

Post score: 3


2 Answers 2


Rosanswers logo

You've got two options. Most straightforward would be a basic custom message with an array. This answer may help. Second option which is more elegant and involves less code is a MultiArray.

Originally posted by Ryan with karma: 3248 on 2014-03-29

This answer was ACCEPTED on the original site

Post score: 2

Original comments

Comment by s1 on 2015-09-22:
can anyone give an example of MultiArray usage please.


Rosanswers logo

here is an example using MultiArray

#if (ARDUINO >= 100)
 #include <Arduino.h>
 #include <WProgram.h>

#include <Servo.h> 
#include <ros.h>
#include "std_msgs/MultiArrayLayout.h"
#include "std_msgs/MultiArrayDimension.h"

#include "std_msgs/UInt16MultiArray.h"

ros::NodeHandle  nh;

Servo servo1;
Servo servo2;

void servo_cb( const std_msgs::UInt16MultiArray&  cmd_msg){
  servo1.write(cmd_msg.data[0]); //set servo angle, should be from 0-180  
  digitalWrite(13, HIGH-digitalRead(13));  //toggle led  

ros::Subscriber<std_msgs::UInt16MultiArray> sub("servo", servo_cb);

void setup(){
  pinMode(13, OUTPUT);


  servo1.attach(9); //attach it to pin 9
  servo2.attach(10);//attach it to pin10

void loop(){


 rostopic pub servo std_msgs/UInt16MultiArray '{data: [<angle1>, <angle2>]}'

Originally posted by Chuong Nguyen with karma: 136 on 2015-10-12

This answer was NOT ACCEPTED on the original site

Post score: 3

Original comments

Comment by s1 on 2015-10-16:
thank you!!!

Comment by s1 on 2015-12-23:
is it possible to delay servo2 so that I can time the action? In other words I dont want all of the servos to fire off at once.

Comment by Frank Lee on 2017-09-05:
Maybe consider publishing the message to servo 2 separately, delay accounted for?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.