Algorithm determines that i am not subscribed to laser topic

hi
i am going thrue this course, and am trying to get good marks while i am at it, but this is what the alghoritm sys:

and this is the code:

#include <ros/ros.h>
#include <nav_msgs/Odometry.h>
#include <geometry_msgs/Twist.h>
#include <sensor_msgs/LaserScan.h>
#include <signal.h>
#include <list>
#include <vector>
#include <string>
#include <iostream>

using namespace std;


class RobotControllClass{

    public:
    nav_msgs::Odometry odom;
    geometry_msgs::Twist cmd;
    sensor_msgs::LaserScan::ConstPtr laser_scan_pointer;
    sensor_msgs::LaserScan laser_scan;
    ros::Publisher pub;
    ros::Publisher pub_laser;
    ros::Subscriber sub_laser;
    ros::NodeHandle nh;
    int counter;
    void publish_laser_scan_info();
    RobotControllClass(ros::NodeHandle *outside_nh){
        nh = *outside_nh;
        pub = nh.advertise<geometry_msgs::Twist> (nh.resolveName(vel_topic), 1000);
        pub_laser = nh.advertise<sensor_msgs::LaserScan> (nh.resolveName(laser_topic_test_publish), 1000);
        sub_laser = nh.subscribe(nh.resolveName(laser_topic_test), 1000, &RobotControllClass::laser_callback_number, this);
        initializer_function();
        }
    void avoid_the_wall();
    void stop_robot();
    void cmd_publish_once();
    void test_write();
    void avoid_the_wall_2();

    private:
    int internal_rate = 15; //refresh rate to wait for sensor data to get initialized
    int counter_i =0 ;
    const string vel_topic = "/cmd_vel";
    const string odom_topic = "/odom";
    const string laser_topic_test = "/kobuki/laser/scan";
    const string laser_topic_test_publish= "/laser_publish";
    void laser_callback_number(const sensor_msgs::LaserScan &msg);
    void initializer_function();

};

void RobotControllClass::laser_callback_number(const sensor_msgs::LaserScan &msg){
    laser_scan = msg;
}

void RobotControllClass::initializer_function(){
    ros::Rate rate_internal(internal_rate);
    ROS_INFO("starting initilaizer function");
    while (laser_scan.ranges.empty()){
        ROS_INFO("Waiting for [%s] to started publishing", laser_topic_test.c_str());
        ros::spinOnce();
        rate_internal.sleep();
    }
    ROS_INFO("%s started publishing", laser_topic_test.c_str());

}

void RobotControllClass::publish_laser_scan_info(){
    ros::Rate rate_internal(internal_rate);
    int i;
    while(ros::ok()){
            printf("data at sensor 360= %f \n", laser_scan.ranges[360]);
            counter_i++;
            printf("counter = %i \n", counter_i);
            ros::spinOnce();
            rate_internal.sleep();
            }
}

void RobotControllClass::avoid_the_wall(){
    ros::Rate rate_internal(15);
    stop_robot();
    printf("starterted moving\n");
    while (laser_scan.ranges[360] > 1){
        cmd.linear.x = 0.25;
        cmd_publish_once();
        printf("distance: %f \n", laser_scan.ranges[360]);
        ros::spinOnce();
        rate_internal.sleep();
    }



    while (laser_scan.ranges[0] > 1){
        cmd.angular.z = 0.8;
        cmd.linear.x = 0.25;
        cmd_publish_once();
        printf(" distance to the right side : %f \n", laser_scan.ranges[0]);
        ros::spinOnce();
        rate_internal.sleep();
    }
    stop_robot();
    cmd.angular.z = 0.8;
    cmd_publish_once();
    printf(" distance to the right side : %f \n", laser_scan.ranges[0]);

    sleep(1);
    stop_robot();

    while ((laser_scan.ranges[0] < 30) or (laser_scan.ranges[360] < 30)){
        
        printf("avoided the wall: %f \n", laser_scan.ranges[0]);
        cmd.linear.x = 0.25;
        cmd_publish_once();
        ros::spinOnce();
        rate_internal.sleep();
    }

    stop_robot();


}

void RobotControllClass::stop_robot(){
    cmd.linear.x = 0;
    cmd.linear.y = 0;
    cmd.linear.z = 0;
    cmd.angular.x = 0;
    cmd.angular.y = 0;
    cmd.angular.z = 0;
    cmd_publish_once();
    
}

void RobotControllClass::cmd_publish_once(){
    ros::Rate rate_internal(internal_rate);
    int i;
    while(ros::ok()){
        i = pub.getNumSubscribers();
        if (i >0){
            //printf ("numbver of subscribers is: %i, \n", i);
            pub.publish(cmd);
            break;
            }
        else{
            ROS_INFO("no connections to [%s] topic", vel_topic.c_str());
            ros::spinOnce();
            rate_internal.sleep();
        }
    }

}

void RobotControllClass::test_write(){
    printf(" values from laser.ranges %f \n", laser_scan.ranges[12]);
}

void RobotControllClass::avoid_the_wall_2(){
    ros::Rate rate_internal(1);
    float speed_x = 0.25;
    float speed_z = 0.8;
    stop_robot();
    bool variable;
    variable = !((laser_scan.ranges[0] < 20) and (laser_scan.ranges[719] < 20) and (laser_scan.ranges[360] < 20));
    cout << variable << endl;
    while (ros::ok()){
        // write an iff statement
        printf("Inside while loop \n");
        printf("laser range [360 = %f \n", laser_scan.ranges[360]);
        if (laser_scan.ranges[360] > 1){
            printf("FORWARD laser range [360 = %f\n", laser_scan.ranges[360]);
            printf("laser_scan.ranges[360] > 1)\n");
            cmd.linear.x = speed_x;
            cmd.angular.z = 0;
            cmd_publish_once();
        }
        else if ((laser_scan.ranges[360] < 1) and !(laser_scan.ranges[0] < laser_scan.ranges[719])){
            printf(" FORWARD RIGHT laser range [360 = %f \n", laser_scan.ranges[360]);
            printf("laser_scan.ranges[360] > 1)\n");
            cmd.linear.x = speed_x;
            cmd.angular.z = speed_z;
            cmd_publish_once();
        }
        else if (laser_scan.ranges[719] < 1){
            printf(" LEFT laser range [719 = %f\n", laser_scan.ranges[0]);
            printf("laser_scan.ranges[719] < 1) \n");
            cmd.linear.x = 0;
            cmd.angular.z = speed_z;
            cmd_publish_once();
        }
        else if (laser_scan.ranges[0] < 1){
            printf(" RIGHT laser range [0 = %f\n", laser_scan.ranges[0]);
            printf("laser_scan.ranges[0] < 1)\n");
            cmd.linear.x = 0;
            cmd.angular.z = -speed_z;
            cmd_publish_once();
        }
        else{
            printf("FORWARD LEFTlaser range [360 = %f \n", laser_scan.ranges[360]);
            printf("laser_scan.ranges[360] > 1)\n");
            cmd.linear.x = speed_x;
            cmd.angular.z = -speed_z;
            cmd_publish_once();
        }
        ros::spinOnce();
        rate_internal.sleep();
        printf("------------------------------------------------------ \n");
        variable = (!(laser_scan.ranges[0] < 20) and !(laser_scan.ranges[719] < 20) and !(laser_scan.ranges[360] < 20));
        cout << "VARIABLE IS: " <<  variable << endl;
        printf("OUTSIDE BREAK STATEMENT \n");
        printf("0 = %f %i \n", laser_scan.ranges[0], (laser_scan.ranges[0] < 20) );
        printf("360 = %f %i \n", laser_scan.ranges[360], (laser_scan.ranges[360] < 20)  );
        printf("719 = %f %i \n", laser_scan.ranges[719], (laser_scan.ranges[719] < 20)  );
        printf("------------------------------------------------------ \n");
        if (((laser_scan.ranges[0] < 20) and (laser_scan.ranges[719] < 20) and (laser_scan.ranges[360] < 20))){
            variable = !((laser_scan.ranges[0] < 20) and (laser_scan.ranges[719] < 20) and (laser_scan.ranges[360] < 20));
            cout << "VARIABLE IS: " <<  variable << endl;
            printf("breaking the loop \n");
            printf("0 = %f %i \n", laser_scan.ranges[0], (laser_scan.ranges[0] < 20) );
            printf("360 = %f %i \n", laser_scan.ranges[360], (laser_scan.ranges[360] < 20)  );
            printf("719 = %f %i \n", laser_scan.ranges[719], (laser_scan.ranges[719] < 20)  );
            break;
        }
    }
    printf("outside while loop \n");
    stop_robot();

}





int main (int argc, char** argv){
    ros::init(argc, argv, "topics_quiz_node");
    ros::NodeHandle nh;
    ROS_INFO("ros beeing started");
    RobotControllClass st10(&nh);
    ros::Rate rate(1);
    //st10.test_write();
    st10.avoid_the_wall_2();
    //st10.publish_laser_scan_info();
    //st10.avoid_the_wall();
    ros::spinOnce();
}

//geometry_msgs nav_msgs roscpp std_msgs sensor_msgs

If you consider the feedback in the post below, things should improve :wink:

Yes, i have gone thrue that also i have read about the spin functions and and read a bit more here:
https://programming.vip/docs/ros-ros-spin-and-ros-spinonce-differences-and-use.html
so how i have sett the functions should have been correct now?
as after the robot is clear of the wall the program can shutdown, there fore i used spinOnce() in the main program,
and in the while loop inside the class, where i am using the if checks to check on the laser scan situation, i should have the spinOnce as well so that after each loop the laser scan callback function process the massage (copies it in to laser_scan object)
or am i misunderstanding something else?
hm if this is the same question (same root cause) as the previous one should i copy it there so i dont spam to much on the forum?