Skip to content

Commit 5bda0a8

Browse files
committed
Twist Controller sync for open sourcing
This Commit builds on a rebased PR #300 The Original author @livanov93 was in 2022 open sourcing part of a PickNik package form 2021. This commit brings the internal picknik and previously open PR #300 back in sync and rebases the open PR onto humble.
1 parent 8f31cf7 commit 5bda0a8

File tree

4 files changed

+51
-41
lines changed

4 files changed

+51
-41
lines changed

cartesian_controllers/include/cartesian_controllers/twist_controller.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 VoodooIT, sole proprietorship
1+
// Copyright 2021, PickNik Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -21,14 +21,14 @@
2121

2222
#include "controller_interface/controller_interface.hpp"
2323
#include "geometry_msgs/msg/twist_stamped.hpp"
24-
#include "realtime_tools/realtime_buffer.h"
25-
2624
#include "cartesian_controllers/visibility_control.h"
25+
#include "realtime_tools/realtime_buffer.h"
2726

2827
namespace cartesian_controllers
2928
{
3029
using CmdType = geometry_msgs::msg::TwistStamped;
3130
using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
31+
3232
class TwistController : public controller_interface::ControllerInterface
3333
{
3434
public:
@@ -44,10 +44,6 @@ class TwistController : public controller_interface::ControllerInterface
4444
CARTESIAN_CONTROLLERS_PUBLIC
4545
CallbackReturn on_init() override;
4646

47-
CARTESIAN_CONTROLLERS_PUBLIC
48-
controller_interface::return_type update(
49-
const rclcpp::Time & time, const rclcpp::Duration & period) override;
50-
5147
CARTESIAN_CONTROLLERS_PUBLIC
5248
CallbackReturn on_configure(const rclcpp_lifecycle::State & previous_state) override;
5349

@@ -57,6 +53,10 @@ class TwistController : public controller_interface::ControllerInterface
5753
CARTESIAN_CONTROLLERS_PUBLIC
5854
CallbackReturn on_deactivate(const rclcpp_lifecycle::State & previous_state) override;
5955

56+
CARTESIAN_CONTROLLERS_PUBLIC
57+
controller_interface::return_type update(
58+
const rclcpp::Time & time, const rclcpp::Duration & period) override;
59+
6060
protected:
6161
std::string joint_name_;
6262
std::vector<std::string> interface_names_;

cartesian_controllers/include/cartesian_controllers/visibility_control.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 VoodooIT, sole proprietorship
1+
// Copyright 2021, PickNik Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

cartesian_controllers/src/twist_controller.cpp

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 VoodooIT, sole proprietorship
1+
// Copyright 2021, PickNik Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,8 +14,17 @@
1414

1515
#include "cartesian_controllers/twist_controller.hpp"
1616

17+
#include <memory>
18+
#include <string>
19+
#include <vector>
20+
21+
#include "controller_interface/helpers.hpp"
22+
#include "hardware_interface/loaned_command_interface.hpp"
23+
1724
namespace cartesian_controllers
1825
{
26+
using hardware_interface::LoanedCommandInterface;
27+
1928
TwistController::TwistController()
2029
: controller_interface::ControllerInterface(),
2130
rt_command_ptr_(nullptr),
@@ -60,38 +69,9 @@ CallbackReturn TwistController::on_init()
6069
return CallbackReturn::SUCCESS;
6170
}
6271

63-
controller_interface::return_type TwistController::update(
64-
const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/)
65-
{
66-
auto twist_commands = rt_command_ptr_.readFromRT();
67-
68-
// no command received yet
69-
if (!twist_commands || !(*twist_commands))
70-
{
71-
return controller_interface::return_type::OK;
72-
}
73-
74-
if (command_interfaces_.size() != 6)
75-
{
76-
RCLCPP_ERROR_THROTTLE(
77-
get_node()->get_logger(), *node_->get_clock(), 1000,
78-
"Twist controller needs does not match number of interfaces needed 6, given (%zu) interfaces",
79-
command_interfaces_.size());
80-
return controller_interface::return_type::ERROR;
81-
}
82-
command_interfaces_[0].set_value((*twist_commands)->twist.linear.x);
83-
command_interfaces_[1].set_value((*twist_commands)->twist.linear.y);
84-
command_interfaces_[2].set_value((*twist_commands)->twist.linear.z);
85-
command_interfaces_[3].set_value((*twist_commands)->twist.angular.x);
86-
command_interfaces_[4].set_value((*twist_commands)->twist.angular.y);
87-
command_interfaces_[5].set_value((*twist_commands)->twist.angular.z);
88-
89-
return controller_interface::return_type::OK;
90-
}
91-
9272
CallbackReturn TwistController::on_configure(const rclcpp_lifecycle::State & /*previous_state*/)
9373
{
94-
joint_name_ = node_->get_parameter("joint").as_string();
74+
joint_name_ = get_node()->get_parameter("joint").as_string();
9575

9676
if (joint_name_.empty())
9777
{
@@ -102,7 +82,7 @@ CallbackReturn TwistController::on_configure(const rclcpp_lifecycle::State & /*p
10282
// Specialized, child controllers set interfaces before calling configure function.
10383
if (interface_names_.empty())
10484
{
105-
interface_names_ = node_->get_parameter("interface_names").as_string_array();
85+
interface_names_ = get_node()->get_parameter("interface_names").as_string_array();
10686
}
10787

10888
if (interface_names_.empty())
@@ -133,7 +113,36 @@ CallbackReturn TwistController::on_deactivate(const rclcpp_lifecycle::State & /*
133113
return CallbackReturn::SUCCESS;
134114
}
135115

116+
controller_interface::return_type TwistController::update(
117+
const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/)
118+
{
119+
auto twist_commands = rt_command_ptr_.readFromRT();
120+
121+
// no command received yet
122+
if (!twist_commands || !(*twist_commands))
123+
{
124+
return controller_interface::return_type::OK;
125+
}
126+
127+
if (command_interfaces_.size() != 6)
128+
{
129+
RCLCPP_ERROR_THROTTLE(
130+
get_node()->get_logger(), *get_node()->get_clock(), 1000,
131+
"Twist controller needs does not match number of interfaces needed 6, given (%zu) interfaces",
132+
command_interfaces_.size());
133+
return controller_interface::return_type::ERROR;
134+
}
135+
command_interfaces_[0].set_value((*twist_commands)->twist.linear.x);
136+
command_interfaces_[1].set_value((*twist_commands)->twist.linear.y);
137+
command_interfaces_[2].set_value((*twist_commands)->twist.linear.z);
138+
command_interfaces_[3].set_value((*twist_commands)->twist.angular.x);
139+
command_interfaces_[4].set_value((*twist_commands)->twist.angular.y);
140+
command_interfaces_[5].set_value((*twist_commands)->twist.angular.z);
141+
142+
return controller_interface::return_type::OK;
143+
}
136144
} // namespace cartesian_controllers
145+
137146
#include "pluginlib/class_list_macros.hpp"
138147

139148
PLUGINLIB_EXPORT_CLASS(

cartesian_controllers/test/test_twist_controller.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "test_twist_controller.hpp"
1616

17+
#include <lifecycle_msgs/msg/state.hpp>
1718
#include <memory>
1819
#include <string>
1920
#include <utility>
@@ -151,7 +152,7 @@ TEST_F(TwistControllerTest, command_callback_test)
151152
auto node_state = controller_->configure();
152153
ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE);
153154

154-
node_state = controller_->activate();
155+
node_state = controller_->get_node()->activate();
155156
ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
156157

157158
// send a new command

0 commit comments

Comments
 (0)