Integration of HCSR04 with Nucleo board. ROS Enabled

Dependencies:   mbed HCSR04

Committer:
zillkhan
Date:
Tue Sep 28 12:46:44 2021 +0000
Revision:
5:1a7d0889cbcf
Parent:
4:13cda079f08d
final changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zillkhan 5:1a7d0889cbcf 1 #include "mbed.h"
zillkhan 1:ca82df4237eb 2 #include <ros.h>
zillkhan 1:ca82df4237eb 3 #include <std_msgs/String.h>
zillkhan 5:1a7d0889cbcf 4 #include "hcsr04.h"
zillkhan 1:ca82df4237eb 5
zillkhan 1:ca82df4237eb 6 ros::NodeHandle nh;
zillkhan 1:ca82df4237eb 7
zillkhan 5:1a7d0889cbcf 8 std_msgs::String data;
zillkhan 5:1a7d0889cbcf 9 ros::Publisher ultrasonic("ultrasonic", &data);
zillkhan 1:ca82df4237eb 10
zillkhan 5:1a7d0889cbcf 11 //D12 TRIGGER D11 ECHO
zillkhan 5:1a7d0889cbcf 12 HCSR04 sensor(D8, D9);
zillkhan 1:ca82df4237eb 13
Tomas 0:d94848220e71 14 int main() {
zillkhan 5:1a7d0889cbcf 15 char buffer[50];
zillkhan 5:1a7d0889cbcf 16
zillkhan 1:ca82df4237eb 17 nh.initNode();
zillkhan 5:1a7d0889cbcf 18 nh.advertise(ultrasonic);
zillkhan 5:1a7d0889cbcf 19
Tomas 0:d94848220e71 20 while (1) { //creates an eternal loop
zillkhan 5:1a7d0889cbcf 21
zillkhan 5:1a7d0889cbcf 22 long distance = sensor.distance();
zillkhan 5:1a7d0889cbcf 23 sprintf (buffer, "%d", distance);
zillkhan 5:1a7d0889cbcf 24
zillkhan 5:1a7d0889cbcf 25 data.data = buffer;
zillkhan 5:1a7d0889cbcf 26 ultrasonic.publish( &data );
zillkhan 5:1a7d0889cbcf 27
zillkhan 1:ca82df4237eb 28 nh.spinOnce();
zillkhan 1:ca82df4237eb 29 wait_ms(1000);
Tomas 0:d94848220e71 30 }
zillkhan 5:1a7d0889cbcf 31 }