This program is porting rosserial_arduino for mbed http://www.ros.org/wiki/rosserial_arduino This program supported the revision of 169 of rosserial. This program contains an example.
Dependencies: rosserial_mbed_lib mbed Servo
examples/ServoControl.cpp@0:06fc856e99ca, 2011-08-19 (annotated)
- Committer:
- nucho
- Date:
- Fri Aug 19 09:06:16 2011 +0000
- Revision:
- 0:06fc856e99ca
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nucho | 0:06fc856e99ca | 1 | //#define COMPILE_SERVOCONTROL_CODE_ROSSERIAL |
nucho | 0:06fc856e99ca | 2 | #ifdef COMPILE_SERVOCONTROL_CODE_ROSSERIAL |
nucho | 0:06fc856e99ca | 3 | |
nucho | 0:06fc856e99ca | 4 | /* |
nucho | 0:06fc856e99ca | 5 | * rosserial Servo Control Example |
nucho | 0:06fc856e99ca | 6 | * |
nucho | 0:06fc856e99ca | 7 | * This sketch demonstrates the control of hobby R/C servos |
nucho | 0:06fc856e99ca | 8 | * using ROS and the arduiono |
nucho | 0:06fc856e99ca | 9 | * |
nucho | 0:06fc856e99ca | 10 | * For the full tutorial write up, visit |
nucho | 0:06fc856e99ca | 11 | * www.ros.org/wiki/rosserial_arduino_demos |
nucho | 0:06fc856e99ca | 12 | * |
nucho | 0:06fc856e99ca | 13 | * For more information on the Arduino Servo Library |
nucho | 0:06fc856e99ca | 14 | * Checkout : |
nucho | 0:06fc856e99ca | 15 | * http://www.arduino.cc/en/Reference/Servo |
nucho | 0:06fc856e99ca | 16 | */ |
nucho | 0:06fc856e99ca | 17 | |
nucho | 0:06fc856e99ca | 18 | #include "mbed.h" |
nucho | 0:06fc856e99ca | 19 | #include "Servo.h" |
nucho | 0:06fc856e99ca | 20 | #include <ros.h> |
nucho | 0:06fc856e99ca | 21 | #include <std_msgs/UInt16.h> |
nucho | 0:06fc856e99ca | 22 | |
nucho | 0:06fc856e99ca | 23 | ros::NodeHandle nh; |
nucho | 0:06fc856e99ca | 24 | |
nucho | 0:06fc856e99ca | 25 | Servo servo(p21); |
nucho | 0:06fc856e99ca | 26 | DigitalOut myled(LED1); |
nucho | 0:06fc856e99ca | 27 | |
nucho | 0:06fc856e99ca | 28 | void servo_cb( const std_msgs::UInt16& cmd_msg) { |
nucho | 0:06fc856e99ca | 29 | servo.position(cmd_msg.data); //set servo angle, should be from 0-180 |
nucho | 0:06fc856e99ca | 30 | myled = !myled; //toggle led |
nucho | 0:06fc856e99ca | 31 | } |
nucho | 0:06fc856e99ca | 32 | |
nucho | 0:06fc856e99ca | 33 | |
nucho | 0:06fc856e99ca | 34 | ros::Subscriber<std_msgs::UInt16> sub("servo", servo_cb); |
nucho | 0:06fc856e99ca | 35 | |
nucho | 0:06fc856e99ca | 36 | int main() { |
nucho | 0:06fc856e99ca | 37 | |
nucho | 0:06fc856e99ca | 38 | nh.initNode(); |
nucho | 0:06fc856e99ca | 39 | nh.subscribe(sub); |
nucho | 0:06fc856e99ca | 40 | |
nucho | 0:06fc856e99ca | 41 | while (1) { |
nucho | 0:06fc856e99ca | 42 | nh.spinOnce(); |
nucho | 0:06fc856e99ca | 43 | wait_ms(1); |
nucho | 0:06fc856e99ca | 44 | } |
nucho | 0:06fc856e99ca | 45 | } |
nucho | 0:06fc856e99ca | 46 | #endif |