Skip to content

Commit

Permalink
Added templates and generation for simulated services
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvannobel committed Jun 9, 2020
1 parent 8fe70f3 commit e0d02d3
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 9 deletions.
18 changes: 9 additions & 9 deletions kortex_driver/scripts/ros_kortex_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,13 @@ def generate_code(request, response):
if package.methods: # package contains at least one RPC
# Generate interface files
current_header_filename = "kortex_driver/generated/interfaces/{}_services_interface.h".format(package.short_name.lower())
current_interface_header_filename = current_header_filename
this_package_context.current_interface_header_filename = current_interface_header_filename
with open(os.path.join("..", "include/" + current_header_filename), 'wt') as services_file:
services_file.write(render("../templates/services_interface.h.jinja2", this_package_context.__dict__))

# Generate robot files
current_interface_header_filename = current_header_filename
current_header_filename = "kortex_driver/generated/robot/{}_services.h".format(package.short_name.lower())
this_package_context.current_interface_header_filename = current_interface_header_filename
this_package_context.current_header_filename = current_header_filename
this_package_context.include_file_names = include_file_names
with open(os.path.join("..", "include/" + current_header_filename), 'wt') as services_file:
Expand All @@ -538,13 +538,13 @@ def generate_code(request, response):
services_file.write(render("../templates/services_robot.cpp.jinja2", this_package_context.__dict__))

# Generate simulation files
# current_header_filename = "kortex_driver/generated/simulation/{}_services.h".format(package.short_name.lower())
# this_package_context.current_header_filename = current_header_filename
# this_package_context.include_file_names = include_file_names
# with open(os.path.join("..", "include/" + current_header_filename), 'wt') as services_file:
# services_file.write(render("../templates/services.h.jinja2", this_package_context.__dict__))
# with open(os.path.join("..", "src/generated/{}_services.cpp".format(package.short_name.lower())), 'wt') as services_file:
# services_file.write(render("../templates/services.cpp.jinja2", this_package_context.__dict__))
current_header_filename = "kortex_driver/generated/simulation/{}_services.h".format(package.short_name.lower())
this_package_context.current_header_filename = current_header_filename
this_package_context.include_file_names = include_file_names
with open(os.path.join("..", "include/" + current_header_filename), 'wt') as services_file:
services_file.write(render("../templates/services_simulation.h.jinja2", this_package_context.__dict__))
with open(os.path.join("..", "src/generated/simulation/{}_services.cpp".format(package.short_name.lower())), 'wt') as services_file:
services_file.write(render("../templates/services_simulation.cpp.jinja2", this_package_context.__dict__))

# Delete unused folders we created for None
for package in packages_dict.values():
Expand Down
81 changes: 81 additions & 0 deletions kortex_driver/templates/services_simulation.cpp.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* KINOVA (R) KORTEX (TM)
*
* Copyright (c) 2019 Kinova inc. All rights reserved.
*
* This software may be modified and distributed under the
* terms of the BSD 3-Clause license.
*
* Refer to the LICENSE file for details.
*
*/

/*
* This file has been auto-generated and should not be modified.
*/

{% for include_file_name in include_file_names -%}
#include "{{include_file_name}}"
{% endfor -%}
#include "{{current_header_filename}}"

{{package.short_name}}SimulationServices::{{package.short_name}}SimulationServices(ros::NodeHandle& node_handle):
I{{package.short_name}}Services(node_handle)
{
m_pub_Error = m_node_handle.advertise<kortex_driver::KortexError>("kortex_error", 1000);
{%- for method in package.methods -%}
{%- if method.is_notification_rpc %}
m_pub_{{method.name}} = m_node_handle.advertise<kortex_driver::{{method_prepend_rpc_package_name}}{{method.name|replace("Topic", "")}}Notification>("{{method.name_lowercase_with_underscores}}", 1000);
m_is_activated_{{method.name}} = false;
{%- endif -%}
{%- endfor %}

m_serviceSetDeviceID = m_node_handle.advertiseService("{{package.short_name_lowercase_with_underscores}}/set_device_id", &{{package.short_name}}SimulationServices::SetDeviceID, this);
m_serviceSetApiOptions = m_node_handle.advertiseService("{{package.short_name_lowercase_with_underscores}}/set_api_options", &{{package.short_name}}SimulationServices::SetApiOptions, this);
{% for method in package.methods %}
m_service{{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}} = m_node_handle.advertiseService("{{package.short_name_lowercase_with_underscores}}/{%- if method.prepend_on_notification -%}activate_publishing_of_{%- endif -%}{{method.name_lowercase_with_underscores}}", &{{package.short_name}}SimulationServices::{{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}, this);
{%- endfor %}
}

bool {{package.short_name}}SimulationServices::SetDeviceID(kortex_driver::SetDeviceID::Request &req, kortex_driver::SetDeviceID::Response &res)
{
ROS_WARN_ONCE("The SetDeviceID service is not implemented in simulation, and has no effect.");
return true;
}

bool {{package.short_name}}SimulationServices::SetApiOptions(kortex_driver::SetApiOptions::Request &req, kortex_driver::SetApiOptions::Response &res)
{
ROS_WARN_ONCE("The SetDeviceID service is not implemented in simulation, and has no effect.");
return true;
}

{% for method in package.methods %}
bool {{package.short_name}}SimulationServices::{{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}(kortex_driver::{{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}::Request &req, kortex_driver::{{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}::Response &res)
{
{% if method.is_rpc_deprecated -%}
ROS_WARN("The {{package.short_name_lowercase_with_underscores}}/{%- if method.prepend_on_notification -%}activate_publishing_of_{%- endif -%}{{method.name_lowercase_with_underscores}} service is now deprecated and will be removed in a future release.");
{% endif -%}

{%- if method.is_notification_rpc %}
m_is_activated_{{method.name}} = true;
{%- endif %}

if ({{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}Handler)
{
res = {{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}Handler(req);
}
else
{
ROS_WARN_ONCE("The simulation handler for {{package.short_name_lowercase_with_underscores}}/{%- if method.prepend_on_notification -%}activate_publishing_of_{%- endif -%}{{method.name_lowercase_with_underscores}} is not implemented, so the service calls will return the default response.");
}
return true;
}
{%- if method.is_notification_rpc %}
void {{package.short_name}}SimulationServices::cb_{{method.name}}({{method.notification_message_cpp_namespace}}::{{method.name|replace("Topic", "")}}Notification notif)
{
kortex_driver::{{method.name|replace("Topic", "")}}Notification ros_msg;
ToRosData(notif, ros_msg);
m_pub_{{method.name}}.publish(ros_msg);
}
{%- endif %}
{% endfor -%}
41 changes: 41 additions & 0 deletions kortex_driver/templates/services_simulation.h.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* KINOVA (R) KORTEX (TM)
*
* Copyright (c) 2019 Kinova inc. All rights reserved.
*
* This software may be modified and distributed under the
* terms of the BSD 3-Clause license.
*
* Refer to the LICENSE file for details.
*
*/

/*
* This file has been auto-generated and should not be modified.
*/

#ifndef _KORTEX_{{package.short_name|upper}}_SIMULATION_SERVICES_H_
#define _KORTEX_{{package.short_name|upper}}_SIMULATION_SERVICES_H_

#include "{{current_interface_header_filename}}"

using namespace std;

class {{package.short_name}}SimulationServices : public I{{package.short_name}}Services
{
public:
{{package.short_name}}SimulationServices(ros::NodeHandle& node_handle);

virtual bool SetDeviceID(kortex_driver::SetDeviceID::Request &req, kortex_driver::SetDeviceID::Response &res) override;
virtual bool SetApiOptions(kortex_driver::SetApiOptions::Request &req, kortex_driver::SetApiOptions::Response &res) override;
{%- for method in package.methods %}
std::function<kortex_driver::{{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}::Response&(kortex_driver::{{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}::Request&)> {{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}Handler = nullptr;
virtual bool {{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}(kortex_driver::{{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}::Request &req, kortex_driver::{{method.prepend_rpc_package_name}}{{method.prepend_on_notification}}{{method.name}}::Response &res) override;
{%- if method.is_notification_rpc %}
virtual void cb_{{method.name}}({{method.notification_message_cpp_namespace}}::{{method.prepend_rpc_package_name}}{{method.name|replace("Topic", "")}}Notification notif) override;
{%- endif %}
{%- endfor %}

};
#endif

0 comments on commit e0d02d3

Please sign in to comment.