0
$\begingroup$

Rosanswers logo

Hi all!!

I am trying to go further with the basic example of Ros Serial with Arduino and a Servo. In this example, the publisher asks for an angle where to drive a servo. I am trying to do this with two servos, but the only thing I can do is to drive both to the same position. I need some tip for how to write two parameters in Ros, so each servo goes to its position. The code I have for the moment is as follows:

ros::NodeHandle nh;

Servo servo, servo2;

void servo_cb( const std_msgs::UInt16& cmd_msg){ servo.write(cmd_msg.data); //set servo angle, should be from 0-180
servo2.write(cmd_msg.data); digitalWrite(13, HIGH-digitalRead(13)); //toggle led
}

ros::Subscriber<std_msgs::UInt16> sub("servo", servo_cb); ros::Subscriber<std_msgs::UInt16> sub2("servo2", servo_cb);

void setup(){ pinMode(13, OUTPUT);

nh.initNode(); nh.subscribe(sub);

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

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

Can anyone help me please??


Originally posted by flipmurry on ROS Answers with karma: 3 on 2013-04-21

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

Hi, try this: https://github.com/mockbot/MockBot/tree/master/MockBotSerial

Cheers Christian


Originally posted by Chrimo with karma: 390 on 2013-04-22

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by flipmurry on 2013-04-22:
THank you!! I will try it tomorrow and I'll tell the result :)

Comment by flipmurry on 2013-04-22:
You just saved my day!! I wrote a code similar to yours and it works as I needed :D thank you very much Christian

Comment by flipmurry on 2013-04-22:
Hi Christian, just one more question! When you write:

rostopic pub /servo1 std_msgs/UInt16 $i$j

is there any way to send an angle to the servo2 in the same line??so I move both servos at the same time but different angles?

Comment by matthewmarkey on 2020-05-29:
This link is dead...

$\endgroup$
0
$\begingroup$

Rosanswers logo

ReHi, good to know, that somebody can reuse my old code ;-) "rostopic pub /servo1 std_msgs/UInt16 $i$j" was just a stresstest for my servos from bash... I expect rostopic did not support multiple topic from a single line. For performance reasons use python, lua or other languages for rapid testing. Cheers Christian


Originally posted by Chrimo with karma: 390 on 2013-04-23

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by flipmurry on 2013-04-23:
Thank you very much, I will try some things and I will post if I have any interesting result!

$\endgroup$

Your Answer

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