Topics_quiz question

Hello, I am stuck in this quiz trying to parse the publisher to the callback function that is being performed for each scan. I tried to use methods that I found such as std::bind as well as using global scope but I had no success at all with any of these.

I’m sure that this shouldn’t be that hard and I’m missing some key aspects, but I don’t want to perform the OOP stuff till it comes in the course (which is how I suppose this is the easy way to do it), so any help is thanked.

Thanks a lot

Hi @ACTISA2, could you give a little more context on what you are trying to do? Something like “I’m trying to have the robot move based on the scan data” would be clarifying.

Are you having compilation errors or runtime errors? If so, could you send some screenshots?

Are you trying to publish a message to a topic in the scan callback function? Even though you should limit the callback actions to a minimum, it can look something like this:

// Callback function for the LIDAR scan
void lidarCallback(const sensor_msgs::LaserScan::ConstPtr& msg, ros::Publisher& pub) {
    // Process the received scan data
    // For simplicity, let's assume we're just checking if there's an obstacle within a certain range
    bool obstacleDetected = false;
    for (float range : msg->ranges) {
        if (range < 1.0) {  // If an obstacle is detected within 1 meter
            obstacleDetected = true;
            break;
        }
    }

    // Publish velocity commands based on the scan data
    geometry_msgs::Twist vel_msg;
    if (obstacleDetected) {
        // Stop moving if an obstacle is detected
        vel_msg.linear.x = 0.0;
        vel_msg.angular.z = 0.0;
    } else {
        // Move forward if no obstacle is detected
        vel_msg.linear.x = 0.5;  // Linear velocity = 0.5 m/s
        vel_msg.angular.z = 0.0;  // Angular velocity = 0.0 rad/s
    }
    pub.publish(vel_msg);
}

Hello @rodrigo55 , sorry if I didn’t explain correctly in my last reply, it was a bit late.
I am having issues trying to do what you are doing right now.

Normally, the subscription function has the next format:
ros::Subscriber sub = nh.subscribe(<my_topic>, <queue_size>, <callback_to_execute>)

In here, I’m not able to pass the subscription variable, because I’m not using parenthesis to insert the publisher as:
ros::Subscriber sub = nh.subscribe(<my_topic>, <queue_size>, <callback_to_execute>(pub))

I tried to use std::bind at the third argument of the subscribe function but it was giving me compile errors.

My issue resumes in how can I input my publisher to a subscriber callback without using OOP.

Edit: I will try to post some pictures as soon as I have some spare time.

Strangely, I tried to create again the publisher variable as ros::Publisher pub; as a global variable outside of any function and it worked like a charm. Weird but I guess I might forgot to build again the package. I did work in a bad network so perhaps it was due to connection faults.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.