Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct savedCodeStr{
std::string topicCallbackH;
std::string topicInterfaceH;
std::string topicParamList;
std::string serviceClientH;
std::string actionC;
std::string actionH;
std::string actionInterfaceH;
Expand Down
8 changes: 8 additions & 0 deletions src/Replacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ void handleGenericEvent(const eventDataStr eventData, const savedCodeStr savedCo

writeAfterCommand(str, "/*SEND_EVENT_LIST*/", eventCodeC);

// Service client header code
std::string serviceClientH = savedCode.serviceClientH;
replaceCommonEventPlaceholders(serviceClientH, eventData);
writeAfterCommand(str, "/*SERVICE_CLIENTS_LIST*/", serviceClientH);

//H
replaceAll(interfaceCodeH, "$eventData.interfaceName$", eventData.interfaceName);
replaceAll(interfaceCodeH, "$eventData.functionNameSnakeCase$", eventData.functionNameSnakeCase);
Expand Down Expand Up @@ -609,6 +614,8 @@ void saveCode(savedCodeStr& savedCode, std::string& code)
deleteSection(code, "/*TOPIC_CALLBACK_H*/", "/*END_TOPIC_CALLBACK_H*/");
saveSection(code, "/*TOPIC_SUBSCRIPTION_H*/", "/*END_TOPIC_SUBSCRIPTION_H*/", savedCode.topicSubscriptionH);
deleteSection(code, "/*TOPIC_SUBSCRIPTION_H*/", "/*END_TOPIC_SUBSCRIPTION_H*/");
saveSection(code, "/*SERVICE_CLIENT*/", "/*END_SERVICE_CLIENT*/", savedCode.serviceClientH);
deleteSection(code, "/*SERVICE_CLIENT*/", "/*END_SERVICE_CLIENT*/");
saveSection(code, "/*ACTION_H*/", "/*END_ACTION_H*/", savedCode.actionH);
deleteSection(code, "/*ACTION_H*/", "/*END_ACTION_H*/");
saveSection(code, "/*ACTION_INTERFACE*/", "/*END_ACTION_INTERFACE*/", savedCode.actionInterfaceH);
Expand Down Expand Up @@ -663,6 +670,7 @@ void replaceEventCode(std::map <std::string, std::string>& codeMap, fileDataStr
deleteCommand(it->second, "/*ACTION_FNC_LIST*/");
//H
deleteCommand(it->second, "/*INTERFACES_LIST*/");
deleteCommand(it->second, "/*SERVICE_CLIENTS_LIST*/");
deleteCommand(it->second, "/*TOPIC_SUBSCRIPTIONS_LIST_H*/");
deleteCommand(it->second, "/*TOPIC_CALLBACK_LIST_H*/");
deleteCommand(it->second, "/*ACTION_LIST_H*/");
Expand Down
3 changes: 3 additions & 0 deletions template_skill/include/TemplateSkill.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class $className$
/*TOPIC_SUBSCRIPTIONS_LIST_H*/
/*TOPIC_SUBSCRIPTION_H*/
rclcpp::Subscription<$eventData.interfaceName$::msg::$eventData.messageNameSnakeCase$>::SharedPtr m_subscription_$eventData.functionName$;/*END_TOPIC_SUBSCRIPTION_H*/
/*SERVICE_CLIENTS_LIST*//*SERVICE_CLIENT*/
std::shared_ptr<rclcpp::Node> $eventData.nodeName$;
std::shared_ptr<rclcpp::Client<$eventData.interfaceName$::srv::$eventData.functionName$>> $eventData.clientName$;/*END_SERVICE_CLIENT*/
/*ACTION_LIST_H*//*ACTION_H*/
std::shared_ptr<rclcpp::Node> m_node_action;
std::mutex m_actionMutex;
Expand Down
88 changes: 50 additions & 38 deletions template_skill/src/TemplateSkill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <QTime>
#include <iostream>
#include <QStateMachine>

#include <cstdlib>
#include <type_traits>

template<typename T>
Expand Down Expand Up @@ -92,47 +92,53 @@ bool $className$::start(int argc, char*argv[])
"$eventData.topicName$", 10, std::bind(&$className$::topic_callback_$eventData.functionName$, this, std::placeholders::_1));
/*END_TOPIC_SUBSCRIPTION*/
/*SEND_EVENT_LIST*//*SEND_EVENT_SRV*/
$eventData.nodeName$ = rclcpp::Node::make_shared(m_name + "SkillNode$eventData.functionName$");
$eventData.clientName$ = $eventData.nodeName$->create_client<$eventData.interfaceName$::srv::$eventData.functionName$>($eventData.serverName$);
$eventData.clientName$->configure_introspection($eventData.nodeName$->get_clock(), rclcpp::SystemDefaultsQoS(), RCL_SERVICE_INTROSPECTION_CONTENTS);

bool wait_succeded{true};
int retries = 0;
while (!$eventData.clientName$->wait_for_service(std::chrono::seconds(1))) {
if (!rclcpp::ok()) {
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Interrupted while waiting for the service '$eventData.functionName$'. Exiting.");
wait_succeded = false;
break;
}
retries++;
if(retries == SERVICE_TIMEOUT) {
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Timed out while waiting for the service '$eventData.functionName$'.");
wait_succeded = false;
break;
}
}
if (!wait_succeded) {
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Service '$eventData.componentName$/$eventData.functionName$' not available.");
std::exit(1);
}
/*SEND_EVENT_LIST*//*SEND_EVENT_SRV*/
m_stateMachine.connectToEvent("$eventData.event$", [this]([[maybe_unused]]const QScxmlEvent & event){
std::shared_ptr<rclcpp::Node> $eventData.nodeName$ = rclcpp::Node::make_shared(m_name + "SkillNode$eventData.functionName$");
std::shared_ptr<rclcpp::Client<$eventData.interfaceName$::srv::$eventData.serviceTypeName$>> $eventData.clientName$ = $eventData.nodeName$->create_client<$eventData.interfaceName$::srv::$eventData.serviceTypeName$>($eventData.serverName$);
auto request = std::make_shared<$eventData.interfaceName$::srv::$eventData.serviceTypeName$::Request>();
$eventData.clientName$->configure_introspection($eventData.nodeName$->get_clock(), rclcpp::SystemDefaultsQoS(), RCL_SERVICE_INTROSPECTION_CONTENTS);
auto request = std::make_shared<$eventData.interfaceName$::srv::$eventData.functionName$::Request>();
auto eventParams = event.data().toMap();
/*PARAM_LIST*//*PARAM*/
request->$IT->FIRST$ = convert<decltype(request->$IT->FIRST$)>(eventParams["$IT->FIRST$"].toString().toStdString());/*END_PARAM*/
bool wait_succeded{true};
int retries = 0;
while (!$eventData.clientName$->wait_for_service(std::chrono::seconds(1))) {
if (!rclcpp::ok()) {
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Interrupted while waiting for the service '$eventData.functionName$'. Exiting.");
wait_succeded = false;
break;
}
retries++;
if(retries == SERVICE_TIMEOUT) {
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Timed out while waiting for the service '$eventData.functionName$'.");
wait_succeded = false;
break;
}
auto result = $eventData.clientName$->async_send_request(request);
const std::chrono::seconds timeout_duration(SERVICE_TIMEOUT);
auto futureResult = rclcpp::spin_until_future_complete($eventData.nodeName$, result, timeout_duration);
if (futureResult == rclcpp::FutureReturnCode::SUCCESS)
{
auto response = result.get();
QVariantMap data;
data.insert("call_succeeded", true);/*RETURN_PARAM_LIST*//*RETURN_PARAM*/
data.insert("$eventData.interfaceDataField$", response->$eventData.interfaceDataField$);/*END_RETURN_PARAM*/
m_stateMachine.submitEvent("$eventData.componentName$.$eventData.functionName$.Return", data);
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "$eventData.componentName$.$eventData.functionName$.Return");
return;
}
else if(futureResult == rclcpp::FutureReturnCode::TIMEOUT){
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Timed out while future complete for the service '$eventData.functionName$'.");
}
if (wait_succeded) {
auto result = $eventData.clientName$->async_send_request(request);
const std::chrono::seconds timeout_duration(SERVICE_TIMEOUT);
auto futureResult = rclcpp::spin_until_future_complete($eventData.nodeName$, result, timeout_duration);
if (futureResult == rclcpp::FutureReturnCode::SUCCESS)
{
auto response = result.get();
QVariantMap data;
data.insert("call_succeeded", true);/*RETURN_PARAM_LIST*//*RETURN_PARAM*/
data.insert("$eventData.interfaceDataField$", response->$eventData.interfaceDataField$);/*END_RETURN_PARAM*/
m_stateMachine.submitEvent("$eventData.componentName$.$eventData.functionName$.Return", data);
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "$eventData.componentName$.$eventData.functionName$.Return");
return;

}
else if(futureResult == rclcpp::FutureReturnCode::TIMEOUT){
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Timed out while future complete for the service '$eventData.functionName$'.");
}
else {
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Failed to call service '$eventData.functionName$'.");
}
QVariantMap data;
data.insert("call_succeeded", false);
Expand Down Expand Up @@ -212,9 +218,14 @@ void $className$::tick( [[maybe_unused]] const std::shared_ptr<bt_interfaces_dum
m_tickResult.store(Status::undefined);
m_stateMachine.submitEvent("CMD_TICK");

int load_counter=0;
auto start_timer = std::chrono::steady_clock::now();
while(m_tickResult.load()== Status::undefined) {
std::this_thread::sleep_for (std::chrono::milliseconds(100));
std::this_thread::sleep_for (std::chrono::milliseconds(5));
load_counter++;
}
auto end_timer = std::chrono::steady_clock::now();
auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_timer - start_timer).count();
switch(m_tickResult.load())
{
/*ACTION*/case Status::running:
Expand All @@ -231,6 +242,7 @@ void $className$::tick( [[maybe_unused]] const std::shared_ptr<bt_interfaces_dum
break;
}
RCLCPP_INFO(m_node->get_logger(), "$className$::tickDone");
RCLCPP_DEBUG(m_node->get_logger(), "$className$ num_retry: %d tick time: %ld", load_counter, duration_ms);
response->is_ok = true;
}/*END_TICK_CMD*/
/*HALT_CMD*/
Expand Down