My service client is correct? ROS 2 C++

Hi @everyone

I was working on the RealRobot Project on C++ and I would like to know if this is a correct service client class to call the find_wall server.

Works well in simulation world, but when I go to the RealRobot the cmd_vel commands doesn’t actualize and the robot keep the first comand I sended.

service_client.cpp

#include "rclcpp/logger.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp/timer.hpp"
#include "std_srvs/srv/detail/empty__struct.hpp"
#include "std_srvs/srv/detail/set_bool__struct.hpp"
#include "std_srvs/srv/empty.hpp"
#include "std_srvs/srv/set_bool.hpp"

#include "jedi/srv/find_wall.hpp"

#include <chrono>
#include <cstdlib>
#include <future>
#include <memory>

using namespace std::chrono_literals;

class ServiceClient : public rclcpp::Node {

private:
  rclcpp::Client<jedi::srv::FindWall>::SharedPtr client_;
  rclcpp::TimerBase::SharedPtr timer_;

  bool service_done_ = false;

  void timer_callback() {

    this->timer_->cancel();
    
    while (!client_->wait_for_service(1s)) {
      if (rclcpp::ok()) {
        RCLCPP_ERROR(
            this->get_logger(),
            "Client interrupted while waiting for service. Terminating...");
        return;
      }
      RCLCPP_INFO(this->get_logger(),
                  "Service Unavailable. Waiting for Service...");
    }

    auto request = std::make_shared<jedi::srv::FindWall::Request>();


    service_done_ = false;
    
    auto result_future = client_->async_send_request(
        request, std::bind(&ServiceClient::response_callback, this,
                           std::placeholders::_1));

  }

  void response_callback(
      rclcpp::Client<jedi::srv::FindWall>::SharedFuture future) {
    auto status = future.wait_for(1s);
    if (status == std::future_status::ready) {


      RCLCPP_INFO(this->get_logger(), "Result: success: %b",
      future.get()->wallfound);

      service_done_ = true;

    } else {

      RCLCPP_INFO(this->get_logger(), "Service In-Progress...");
    
    }
  }

public:
  ServiceClient() : Node("service_client") {

    client_ = this->create_client<jedi::srv::FindWall>("find_wall");

    timer_ = this->create_wall_timer(
        1s, 
        std::bind(&ServiceClient::timer_callback, this));

  }

  bool is_service_done() const {
    return this->service_done_;
  }
};

int main(int argc, char *argv[]) {
  rclcpp::init(argc, argv);

  auto service_client = std::make_shared<ServiceClient>();
  while (!service_client->is_service_done()) {
    rclcpp::spin_some(service_client);
  }

  rclcpp::shutdown();
  return 0;
}

Hello @Voltedge ,

The code looks correct to me. I don’t think the error comes from this service client. Indeed, this client is not sending velocities to the robot, all it does is send a request to the service /find_wall. Could you provide some more details about the behavior you are getting with the real robot in contrast with the simulated behavior?

Hi @aezquerrostudent

I solved it a couple of days ago, I had forgotten to close the thread.

Anyways, thank you very much for taking the time to answer