Demo for rosserial working with ROS2
Dependencies: mbed ros_lib_melodic
main.cpp@0:91fdcf1ce3aa, 2021-10-29 (annotated)
- Committer:
- stivending
- Date:
- Fri Oct 29 12:36:50 2021 +0000
- Revision:
- 0:91fdcf1ce3aa
a working sample keeping to publish data;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stivending | 0:91fdcf1ce3aa | 1 | /* |
stivending | 0:91fdcf1ce3aa | 2 | * rosserial Publisher Example |
stivending | 0:91fdcf1ce3aa | 3 | * Prints "hello world!" |
stivending | 0:91fdcf1ce3aa | 4 | */ |
stivending | 0:91fdcf1ce3aa | 5 | |
stivending | 0:91fdcf1ce3aa | 6 | #include "mbed.h" |
stivending | 0:91fdcf1ce3aa | 7 | #include "string.h" |
stivending | 0:91fdcf1ce3aa | 8 | #include <ros.h> |
stivending | 0:91fdcf1ce3aa | 9 | #include <std_msgs/String.h> |
stivending | 0:91fdcf1ce3aa | 10 | |
stivending | 0:91fdcf1ce3aa | 11 | ros::NodeHandle nh; |
stivending | 0:91fdcf1ce3aa | 12 | |
stivending | 0:91fdcf1ce3aa | 13 | std_msgs::String str_msg; |
stivending | 0:91fdcf1ce3aa | 14 | ros::Publisher chatter("chatter", &str_msg); |
stivending | 0:91fdcf1ce3aa | 15 | |
stivending | 0:91fdcf1ce3aa | 16 | char hello[] = "Hello from Nucleo"; |
stivending | 0:91fdcf1ce3aa | 17 | DigitalOut led = LED1; |
stivending | 0:91fdcf1ce3aa | 18 | DigitalIn but(USER_BUTTON); |
stivending | 0:91fdcf1ce3aa | 19 | |
stivending | 0:91fdcf1ce3aa | 20 | int main() { |
stivending | 0:91fdcf1ce3aa | 21 | nh.initNode(); |
stivending | 0:91fdcf1ce3aa | 22 | nh.advertise(chatter); |
stivending | 0:91fdcf1ce3aa | 23 | |
stivending | 0:91fdcf1ce3aa | 24 | while (1) { |
stivending | 0:91fdcf1ce3aa | 25 | //if(but == 0) { |
stivending | 0:91fdcf1ce3aa | 26 | // while(but == 0); |
stivending | 0:91fdcf1ce3aa | 27 | led = !led; |
stivending | 0:91fdcf1ce3aa | 28 | str_msg.data = hello; |
stivending | 0:91fdcf1ce3aa | 29 | chatter.publish( &str_msg ); |
stivending | 0:91fdcf1ce3aa | 30 | nh.spinOnce(); |
stivending | 0:91fdcf1ce3aa | 31 | |
stivending | 0:91fdcf1ce3aa | 32 | //} |
stivending | 0:91fdcf1ce3aa | 33 | |
stivending | 0:91fdcf1ce3aa | 34 | wait_ms(100); |
stivending | 0:91fdcf1ce3aa | 35 | } |
stivending | 0:91fdcf1ce3aa | 36 | } |