GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
robot_state_node.cpp
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#include "robot_state_node.h"
9
10#include <iostream>
11#include <sstream>
12#include <vector>
13
14#include "leg_state.h"
15#include "math_util.h"
16#include "string_util.h"
17
18namespace designlab {
19
21 const bool base) {
22 const Vector3 dif = new_com - center_of_mass_global_coord;
23 const Vector3 com_dif = RotateVector3(dif, posture.GetConjugate());
24
26
27 for (int i = 0; i < HexapodConst::kLegNum; i++) {
29 leg_pos[i] -= com_dif;
31 } else {
32 if (base) leg_reference_pos[i] -= com_dif;
33 }
34 }
35}
36
38 const std::shared_ptr<const IHexapodCoordinateConverter>& converter_ptr,
39 const Quaternion& new_posture) {
40 const Quaternion dif = new_posture * posture.GetConjugate();
41
42 posture = new_posture;
43
44 for (int i = 0; i < HexapodConst::kLegNum; i++) {
46 Vector3 leg_pos_robot_coord =
47 converter_ptr->ConvertLegToRobotCoordinate(leg_pos[i], i);
48 leg_pos_robot_coord =
49 RotateVector3(leg_pos_robot_coord, dif.GetConjugate());
50 leg_pos[i] =
51 converter_ptr->ConvertRobotToLegCoordinate(leg_pos_robot_coord, i);
52
53 Vector3 leg_reference_pos_robot_coord =
54 converter_ptr->ConvertLegToRobotCoordinate(leg_reference_pos[i], i);
55 leg_reference_pos_robot_coord =
56 RotateVector3(leg_reference_pos_robot_coord, dif.GetConjugate());
57 leg_reference_pos[i] = converter_ptr->ConvertRobotToLegCoordinate(
58 leg_reference_pos_robot_coord, i);
59 }
60 }
61}
62
63std::string RobotStateNode::ToString() const {
64 // \t はタブを表す文字.また,スペースのみを追加したい場合は std::string(" ")
65 // とする.
66
67 std::string res; // 結果として返す文字列.
68
69 // 脚状態のビット列.
70 res += "Leg State Bit : " + leg_state.to_string() + "\n";
71 res += "\n";
72
73 // 重心位置の出力.
75 res += "Com Pattern : " + string_util::EnumToStringRemoveTopK(com) + "(" +
76 std::to_string(static_cast<int>(com)) + ")\n";
77
78 // 脚の接地状態.
79 res += "Ground : ";
80
81 for (int i = 0; i < HexapodConst::kLegNum; ++i) {
82 res += (leg_func::IsGrounded(leg_state, i) ? "ground" : "lifted") +
83 std::string(" ");
84 }
85
86 res += "\n";
87
88 // 脚の階層.
89 res += "Hierarchy : ";
90
91 for (int i = 0; i < HexapodConst::kLegNum; ++i) {
93 res += string_util::EnumToStringRemoveTopK(dis_leg_pos) + "(" +
94 std::to_string(static_cast<int>(dis_leg_pos)) + ") ";
95 }
96
97 res += "\n\n";
98
99 // 脚位置.
100 res += "Leg Position : \n";
101
102 for (int i = 0; i < HexapodConst::kLegNum; i++) {
103 res += " " + std::to_string(i) + ":" + leg_pos[i].ToString() + "\n";
104 }
105
106 res += "Leg Base Position : \n";
107
108 for (int i = 0; i < HexapodConst::kLegNum; i++) {
109 res +=
110 " " + std::to_string(i) + ":" + leg_reference_pos[i].ToString() + "\n";
111 }
112
113 // 重心位置.
114 res += "\nGlobal Center of Mass : " + center_of_mass_global_coord.ToString() +
115 "\n";
116
117 // 回転姿勢.
118 res += "Quaternion : " + posture.ToString() + "\n";
119
120 // 次動作.
121 res += "\n";
122 res += "(Next Move : " + string_util::EnumToStringRemoveTopK(next_move) +
123 std::string(")\n");
124 res += "(Depth : " + std::to_string(depth) + std::string(")\n");
125 res +=
126 "(parent number : " + std::to_string(parent_index) + std::string(")\n");
127
128 return res;
129}
130
131std::string RobotStateNode::ToCsvString() const {
132 std::stringstream ss;
133
134 ss << *this;
135
136 return ss.str();
137}
138
140 std::vector<std::string> data = string_util::Split(str, ",");
141
142 RobotStateNode res;
143 int cnt = 0;
144
145 try {
146 // 脚状態のビット列.
147 res.leg_state = std::bitset<leg_func::kLegStateBitNum>(data[cnt++]);
148
149 // 脚の位置.
150 for (int i = 0; i < HexapodConst::kLegNum; i++) {
151 res.leg_pos[i] = Vector3{std::stof(data[cnt++]), std::stof(data[cnt++]),
152 std::stof(data[cnt++])};
153 }
154
155 // 脚の基準位置.
156 for (int i = 0; i < HexapodConst::kLegNum; i++) {
157 res.leg_reference_pos[i] =
158 Vector3{std::stof(data[cnt++]), std::stof(data[cnt++]),
159 std::stof(data[cnt++])};
160 }
161
162 // 重心位置.
164 std::stof(data[cnt++]), std::stof(data[cnt++]), std::stof(data[cnt++])};
165
166 // 回転姿勢.
167 res.posture = Quaternion{std::stof(data[cnt++]), std::stof(data[cnt++]),
168 std::stof(data[cnt++]), std::stof(data[cnt++])};
169
170 // 次動作.
171 res.next_move = magic_enum::enum_cast<HexapodMove>(data[cnt++]).value();
172
173 // 親ノードの番号.
174 res.parent_index = std::stoi(data[cnt++]);
175
176 // 深さ.
177 res.depth = std::stoi(data[cnt++]);
178 } catch (...) {
180 }
181
182 return res;
183}
184
185} // namespace designlab
static constexpr int kLegNum
bool IsGrounded(const LegStateBit &leg_state, const int leg_index)
脚番号 leg_index 0 ~ 5 に応じて,その脚が接地しているかを調べる. 脚は右前脚を0番として,時計回りに0,1,2,3,4,5となる.左前足が5番.
Definition leg_state.cpp:46
DiscreteLegPos GetDiscreteLegPos(const LegStateBit &leg_state, const int leg_index)
脚状態を取得する.
enums::DiscreteComPos GetDiscreteComPos(const LegStateBit &leg_state)
現在の脚状態から重心パターンを取得する.
std::vector< std::string > Split(const std::string &str, const std::string &separator)
文字列を分割する関数.指定した文字で文字列を分割する. 分割した結果,空白が含まれる場合や文字列がない場合は,そのまま返す. 最後が区切り文字で終わる場合は,...
std::string EnumToStringRemoveTopK(const T &enum_value)
enumを文字列に変換する関数. Google C++ coding style だと enumの要素は 先頭にkをつけてキャメルケースで書くことが推奨されている....
Definition string_util.h:52
DiscreteLegPos
離散化された脚位置を表す列挙体. 先行研究では 1~7の int型の数値で表現されているが, 可読性を上げるために列挙体にした. 離散化された脚位置は 3bit (0 ~ 7)の範囲で表現される...
Vector3 RotateVector3(const Vector3 &vec, const EulerXYZ &rot)
回転させたベクトルを返す.三角関数の処理が多く重たいので注意.
クォータニオンを表す構造体.
std::string ToString() const
クォータニオンを文字列に変換する. w, x, y, z の順で出力する.
constexpr Quaternion GetConjugate() const noexcept
クォータニオンの共役を返す. 共役なクォータニオンとは,ベクトル成分の符号を反転させたもの q = w + xi + yj + zk とすると, qの共役は w - xi - yj - zk となる...
グラフ構造のためのノード(頂点).
std::array< Vector3, HexapodConst::kLegNum > leg_pos
[4 * 3 * 6 = 72 byte] 脚先の座標.(coxa(脚の付け根)を原点とする)
leg_func::LegStateBit leg_state
[4 byte] 脚状態,重心パターンを bitで表す.旧名 leg_con.
std::string ToCsvString() const
ノードの情報を csv形式の文字列に変換する関数. カンマ区切りで出力する.
static RobotStateNode FromString(const std::string &str)
文字列をノードの情報に変換する関数.
Vector3 center_of_mass_global_coord
[4 * 3 = 12byte] グローバル座標系における重心の位置.旧名 GCOM
std::string ToString() const
ノードの情報を文字列に変換する関数. デバッグ用に詳細な情報を出力する.
void ChangePosture(const std::shared_ptr< const IHexapodCoordinateConverter > &converter_ptr, const Quaternion &new_posture)
クォータニオンを変更し,胴体を回転させる関数.
Quaternion posture
[4 * 4 = 16byte] 姿勢を表すクォータニオン.
std::array< Vector3, HexapodConst::kLegNum > leg_reference_pos
int depth
[4 byte] 自身の深さ.一番上の親が深さ0となる.
void ChangeGlobalCenterOfMass(const Vector3 &new_com, bool do_change_leg_base_pos)
重心位置を変更する関数.
3次元の位置ベクトルを表す構造体.
std::string ToString() const
このベクトルを文字列にして返す. (x, y, z) の形式,小数点以下3桁まで.