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

rosserial RosServoControl arduino with two servos

asked 2013-04-21 23:20:46 -0500

flipmurry gravatar image

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);<="" p="">

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

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2013-04-22 02:43:36 -0500

Chrimo gravatar image
edit flag offensive delete link more

Comments

THank you!! I will try it tomorrow and I'll tell the result :)

flipmurry gravatar image flipmurry  ( 2013-04-22 05:58:44 -0500 )edit

You just saved my day!! I wrote a code similar to yours and it works as I needed :D thank you very much Christian

flipmurry gravatar image flipmurry  ( 2013-04-22 23:02:55 -0500 )edit

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?

flipmurry gravatar image flipmurry  ( 2013-04-22 23:07:44 -0500 )edit

This link is dead...

matthewmarkey gravatar image matthewmarkey  ( 2020-05-29 06:32:20 -0500 )edit
1

answered 2013-04-23 01:47:03 -0500

Chrimo gravatar image

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

edit flag offensive delete link more

Comments

Thank you very much, I will try some things and I will post if I have any interesting result!

flipmurry gravatar image flipmurry  ( 2013-04-23 21:28:19 -0500 )edit
0

answered 2020-02-21 12:31:54 -0500

updated 2020-02-21 14:11:37 -0500

gvdhoorn gravatar image

hey, let me help you with this... I am also doing the same

  #include "Arduino.h"
#else
  #include <wprogram.h>  
#endif

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

ros::NodeHandle nh;

Servo servo1;
Servo servo2;

void servo1_cb( const std_msgs::UInt16& cmd_msg)
{
  servo1.write(cmd_msg.data); //set servo angle, should be from 0-180  
}
void servo2_cb( const std_msgs::UInt16& cmd_msg)
{
  servo2.write(cmd_msg.data);
}

ros::Subscriber<std_msgs::UInt16> sub1("servo1", servo1_cb);
ros::Subscriber<std_msgs::UInt16> sub2("servo2", servo2_cb);

void setup()
{

  nh.initNode();
  nh.subscribe(sub1);
  nh.subscribe(sub2);

  servo1.attach(9); //attach it to pin 9
  servo2.attach(10); // attach it to pin 10
}

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

in the terminal use different servo names as servo1 or servo2 and insert your angle.

I am having trouble in subscribing this topic into the keyboard keypress (when one key is pressed- the command need to move in 180 angles) any ideas???? please help. also, help me how to write a node to subscribe and publish outer message (wireless topic) topic comes from Ros (not Arduino) that can control the servo.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-04-21 23:20:46 -0500

Seen: 1,177 times

Last updated: Feb 21 '20