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 "math_util.h"
15#include "string_util.h"
16#include "leg_state.h"
17
18
19namespace designlab
20{
21
22void RobotStateNode::ChangeGlobalCenterOfMass(const Vector3& new_com, const bool base)
23{
24 const Vector3 dif = new_com - center_of_mass_global_coord;
25 const Vector3 com_dif = RotateVector3(dif, posture.GetConjugate());
26
28
29 for (int i = 0; i < HexapodConst::kLegNum; i++)
30 {
32 {
33 leg_pos[i] -= com_dif;
35 }
36 else
37 {
38 if (base)leg_reference_pos[i] -= com_dif;
39 }
40 }
41}
42
44 const std::shared_ptr<const IHexapodCoordinateConverter>& converter_ptr,
45 const Quaternion& new_posture)
46{
47 const Quaternion dif = new_posture * posture.GetConjugate();
48
49 posture = new_posture;
50
51 for (int i = 0; i < HexapodConst::kLegNum; i++)
52 {
54 {
55 Vector3 leg_pos_robot_coord =
56 converter_ptr->ConvertLegToRobotCoordinate(leg_pos[i], i);
57 leg_pos_robot_coord = RotateVector3(leg_pos_robot_coord, dif.GetConjugate());
58 leg_pos[i] = converter_ptr->ConvertRobotToLegCoordinate(leg_pos_robot_coord, i);
59
60 Vector3 leg_reference_pos_robot_coord =
61 converter_ptr->ConvertLegToRobotCoordinate(leg_reference_pos[i], i);
62 leg_reference_pos_robot_coord =
63 RotateVector3(leg_reference_pos_robot_coord, dif.GetConjugate());
65 converter_ptr->ConvertRobotToLegCoordinate(leg_reference_pos_robot_coord, i);
66 }
67 }
68}
69
70std::string RobotStateNode::ToString() const
71{
72 // \t はタブを表す文字.また,スペースのみを追加したい場合は std::string(" ") とする.
73
74 std::string res; // 結果として返す文字列.
75
76 // 脚状態のビット列.
77 res += "Leg State Bit : " + leg_state.to_string() + "\n";
78 res += "\n";
79
80 // 重心位置の出力.
82 res += "Com Pattern : " + string_util::EnumToStringRemoveTopK(com) +
83 "(" + std::to_string(static_cast<int>(com)) + ")\n";
84
85 // 脚の接地状態.
86 res += "Ground : ";
87
88 for (int i = 0; i < HexapodConst::kLegNum; ++i)
89 {
90 res += (leg_func::IsGrounded(leg_state, i) ? "ground" : "lifted") + std::string(" ");
91 }
92
93 res += "\n";
94
95 // 脚の階層.
96 res += "Hierarchy : ";
97
98 for (int i = 0; i < HexapodConst::kLegNum; ++i)
99 {
101 res += string_util::EnumToStringRemoveTopK(dis_leg_pos) +
102 "(" + std::to_string(static_cast<int>(dis_leg_pos)) + ") ";
103 }
104
105 res += "\n\n";
106
107 // 脚位置.
108 res += "Leg Position : \n";
109
110 for (int i = 0; i < HexapodConst::kLegNum; i++)
111 {
112 res += " " + std::to_string(i) + ":" + leg_pos[i].ToString() + "\n";
113 }
114
115 res += "Leg Base Position : \n";
116
117 for (int i = 0; i < HexapodConst::kLegNum; i++)
118 {
119 res += " " + std::to_string(i) + ":" + leg_reference_pos[i].ToString() + "\n";
120 }
121
122 // 重心位置.
123 res += "\nGlobal Center of Mass : " + center_of_mass_global_coord.ToString() + "\n";
124
125 // 回転姿勢.
126 res += "Quaternion : " + posture.ToString() + "\n";
127
128 // 次動作.
129 res += "\n";
130 res += "(Next Move : " + string_util::EnumToStringRemoveTopK(next_move) +
131 std::string(")\n");
132 res += "(Depth : " + std::to_string(depth) + std::string(")\n");
133 res += "(parent number : " + std::to_string(parent_index) + std::string(")\n");
134
135 return res;
136}
137
139{
140 std::stringstream ss;
141
142 ss << *this;
143
144 return ss.str();
145}
146
148{
149 std::vector<std::string> data = string_util::Split(str, ",");
150
151 RobotStateNode res;
152 int cnt = 0;
153
154 try
155 {
156 // 脚状態のビット列.
157 res.leg_state = std::bitset<leg_func::kLegStateBitNum>(data[cnt++]);
158
159 // 脚の位置.
160 for (int i = 0; i < HexapodConst::kLegNum; i++)
161 {
162 res.leg_pos[i] =
163 Vector3{ std::stof(data[cnt++]), std::stof(data[cnt++]), std::stof(data[cnt++]) };
164 }
165
166 // 脚の基準位置.
167 for (int i = 0; i < HexapodConst::kLegNum; i++)
168 {
169 res.leg_reference_pos[i] =
170 Vector3{ std::stof(data[cnt++]), std::stof(data[cnt++]), std::stof(data[cnt++]) };
171 }
172
173 // 重心位置.
175 Vector3{ std::stof(data[cnt++]), std::stof(data[cnt++]), std::stof(data[cnt++]) };
176
177 // 回転姿勢.
178 res.posture =
179 Quaternion{ std::stof(data[cnt++]),
180 std::stof(data[cnt++]), std::stof(data[cnt++]), std::stof(data[cnt++]) };
181
182 // 次動作.
183 res.next_move = magic_enum::enum_cast<HexapodMove>(data[cnt++]).value();
184
185 // 親ノードの番号.
186 res.parent_index = std::stoi(data[cnt++]);
187
188 // 深さ.
189 res.depth = std::stoi(data[cnt++]);
190 }
191 catch (...)
192 {
194 }
195
196 return res;
197}
198
199
200} // namespace designlab
static constexpr int kLegNum
DiscreteLegPos
離散化された脚位置を表す列挙体. 先行研究では 1~7の int型の数値で表現されているが, 可読性を上げるために列挙体にした. 離散化された脚位置は 3bit (0 ~ 7)の範囲で表現される...
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:43
enums::DiscreteComPos GetDiscreteComPos(const LegStateBit &leg_state)
現在の脚状態から重心パターンを取得する.
enums::DiscreteLegPos GetDiscreteLegPos(const LegStateBit &leg_state, const int leg_index)
脚状態を取得する.
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:54
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 となる...
グラフ構造のためのノード(頂点).旧名 LNODE
std::array< Vector3, HexapodConst::kLegNum > leg_pos
[4 * 3 * 6 = 72 byte] 脚先の座標.(coxa(脚の付け根)を原点とする)
void ChangeGlobalCenterOfMass(const designlab::Vector3 &new_com, bool do_change_leg_base_pos)
重心位置を変更する関数.
leg_func::LegStateBit leg_state
[4 byte] 脚状態,重心パターンを bitで表す.旧名 leg_con.
std::string ToCsvString() const
ノードの情報を csv形式の文字列に変換する関数. カンマ区切りで出力する.
static RobotStateNode FromString(const std::string &str)
文字列をノードの情報に変換する関数.
void ChangePosture(const std::shared_ptr< const IHexapodCoordinateConverter > &converter_ptr, const designlab::Quaternion &new_posture)
クォータニオンを変更し,胴体を回転させる関数.
Vector3 center_of_mass_global_coord
[4 * 3 = 12byte] グローバル座標系における重心の位置.旧名 GCOM
std::string ToString() const
ノードの情報を文字列に変換する関数. デバッグ用に詳細な情報を出力する.
Quaternion posture
[4 * 4 = 16byte] 姿勢を表すクォータニオン.
std::array< Vector3, HexapodConst::kLegNum > leg_reference_pos
int depth
[4 byte] 自身の深さ.一番上の親が深さ0となる.
3次元の位置ベクトルを表す構造体.
std::string ToString() const
このベクトルを文字列にして返す. (x, y, z) の形式,小数点以下3桁まで.