Integration of HCSR04 with Nucleo board. ROS Enabled

Dependencies:   mbed HCSR04

main.cpp

Committer:
zillkhan
Date:
2021-09-27
Revision:
2:2bdc0f9b1a69
Parent:
1:ca82df4237eb

File content as of revision 2:2bdc0f9b1a69:

#include "mbed.h"
#include <ros.h>
#include <std_msgs/String.h>
#include "hcsr04.h"

ros::NodeHandle  nh;

std_msgs::String data;
ros::Publisher ultrasonic("ultrasonic", &data);

//D12 TRIGGER D11 ECHO
HCSR04 sensor(D8, D9); 

int main() {
    char buffer[50];
    
    nh.initNode();
    nh.advertise(ultrasonic);
    
    while (1) { //creates an eternal loop
        
        long distance = sensor.distance();  
        sprintf (buffer, "%d", distance);
        
        data.data = buffer;
        ultrasonic.publish( &data );
        
        nh.spinOnce();
        wait_ms(1000);
    }
}