36#include <rclcpp/clock.hpp>
37#include <rclcpp/logging.hpp>
40#pragma GCC diagnostic ignored "-Wold-style-cast"
55 robot_model_ = robot_model;
58 auto param_listener = online_signal_smoothing::ParamListener(node);
59 params_ = param_listener.get_params();
63 ruckig::InputParameter<ruckig::DynamicDOFs> ruckig_input(num_joints);
64 if (!getVelAccelJerkBounds(ruckig_input.max_velocity, ruckig_input.max_acceleration, ruckig_input.max_jerk))
68 ruckig_input.current_position = std::vector<double>(num_joints, 0.0);
69 ruckig_input.current_velocity = std::vector<double>(num_joints, 0.0);
70 ruckig_input.current_acceleration = std::vector<double>(num_joints, 0.0);
71 ruckig_input.synchronization = ruckig::Synchronization::Phase;
73 ruckig_input_ = ruckig_input;
75 ruckig_output_.emplace(ruckig::OutputParameter<ruckig::DynamicDOFs>(num_joints));
77 ruckig_.emplace(ruckig::Ruckig<ruckig::DynamicDOFs>(num_joints, params_.update_period));
83 Eigen::VectorXd& accelerations)
85 if (!ruckig_input_ || !ruckig_output_ || !ruckig_)
87 RCLCPP_ERROR_STREAM(
getLogger(),
"The Ruckig smoother was not initialized");
91 if (have_initial_ruckig_output_)
93 ruckig_output_->pass_to_input(*ruckig_input_);
97 ruckig_input_->target_position = std::vector<double>(positions.data(), positions.data() + positions.size());
100 const size_t num_joints = ruckig_input_->current_acceleration.size();
101 for (
size_t i = 0; i < num_joints; ++i)
103 ruckig_input_->target_velocity.at(i) =
104 ruckig_input_->current_velocity.at(i) + ruckig_input_->current_acceleration.at(i) * params_.update_period;
109 ruckig::Result ruckig_result = ruckig_->update(*ruckig_input_, *ruckig_output_);
116 if (ruckig_result != ruckig::Result::Finished && ruckig_result != ruckig::Result::Working &&
117 ruckig_result != ruckig::Result::ErrorSynchronizationCalculation)
120 RCLCPP_ERROR_STREAM(
getLogger(),
"Ruckig jerk-limited smoothing failed with code: " << ruckig_result);
123 have_initial_ruckig_output_ =
false;
128 positions = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(ruckig_output_->new_position.data(),
129 ruckig_output_->new_position.size());
130 velocities = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(ruckig_output_->new_velocity.data(),
131 ruckig_output_->new_velocity.size());
132 accelerations = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(ruckig_output_->new_acceleration.data(),
133 ruckig_output_->new_acceleration.size());
134 have_initial_ruckig_output_ =
true;
140 const Eigen::VectorXd& accelerations)
143 ruckig_input_->current_position = std::vector<double>(positions.data(), positions.data() + positions.size());
144 ruckig_input_->current_velocity = std::vector<double>(velocities.data(), velocities.data() + velocities.size());
145 ruckig_input_->current_acceleration =
146 std::vector<double>(accelerations.data(), accelerations.data() + accelerations.size());
148 have_initial_ruckig_output_ =
false;
152bool RuckigFilterPlugin::getVelAccelJerkBounds(std::vector<double>& joint_velocity_bounds,
153 std::vector<double>& joint_acceleration_bounds,
154 std::vector<double>& joint_jerk_bounds)
158 RCLCPP_ERROR(
getLogger(),
"The robot model was not initialized.");
162 joint_velocity_bounds.clear();
163 joint_acceleration_bounds.clear();
164 joint_jerk_bounds.clear();
166 auto joint_model_group = robot_model_->getJointModelGroup(params_.planning_group_name);
167 for (
const auto& joint : joint_model_group->getActiveJointModels())
169 const auto& bound = joint->getVariableBounds(joint->getName());
171 if (bound.velocity_bounded_)
174 joint_velocity_bounds.push_back(bound.max_velocity_);
178 RCLCPP_ERROR_STREAM(
getLogger(),
"No joint velocity limit defined for " << joint->getName() <<
".");
182 if (bound.acceleration_bounded_)
185 joint_acceleration_bounds.push_back(bound.max_acceleration_);
189 RCLCPP_WARN_STREAM(
getLogger(),
"No joint acceleration limit defined for " << joint->getName() <<
".");
193 if (bound.jerk_bounded_)
196 joint_jerk_bounds.push_back(bound.max_jerk_);
201 RCLCPP_WARN_STREAM(
getLogger(),
"No joint jerk limit was defined for "
202 << joint->getName() <<
". The output from Ruckig will not be jerk-limited.");
210void RuckigFilterPlugin::printRuckigState()
212 RCLCPP_INFO_STREAM(
getLogger(), ruckig_->delta_time <<
"\nRuckig input:\n"
213 << ruckig_input_->to_string() <<
"\nRuckig output:\n"
214 << ruckig_output_->to_string());
218#include <pluginlib/class_list_macros.hpp>
PLUGINLIB_EXPORT_CLASS(cached_ik_kinematics_plugin::CachedIKKinematicsPlugin< kdl_kinematics_plugin::KDLKinematicsPlugin >, kinematics::KinematicsBase)
bool doSmoothing(Eigen::VectorXd &positions, Eigen::VectorXd &velocities, Eigen::VectorXd &accelerations) override
bool initialize(rclcpp::Node::SharedPtr node, moveit::core::RobotModelConstPtr robot_model, size_t num_joints) override
bool reset(const Eigen::VectorXd &positions, const Eigen::VectorXd &velocities, const Eigen::VectorXd &accelerations) override
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
rclcpp::Logger getLogger()