(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Servo Controller Example

Description: Tutorial for controlling an R/C servo with rosserial and an Arduino

Tutorial Level:

Next Tutorial: IR Ranger

  Show EOL distros: 

This tutorial explains how to control an R/C servo through ROS by using an Arduino and rosserial. This can be used to control a release mechanism, a cheap robot arm, ROS powered biped, or anything where you need a cheap actuator. The code provided is a very basic example and shows the control of a single hobby servo.



This example assumes that you have an Arduino and a hobby r/c servo. The r/c servo can be purchased from your local hobby shop, Towerhobbies, Sparkfun, etc.

The hobby servo r/c are great little actuators because they are relatively cheap (as low as $10) but contain a gear box and motor control electronics. They are controlled by sending a squarewave pulse of 1-2 milliseconds in width every 20 milliseconds. This typically moves the servo arm from 0-180 degrees. Hobby servo's come in a huge variety of sizes, torques, and angular precision.


The code for this tutorial is made extremely simple through the use of the Arduino Servo library. The Servo Library handles all of the low level control to generate and maintain the servo pulses. All your code needs to do is specify the pin the servo is attached to and then write the angle to the servo object. Underneath, the Servo library uses the Arduino's built in timer interrupts to generate the correct pulses. In this example, we only control one servo, but the same library can be used to control up to 12 servos on most Arduino boards and 48 on the Arduino Mega.

   1 /*
   2  * rosserial Servo Control Example
   3  *
   4  * This sketch demonstrates the control of hobby R/C servos
   5  * using ROS and the arduiono
   6  * 
   7  * For the full tutorial write up, visit
   8  * www.ros.org/wiki/rosserial_arduino_demos
   9  *
  10  * For more information on the Arduino Servo Library
  11  * Checkout :
  12  * http://www.arduino.cc/en/Reference/Servo
  13  */
  15 #if defined(ARDUINO) && ARDUINO >= 100
  16   #include "Arduino.h"
  17 #else
  18   #include <WProgram.h>
  19 #endif
  21 #include <Servo.h> 
  22 #include <ros.h>
  23 #include <std_msgs/UInt16.h>
  25 ros::NodeHandle  nh;
  27 Servo servo;
  29 void servo_cb( const std_msgs::UInt16& cmd_msg){
  30   servo.write(cmd_msg.data); //set servo angle, should be from 0-180  
  31   digitalWrite(13, HIGH-digitalRead(13));  //toggle led  
  32 }
  35 ros::Subscriber<std_msgs::UInt16> sub("servo", servo_cb);
  37 void setup(){
  38   pinMode(13, OUTPUT);
  40   nh.initNode();
  41   nh.subscribe(sub);
  43   servo.attach(9); //attach it to pin 9
  44 }
  46 void loop(){
  47   nh.spinOnce();
  48   delay(1);
  49 }

The key servo specific areas here are the fact that we made a global Servo object, attached to the correct arduino pin, and then on every servo topic call back, we write the servos new angle to the servo object.


First, startup your roscore and the rosserial python node in their own terminal windows.


rosrun rosserial_python serial_node.py _port:=/dev/ttyUSB0

rosrun rosserial_python serial_node.py /dev/ttyUSB0

Now, in a new terminal window, use rostopic pub to control your servo! Simply specify the angle from 0-180 and watch it move.

rostopic pub servo std_msgs/UInt16  <angle>

Wiki: rosserial_arduino/Tutorials/Servo Controller (last edited 2014-09-22 16:31:23 by AustinHendrix)