diff --git a/lbr_demos/lbr_demos_fri_ros2_advanced_cpp/src/admittance_controller.hpp b/lbr_demos/lbr_demos_fri_ros2_advanced_cpp/src/admittance_controller.hpp index d6d85f8a..70490451 100644 --- a/lbr_demos/lbr_demos_fri_ros2_advanced_cpp/src/admittance_controller.hpp +++ b/lbr_demos/lbr_demos_fri_ros2_advanced_cpp/src/admittance_controller.hpp @@ -14,7 +14,7 @@ #include "lbr_fri_msgs/msg/lbr_position_command.hpp" #include "lbr_fri_msgs/msg/lbr_state.hpp" -#include "lbr_fri_ros2/damped_least_squares.hpp" +#include "lbr_fri_ros2/lstsq.hpp" namespace lbr_fri_ros2 { class AdmittanceController { @@ -50,7 +50,7 @@ class AdmittanceController { jacobian_solver_->JntToJac(q_, jacobian_); - jacobian_inv_ = damped_least_squares(jacobian_.data); + jacobian_inv_ = lstsq(jacobian_.data); f_ext_ = jacobian_inv_.transpose() * tau_ext_; for (int i = 0; i < 6; i++) { diff --git a/lbr_fri_ros2/include/lbr_fri_ros2/damped_least_squares.hpp b/lbr_fri_ros2/include/lbr_fri_ros2/lstsq.hpp similarity index 56% rename from lbr_fri_ros2/include/lbr_fri_ros2/damped_least_squares.hpp rename to lbr_fri_ros2/include/lbr_fri_ros2/lstsq.hpp index 1f307608..16019a04 100644 --- a/lbr_fri_ros2/include/lbr_fri_ros2/damped_least_squares.hpp +++ b/lbr_fri_ros2/include/lbr_fri_ros2/lstsq.hpp @@ -1,5 +1,7 @@ -#ifndef LBR_FRI_ROS2__DAMPED_LEAST_SQUARES_HPP_ -#define LBR_FRI_ROS2__DAMPED_LEAST_SQUARES_HPP_ +#ifndef LBR_FRI_ROS2__LSTSQ_HPP_ +#define LBR_FRI_ROS2__LSTSQ_HPP_ + +#include #include "eigen3/Eigen/Core" #include "eigen3/Eigen/SVD" @@ -7,8 +9,8 @@ namespace lbr_fri_ros2 { template Eigen::Matrix -damped_least_squares(const MatT &mat, typename MatT::Scalar lambda = - typename MatT::Scalar{2e-1}) // choose appropriately +lstsq(const MatT &mat, + typename MatT::Scalar lambda = typename MatT::Scalar{2e-1}) // choose appropriately { typedef typename MatT::Scalar Scalar; auto svd = mat.jacobiSvd(Eigen::ComputeFullU | Eigen::ComputeFullV); @@ -16,11 +18,12 @@ damped_least_squares(const MatT &mat, typename MatT::Scalar lambda = Eigen::Matrix dampedSingularValuesInv( mat.cols(), mat.rows()); dampedSingularValuesInv.setZero(); - for (unsigned int i = 0; i < singularValues.size(); ++i) { - dampedSingularValuesInv(i, i) = - singularValues(i) / (singularValues(i) * singularValues(i) + lambda * lambda); - } + std::for_each(singularValues.data(), singularValues.data() + singularValues.size(), + [&, i = 0](Scalar &s)[mutable] { + dampedSingularValuesInv(i, i) = s / (s * s + lambda * lambda); + ++i; + }); return svd.matrixV() * dampedSingularValuesInv * svd.matrixU().adjoint(); } } // end of namespace lbr_fri_ros2 -#endif // LBR_FRI_ROS2__DAMPED_LEAST_SQUARES_HPP_ +#endif // LBR_FRI_ROS2__LSTSQ_HPP_