Rosanswers logo

I want to control a servo attached to arduino by subscribing to /joint_states topic so that when the change the slider position the servo moves accordingly. I tried with the following code but the servo doesn't change its position when i change the slider position on rviz. The led on Arduino blinks as written in the callback function but the servo isn't moving.

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

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

ros::NodeHandle  nh;
Servo servo;

double ang=0;

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

ros::Subscriber<sensor_msgs::JointState> sub("joint_states", servo_cb);

void setup(){
  pinMode(13, OUTPUT);
  servo.attach(9); //attach it to pin 9

void loop(){

double radiansToDegrees(float position_radians)

  position_radians = position_radians + 1.6;

  return position_radians * 57.2958;


Originally posted by Yogi_4 on ROS Answers with karma: 38 on 2017-08-31

Post score: 0

Original comments

Comment by lagankapoor on 2018-01-15:
@Yogi_4 may you help me for this ?

Comment by Yogi_4 on 2018-01-16:
What is the issue? @lagankapoor

Comment by lagankapoor on 2018-01-18:
I have one Lynxmotion Arm and i want to use that with MoveIt , if any more detail needed please visit my new question that i post today


1 Answer 1


Rosanswers logo

I got it solved issue was with baud rate

Originally posted by Yogi_4 with karma: 38 on 2017-09-01

This answer was ACCEPTED on the original site

Post score: 0


Your Answer

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