Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

this is my code, but i get just zeros, and when i run just topic range, i get the real value. have you any idea for this problem?
#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();
}
}

this is my code, but i get just zeros, and when zeros i run just topic range, /scan, i get the real value. value when i run /range topic. have you any idea for this problem?

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

"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();
 }
 }