Hi @pabled91 ,
Welcome to this Community!
I see that you have tried to implement the Service Client logic from my post : ROS2 Service Client in C++ with Classes with Node Inheritance.
The problem is that the way you have implemented it is wrong.
The timer callback will keep calling the service after a certain time. The best way to avoid this is to wait for the amount of duration until the timer finishes. But unfortunately, you cannot change the timeout in the timer callback dynamically. (You can but it is a big process not suitable for beginners.)
The simpler method is to not use the timer. The steps to do that will be as follows:
- Move the
wait_for_servicecode to the constructor. - Move the
async_send_requestpart to a function call. - Call the function call to initialize a service in the
mainfunction.
Here is the altered version [WARNING: NOT TESTED]:
#include "rclcpp/rclcpp.hpp"
#include "rclcpp/timer.hpp"
#include "services_quiz_srv/srv/spin.hpp"
#include <chrono>
#include <cstdlib>
#include <future>
#include <memory>
using namespace std::chrono_literals;
using Spin = services_quiz_srv::srv::Spin;
class ServiceClient : public rclcpp::Node {
private:
rclcpp::Client<Spin>::SharedPtr client_;
rclcpp::TimerBase::SharedPtr timer_;
bool service_done_ = false;
void response_callback(rclcpp::Client<Spin>::SharedFuture future) {
RCLCPP_INFO(this->get_logger(), "response callback");
auto status = future.wait_for(1s);
if (status == std::future_status::ready) {
RCLCPP_INFO(this->get_logger(), "Result: success");
service_done_ = true;
} else {
RCLCPP_INFO(this->get_logger(), "Service In-Progress...");
}
RCLCPP_INFO(this->get_logger(), "end response callback");
}
public:
ServiceClient() : Node("rotate_client") {
RCLCPP_INFO(this->get_logger(), "rotate_client launched");
client_ = this->create_client<Spin>("rotate");
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...");
}
RCLCPP_INFO(this->get_logger(), "service reached");
}
void call_service(std::string direction, float ang_vel, int time) {
auto request = std::make_shared<Spin::Request>();
request->direction = direction;
request->angular_velocity = ang_vel;
request->time = time;
service_done_ = false;
auto result_future = client_->async_send_request(
request, std::bind(&ServiceClient::response_callback, this,
std::placeholders::_1));
}
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>();
service_client->call_service();
while (!service_client->is_service_done()) {
rclcpp::spin_some(service_client);
}
rclcpp::shutdown();
return 0;
}
// End of Code
Here is Tip: Don’t just copy-paste the code and change a few lines hoping you can adapt the code to your functionality. It will never work that way. Understand the logic flow and then make changes to suit your logic.
Regards,
Girish