Example of a Publisher and Subscriber ROS nodes for Mbed enable board using Twist messages.
Dependencies: mbed ros_lib_kinetic
main.cpp@1:fb8f963047b0, 2018-08-13 (annotated)
- Committer:
- FernandoLG
- Date:
- Mon Aug 13 19:25:13 2018 +0000
- Revision:
- 1:fb8f963047b0
- Parent:
- 0:2595562b0c61
Example of Publisher Subscriber node for Mbed boards using ROS Twist messages.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
FernandoLG | 0:2595562b0c61 | 1 | /* |
FernandoLG | 0:2595562b0c61 | 2 | * rosserial Subscriber Example |
FernandoLG | 0:2595562b0c61 | 3 | * Blinks an LED on callback |
FernandoLG | 0:2595562b0c61 | 4 | */ |
FernandoLG | 0:2595562b0c61 | 5 | #include "mbed.h" |
FernandoLG | 0:2595562b0c61 | 6 | #include <ros.h> |
FernandoLG | 1:fb8f963047b0 | 7 | #include <geometry_msgs/Twist.h> |
FernandoLG | 0:2595562b0c61 | 8 | |
FernandoLG | 0:2595562b0c61 | 9 | ros::NodeHandle nh; |
FernandoLG | 0:2595562b0c61 | 10 | DigitalOut myled(LED1); |
FernandoLG | 0:2595562b0c61 | 11 | |
FernandoLG | 1:fb8f963047b0 | 12 | geometry_msgs::Twist commandRead; |
FernandoLG | 0:2595562b0c61 | 13 | ros::Publisher chatter("chatter", &commandRead); |
FernandoLG | 0:2595562b0c61 | 14 | |
FernandoLG | 0:2595562b0c61 | 15 | // /* |
FernandoLG | 1:fb8f963047b0 | 16 | void handlerFunction(const geometry_msgs::Twist& command){ |
FernandoLG | 1:fb8f963047b0 | 17 | commandRead = command; |
FernandoLG | 0:2595562b0c61 | 18 | } |
FernandoLG | 0:2595562b0c61 | 19 | // */ |
FernandoLG | 0:2595562b0c61 | 20 | |
FernandoLG | 1:fb8f963047b0 | 21 | ros::Subscriber<geometry_msgs::Twist> sub("cmd_vel", &handlerFunction); |
FernandoLG | 0:2595562b0c61 | 22 | |
FernandoLG | 0:2595562b0c61 | 23 | int main() { |
FernandoLG | 0:2595562b0c61 | 24 | nh.initNode(); |
FernandoLG | 0:2595562b0c61 | 25 | nh.subscribe(sub); |
FernandoLG | 0:2595562b0c61 | 26 | nh.advertise(chatter); |
FernandoLG | 1:fb8f963047b0 | 27 | |
FernandoLG | 1:fb8f963047b0 | 28 | commandRead.linear.x = 6.9; |
FernandoLG | 0:2595562b0c61 | 29 | |
FernandoLG | 0:2595562b0c61 | 30 | while (1) { |
FernandoLG | 1:fb8f963047b0 | 31 | //commandRead = command; |
FernandoLG | 0:2595562b0c61 | 32 | chatter.publish( &commandRead); |
FernandoLG | 0:2595562b0c61 | 33 | nh.spinOnce(); |
FernandoLG | 1:fb8f963047b0 | 34 | wait_ms(10); |
FernandoLG | 0:2595562b0c61 | 35 | } |
FernandoLG | 0:2595562b0c61 | 36 | } |