conversation solution [closed]

asked 2016-04-29 02:35:15 -0600

Emilien gravatar image

updated 2016-04-29 05:00:06 -0600

hi, I have an sonar sensor connected with arduino uno and i get range data. i want to convert range data to Laser_scan. have you any solution to do that please? this is my code:

    #include <ros/ros.h>
#include <tf/transform_broadcaster.h>
#include <sensor_msgs/LaserScan.h>
#include "sensor_msgs/Range.h"
#include "std_msgs/String.h"


double range;
void rangeCallback(const sensor_msgs::Range::ConstPtr& range_msg)
{   
    range = range_msg->range;
    ROS_INFO("I heard: [%f]", range);
}

int main(int argc, char** argv){
ros::init(argc, argv, "range_listner");
ros::init(argc, argv, "laser_scan_publisher");
tf::TransformBroadcaster broadcaster;
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/ultrasound", 1000, rangeCallback);
ros::Publisher scan_pub = n.advertise<sensor_msgs::LaserScan>("scan",50);
unsigned int num_readings = 20;
double laser_frequency = 40;
double ranges[num_readings];
int count = 10;
ros::Rate r(1.0);
while(n.ok()){

    for(unsigned int i=0; i<num_readings; i++)
    {
        ranges[i] = range;
    }
    ros::Time scan_time = ros::Time::now();
    sensor_msgs::LaserScan scan;
    scan.header.stamp = scan_time;
    scan.header.frame_id = "base_link";
    scan.angle_min = -1.57;
    scan.angle_max = 1.57;  
    scan.angle_increment = 3.14 / num_readings;
    scan.time_increment = (1/laser_frequency)/(num_readings);
    scan.range_min = 0.0;
    scan.range_max = 100.0;
    scan.ranges.resize(num_readings);
    scan.intensities.resize(num_readings);
    for(unsigned int i=0; i<num_readings; i++)
    {
        scan.ranges[i] = ranges[i];
    }
    scan_pub.publish(scan);
    ++count;
    broadcaster.sendTransform(
            tf::StampedTransform(
                tf::Transform(tf::Quaternion(0, 0, 0, 1), tf::Vector3(0.1, 0.0, 0.2)),
                ros::Time::now(),"base_link", "base_laser"));
    r.sleep();
}
}
edit retag flag offensive reopen merge delete

Closed for the following reason Question does not follow our guidelines for questions. Please see: http://wiki.ros.org/Support for more details. by tfoote
close date 2016-05-08 14:23:27.550392

Comments

You look to have a solution that does approximately what you want. I don't see how to answer your question. I'm going to close this. Please reask your question after reviewing our guide to asking questions.

tfoote gravatar imagetfoote ( 2016-05-08 14:23:20 -0600 )edit