Robotics StackExchange | Archived questions

control servo using sensor_msgs/JointState

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 servocb( const sensormsgs::JointState &giro)

{

servo.write((int)giro.position[0]);

servo1.write((int)giro.position[1]);

servo2.write((int)giro.position[2]);

}

ros::Subscriber sub("servo", servo_cb);

void setup()

{

pinMode(13, OUTPUT);

nh.initNode();

nh.subscribe(sub);

servo2.attach(11);

servo.attach(9);

servo1.attach(8);

}

void loop() {

nh.spinOnce();

delay(1);

}

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?

Thanks

Asked by Emilio on 2015-08-13 08:17:43 UTC

Comments

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)?

Asked by Adolfo Rodriguez T on 2015-08-13 08:59:54 UTC

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.

Asked by Emilio on 2015-08-13 11:28:00 UTC

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.

Asked by Adolfo Rodriguez T on 2015-08-17 03:59:08 UTC

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.

Asked by Emilio on 2015-08-17 05:25:27 UTC

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?

Asked by Adolfo Rodriguez T on 2015-08-17 05:57:34 UTC

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.

Asked by Emilio on 2015-08-18 05:35:14 UTC

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[].

Asked by Adolfo Rodriguez T on 2015-08-18 05:59:16 UTC

I write in void setup:

unsigned num=giro.name.size(); 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**’ num=giro.name.size()

Asked by Emilio on 2015-08-18 10:58:09 UTC

Answers

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));

servo.write((int)giro.position[0]);

servo1.write((int)giro.position[1]);

servo2.write((int)giro.position[2]);

}

Asked by Emilio on 2015-08-18 12:09:02 UTC

Comments