GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
graph_searcher_spot_turn.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
9
10#include <vector>
11
12#include "cassert_define.h"
13
14namespace designlab {
15
17 const std::shared_ptr<const IHexapodCoordinateConverter>& converter_ptr,
18 const std::shared_ptr<const IHexapodPostureValidator>& checker_ptr)
19 : converter_ptr_(converter_ptr),
20 checker_ptr_(checker_ptr),
21 evaluator_(InitializeEvaluator()) {}
22
23std::tuple<GraphSearchResult, GraphSearchEvaluationValue, RobotStateNode>
25 const RobotOperation& operation,
26 const DividedMapState& divided_map_state,
27 const int max_depth) const {
30
31 if (!graph.HasRoot()) {
33 "ルートノードがありません."};
34 return {result, GraphSearchEvaluationValue{}, RobotStateNode{}};
35 }
36
37 // 初期化.
38 Quaternion target_quaternion;
39
41 target_quaternion = operation.spot_turn_last_posture;
42 } else {
43 // 回転軸周りに20deg回転クォータニオンを作成する.
44 const auto rot_quaternion = Quaternion::MakeByAngleAxis(
46 target_quaternion = rot_quaternion * graph.GetRootNode().posture;
47 }
48
49 const float target_z_value = InitTargetZValue(
50 graph.GetRootNode(), divided_map_state, target_quaternion);
51
52 GraphSearchEvaluationValue max_evaluation_value =
53 evaluator_.InitializeEvaluationValue();
54 int max_evaluation_value_index = -1;
55
56 for (int i = 0; i < graph.GetGraphSize(); i++) {
57 // 最大深さのノードのみを評価する.
58 if (graph.GetNode(i).depth != max_depth) {
59 continue;
60 }
61
62 // 評価値を計算する.
63 GraphSearchEvaluationValue candidate_evaluation_value =
64 evaluator_.InitializeEvaluationValue();
65
66 candidate_evaluation_value.value.at(kTagAmountOfTurn) =
67 GetAmountOfTurnEvaluationValue(graph.GetNode(i), target_quaternion);
68 candidate_evaluation_value.value.at(kTagLegRot) =
69 GetLegRotEvaluationValue(graph.GetNode(i), graph.GetRootNode());
70 candidate_evaluation_value.value.at(kTagZDiff) =
71 GetZDiffEvaluationValue(graph.GetNode(i), target_z_value);
72
73 // 評価値を比較する.
74 if (evaluator_.LeftIsBetter(candidate_evaluation_value,
75 max_evaluation_value)) {
76 max_evaluation_value = candidate_evaluation_value;
77 max_evaluation_value_index = i;
78 }
79 }
80
81 // インデックスが範囲外ならば失敗.
82 if (max_evaluation_value_index < 0 ||
83 graph.GetGraphSize() <= max_evaluation_value_index) {
85 "最大評価値のインデックスが範囲外です."};
86 return {result, GraphSearchEvaluationValue{}, RobotStateNode{}};
87 }
88
89 const GraphSearchResult result = {enums::Result::kSuccess, ""};
90
91 return {
92 result,
93 max_evaluation_value,
94 graph.GetParentNode(max_evaluation_value_index, 1),
95 };
96}
97
98std::tuple<GraphSearchResult, GraphSearchEvaluationValue, RobotStateNode>
100 const std::vector<GaitPatternGraphTree>& graph_vector,
101 const RobotOperation& operation, const DividedMapState& divided_map_state,
102 int max_depth) const {
103 std::vector<
104 std::tuple<GraphSearchResult, GraphSearchEvaluationValue, RobotStateNode>>
105 result_vector;
106
107 for (const auto& graph : graph_vector) {
108 const auto result =
109 SearchGraphTree(graph, operation, divided_map_state, max_depth);
110
111 result_vector.push_back(result);
112 }
113
114 // 最大評価値を持つものを探す.
115 GraphSearchEvaluationValue max_evaluation_value =
116 evaluator_.InitializeEvaluationValue();
117 int max_evaluation_value_index = -1;
118
119 for (int i = 0; i < result_vector.size(); i++) {
120 const auto& [result, evaluation_value, _] = result_vector.at(i);
121
122 if (result.result != enums::Result::kSuccess) {
123 continue;
124 }
125
126 if (evaluator_.LeftIsBetter(evaluation_value, max_evaluation_value)) {
127 max_evaluation_value = evaluation_value;
128 max_evaluation_value_index = i;
129 }
130 }
131
132 // インデックスが範囲外ならば失敗.
133 if (max_evaluation_value_index < 0 ||
134 max_evaluation_value_index >= result_vector.size()) {
136 "最大評価値のインデックスが範囲外です."};
137 return {result, GraphSearchEvaluationValue{}, RobotStateNode{}};
138 }
139
140 return result_vector[max_evaluation_value_index];
141}
142
143GraphSearchEvaluator GraphSearcherSpotTurn::InitializeEvaluator() const {
144 GraphSearchEvaluator::EvaluationMethod amount_of_turn_method = {
145 .is_lower_better = false,
146 .margin = 0.0f,
147 };
148
149 GraphSearchEvaluator::EvaluationMethod leg_rot_method = {
150 .is_lower_better = false,
151 .margin = 50.0f,
152 };
153
154 GraphSearchEvaluator::EvaluationMethod z_diff_method = {
155 .is_lower_better = true,
156 .margin = 1.0f,
157 };
158
159 GraphSearchEvaluator ret({{kTagAmountOfTurn, amount_of_turn_method},
160 {kTagLegRot, leg_rot_method},
161 {kTagZDiff, z_diff_method}},
162 {
163 kTagAmountOfTurn,
164 kTagLegRot,
165 kTagZDiff,
166 });
167
168 return ret;
169}
170
171float GraphSearcherSpotTurn::InitTargetZValue(
172 const RobotStateNode& node, const DividedMapState& divided_map_state,
173 [[maybe_unused]] const Quaternion& target_quat) const {
174 const int div = 60;
175 const float min_z = -150.0f;
176 const float max_z = 150.0f;
177
178 for (int i = 0; i < div; i++) {
179 const float z = min_z + (max_z - min_z) / static_cast<float>(div) *
180 static_cast<float>(i);
181
182 Vector3 pos = node.center_of_mass_global_coord;
183 pos.z += z;
184
185 RobotStateNode temp_node = node;
186 temp_node.ChangeGlobalCenterOfMass(pos, false);
187 temp_node.ChangePosture(converter_ptr_, target_quat);
188
189 if (!checker_ptr_->IsBodyInterferingWithGround(temp_node,
190 divided_map_state)) {
191 // std::cout << "z = " << node.center_of_mass_global_coord.z + z <<
192 // std::endl;
193 return node.center_of_mass_global_coord.z + z;
194 }
195 }
196
197 return node.center_of_mass_global_coord.z;
198}
199
200float GraphSearcherSpotTurn::GetAmountOfTurnEvaluationValue(
201 const RobotStateNode& node, const Quaternion& target_quat) const {
202 // 目標姿勢を Qt,現在の姿勢を Qc とする.
203 // Qt = Qc * Qd となるような Qd を求める.
204 // Qd = Qc^-1 * Qt となる.
205
206 // 最終姿勢を表すクォータニオンとの差分を計算する.
207 const Quaternion target_to_current =
208 node.posture.GetConjugate() * target_quat;
209
210 return target_to_current.w;
211}
212
213float GraphSearcherSpotTurn::GetLegRotEvaluationValue(
214 const RobotStateNode& node, const RobotStateNode& root_node) const {
215 float result = 0.0f;
216
217 for (int i = 0; i < HexapodConst::kLegNum; i++) {
218 if (leg_func::IsGrounded(node.leg_state, i)) {
219 result +=
220 (node.leg_pos[i].ProjectedXY() - root_node.leg_pos[i].ProjectedXY())
221 .GetLength();
222 } else {
223 result += (node.leg_pos[i] - root_node.leg_pos[i]).GetLength();
224 }
225 }
226
227 return result;
228
229 // const float margin = 50.0f;
230}
231
232float GraphSearcherSpotTurn::GetZDiffEvaluationValue(
233 const RobotStateNode& node, const float target_z_value) const {
234 return abs(node.center_of_mass_global_coord.z - target_z_value);
235}
236
237} // namespace designlab
マップを格子状に分割して管理するクラス.
RobotStateNode 構造体をノードとする木構造のグラフのクラス.
const RobotStateNode & GetRootNode() const
グラフの根ノードの参照を返す.
const RobotStateNode & GetParentNode(const int index, const int depth) const
指定したノードの親ノードの参照を返す.depthは親ノードの深さを指定する.
const RobotStateNode & GetNode(const int index) const
グラフのノードの参照を返す.
constexpr bool HasRoot() const
グラフが根ノードを持つかどうかを返す. 根ノードとは,親を持たないノードのこと. 一番最初に追加するノードは必ず根になるため, 根を持つかどうかはノードの総数が0でないかどうかで判定できる.
constexpr int GetGraphSize() const
グラフのノードの総数を返す.
グラフ探索の評価値を評価するクラス.
bool LeftIsBetter(const GraphSearchEvaluationValue &left, const GraphSearchEvaluationValue &right, bool return_true_case_of_equal=true) const
2つの評価値を比較する.左側の評価値が良い場合は true を返す.
GraphSearchEvaluationValue InitializeEvaluationValue() const
評価値を初期化する. 自身の持つ評価方法を用いて,評価値を初期化する.
std::tuple< GraphSearchResult, GraphSearchEvaluationValue, RobotStateNode > SearchGraphTree(const GaitPatternGraphTree &graph, const RobotOperation &operation, const DividedMapState &divided_map_state, int max_depth) const override
グラフを受け取り,その中から最適な次の動作を出力する.
GraphSearcherSpotTurn(const std::shared_ptr< const IHexapodCoordinateConverter > &converter_ptr, const std::shared_ptr< const IHexapodPostureValidator > &checker_ptr)
std::tuple< GraphSearchResult, GraphSearchEvaluationValue, RobotStateNode > SearchGraphTreeVector(const std::vector< GaitPatternGraphTree > &graph_vector, const RobotOperation &operation, const DividedMapState &divided_map_state, int max_depth) const override
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
constexpr T ConvertDegToRad(const T deg) noexcept
角度を [deg] から [rad] に変換する関数.
Definition math_util.h:119
@ kSpotTurnRotAxis
その場で旋回させる(回転軸を示し,その軸周りの右ねじの回転)
@ kSpotTurnLastPosture
その場で旋回させる(最終的な姿勢 Posture を示す)
グラフ探索の評価値を格納する構造体.
bool is_lower_better
評価値が小さいほど良い場合は true.
グラフ探索の結果を表す構造体.
クォータニオンを表す構造体.
static Quaternion MakeByAngleAxis(float rad_angle, const Vector3 &axis)
回転軸と回転角からクォータニオンを作成する. q = cos(θ/2) * w + sin(θ/2) * { v.x + v.y + v.z } となる. ノルムは必ず1になる.
探索において目標となる座標や角度,評価する値についてまとめた構造体.
RobotOperationType operation_type
Quaternion spot_turn_last_posture
旋回時の回転軸.右ねじの回転.
グラフ構造のためのノード(頂点).
Quaternion posture
[4 * 4 = 16byte] 姿勢を表すクォータニオン.
int depth
[4 byte] 自身の深さ.一番上の親が深さ0となる.