77#include < QTime>
88#include < iostream>
99#include < QStateMachine>
10-
10+ # include < cstdlib >
1111#include < type_traits>
1212
1313template <typename T>
@@ -92,47 +92,53 @@ bool $className$::start(int argc, char*argv[])
9292 " $eventData.topicName$" , 10 , std::bind (&$className$::topic_callback_$eventData.functionName $, this , std::placeholders::_1));
9393 /* END_TOPIC_SUBSCRIPTION*/
9494 /* SEND_EVENT_LIST*/ /* SEND_EVENT_SRV*/
95+ $eventData.nodeName $ = rclcpp::Node::make_shared (m_name + " SkillNode$eventData.functionName$" );
96+ $eventData.clientName $ = $eventData.nodeName $->create_client <$eventData.interfaceName $::srv::$eventData.functionName $>($eventData.serverName $);
97+ $eventData.clientName $->configure_introspection ($eventData.nodeName $->get_clock (), rclcpp::SystemDefaultsQoS (), RCL_SERVICE_INTROSPECTION_CONTENTS);
98+
99+ bool wait_succeded{true };
100+ int retries = 0 ;
101+ while (!$eventData.clientName $->wait_for_service (std::chrono::seconds (1 ))) {
102+ if (!rclcpp::ok ()) {
103+ RCLCPP_ERROR (rclcpp::get_logger (" rclcpp" ), " Interrupted while waiting for the service '$eventData.functionName$'. Exiting." );
104+ wait_succeded = false ;
105+ break ;
106+ }
107+ retries++;
108+ if (retries == SERVICE_TIMEOUT) {
109+ RCLCPP_ERROR (rclcpp::get_logger (" rclcpp" ), " Timed out while waiting for the service '$eventData.functionName$'." );
110+ wait_succeded = false ;
111+ break ;
112+ }
113+ }
114+ if (!wait_succeded) {
115+ RCLCPP_ERROR (rclcpp::get_logger (" rclcpp" ), " Service '$eventData.componentName$/$eventData.functionName$' not available." );
116+ std::exit (1 );
117+ }
118+ /* SEND_EVENT_LIST*/ /* SEND_EVENT_SRV*/
95119 m_stateMachine.connectToEvent (" $eventData.event$" , [this ]([[maybe_unused]]const QScxmlEvent & event){
96- std::shared_ptr<rclcpp::Node> $eventData.nodeName $ = rclcpp::Node::make_shared (m_name + " SkillNode$eventData.functionName$" );
97- std::shared_ptr<rclcpp::Client<$eventData.interfaceName $::srv::$eventData.serviceTypeName $>> $eventData.clientName $ = $eventData.nodeName $->create_client <$eventData.interfaceName $::srv::$eventData.serviceTypeName $>($eventData.serverName $);
98- auto request = std::make_shared<$eventData.interfaceName $::srv::$eventData.serviceTypeName $::Request>();
99- $eventData.clientName $->configure_introspection ($eventData.nodeName $->get_clock (), rclcpp::SystemDefaultsQoS (), RCL_SERVICE_INTROSPECTION_CONTENTS);
120+ auto request = std::make_shared<$eventData.interfaceName $::srv::$eventData.functionName $::Request>();
100121 auto eventParams = event.data ().toMap ();
101122 /* PARAM_LIST*/ /* PARAM*/
102123 request->$IT->FIRST $ = convert<decltype (request->$IT->FIRST $)>(eventParams[" $IT->FIRST$" ].toString ().toStdString ());/* END_PARAM*/
103- bool wait_succeded{true };
104- int retries = 0 ;
105- while (!$eventData.clientName $->wait_for_service (std::chrono::seconds (1 ))) {
106- if (!rclcpp::ok ()) {
107- RCLCPP_ERROR (rclcpp::get_logger (" rclcpp" ), " Interrupted while waiting for the service '$eventData.functionName$'. Exiting." );
108- wait_succeded = false ;
109- break ;
110- }
111- retries++;
112- if (retries == SERVICE_TIMEOUT) {
113- RCLCPP_ERROR (rclcpp::get_logger (" rclcpp" ), " Timed out while waiting for the service '$eventData.functionName$'." );
114- wait_succeded = false ;
115- break ;
116- }
124+ auto result = $eventData.clientName $->async_send_request (request);
125+ const std::chrono::seconds timeout_duration (SERVICE_TIMEOUT);
126+ auto futureResult = rclcpp::spin_until_future_complete ($eventData.nodeName $, result, timeout_duration);
127+ if (futureResult == rclcpp::FutureReturnCode::SUCCESS)
128+ {
129+ auto response = result.get ();
130+ QVariantMap data;
131+ data.insert (" call_succeeded" , true );/* RETURN_PARAM_LIST*/ /* RETURN_PARAM*/
132+ data.insert (" $eventData.interfaceDataField$" , response->$eventData.interfaceDataField $);/* END_RETURN_PARAM*/
133+ m_stateMachine.submitEvent (" $eventData.componentName$.$eventData.functionName$.Return" , data);
134+ RCLCPP_INFO (rclcpp::get_logger (" rclcpp" ), " $eventData.componentName$.$eventData.functionName$.Return" );
135+ return ;
136+ }
137+ else if (futureResult == rclcpp::FutureReturnCode::TIMEOUT){
138+ RCLCPP_ERROR (rclcpp::get_logger (" rclcpp" ), " Timed out while future complete for the service '$eventData.functionName$'." );
117139 }
118- if (wait_succeded) {
119- auto result = $eventData.clientName $->async_send_request (request);
120- const std::chrono::seconds timeout_duration (SERVICE_TIMEOUT);
121- auto futureResult = rclcpp::spin_until_future_complete ($eventData.nodeName $, result, timeout_duration);
122- if (futureResult == rclcpp::FutureReturnCode::SUCCESS)
123- {
124- auto response = result.get ();
125- QVariantMap data;
126- data.insert (" call_succeeded" , true );/* RETURN_PARAM_LIST*/ /* RETURN_PARAM*/
127- data.insert (" $eventData.interfaceDataField$" , response->$eventData.interfaceDataField $);/* END_RETURN_PARAM*/
128- m_stateMachine.submitEvent (" $eventData.componentName$.$eventData.functionName$.Return" , data);
129- RCLCPP_INFO (rclcpp::get_logger (" rclcpp" ), " $eventData.componentName$.$eventData.functionName$.Return" );
130- return ;
131-
132- }
133- else if (futureResult == rclcpp::FutureReturnCode::TIMEOUT){
134- RCLCPP_ERROR (rclcpp::get_logger (" rclcpp" ), " Timed out while future complete for the service '$eventData.functionName$'." );
135- }
140+ else {
141+ RCLCPP_ERROR (rclcpp::get_logger (" rclcpp" ), " Failed to call service '$eventData.functionName$'." );
136142 }
137143 QVariantMap data;
138144 data.insert (" call_succeeded" , false );
@@ -212,9 +218,14 @@ void $className$::tick( [[maybe_unused]] const std::shared_ptr<bt_interfaces_dum
212218 m_tickResult.store (Status::undefined);
213219 m_stateMachine.submitEvent (" CMD_TICK" );
214220
221+ int load_counter=0 ;
222+ auto start_timer = std::chrono::steady_clock::now ();
215223 while (m_tickResult.load ()== Status::undefined) {
216- std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
224+ std::this_thread::sleep_for (std::chrono::milliseconds (5 ));
225+ load_counter++;
217226 }
227+ auto end_timer = std::chrono::steady_clock::now ();
228+ auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_timer - start_timer).count ();
218229 switch (m_tickResult.load ())
219230 {
220231 /* ACTION*/ case Status::running:
@@ -231,6 +242,7 @@ void $className$::tick( [[maybe_unused]] const std::shared_ptr<bt_interfaces_dum
231242 break ;
232243 }
233244 RCLCPP_INFO (m_node->get_logger (), " $className$::tickDone" );
245+ RCLCPP_DEBUG (m_node->get_logger (), " $className$ num_retry: %d tick time: %ld" , load_counter, duration_ms);
234246 response->is_ok = true ;
235247}/* END_TICK_CMD*/
236248/* HALT_CMD*/
0 commit comments