|
| 1 | +// Copyright (c) 2022, PickNik, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +/// \author: Andy Zelenak, Paul Gesel |
| 16 | +/// \description: Base class for kinematics interface |
| 17 | + |
| 18 | +#include "kinematics_interface/kinematics_interface.hpp" |
| 19 | + |
| 20 | +namespace kinematics_interface |
| 21 | +{ |
| 22 | + |
| 23 | +rclcpp::Logger LOGGER = rclcpp::get_logger("kinematics_interface"); |
| 24 | + |
| 25 | +bool KinematicsInterface::convert_cartesian_deltas_to_joint_deltas( |
| 26 | + std::vector<double> & joint_pos_vec, const std::vector<double> & delta_x_vec, |
| 27 | + const std::string & link_name, std::vector<double> & delta_theta_vec) |
| 28 | +{ |
| 29 | + auto joint_pos = Eigen::Map<const Eigen::VectorXd>(joint_pos_vec.data(), joint_pos_vec.size()); |
| 30 | + auto delta_x = Eigen::Map<const Eigen::VectorXd>(delta_x_vec.data(), delta_x_vec.size()); |
| 31 | + // TODO(anyone): heap allocation should be removed for realtime use |
| 32 | + Eigen::VectorXd delta_theta = |
| 33 | + Eigen::Map<Eigen::VectorXd>(delta_theta_vec.data(), delta_theta_vec.size()); |
| 34 | + |
| 35 | + bool ret = convert_cartesian_deltas_to_joint_deltas(joint_pos, delta_x, link_name, delta_theta); |
| 36 | + for (auto i = 0ul; i < delta_theta_vec.size(); i++) |
| 37 | + { |
| 38 | + delta_theta_vec[i] = delta_theta[i]; |
| 39 | + } |
| 40 | + return ret; |
| 41 | +} |
| 42 | + |
| 43 | +bool KinematicsInterface::convert_joint_deltas_to_cartesian_deltas( |
| 44 | + const std::vector<double> & joint_pos_vec, const std::vector<double> & delta_theta_vec, |
| 45 | + const std::string & link_name, std::vector<double> & delta_x_vec) |
| 46 | +{ |
| 47 | + auto joint_pos = Eigen::Map<const Eigen::VectorXd>(joint_pos_vec.data(), joint_pos_vec.size()); |
| 48 | + Eigen::VectorXd delta_theta = |
| 49 | + Eigen::Map<const Eigen::VectorXd>(delta_theta_vec.data(), delta_theta_vec.size()); |
| 50 | + if (delta_x_vec.size() != 6) |
| 51 | + { |
| 52 | + RCLCPP_ERROR( |
| 53 | + LOGGER, "The length of the cartesian delta vector (%zu) must be 6.", delta_x_vec.size()); |
| 54 | + return false; |
| 55 | + } |
| 56 | + Eigen::Matrix<double, 6, 1> delta_x(delta_x_vec.data()); |
| 57 | + bool ret = convert_joint_deltas_to_cartesian_deltas(joint_pos, delta_theta, link_name, delta_x); |
| 58 | + for (auto i = 0ul; i < delta_x_vec.size(); i++) |
| 59 | + { |
| 60 | + delta_x_vec[i] = delta_x[i]; |
| 61 | + } |
| 62 | + return ret; |
| 63 | +} |
| 64 | + |
| 65 | +bool KinematicsInterface::calculate_link_transform( |
| 66 | + const std::vector<double> & joint_pos_vec, const std::string & link_name, |
| 67 | + Eigen::Isometry3d & transform) |
| 68 | +{ |
| 69 | + auto joint_pos = Eigen::Map<const Eigen::VectorXd>(joint_pos_vec.data(), joint_pos_vec.size()); |
| 70 | + |
| 71 | + return calculate_link_transform(joint_pos, link_name, transform); |
| 72 | +} |
| 73 | + |
| 74 | +bool KinematicsInterface::calculate_jacobian( |
| 75 | + const std::vector<double> & joint_pos_vec, const std::string & link_name, |
| 76 | + Eigen::Matrix<double, 6, Eigen::Dynamic> & jacobian) |
| 77 | +{ |
| 78 | + auto joint_pos = Eigen::Map<const Eigen::VectorXd>(joint_pos_vec.data(), joint_pos_vec.size()); |
| 79 | + |
| 80 | + return calculate_jacobian(joint_pos, link_name, jacobian); |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace kinematics_interface |
0 commit comments