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

Servo control arduino [closed]

asked 2017-08-31 02:40:20 -0500

Yogi_4 gravatar image

updated 2017-08-31 11:43:21 -0500

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"
#else
  #include <WProgram.h>
#endif

#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){
  ang=radiansToDegrees(cmd_msg.position[0]);
  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);
  nh.getHardware()->setBaud(115200);
  nh.initNode();
  nh.subscribe(sub);

  servo.attach(9); //attach it to pin 9
}

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

double radiansToDegrees(float position_radians)
{

  position_radians = position_radians + 1.6;

  return position_radians * 57.2958;

}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Yogi_4
close date 2017-09-01 23:57:41.709257

Comments

@Yogi_4 may you help me for this ?

lagankapoor gravatar image lagankapoor  ( 2018-01-15 03:07:40 -0500 )edit

What is the issue? @lagankapoor

Yogi_4 gravatar image Yogi_4  ( 2018-01-16 17:24:04 -0500 )edit

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

lagankapoor gravatar image lagankapoor  ( 2018-01-17 23:07:52 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-09-01 00:15:13 -0500

Yogi_4 gravatar image

I got it solved issue was with baud rate

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-08-31 02:40:20 -0500

Seen: 544 times

Last updated: Sep 01 '17