Ask Your Question
1

ServoControl with two or more servos on arduino rosserial

asked 2015-06-30 15:34:36 -0500

andres1015415 gravatar image

*Hello friends. I want to make the example of Servo Control of Arduino, initially with two Servos, but have not successful. I tried to change the code this way but does not work.*

ros::NodeHandle nh;

Servo servo; Servo servo2;

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

ros::Subscriber<std_msgs::uint16> sub("servo", servo_cb); ros::Subscriber<std_msgs::uint16> sub("servo2", servo_cb); void setup() { pinMode(13, OUTPUT);

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

servo2.attach(7); servo.attach(9); }

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

I hope someone can help me. Thanks for your time

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-10-12 10:00:41 -0500

Chuong Nguyen gravatar image

updated 2015-10-12 10:26:42 -0500

Here is an simple example:

#if (ARDUINO >= 100)
 #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  
  digitalWrite(13, HIGH-digitalRead(13));  //toggle led  
}

void servo2_cb( const std_msgs::UInt16&  cmd_msg){
  servo2.write(cmd_msg.data); //set servo angle, should be from 0-180  
  //digitalWrite(13, HIGH-digitalRead(13));  //toggle led  
}
ros::Subscriber<std_msgs::UInt16> sub1("servo1", servo1_cb);
ros::Subscriber<std_msgs::UInt16> sub2("servo2", servo2_cb);

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

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

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

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

publisher:

rostopic pub servo1 std_msgs/UInt16  <angle1> & rostopic pub servo2 std_msgs/UInt16  <angle2>
edit flag offensive delete link more

Comments

Another Example uses MutiArray

http://answers.ros.org/question/14595...

Chuong Nguyen gravatar image Chuong Nguyen  ( 2015-10-12 10:32:40 -0500 )edit

have you a code o node that publish angle ?

Emilien gravatar image Emilien  ( 2016-05-03 13:44:21 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-06-30 15:34:36 -0500

Seen: 736 times

Last updated: Oct 12 '15