Zill Khan
/
ROS_HCSR04_test
Integration of HCSR04 with Nucleo board. ROS Enabled
main.cpp@2:2bdc0f9b1a69, 2021-09-27 (annotated)
- Committer:
- zillkhan
- Date:
- Mon Sep 27 09:36:02 2021 +0000
- Revision:
- 2:2bdc0f9b1a69
- Parent:
- 1:ca82df4237eb
The final code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tomas | 0:d94848220e71 | 1 | #include "mbed.h" |
zillkhan | 1:ca82df4237eb | 2 | #include <ros.h> |
zillkhan | 1:ca82df4237eb | 3 | #include <std_msgs/String.h> |
zillkhan | 2:2bdc0f9b1a69 | 4 | #include "hcsr04.h" |
zillkhan | 1:ca82df4237eb | 5 | |
zillkhan | 1:ca82df4237eb | 6 | ros::NodeHandle nh; |
zillkhan | 1:ca82df4237eb | 7 | |
zillkhan | 2:2bdc0f9b1a69 | 8 | std_msgs::String data; |
zillkhan | 2:2bdc0f9b1a69 | 9 | ros::Publisher ultrasonic("ultrasonic", &data); |
zillkhan | 1:ca82df4237eb | 10 | |
zillkhan | 2:2bdc0f9b1a69 | 11 | //D12 TRIGGER D11 ECHO |
zillkhan | 2:2bdc0f9b1a69 | 12 | HCSR04 sensor(D8, D9); |
zillkhan | 1:ca82df4237eb | 13 | |
Tomas | 0:d94848220e71 | 14 | int main() { |
zillkhan | 2:2bdc0f9b1a69 | 15 | char buffer[50]; |
zillkhan | 1:ca82df4237eb | 16 | |
zillkhan | 1:ca82df4237eb | 17 | nh.initNode(); |
zillkhan | 2:2bdc0f9b1a69 | 18 | nh.advertise(ultrasonic); |
zillkhan | 1:ca82df4237eb | 19 | |
Tomas | 0:d94848220e71 | 20 | while (1) { //creates an eternal loop |
zillkhan | 1:ca82df4237eb | 21 | |
zillkhan | 2:2bdc0f9b1a69 | 22 | long distance = sensor.distance(); |
zillkhan | 2:2bdc0f9b1a69 | 23 | sprintf (buffer, "%d", distance); |
zillkhan | 2:2bdc0f9b1a69 | 24 | |
zillkhan | 2:2bdc0f9b1a69 | 25 | data.data = buffer; |
zillkhan | 2:2bdc0f9b1a69 | 26 | ultrasonic.publish( &data ); |
zillkhan | 1:ca82df4237eb | 27 | |
zillkhan | 1:ca82df4237eb | 28 | nh.spinOnce(); |
zillkhan | 1:ca82df4237eb | 29 | wait_ms(1000); |
Tomas | 0:d94848220e71 | 30 | } |
Tomas | 0:d94848220e71 | 31 | } |