24 const std::shared_ptr<const IHexapodPostureValidator>& checker_ptr) :
25 checker_ptr_(checker_ptr),
26 evaluator_(InitializeEvaluator())
34 const int max_depth)
const
47 Vector3 normalized_move_direction;
53 normalized_move_direction.
z = 0.0f;
54 normalized_move_direction = normalized_move_direction.
GetNormalized();
59 normalized_move_direction.
z = 0.0f;
60 normalized_move_direction = normalized_move_direction.
GetNormalized();
64 const float target_z_value = InitTargetZValue(graph.
GetRootNode(), divided_map_state, normalized_move_direction);
69 int max_evaluation_value_index = -1;
84 candidate_evaluation_value.
value.at(kTagMoveForward) = GetMoveForwardEvaluationValue(graph.
GetNode(i), graph.
GetRootNode(), normalized_move_direction);
87 candidate_evaluation_value.
value.at(kTagLegRot) = GetLegRotEvaluationValue(graph.
GetNode(i), graph.
GetRootNode());
94 if (evaluator_.
LeftIsBetter(candidate_evaluation_value, max_evaluation_value))
98 max_evaluation_value = candidate_evaluation_value;
99 max_evaluation_value_index = i;
110 if (max_evaluation_value_index < 0)
123 return { result, max_evaluation_value, graph.
GetParentNode(max_evaluation_value_index, 1) };
127 const std::vector<GaitPatternGraphTree>& graph_vector,
132 std::vector<std::tuple<GraphSearchResult, GraphSearchEvaluationValue, RobotStateNode>> result_vector;
133 result_vector.resize(graph_vector.size());
135 for (
size_t i = 0; i < graph_vector.size(); i++)
138 result_vector[i] =
SearchGraphTree(graph_vector[i], operation, divided_map_state, max_depth);
143 int max_evaluation_value_index = -1;
145 for (
int i = 0; i < result_vector.size(); i++)
147 const auto& [result, evaluation_value, _] = result_vector[i];
156 if (evaluator_.
LeftIsBetter(evaluation_value, max_evaluation_value))
159 max_evaluation_value = evaluation_value;
160 max_evaluation_value_index = i;
165 if (max_evaluation_value_index < 0 || result_vector.size() <= max_evaluation_value_index)
171 return result_vector[max_evaluation_value_index];
182 GraphSearchEvaluator::EvaluationMethod leg_rot_method =
184 .is_lower_better =
false,
188 GraphSearchEvaluator::EvaluationMethod z_diff_method =
190 .is_lower_better =
true,
194 GraphSearchEvaluator ret({ {kTagMoveForward, move_forward_method}, {kTagLegRot, leg_rot_method}, {kTagZDiff, z_diff_method} },
195 { kTagZDiff, kTagMoveForward, kTagLegRot });
200float GraphSearcherStraightMove::InitTargetZValue(
201 const RobotStateNode& node,
202 const DividedMapState& divided_map_state,
203 const Vector3& move_direction)
const
205 const float move_length = 100.0f;
207 const Vector3 target_position = move_direction * move_length;
210 const float min_z = -150.0f;
211 const float max_z = 150.0f;
213 for (
int i = 0; i < div; i++)
215 const float z = min_z + (max_z - min_z) /
static_cast<float>(div) *
static_cast<float>(i);
217 Vector3 pos = node.center_of_mass_global_coord;
218 pos += target_position;
221 RobotStateNode temp_node = node;
222 temp_node.ChangeGlobalCenterOfMass(pos,
false);
224 if (!checker_ptr_->IsBodyInterferingWithGround(temp_node, divided_map_state))
226 return node.center_of_mass_global_coord.z + z;
230 return node.center_of_mass_global_coord.z;
233float GraphSearcherStraightMove::GetMoveForwardEvaluationValue(
234 const RobotStateNode& node,
235 const RobotStateNode& root_node,
236 const Vector3& normalized_move_direction)
const
241 const Vector3 root_to_current = node.center_of_mass_global_coord - root_node.center_of_mass_global_coord;
244 const float result = root_to_current.Dot(normalized_move_direction);
251float GraphSearcherStraightMove::GetLegRotEvaluationValue(
252 const RobotStateNode& node,
253 const RobotStateNode& root_node)
const
261 result += (node.leg_pos[i].ProjectedXY() -
262 root_node.leg_pos[i].ProjectedXY()).GetLength();
266 result += (node.leg_pos[i] - root_node.leg_pos[i]).GetLength();
275float GraphSearcherStraightMove::GetZDiffEvaluationValue(
276 const std::vector<float>& com_trajectory,
277 const float target_z_value)
const
279 float result = abs(com_trajectory.back() - target_z_value);
281 if (com_trajectory.size() == 3)
283 if ((com_trajectory[0] - com_trajectory[1]) * (com_trajectory[0] - com_trajectory[2]) <= 0)
285 result += abs(com_trajectory[0] - com_trajectory[1]);
static void FormatOutput(OutputDetail detail, const std::format_string< Args... > str, Args &&... args)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
RobotStateNode 構造体をノードとする木構造のグラフのクラス.
const RobotStateNode & GetRootNode() const
グラフの根ノードの参照を返す.
std::vector< float > GetCoMVerticalTrajectory(const int index) 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 > SearchGraphTreeVector(const std::vector< GaitPatternGraphTree > &graph_vector, const RobotOperation &operation, const DividedMapState ÷d_map_state, int max_depth) const override
std::tuple< GraphSearchResult, GraphSearchEvaluationValue, RobotStateNode > SearchGraphTree(const GaitPatternGraphTree &graph, const RobotOperation &operation, const DividedMapState ÷d_map_state, int max_depth) const override
グラフを受け取り,その中から最適な次の動作を出力する.
GraphSearcherStraightMove(const std::shared_ptr< const IHexapodPostureValidator > &checker_ptr)
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番.
constexpr bool IsEqual(const T num1, const T num2) noexcept
C++において,小数同士の計算は誤差が出てしまう. 誤差込みで値が等しいか調べる.
std::string EnumToStringRemoveTopK(const T &enum_value)
enumを文字列に変換する関数. Google C++ coding style だと enumの要素は 先頭にkをつけてキャメルケースで書くことが推奨されている. 例えば,
@ kStraightMoveVector
直線移動をさせる(移動したい方向をベクトルで示す)
@ kStraightMovePosition
直線移動をさせる(移動したい座標を示す)
@ kDebug
デバッグ時のみ出力,一番優先度が低い.
HexapodMove
ロボットが次にどの動作をするのかを表す列挙体.
std::map< Tag, float > value
bool is_lower_better
評価値が小さいほど良い場合は true.
探索において目標となる座標や角度,評価する値についてまとめた構造体.
Vector3 straight_move_vector
< 目標方向.正規化されたベクトル.
RobotOperationType operation_type
Vector3 straight_move_position
目標姿勢 ( posture )
グラフ構造のためのノード(頂点).旧名 LNODE
Vector3 center_of_mass_global_coord
[4 * 3 = 12byte] グローバル座標系における重心の位置.旧名 GCOM
int depth
[4 byte] 自身の深さ.一番上の親が深さ0となる.
Vector3 GetNormalized() const noexcept
単位ベクトルを返す. normalizeとは,ベクトルを正規化(単位ベクトルに変換)する操作を表す. 絶対値が0のベクトルの場合,そのまま0ベクトルを返す.
static constexpr Vector3 GetFrontVec() noexcept
正面に進む単位ベクトルを返す. 静的な関数なので,Vector3::GetFrontVec() と呼び出せる.
float GetLength() const noexcept
ベクトルの長さを返す.