Unable to call a publisher function within a subscriber callback.
Am new to ROS, currently trying to call the function that publishes velocity
move()
stop_moving()
in a subscriber Callback laserCallback
The laserCallback function works when the commented lines // are put in. It doesn't work when the function 1 and 2 is called. Why is this? when they are essentially the same thing.
ros::Publisher chatter_pub;
geometry_msgs::Twist pub_obj;
void move(){
pub_obj.linear.x=1.0;
pub_obj.linear.y=0.0;
pub_obj.linear.z=0.0;
pub_obj.angular.x=0.0;
pub_obj.angular.y=0.0;
pub_obj.angular.z=0.0;
chatter_pub.publish(pub_obj);
}
void stop_moving()
{
pub_obj.linear.x=0.0;
pub_obj.linear.y=0.0;
pub_obj.linear.z=0.0;
pub_obj.angular.x=0.0;
pub_obj.angular.y=0.0;
pub_obj.angular.z=0.5;
chatter_pub.publish(pub_obj);
}
void laserCallback(const sensor_msgs::LaserScan::ConstPtr& array){
int i=0;
for (int i=0; i<720; i++){
new_array[i]= array->ranges[i];
//printf("%f",new_array[i]);
if(new_array[i]<number)
{
//printf("%f", new_array[i]);
void stop_moving();
//pub_obj.linear.x=0.0;
//pub_obj.angular.z=0.5;
//chatter_pub.publish(pub_obj);
}
else
{ void move();
//pub_obj.linear.x=1.0;
//chatter_pub.publish(pub_obj);
}
}
}
int main(int argc, char** argv){
ros::init(argc,argv,"scan_sub_node");
ros::NodeHandle nh;
ros::Rate r(10);
chatter_pub = nh.advertise<geometry_msgs::Twist>("/car_diff_drive_controller/cmd_vel", 1);
ros::Subscriber sub_laser = nh.subscribe("/scan", 1000 , laserCallback);
//ros::Timer timer1=nh.createTimer(ros::Duration(5.0), msgCallback); //1 Hz
while(ros::ok()){
ros::spinOnce();
r.sleep();
}
return 0;
};
Asked by Dickson on 2019-08-08 14:07:58 UTC
Comments