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

ServoControl with two or more servos on arduino rosserial

asked 2015-06-30 15:34:36 -0600

andres1015415 gravatar image

*Hello friends. I want to make the example of Servo Control of Arduino, initially with two Servos, but have not successful. I tried to change the code this way but does not work.*

ros::NodeHandle nh;

Servo servo; Servo servo2;

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

ros::Subscriber<std_msgs::uint16> sub("servo", servo_cb); ros::Subscriber<std_msgs::uint16> sub("servo2", servo_cb); void setup() { pinMode(13, OUTPUT);

nh.initNode(); nh.subscribe(sub);

servo2.attach(7); servo.attach(9); }

void loop(){ nh.spinOnce(); delay(1); }

I hope someone can help me. Thanks for your time

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-10-12 10:00:41 -0600

Chuong Nguyen gravatar image

updated 2015-10-12 10:26:42 -0600

Here is an simple example:

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

#include <Servo.h> 
#include <ros.h>
#include <std_msgs/UInt16.h>

ros::NodeHandle  nh;

Servo servo1;
Servo servo2;

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

void servo2_cb( const std_msgs::UInt16&  cmd_msg){
  servo2.write(; //set servo angle, should be from 0-180  
  //digitalWrite(13, HIGH-digitalRead(13));  //toggle led  
ros::Subscriber<std_msgs::UInt16> sub1("servo1", servo1_cb);
ros::Subscriber<std_msgs::UInt16> sub2("servo2", servo2_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 servo1 std_msgs/UInt16  <angle1> & rostopic pub servo2 std_msgs/UInt16  <angle2>
edit flag offensive delete link more


Another Example uses MutiArray

Chuong Nguyen gravatar image Chuong Nguyen  ( 2015-10-12 10:32:40 -0600 )edit

have you a code o node that publish angle ?

Emilien gravatar image Emilien  ( 2016-05-03 13:44:21 -0600 )edit

Question Tools

1 follower


Asked: 2015-06-30 15:34:36 -0600

Seen: 887 times

Last updated: Oct 12 '15