0
$\begingroup$

Rosanswers logo

hello, i am writing a node which subscribes to /scan topic make some calculations and according to the calculated values it publishes different messages on the /robotState topic in the callback function.

Here is my code:

#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#include "std_msgs/String.h"
#include <sstream>

void switcherCallback(const sensor_msgs::LaserScan::ConstPtr& msg)
{
    ros::NodeHandle p;
    ros::Publisher state_pub = p.advertise<std_msgs::String>("robotState",1000);
    ros::Rate loop_rate(1);
    while(ros::ok())
    {
    int f=0;
    for(int i=0; i<msg->ranges.size(); i++){
        if(msg->ranges[i]<1.5f){
            f=1;
            break;
        }
    }
    if(f==1){
        std_msgs::String msg;
        std::stringstream ss;
        ss<<"1";
        msg.data = ss.str();
        state_pub.publish(msg);
    }else{
        std_msgs::String msg;
        std::stringstream ss;
        ss<<"0";
        msg.data = ss.str();
        state_pub.publish(msg);
    }
    ros::spinOnce();
    loop_rate.sleep();
    }
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "switcher");
    ros::NodeHandle n;
    ros::Subscriber sub = n.subscribe<sensor_msgs::LaserScan>("scan", 1000, switcherCallback);
    ros::spin();
    return 0;
}

But whenever i executes the code it shows the following error:

Bus error (core dumped)

i am just wandering why this error is occurred and how can i put both publisher and subscriber in a single node?


Originally posted by Aarif on ROS Answers with karma: 351 on 2015-02-04

Post score: 0


Original comments

Comment by Jie Sky on 2015-05-06:
hi Aarif ,I am doing the same work,but it do not work .can you tell me how you make it work? thanks a lot

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

You shouldn't call ros::spinOnce() inside the callback. What it does is that it checks the subscriber queue, then calls your callback again, so that now you're calling the callback recursively. Instead, process the single message and exit the callback, which will drop you back into the ros::spin() call in main().

Also, you shouldn't re-create the publisher every time you want to publish a message (inside the callback). Instead, create it once (in main) and use it inside the callback. Otherwise, the subscribers won't be able to read the message before the publisher is destroyed again (when exiting the callback).


Originally posted by Martin Günther with karma: 11816 on 2015-02-04

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Aarif on 2015-02-06:
Thanks @Martin Günther, it worked.

Comment by Jie Sky on 2015-05-06:
hi Aarif ,I am doing the same work,but it do not work .can you tell me how you make it work? thanks a lot

Comment by Aarif on 2015-08-26:
hi @Jie Sky, do exactly as explained in the answer. i hope it will work for you too... :)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.