ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

I'd like to publish a subscriber code.

asked 2021-07-13 05:28:11 -0500

kk_jo gravatar image

updated 2021-07-13 12:30:34 -0500

I get this kind of error.I don't know. Please help me.

/home/jo/catkin_ws/src/rplidar_ros/src/client.cpp: In constructor ‘SubscribeAndPublish::SubscribeAndPublish()’:
/home/jo/catkin_ws/src/rplidar_ros/src/client.cpp:12:62: error: ‘callback’ is not a member of ‘SubscribeAndPublish’
         sub_ = n_.subscribe("/scan",1, &SubscribeAndPublish::callback, this);
                                                              ^~~~~~~~
/home/jo/catkin_ws/src/rplidar_ros/src/client.cpp: In member function ‘void SubscribeAndPublish::scanCallback(const ConstPtr&)’:
/home/jo/catkin_ws/src/rplidar_ros/src/client.cpp:21:17: error: ‘scan’ was not declared in this scope
     int count = scan->scan_time / scan->time_increment;
                 ^~~~
/home/jo/catkin_ws/src/rplidar_ros/src/client.cpp:21:17: note: suggested alternative: ‘scanf’
     int count = scan->scan_time / scan->time_increment;
                 ^~~~
                 scanf
/home/jo/catkin_ws/src/rplidar_ros/src/client.cpp: In function ‘int main(int, char**)’:
/home/jo/catkin_ws/src/rplidar_ros/src/client.cpp:43:78: error: ‘scanCallback’ was not declared in this scope
 Subscriber sub = n.subscribe<sensor_msgs::LaserScan>("/scan", 1000, scanCallback);
                                                                     ^~~~~~~~~~~~
rplidar_ros/CMakeFiles/rplidarNodeClient.dir/build.make:62: recipe for target 'rplidar_ros/CMakeFiles/rplidarNodeClient.dir/src/client.cpp.o' failed
make[2]: *** [rplidar_ros/CMakeFiles/rplidarNodeClient.dir/src/client.cpp.o] Error 1
CMakeFiles/Makefile2:507: recipe for target 'rplidar_ros/CMakeFiles/rplidarNodeClient.dir/all' failed
make[1]: *** [rplidar_ros/CMakeFiles/rplidarNodeClient.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

This is the code I wrote.

#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"

class SubscribeAndPublish
{
public:
    SubscribeAndPublish()
    {
        pub_ = n_.advertise<sensor_msgs::LaserScan>("/scan",1);

        sub_ = n_.subscribe("/scan",1, &SubscribeAndPublish::callback, this);
    }


#define RAD2DEG(x) ((x)*180./M_PI)

void scanCallback(const sensor_msgs::LaserScan::ConstPtr& input)
{
    sensor_msgs::LaserScan output;  
    int count = scan->scan_time / scan->time_increment;
    ROS_INFO("I heard a laser scan %s[%d]:", scan->header.frame_id.c_str(), count);
    ROS_INFO("angle_range, %f, %f", RAD2DEG(scan->angle_min), RAD2DEG(scan->angle_max));

    for(int i = 0; i < count; i++) {
        float degree = RAD2DEG(scan->angle_min + scan->angle_increment * i);
        ROS_INFO(": [%f, %f]", degree, scan->ranges[i]);
    }
    pub_.publish(output);
}

private:
    ros::NodeHandle n_;
    ros::Publisher pub_;
    ros::Subscriber sub_;
};

int main(int argc, char **argv)
{
    ros::init(argc, argv, "rplidar_node_client");
    ros::NodeHandle n;

    ros::Subscriber sub = n.subscribe<sensor_msgs::LaserScan>("/scan", 1000, scanCallback);

    SubscribeAndPublish SAPObject;

    ros::spin();

    return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2021-07-13 13:33:06 -0500

abhishek47 gravatar image

The error actually tells you the problem pretty clearly: "‘scan’ was not declared in this scope".

The callback function scanCallback receives messages available through the argument input. Change all scan-> to input->.

edit flag offensive delete link more

Comments

Thanks a lot. I will study according to your advice and try again. Can I ask a question if there is an error?

kk_jo gravatar image kk_jo  ( 2021-07-14 00:23:37 -0500 )edit

Please help me one more time. Please, this error occurs when you fix it as advised. How can I fix it?I'm still studying and searching.You're a cool guy.

/home/jo/catkin_ws/src/rplidar_ros/src/client.cpp: In constructor ‘SubscribeAndPublish::SubscribeAndPublish()’: /home/jo/catkin_ws/src/rplidar_ros/src/client.cpp:13:62: error: ‘scancallback’ is not a member of ‘SubscribeAndPublish’ sub_ = n_.subscribe("/scan",1, &SubscribeAndPublish::scancallback, this); ^~~~~~~~~~~~

kk_jo gravatar image kk_jo  ( 2021-07-14 01:50:48 -0500 )edit

Question Tools

Stats

Asked: 2021-07-13 05:28:11 -0500

Seen: 125 times

Last updated: Jul 13 '21