72 std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> joint_handle,
73 const rclcpp_lifecycle::LifecycleNode::SharedPtr & )
75 joint_handle_ = joint_handle;
79 void starting(
const rclcpp::Time & ) {}
80 void stopping(
const rclcpp::Time & ) {}
83 double desired_position,
double ,
double ,
84 double ,
double max_allowed_effort)
87 joint_handle_->get().set_value(desired_position);
88 return max_allowed_effort;
92 std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> joint_handle_;
113 template <
typename ParameterT>
115 const std::shared_ptr<rclcpp_lifecycle::LifecycleNode> & node,
const std::string & name,
116 const ParameterT & default_value)
118 if (!node->has_parameter(name))
120 return node->declare_parameter<ParameterT>(name, default_value);
124 return node->get_parameter(name).get_value<ParameterT>();
129 std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> joint_handle,
130 const std::shared_ptr<rclcpp_lifecycle::LifecycleNode> & node)
132 joint_handle_ = joint_handle;
134 const std::string prefix =
"gains." + joint_handle_->get().get_prefix_name();
135 const auto k_p = auto_declare<double>(node, prefix +
".p", 0.0);
136 const auto k_i = auto_declare<double>(node, prefix +
".i", 0.0);
137 const auto k_d = auto_declare<double>(node, prefix +
".d", 0.0);
138 const auto i_clamp = auto_declare<double>(node, prefix +
".i_clamp", 0.0);
140 pid_ = std::make_shared<control_toolbox::Pid>(k_p, k_i, k_d, i_clamp, -i_clamp);
144 void starting(
const rclcpp::Time & )
152 joint_handle_->get().set_value(0.0);
155 void stopping(
const rclcpp::Time & ) {}
157 double updateCommand(
158 double ,
double ,
double error_position,
159 double error_velocity,
double max_allowed_effort)
167 const auto period = std::chrono::steady_clock::now() - last_update_time_;
170 pid_->computeCommand(error_position, error_velocity,
static_cast<uint64_t
>(period.count()));
171 command = std::min<double>(
172 fabs(max_allowed_effort), std::max<double>(-fabs(max_allowed_effort), command));
173 joint_handle_->get().set_value(command);
174 last_update_time_ = std::chrono::steady_clock::now();
179 using PidPtr = std::shared_ptr<control_toolbox::Pid>;
181 std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> joint_handle_;
182 std::chrono::steady_clock::time_point last_update_time_;