Skip to content

Commit 8cfbd91

Browse files
Rework plugin API
1 parent 48b6b56 commit 8cfbd91

File tree

3 files changed

+89
-56
lines changed

3 files changed

+89
-56
lines changed

joint_trajectory_controller_plugins/include/joint_trajectory_controller_plugins/pid_trajectory_plugin.hpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,60 +32,64 @@ namespace joint_trajectory_controller_plugins
3232
class PidTrajectoryPlugin : public TrajectoryControllerBase
3333
{
3434
public:
35-
bool initialize(
36-
rclcpp_lifecycle::LifecycleNode::SharedPtr node,
37-
std::vector<size_t> map_cmd_to_joints) override;
35+
bool update_gains_rt() override;
3836

39-
bool configure() override;
37+
void compute_commands(
38+
std::vector<double> & tmp_command, const trajectory_msgs::msg::JointTrajectoryPoint current,
39+
const trajectory_msgs::msg::JointTrajectoryPoint error,
40+
const trajectory_msgs::msg::JointTrajectoryPoint desired,
41+
const rclcpp::Duration & duration_since_start, const rclcpp::Duration & period) override;
4042

41-
bool activate() override;
43+
void reset() override;
4244

43-
bool compute_control_law_non_rt_impl(
44-
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & /*trajectory*/) override
45+
void start() override
4546
{
4647
// nothing to do
47-
return true;
4848
}
4949

50-
bool compute_control_law_rt_impl(
51-
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & /*trajectory*/) override
50+
protected:
51+
// Parameters from ROS for joint_trajectory_controller_plugins
52+
std::shared_ptr<ParamListener> param_listener_;
53+
54+
bool on_initialize(void) override;
55+
56+
rclcpp::Logger set_logger() const override
5257
{
53-
// nothing to do
54-
return true;
58+
return node_->get_logger().get_child("PidTrajectoryPlugin");
5559
}
5660

57-
bool update_gains_rt() override;
61+
bool on_configure() override;
5862

59-
void compute_commands(
60-
std::vector<double> & tmp_command, const trajectory_msgs::msg::JointTrajectoryPoint current,
61-
const trajectory_msgs::msg::JointTrajectoryPoint error,
62-
const trajectory_msgs::msg::JointTrajectoryPoint desired,
63-
const rclcpp::Duration & duration_since_start, const rclcpp::Duration & period) override;
63+
bool on_activate() override;
6464

65-
void reset() override;
65+
bool on_compute_control_law_non_rt(
66+
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & /*trajectory*/) override
67+
{
68+
// nothing to do
69+
return true;
70+
}
6671

67-
void start() override
72+
bool on_compute_control_law_rt(
73+
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & /*trajectory*/) override
6874
{
6975
// nothing to do
76+
return true;
7077
}
7178

72-
protected:
79+
private:
7380
/**
7481
* @brief parse PID gains from parameter struct
7582
*/
7683
void parse_gains();
7784

7885
// number of command joints
7986
size_t num_cmd_joints_;
80-
// map from joints in the message to command joints
81-
std::vector<size_t> map_cmd_to_joints_;
8287
// PID controllers
8388
std::vector<PidPtr> pids_;
8489
// Feed-forward velocity weight factor when calculating closed loop pid adapter's command
8590
std::vector<double> ff_velocity_scale_;
8691

8792
// Parameters from ROS for joint_trajectory_controller_plugins
88-
std::shared_ptr<ParamListener> param_listener_;
8993
Params params_;
9094
};
9195

joint_trajectory_controller_plugins/include/joint_trajectory_controller_plugins/trajectory_controller_base.hpp

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,28 @@ class TrajectoryControllerBase
3737

3838
/**
3939
* @brief initialize the controller plugin.
40-
* declare parameters
4140
* @param node the node handle to use for parameter handling
4241
* @param map_cmd_to_joints a mapping from the joint names in the trajectory messages to the
4342
* command joints
4443
*/
45-
virtual bool initialize(
46-
rclcpp_lifecycle::LifecycleNode::SharedPtr node, std::vector<size_t> map_cmd_to_joints) = 0;
44+
bool initialize(
45+
rclcpp_lifecycle::LifecycleNode::SharedPtr node, std::vector<size_t> map_cmd_to_joints)
46+
{
47+
node_ = node;
48+
map_cmd_to_joints_ = map_cmd_to_joints;
49+
logger_ = this->set_logger();
50+
return on_initialize();
51+
};
4752

4853
/**
4954
* @brief configure the controller plugin.
50-
* parse read-only parameters, pre-allocate memory for the controller
5155
*/
52-
virtual bool configure() = 0;
56+
bool configure() { return on_configure(); }
5357

5458
/**
5559
* @brief activate the controller plugin.
56-
* parse parameters
5760
*/
58-
virtual bool activate() = 0;
61+
bool activate() { return on_activate(); }
5962

6063
/**
6164
* @brief compute the control law for the given trajectory
@@ -64,15 +67,15 @@ class TrajectoryControllerBase
6467
* of the trajectory until it finishes
6568
*
6669
* this method is not virtual, any overrides won't be called by JTC. Instead, override
67-
* compute_control_law_non_rt_impl for your implementation
70+
* on_compute_control_law_non_rt for your implementation
6871
*
6972
* @return true if the gains were computed, false otherwise
7073
*/
7174
bool compute_control_law_non_rt(
7275
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & trajectory)
7376
{
7477
rt_control_law_ready_.writeFromNonRT(false);
75-
auto ret = compute_control_law_non_rt_impl(trajectory);
78+
auto ret = on_compute_control_law_non_rt(trajectory);
7679
rt_control_law_ready_.writeFromNonRT(true);
7780
return ret;
7881
}
@@ -83,7 +86,7 @@ class TrajectoryControllerBase
8386
* this method must finish quickly (within one controller-update rate)
8487
*
8588
* this method is not virtual, any overrides won't be called by JTC. Instead, override
86-
* compute_control_law_rt_impl for your implementation
89+
* on_compute_control_law_rt for your implementation
8790
*
8891
* @return true if the gains were computed, false otherwise
8992
*/
@@ -92,7 +95,7 @@ class TrajectoryControllerBase
9295
{
9396
// TODO(christophfroehlich): Need a lock-free write here
9497
rt_control_law_ready_.writeFromNonRT(false);
95-
auto ret = compute_control_law_rt_impl(trajectory);
98+
auto ret = on_compute_control_law_rt(trajectory);
9699
rt_control_law_ready_.writeFromNonRT(true);
97100
return ret;
98101
}
@@ -145,22 +148,58 @@ class TrajectoryControllerBase
145148
protected:
146149
// the node handle for parameter handling
147150
rclcpp_lifecycle::LifecycleNode::SharedPtr node_;
151+
// map from joints in the message to command joints
152+
std::vector<size_t> map_cmd_to_joints_;
148153
// Are we computing the control law or is it valid?
149154
realtime_tools::RealtimeBuffer<bool> rt_control_law_ready_;
150155

156+
/**
157+
* @brief Get the logger for this plugin
158+
*/
159+
rclcpp::Logger get_logger() const { return logger_; }
160+
/**
161+
* @brief Get the logger for this plugin
162+
*/
163+
virtual rclcpp::Logger set_logger() const = 0;
164+
151165
/**
152166
* @brief compute the control law from the given trajectory (in the non-RT loop)
153167
* @return true if the gains were computed, false otherwise
154168
*/
155-
virtual bool compute_control_law_non_rt_impl(
169+
virtual bool on_compute_control_law_non_rt(
156170
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & trajectory) = 0;
157171

158172
/**
159173
* @brief compute the control law for a single point (in the RT loop)
160174
* @return true if the gains were computed, false otherwise
161175
*/
162-
virtual bool compute_control_law_rt_impl(
176+
virtual bool on_compute_control_law_rt(
163177
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & trajectory) = 0;
178+
179+
/**
180+
* @brief initialize the controller plugin.
181+
*
182+
* declare parameters
183+
*/
184+
virtual bool on_initialize(void) = 0;
185+
186+
/**
187+
* @brief configure the controller plugin.
188+
*
189+
* parse read-only parameters, pre-allocate memory for the controller
190+
*/
191+
virtual bool on_configure() = 0;
192+
193+
/**
194+
* @brief activate the controller plugin.
195+
*
196+
* parse parameters
197+
*/
198+
virtual bool on_activate() = 0;
199+
200+
private:
201+
// child logger for this plugin
202+
rclcpp::Logger logger_ = rclcpp::get_logger("joint_trajectory_controller_plugins");
164203
};
165204

166205
} // namespace joint_trajectory_controller_plugins

joint_trajectory_controller_plugins/src/pid_trajectory_plugin.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717
namespace joint_trajectory_controller_plugins
1818
{
1919

20-
bool PidTrajectoryPlugin::initialize(
21-
rclcpp_lifecycle::LifecycleNode::SharedPtr node, std::vector<size_t> map_cmd_to_joints)
20+
bool PidTrajectoryPlugin::on_initialize()
2221
{
23-
node_ = node;
24-
map_cmd_to_joints_ = map_cmd_to_joints;
25-
2622
try
2723
{
2824
// Create the parameter listener and get the parameters
@@ -37,7 +33,7 @@ bool PidTrajectoryPlugin::initialize(
3733
return true;
3834
}
3935

40-
bool PidTrajectoryPlugin::configure()
36+
bool PidTrajectoryPlugin::on_configure()
4137
{
4238
try
4339
{
@@ -53,14 +49,12 @@ bool PidTrajectoryPlugin::configure()
5349
num_cmd_joints_ = params_.command_joints.size();
5450
if (num_cmd_joints_ == 0)
5551
{
56-
RCLCPP_ERROR(node_->get_logger(), "[PidTrajectoryPlugin] No command joints specified.");
52+
RCLCPP_ERROR(get_logger(), "No command joints specified.");
5753
return false;
5854
}
5955
if (num_cmd_joints_ != map_cmd_to_joints_.size())
6056
{
61-
RCLCPP_ERROR(
62-
node_->get_logger(),
63-
"[PidTrajectoryPlugin] map_cmd_to_joints has to be of size num_cmd_joints.");
57+
RCLCPP_ERROR(get_logger(), "map_cmd_to_joints has to be of size num_cmd_joints.");
6458
return false;
6559
}
6660
pids_.resize(num_cmd_joints_); // memory for the shared pointers, will be nullptr
@@ -74,7 +68,7 @@ bool PidTrajectoryPlugin::configure()
7468
return true;
7569
}
7670

77-
bool PidTrajectoryPlugin::activate()
71+
bool PidTrajectoryPlugin::on_activate()
7872
{
7973
params_ = param_listener_->get_params();
8074
parse_gains();
@@ -97,8 +91,7 @@ void PidTrajectoryPlugin::parse_gains()
9791
for (size_t i = 0; i < num_cmd_joints_; ++i)
9892
{
9993
RCLCPP_DEBUG(
100-
node_->get_logger(), "[PidTrajectoryPlugin] params_.command_joints %lu : %s", i,
101-
params_.command_joints[i].c_str());
94+
get_logger(), "params_.command_joints %lu : %s", i, params_.command_joints[i].c_str());
10295

10396
const auto & gains = params_.gains.command_joints_map.at(params_.command_joints[i]);
10497
control_toolbox::AntiWindupStrategy antiwindup_strat;
@@ -111,15 +104,12 @@ void PidTrajectoryPlugin::parse_gains()
111104
gains.p, gains.i, gains.d, gains.u_clamp_max, gains.u_clamp_min, antiwindup_strat);
112105
ff_velocity_scale_[i] = gains.ff_velocity_scale;
113106

114-
RCLCPP_DEBUG(node_->get_logger(), "[PidTrajectoryPlugin] gains.p: %f", gains.p);
115-
RCLCPP_DEBUG(
116-
node_->get_logger(), "[PidTrajectoryPlugin] ff_velocity_scale_: %f", ff_velocity_scale_[i]);
107+
RCLCPP_DEBUG(get_logger(), "gains.p: %f", gains.p);
108+
RCLCPP_DEBUG(get_logger(), "ff_velocity_scale_: %f", ff_velocity_scale_[i]);
117109
}
118110

119111
RCLCPP_INFO(
120-
node_->get_logger(),
121-
"[PidTrajectoryPlugin] Loaded PID gains from ROS parameters for %lu joint(s).",
122-
num_cmd_joints_);
112+
get_logger(), "Loaded PID gains from ROS parameters for %lu joint(s).", num_cmd_joints_);
123113
}
124114

125115
void PidTrajectoryPlugin::compute_commands(

0 commit comments

Comments
 (0)