Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating templates with developments from mecanum_controller #106

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
f885b01
added reference_names and modified cmd and state joint names param
Feb 21, 2023
c7b2e7f
updated .cpp and .hpp from mecanum
Feb 21, 2023
4bacc37
update yaml
Feb 21, 2023
418af78
update test preceeding load
Feb 21, 2023
cca54cd
update test.hpp
Feb 21, 2023
06550cf
updated tests in templates
Feb 21, 2023
e711a73
Apply suggestions from code review
Robotgir Feb 23, 2023
76e1cd9
Merge branch 'master' into updating-templates-from-mecanum
destogl Feb 23, 2023
98e5b80
Apply suggestions from code review
Robotgir Feb 24, 2023
baa7cb2
correction
Feb 24, 2023
7510fca
corrections
Feb 24, 2023
b4e62b8
corrections
Feb 24, 2023
de291d4
correction
Feb 24, 2023
82550f7
ran pre-commit
Feb 24, 2023
f5c327f
added missing test when wrong num of joints
Mar 14, 2023
20ceb87
adjusting .hpp w.r.t chainable .hpp
Mar 14, 2023
2fd0dca
updated regular controller with renaming tests and improvements from …
Mar 14, 2023
00d6daa
updated regular controller with improvements from mecanum
Mar 14, 2023
c3fcaa8
Merge branch 'master' into updating-templates-from-mecanum
destogl Mar 21, 2023
f298666
Small fiexes to make code more general
destogl Mar 21, 2023
b996dbf
all tests pass except load_contr on standard controller
Mar 23, 2023
56c463d
corrections
Mar 23, 2023
3f3e1db
correction
Mar 23, 2023
4e5fca2
ran precommit and changed config file to one from ros2_control
Mar 23, 2023
1ab15d7
all tests pass except two
Mar 23, 2023
d79cea1
correction
Apr 3, 2023
cdd1779
corrections
Apr 3, 2023
541215e
correction
Apr 3, 2023
5c485e7
corrections
Apr 3, 2023
d0c6f70
Add Changes from master.
destogl May 16, 2023
a971696
Merge remote-tracking branch 'origin/master' into updating-templates-…
destogl May 16, 2023
d55376f
Correct formatting.
destogl May 16, 2023
24fb0e2
Apply suggestions from code review
destogl May 16, 2023
ce73a02
Fixed wrong format when printing size
gwalck May 16, 2023
11601af
Fixed deprecated warnings
gwalck May 16, 2023
b65756a
Simplified reference access
gwalck May 16, 2023
5808d19
Fixed library path for dummy_controller
gwalck May 16, 2023
2981921
added missing old_time_stamp test
gwalck May 17, 2023
e4316ea
Fixed new API of update_reference_from_subscribers
gwalck May 17, 2023
362ed82
Fix NR_CMD_ITFS and NR_STATE_ITFS
gwalck May 17, 2023
3d3c03b
lint
gwalck Jun 1, 2023
6b0bbdd
Merge branch 'master' into updating-templates-from-mecanum
destogl Aug 17, 2023
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
91 changes: 57 additions & 34 deletions templates/ros2_control/controller/dummy_chainable_controller.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, Stogl Robotics Consulting UG (haftungsbeschränkt) (template)
// Copyright (c) 2023, Stogl Robotics Consulting UG (haftungsbeschränkt) (template)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,8 +41,10 @@ using ControllerReferenceMsg = dummy_package_namespace::DummyClassName::Controll

// called from RT control loop
void reset_controller_reference_msg(
const std::shared_ptr<ControllerReferenceMsg> & msg, const std::vector<std::string> & joint_names)
const std::shared_ptr<ControllerReferenceMsg> & msg, const std::vector<std::string> & joint_names,
const std::shared_ptr<rclcpp_lifecycle::LifecycleNode> & node)
{
msg->header.stamp = node->now();
msg->joint_names = joint_names;
msg->displacements.resize(joint_names.size(), std::numeric_limits<double>::quiet_NaN());
msg->velocities.resize(joint_names.size(), std::numeric_limits<double>::quiet_NaN());
Expand Down Expand Up @@ -77,21 +79,22 @@ controller_interface::CallbackReturn DummyClassName::on_configure(
{
params_ = param_listener_->get_params();

if (!params_.state_joints.empty())
if (!params_.state_joint_names.empty())
{
state_joints_ = params_.state_joints;
state_joint_names_ = params_.state_joint_names;
}
else
{
state_joints_ = params_.joints;
state_joint_names_ = params_.command_joint_names;
}

if (params_.joints.size() != state_joints_.size())
if (params_.command_joint_names.size() != state_joint_names_.size())
{
RCLCPP_FATAL(
get_node()->get_logger(),
"Size of 'joints' (%zu) and 'state_joints' (%zu) parameters has to be the same!",
params_.joints.size(), state_joints_.size());
"Size of 'command_joint_names' (%zu) and 'state_joint_names' (%zu) parameters has to be the "
"same!",
params_.command_joint_names.size(), state_joint_names_.size());
return CallbackReturn::FAILURE;
}

Expand All @@ -106,7 +109,7 @@ controller_interface::CallbackReturn DummyClassName::on_configure(
std::bind(&DummyClassName::reference_callback, this, std::placeholders::_1));

std::shared_ptr<ControllerReferenceMsg> msg = std::make_shared<ControllerReferenceMsg>();
reset_controller_reference_msg(msg, params_.joints);
reset_controller_reference_msg(msg, params_.command_joint_names, get_node());
input_ref_.writeFromNonRT(msg);

auto set_slow_mode_service_callback =
Expand All @@ -127,13 +130,15 @@ controller_interface::CallbackReturn DummyClassName::on_configure(

set_slow_control_mode_service_ = get_node()->create_service<ControllerModeSrvType>(
"~/set_slow_control_mode", set_slow_mode_service_callback,
rmw_qos_profile_services_hist_keep_all);
rclcpp::QoS(
rclcpp::QoSInitialization::from_rmw(rmw_qos_profile_services_hist_keep_all),
rmw_qos_profile_services_hist_keep_all));

try
{
// State publisher
s_publisher_ =
get_node()->create_publisher<ControllerStateMsg>("~/state", rclcpp::SystemDefaultsQoS());
s_publisher_ = get_node()->create_publisher<ControllerStateMsg>(
"~/controller_state", rclcpp::SystemDefaultsQoS());
state_publisher_ = std::make_unique<ControllerStatePublisher>(s_publisher_);
}
catch (const std::exception & e)
Expand All @@ -146,7 +151,7 @@ controller_interface::CallbackReturn DummyClassName::on_configure(

// TODO(anyone): Reserve memory in state publisher depending on the message type
state_publisher_->lock();
state_publisher_->msg_.header.frame_id = params_.joints[0];
state_publisher_->msg_.header.frame_id = params_.command_joint_names[0];
state_publisher_->unlock();

RCLCPP_INFO(get_node()->get_logger(), "configure successful");
Expand All @@ -158,8 +163,8 @@ controller_interface::InterfaceConfiguration DummyClassName::command_interface_c
controller_interface::InterfaceConfiguration command_interfaces_config;
command_interfaces_config.type = controller_interface::interface_configuration_type::INDIVIDUAL;

command_interfaces_config.names.reserve(params_.joints.size());
for (const auto & joint : params_.joints)
command_interfaces_config.names.reserve(params_.command_joint_names.size());
for (const auto & joint : params_.command_joint_names)
Robotgir marked this conversation as resolved.
Show resolved Hide resolved
{
command_interfaces_config.names.push_back(joint + "/" + params_.interface_name);
}
Expand All @@ -172,8 +177,8 @@ controller_interface::InterfaceConfiguration DummyClassName::state_interface_con
controller_interface::InterfaceConfiguration state_interfaces_config;
state_interfaces_config.type = controller_interface::interface_configuration_type::INDIVIDUAL;

state_interfaces_config.names.reserve(state_joints_.size());
for (const auto & joint : state_joints_)
state_interfaces_config.names.reserve(state_joint_names_.size());
for (const auto & joint : state_joint_names_)
{
state_interfaces_config.names.push_back(joint + "/" + params_.interface_name);
}
Expand All @@ -183,30 +188,40 @@ controller_interface::InterfaceConfiguration DummyClassName::state_interface_con

void DummyClassName::reference_callback(const std::shared_ptr<ControllerReferenceMsg> msg)
{
if (msg->joint_names.size() == params_.joints.size())
// if no timestamp provided use current time for command timestamp
if (msg->header.stamp.sec == 0 && msg->header.stamp.nanosec == 0u)
{
RCLCPP_WARN(
get_node()->get_logger(),
"Timestamp in header is missing, using current time as command "
"timestamp.");
msg->header.stamp = get_node()->now();
}

if (msg->joint_names.size() == params_.command_joint_names.size())
{
input_ref_.writeFromNonRT(msg);
}
else
{
RCLCPP_ERROR(
get_node()->get_logger(),
"Received %zu , but expected %zu joints in command. Ignoring message.",
msg->joint_names.size(), params_.joints.size());
"Received %zu , but expected %zu command_joint_names in command. Ignoring message.",
msg->joint_names.size(), params_.command_joint_names.size());
}
}

std::vector<hardware_interface::CommandInterface> DummyClassName::on_export_reference_interfaces()
{
reference_interfaces_.resize(state_joints_.size(), std::numeric_limits<double>::quiet_NaN());
reference_interfaces_.resize(NR_REF_ITFS, std::numeric_limits<double>::quiet_NaN());

std::vector<hardware_interface::CommandInterface> reference_interfaces;
reference_interfaces.reserve(reference_interfaces_.size());

for (size_t i = 0; i < reference_interfaces_.size(); ++i)
for (size_t i = 0; i < NR_REF_ITFS; ++i)
{
reference_interfaces.push_back(hardware_interface::CommandInterface(
get_node()->get_name(), state_joints_[i] + "/" + params_.interface_name,
get_node()->get_name(), state_joint_names_[i] + "/" + params_.interface_name,
&reference_interfaces_[i]));
}

Expand All @@ -223,36 +238,35 @@ controller_interface::CallbackReturn DummyClassName::on_activate(
const rclcpp_lifecycle::State & /*previous_state*/)
{
// Set default value in command
reset_controller_reference_msg(*(input_ref_.readFromRT()), state_joints_);
reset_controller_reference_msg(*(input_ref_.readFromRT()), state_joint_names_, get_node());

return controller_interface::CallbackReturn::SUCCESS;
}

controller_interface::CallbackReturn DummyClassName::on_deactivate(
const rclcpp_lifecycle::State & /*previous_state*/)
{
// TODO(anyone): depending on number of interfaces, use definitions, e.g., `CMD_MY_ITFS`,
// TODO(anyone): depending on number of interfaces, use definitions, e.g., `NR_CMD_ITFS`,
// instead of a loop
destogl marked this conversation as resolved.
Show resolved Hide resolved
for (size_t i = 0; i < command_interfaces_.size(); ++i)
for (size_t i = 0; i < NR_CMD_ITFS; ++i)
{
command_interfaces_[i].set_value(std::numeric_limits<double>::quiet_NaN());
}
return controller_interface::CallbackReturn::SUCCESS;
}

controller_interface::return_type DummyClassName::update_reference_from_subscribers()
controller_interface::return_type DummyClassName::update_reference_from_subscribers(
const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/)
{
auto current_ref = input_ref_.readFromRT();

// TODO(anyone): depending on number of interfaces, use definitions, e.g., `CMD_MY_ITFS`,
// TODO(anyone): depending on number of interfaces, use definitions, e.g., `NR_CMD_ITFS`,
// instead of a loop
destogl marked this conversation as resolved.
Show resolved Hide resolved
for (size_t i = 0; i < reference_interfaces_.size(); ++i)
for (size_t i = 0; i < NR_REF_ITFS; ++i)
{
if (!std::isnan((*current_ref)->displacements[i]))
{
reference_interfaces_[i] = (*current_ref)->displacements[i];

(*current_ref)->displacements[i] = std::numeric_limits<double>::quiet_NaN();
}
}
return controller_interface::return_type::OK;
Expand All @@ -261,9 +275,9 @@ controller_interface::return_type DummyClassName::update_reference_from_subscrib
controller_interface::return_type DummyClassName::update_and_write_commands(
const rclcpp::Time & time, const rclcpp::Duration & /*period*/)
{
// TODO(anyone): depending on number of interfaces, use definitions, e.g., `CMD_MY_ITFS`,
// TODO(anyone): depending on number of interfaces, use definitions, e.g., `NR_CMD_ITFS`,
// instead of a loop
destogl marked this conversation as resolved.
Show resolved Hide resolved
for (size_t i = 0; i < command_interfaces_.size(); ++i)
for (size_t i = 0; i < NR_CMD_ITFS; ++i)
{
if (!std::isnan(reference_interfaces_[i]))
{
Expand All @@ -275,16 +289,25 @@ controller_interface::return_type DummyClassName::update_and_write_commands(

reference_interfaces_[i] = std::numeric_limits<double>::quiet_NaN();
}
else
{
// NOTE: use this only if using velocity input
command_interfaces_[i].set_value(0.0);
}
}

if (state_publisher_ && state_publisher_->trylock())
{
state_publisher_->msg_.header.stamp = time;
state_publisher_->msg_.set_point = command_interfaces_[CMD_MY_ITFS].get_value();
state_publisher_->msg_.set_point = command_interfaces_[NR_CMD_ITFS - 1].get_value();

state_publisher_->unlockAndPublish();
}

for (size_t i = 0; i < NR_REF_ITFS; ++i)
{
reference_interfaces_[i] = std::numeric_limits<double>::quiet_NaN();
}
return controller_interface::return_type::OK;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<library path="dummy_package_namespace">
<library path="dummy_controller">
<class name="dummy_package_namespace/DummyClassName"
type="dummy_package_namespace::DummyClassName" base_class_type="controller_interface::ChainableControllerInterface">
<description>
Expand Down
Loading
Loading