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

Run multiple servo on ROS with arduino.

asked 2014-03-29 02:04:41 -0600

sonny gravatar image

updated 2015-04-15 05:04:38 -0600

130s gravatar image

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.


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-03-29 05:44:20 -0600

Ryan gravatar image

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.

edit flag offensive delete link more


can anyone give an example of MultiArray usage please.

s1 gravatar image s1  ( 2015-09-22 07:46:30 -0600 )edit

answered 2015-10-12 10:31:50 -0600

Chuong Nguyen gravatar image

updated 2015-10-12 11:59:06 -0600

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([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>]}'
edit flag offensive delete link more


thank you!!!

s1 gravatar image s1  ( 2015-10-16 20:26:16 -0600 )edit

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.

s1 gravatar image s1  ( 2015-12-23 12:23:06 -0600 )edit

Maybe consider publishing the message to servo 2 separately, delay accounted for?

Frank Lee gravatar image Frank Lee  ( 2017-09-05 08:27:50 -0600 )edit

Question Tools



Asked: 2014-03-29 02:04:41 -0600

Seen: 2,678 times

Last updated: Oct 12 '15