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

Run multiple servo on ROS with arduino.

asked 2014-03-29 02:04:41 -0500

sonny gravatar image

updated 2015-04-15 05:04:38 -0500

130s gravatar image

Hello everybody,

i wanted to know is there any way out by which i can 2 or more servos at a time from a single subscriber. Currently i am running one servo on a single channel of arduino Due. But i require two servos to be run at a same time by giving them different angle values. like;

rostopic pub Servo1 std_msgs/Int16 230;

this passes 230 degree to the servo attached. But i want to control two at a time like;

rostopic pub Servo1 std_msgs/Int16 230 130 240;

230 for 1st servo; (degrees) 130 for 2nd servo; (degrees) 240 for 3rd servo...(degrees)

I would appreciate if someone puts a light on this issue.

thnks

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2014-03-29 05:44:20 -0500

Ryan gravatar image

You've got two options. Most straightforward would be a basic custom message with an array. This answer may help. Second option which is more elegant and involves less code is a MultiArray.

edit flag offensive delete link more

Comments

can anyone give an example of MultiArray usage please.

s1 gravatar image s1  ( 2015-09-22 07:46:30 -0500 )edit
3

answered 2015-10-12 10:31:50 -0500

Chuong Nguyen gravatar image

updated 2015-10-12 11:59:06 -0500

here is an example using MultiArray

#if (ARDUINO >= 100)
 #include <Arduino.h>
#else
 #include <WProgram.h>
#endif

#include <Servo.h> 
#include <ros.h>
#include "std_msgs/MultiArrayLayout.h"
#include "std_msgs/MultiArrayDimension.h"

#include "std_msgs/UInt16MultiArray.h"

ros::NodeHandle  nh;

Servo servo1;
Servo servo2;

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


ros::Subscriber<std_msgs::UInt16MultiArray> sub("servo", servo_cb);

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

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


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

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

Publisher:

 rostopic pub servo std_msgs/UInt16MultiArray '{data: [<angle1>, <angle2>]}'
edit flag offensive delete link more

Comments

thank you!!!

s1 gravatar image s1  ( 2015-10-16 20:26:16 -0500 )edit

is it possible to delay servo2 so that I can time the action? In other words I dont want all of the servos to fire off at once.

s1 gravatar image s1  ( 2015-12-23 12:23:06 -0500 )edit

Maybe consider publishing the message to servo 2 separately, delay accounted for?

Frank Lee gravatar image Frank Lee  ( 2017-09-05 08:27:50 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2014-03-29 02:04:41 -0500

Seen: 2,832 times

Last updated: Oct 12 '15