ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

The node handle should work even if it is described in a service function. If you don't unsubscribe an existing one before subscribing a new one, it may continue to subscribe to the existing one.

ros::Subscriber subPCL; // added
bool startrowCallback(row_follower::start_row_mode::Request &req, row_follower::start_row_mode::Response &res){
    ros::NodeHandle n; // added
    if (!rowMode){
        rowMode = true;
        drivingDirection = req.direction;
        if (drivingDirection == "front") {
             subPCL.shutdown(); // added
             subPCL = n.subscribe<PointCloud>("/front_cloud", 10, pclCallback); // changed
        }
        if (drivingDirection == "back"){
            subPCL.shutdown(); // added
            subPCL = n.subscribe<PointCloud>("/back_cloud", 10, pclCallback);  // changed
        }

        res.done = true;
    }
    else{
        res.done = false;
    }

    return res.done;
}