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

control servo using sensor_msgs/JointState

asked 2015-08-13 08:17:43 -0600

Emilio gravatar image

updated 2015-08-15 12:18:45 -0600

I want to control some servos using sensor_msgs/JointState on Arduino UNO. I'm using Indigo. I write this code called move.cpp that i upload in the board.

ros::NodeHandle nh;

sensor_msgs::JointState giro;

Servo servo,servo1,servo2;

void servo_cb( const sensor_msgs::JointState &giro)






ros::Subscriber<sensor_msgs::jointstate> sub("servo", servo_cb);

void setup()


pinMode(13, OUTPUT);







void loop() {




If i try to control two servos with the same code without one of then servo.write that works well, But when i try it with three servos they don't work and any error occurs. Someone know what can be the problem?


edit retag flag offensive close merge delete


You don't provide enough information to get a useful answer. What error do you get?, is the failing component written by you or a third party?, what is your ROS setup (distro, binaries/source build)?

Adolfo Rodriguez T gravatar image Adolfo Rodriguez T  ( 2015-08-13 08:59:54 -0600 )edit

Sorry i just edit the post ! when i upload the code in the board and run rostopic pub i don't get any error message so i think that all is ok but servos don't move to the position i indicate in the position array in the command.

Emilio gravatar image Emilio  ( 2015-08-13 11:28:00 -0600 )edit

Does any combination of two servos work?. This is to figure out if the problem is related to a specific servo, like servo2, or to _any_ third servo.

Adolfo Rodriguez T gravatar image Adolfo Rodriguez T  ( 2015-08-17 03:59:08 -0600 )edit

Yes, I change the pins on the board but always work well using two servos.I think the problem is related to any third servo.When i run rostopic command in a terminal i don't get any error but servos don't work. I`m new user ROS so i don't know where can be the problem.

Emilio gravatar image Emilio  ( 2015-08-17 05:25:27 -0600 )edit

From what you say, it doesn't seem like the error is on the ROS side of things. Can you make three servos work in a simple, ROS-less example?

Adolfo Rodriguez T gravatar image Adolfo Rodriguez T  ( 2015-08-17 05:57:34 -0600 )edit

If i use geometry_msgs/Vector3 instead of sensor_msgs/JointState to send the position to three servos with the same code changing the struct to Vector3 type, they work well so i think servos are not the problem and i don't know why with one type of message they work and they don't with other type.

Emilio gravatar image Emilio  ( 2015-08-18 05:35:14 -0600 )edit

Vector3 is a fixed-size array. The memebers of JointState are dynamically-sized, and implemented with std::vector in C++. Maybe you're not properly resizing your JointState message. When populating it, you should either push_back() vector elements, or resize() and then use operator[].

Adolfo Rodriguez T gravatar image Adolfo Rodriguez T  ( 2015-08-18 05:59:16 -0600 )edit

I write in void setup:

unsigned; giro.position.resize(n); giro.velo.. giro.eff..

I get the following error

emilio_arduino/firmware/move.cpp:50:16: error: request for member ‘size’ in‘giro.sensor_msgs::JointState::name’, which is of non-class type ‘char**’

Emilio gravatar image Emilio  ( 2015-08-18 10:58:09 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2015-08-18 12:09:02 -0600

Emilio gravatar image

Adolfo Rodriguez T i write here because i can write more text.

So I write in void function to turn on/off a blink each time i run a rostopic pub to check if when i run rostopic pub the program enter in void function. when i pub to two servos the blink state change, but when i pub to three servos blink doesn't change, so i think the problem can be this.

void servo_cb( const sensor_msgs::JointState &giro)


digitalWrite(13, HIGH-digitalRead(13));





edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-08-13 08:17:43 -0600

Seen: 599 times

Last updated: Aug 18 '15