how to put publisher and subscriber in a single Node?
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?
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