|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +#include <algorithm> |
15 | 16 | #include <fstream> |
16 | 17 | #include <iostream> |
| 18 | +#include <limits> |
17 | 19 | #include <string> |
18 | 20 |
|
19 | 21 | #include "kinematics_utils.hpp" |
@@ -57,6 +59,7 @@ std::vector<manipulators_link::Link> parse_link_config_file(const std::string & |
57 | 59 | const int COL_INERTIA_YZ = 22; |
58 | 60 | const int COL_INERTIA_ZZ = 23; |
59 | 61 | const int COL_AXIS_OF_ROTATION = 29; |
| 62 | + const int COL_DXL_ID = 32; |
60 | 63 | const double MM_TO_METERS = 1e-3; |
61 | 64 | const double MM2_TO_METERS2 = 1e-9; |
62 | 65 | const double G_TO_KG = 1e-3; |
@@ -157,6 +160,11 @@ std::vector<manipulators_link::Link> parse_link_config_file(const std::string & |
157 | 160 | // link.c = rot * link.c; |
158 | 161 | // link.I = rot * link.I * rot.transpose(); |
159 | 162 |
|
| 163 | + try { |
| 164 | + link.dxl_id = std::stoi(str_vec[COL_DXL_ID]); |
| 165 | + } catch (...) { |
| 166 | + } |
| 167 | + |
160 | 168 | links.push_back(link); |
161 | 169 | } |
162 | 170 |
|
@@ -228,4 +236,117 @@ Eigen::Matrix3d rotation_from_euler_ZYX( |
228 | 236 | return q.matrix(); |
229 | 237 | } |
230 | 238 |
|
| 239 | +Eigen::Vector3d rotation_to_axis_angle_representation(const Eigen::Matrix3d & mat) { |
| 240 | + // 姿勢行列から等価角軸ベクトルへの変換 |
| 241 | + // 参考:https://www.jstage.jst.go.jp/article/jrsj/29/3/29_3_269/_pdf/-char/ja |
| 242 | + auto l = Eigen::Vector3d( |
| 243 | + mat(2, 1) - mat(1, 2), |
| 244 | + mat(0, 2) - mat(2, 0), |
| 245 | + mat(1, 0) - mat(0, 1)); |
| 246 | + auto l_norm = l.norm(); |
| 247 | + |
| 248 | + if (l_norm != 0) { |
| 249 | + return (std::atan2(l_norm, mat.trace() - 1) / l_norm) * l; |
| 250 | + } else if (mat(0, 0) > 0 && mat(1, 1) > 0 && mat(2, 2) > 0) { |
| 251 | + // 行列の対角成分がすべて1のとき、ゼロベクトルを返す |
| 252 | + return Eigen::Vector3d::Zero(); |
| 253 | + } |
| 254 | + // 行列の対角成分が(1, -1, -1), (-1, 1, -1), (-1, -1, 1)のときの処理 |
| 255 | + return M_PI_2 * Eigen::Vector3d(mat(0, 0) + 1, mat(1, 1) + 1, mat(2, 2) + 1); |
| 256 | +} |
| 257 | + |
| 258 | +std::vector<link_id_t> find_route(const links_t & links, const link_id_t & target_id) { |
| 259 | + // 目標リンク(target_id)までの経路を抽出する |
| 260 | + // 返り値のベクトルの末尾がtarget_idになる |
| 261 | + std::vector<link_id_t> id_list; |
| 262 | + |
| 263 | + if (target_id <= 1 || target_id >= links.size()) { |
| 264 | + std::cerr << __func__ << ": 目標リンクIDには1より大きく、" |
| 265 | + << "linksのサイズより小さい数値をセットしてください" << std::endl; |
| 266 | + return id_list; |
| 267 | + } |
| 268 | + |
| 269 | + id_list.push_back(target_id); |
| 270 | + |
| 271 | + auto parent_id = links[target_id].parent; |
| 272 | + // ベースリンク(ID = 1)は含めない |
| 273 | + while (parent_id != 1) { |
| 274 | + id_list.push_back(parent_id); |
| 275 | + parent_id = links[parent_id].parent; |
| 276 | + } |
| 277 | + |
| 278 | + // 末尾をtarget_idにするため、ベクトルの並びを逆順にする |
| 279 | + std::reverse(id_list.begin(), id_list.end()); |
| 280 | + return id_list; |
| 281 | +} |
| 282 | + |
| 283 | +q_list_t get_q_list(const links_t & links, const std::vector<link_id_t> & id_list) { |
| 284 | + // リンク構成から指定されたIDの関節位置qを抽出する |
| 285 | + q_list_t q_list; |
| 286 | + for (auto target_id : id_list) { |
| 287 | + if (target_id < links.size()) { |
| 288 | + q_list[target_id] = links[target_id].q; |
| 289 | + } else { |
| 290 | + std::cerr << __func__ << ": 無効なIDです:" << target_id << std::endl; |
| 291 | + } |
| 292 | + } |
| 293 | + return q_list; |
| 294 | +} |
| 295 | + |
| 296 | +bool set_q_list(links_t & links, const q_list_t & q_list, const bool & within_limit) { |
| 297 | + // リンク構成の指定されたIDに関節位置qを書き込む |
| 298 | + bool result = true; |
| 299 | + for (const auto & [target_id, q_value] : q_list) { |
| 300 | + if (target_id < links.size()) { |
| 301 | + if (within_limit) { |
| 302 | + links[target_id].set_q_within_limit(q_value); |
| 303 | + } else { |
| 304 | + links[target_id].q = q_value; |
| 305 | + } |
| 306 | + } else { |
| 307 | + std::cerr << __func__ << ": 無効なIDです:" << target_id << std::endl; |
| 308 | + result = false; |
| 309 | + } |
| 310 | + } |
| 311 | + return result; |
| 312 | +} |
| 313 | + |
| 314 | +Eigen::Vector3d calc_error_R(const Eigen::Matrix3d & target, const Eigen::Matrix3d & current) { |
| 315 | + // 回転行列の差を求める |
| 316 | + return rotation_to_axis_angle_representation(target * current.transpose()); |
| 317 | +} |
| 318 | + |
| 319 | +Eigen::Vector3d calc_error_p(const Eigen::Vector3d & target, const Eigen::Vector3d & current) { |
| 320 | + // 位置の差を求める |
| 321 | + return target - current; |
| 322 | +} |
| 323 | + |
| 324 | +Eigen::VectorXd calc_error( |
| 325 | + const Eigen::Vector3d & target_p, const Eigen::Matrix3d & target_R, |
| 326 | + const manipulators_link::Link & current_link) { |
| 327 | + // 目標位置・姿勢と、リンクの現在位置・姿勢との差を求める |
| 328 | + auto error_p = calc_error_p(target_p, current_link.p); |
| 329 | + auto error_r = calc_error_R(target_R, current_link.R); |
| 330 | + |
| 331 | + Eigen::VectorXd error(6); |
| 332 | + error << error_p, error_r; |
| 333 | + return error; |
| 334 | +} |
| 335 | + |
| 336 | +Eigen::MatrixXd calc_basic_jacobian(const links_t & links, const link_id_t & target_id) { |
| 337 | + // 基礎ヤコビ行列を求める(各軸は回転関節) |
| 338 | + auto route = find_route(links, target_id); |
| 339 | + auto target_p = links[target_id].p; |
| 340 | + Eigen::MatrixXd J(6, route.size()); |
| 341 | + J.setZero(); |
| 342 | + |
| 343 | + for (int i=0; i < route.size(); i++) { |
| 344 | + auto link_id = route[i]; |
| 345 | + auto a = links[link_id].R * links[link_id].a; |
| 346 | + J.col(i) << a.cross(target_p - links[link_id].p), a; |
| 347 | + } |
| 348 | + |
| 349 | + return J; |
| 350 | +} |
| 351 | + |
231 | 352 | } // namespace kinematics_utils |
0 commit comments