SMACC2
Loading...
Searching...
No Matches
cb_call_service.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20#pragma once
23
24namespace smacc2
25{
26namespace client_behaviors
27{
28using namespace std::chrono_literals;
29template <typename ServiceType>
31{
32public:
33 CbServiceCall(const char * serviceName)
34 : serviceName_(serviceName),
35 request_(std::make_shared<typename ServiceType::Request>()),
36 pollRate_(100ms)
37 {
38 }
39
41 const char * serviceName, std::shared_ptr<typename ServiceType::Request> request,
42 std::chrono::milliseconds pollRate = 100ms)
43 : serviceName_(serviceName), request_(request), result_(nullptr), pollRate_(pollRate)
44 {
45 }
46
47 void onEntry() override
48 {
49 RCLCPP_INFO_STREAM(
50 getLogger(), "[" << this->getName() << "] creating ros service client: " << serviceName_);
51
52 client_ = getNode()->template create_client<ServiceType>(serviceName_);
53
54 RCLCPP_INFO_STREAM(
55 getLogger(), "[" << this->getName() << "] making service request to " << serviceName_);
56
57 resultFuture_ = client_->async_send_request(request_).future.share();
58
59 std::future_status status = resultFuture_.wait_for(0s);
60
61 RCLCPP_INFO_STREAM(
62 getLogger(), "thread state: " << (int)status << " ok " << rclcpp::ok() << " shutdown "
63 << this->isShutdownRequested() << "");
64
65 while (status != std::future_status::ready && rclcpp::ok() && !this->isShutdownRequested())
66 {
67 RCLCPP_INFO_STREAM_THROTTLE(
68 getLogger(), *getNode()->get_clock(), 1000,
69 "[" << this->getName() << "] waiting response ");
70 rclcpp::sleep_for(pollRate_);
71 status = resultFuture_.wait_for(0s);
72 }
73
74 if (status == std::future_status::ready)
75 {
76 RCLCPP_INFO_STREAM(getLogger(), "[" << this->getName() << "] response received ");
77 result_ = resultFuture_.get();
79 this->postSuccessEvent();
80 }
81 else
82 {
83 RCLCPP_INFO_STREAM(getLogger(), "[" << this->getName() << "] response not received ");
84 this->postFailureEvent();
85 }
86 }
87
88 std::shared_future<std::shared_ptr<typename ServiceType::Response>> resultFuture_;
89
90protected:
91 std::shared_ptr<rclcpp::Client<ServiceType>> client_;
92 std::string serviceName_;
93 std::shared_ptr<typename ServiceType::Request> request_;
94 typename std::shared_ptr<typename ServiceType::Response> result_;
95 std::chrono::milliseconds pollRate_;
96
97 virtual void onServiceResponse(std::shared_ptr<typename ServiceType::Response> /*result*/)
98 {
99 RCLCPP_INFO_STREAM(getLogger(), "[" << this->getName() << "] response received ");
100 }
101};
102
103} // namespace client_behaviors
104} // namespace smacc2
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...
std::shared_ptr< typename ServiceType::Response > result_
std::shared_ptr< typename ServiceType::Request > request_
std::shared_ptr< rclcpp::Client< ServiceType > > client_
CbServiceCall(const char *serviceName, std::shared_ptr< typename ServiceType::Request > request, std::chrono::milliseconds pollRate=100ms)
virtual void onServiceResponse(std::shared_ptr< typename ServiceType::Response >)
std::shared_future< std::shared_ptr< typename ServiceType::Response > > resultFuture_