Skip to content

Instantly share code, notes, and snippets.

@bmagyar
Created February 10, 2017 13:01
Show Gist options
  • Save bmagyar/03f03622a81f879813cd5efc275da039 to your computer and use it in GitHub Desktop.
Save bmagyar/03f03622a81f879813cd5efc275da039 to your computer and use it in GitHub Desktop.
/*
* rosserial Servo Control Example
*
* This sketch demonstrates the control of hobby R/C servos
* using ROS and the arduiono
*
* For the full tutorial write up, visit
* www.ros.org/wiki/rosserial_arduino_demos
*
* For more information on the Arduino Servo Library
* Checkout :
* http://www.arduino.cc/en/Reference/Servo
*/
#include <WProgram.h>
#include <Servo.h>
#include <ros.h>
#include <std_msgs/UInt16.h>
ros::NodeHandle nh;
Servo servo;
std_msgs::UInt16 test;
ros::Publisher p("servo_pos", &test);
void servo_cb( const std_msgs::UInt16& cmd_msg){
servo.write(cmd_msg.data); //set servo angle, should be from 0-180
delay(10);
test.data = cmd_msg.data;
p.publish(&test);
digitalWrite(13, HIGH-digitalRead(13)); //toggle led
}
ros::Subscriber<std_msgs::UInt16> sub("servo", servo_cb);
void setup(){
pinMode(13, OUTPUT);
nh.initNode();
nh.subscribe(sub);
nh.advertise(p);
servo.attach(5); //attach it to pin 9
}
void loop(){
nh.spinOnce();
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment