GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
system_main_result_viewer.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 <filesystem>
11#include <optional>
12#include <vector>
13
14#include <boost/thread.hpp>
15
16#include "cmdio_util.h"
17#include "file_tree.h"
19#include "map_state.h"
21
22
23namespace designlab
24{
25
27 const std::shared_ptr<GraphicDataBroker>& broker_ptr,
28 const std::shared_ptr<const ApplicationSettingRecord> setting_ptr,
29 const std::shared_ptr<const IHexapodJointCalculator> joint_calculator,
30 const std::shared_ptr<const IHexapodCoordinateConverter> converter) :
31 broker_ptr_(broker_ptr),
32 joint_calculator_(joint_calculator),
33 converter_(converter)
34{
35}
36
38{
39 using enum OutputDetail;
40
41 CmdIOUtil::OutputTitle("Result Viewer System");
42
43 while (true)
44 {
45 // ファイルツリーを表示し,ファイルを選択する.
46 FileTree file_tree;
47
48 std::string res_path;
49
51 {
52 CmdIOUtil::Output("No data were found. Terminate.", kError);
53
54 break;
55 }
56
57 // ファイルを読み込む.
58
59 std::vector<RobotStateNode> graph; // データを受け取るための変数.
60 MapState map_state;
61
62 if (result_importer_.ImportNodeListAndMapState(res_path, &graph, &map_state))
63 {
64 // 異常値を出力する.
65 // OutputErrorLegPos(res_path, graph);
66
67 // データを仲介人に渡す.
68 broker_ptr_->graph.SetData(graph);
69 broker_ptr_->map_state.SetData(map_state);
70 broker_ptr_->simulation_end_index.SetData({ graph.size() - 1 });
71
72 // データを表示する.
73 CmdIOUtil::Output("Displays data.", kSystem);
78 }
79 else
80 {
81 CmdIOUtil::Output("Failed to read the file. Exit.", kError);
82 }
83
84 // 終了するかどうかを選択
85
86 if (CmdIOUtil::InputYesNo("Do you want to exit this mode?"))
87 {
89
90 break;
91 }
92
94 }
95}
96
97void SystemMainResultViewer::OutputErrorLegPos(const std::string& file, const std::vector<RobotStateNode>& nodes)
98{
99 CmdIOUtil::Output("Outputs data of abnormal values.", OutputDetail::kSystem);
100
101 // 後ろの .csv を削除する
102 std::string file_name = file.substr(0, file.size() - 4);
103
104 const std::string output_path = file_name + "_error_leg_pos.txt";
105
106 CmdIOUtil::Output("Output Directory: " + output_path, OutputDetail::kSystem);
107
108 // すでにファイルが存在する場合は削除する.
109 if (std::filesystem::exists(output_path))
110 {
111 std::filesystem::remove(output_path);
112 }
113
114 // まず,脚接地点が可動範囲外のノードを探す.
115 std::vector<Vector2> error_leg_pos;
116
117 for (int i = 0; i < nodes.size(); i++)
118 {
119 for (int j = 0; j < HexapodConst::kLegNum; j++)
120 {
121 HexapodJointState joint_state = joint_calculator_->CalculateJointState(j, nodes[i].leg_pos[j]);
122
123 if (!joint_calculator_->IsValidJointState(j, nodes[i].leg_pos[j], joint_state))
124 {
125 const auto length = nodes[i].leg_pos[j].ProjectedXY().GetLength();
126
127 error_leg_pos.push_back({ length, nodes[i].leg_pos[j].z });
128 }
129 }
130 }
131
132 // 次に脚軌道が可動範囲外を通過するノードを探す.
133 std::vector<Vector2> error_first;
134 std::vector<Vector2> error_second;
135 std::vector<Vector2> error_end;
136
137 for (int i = 0; i < nodes.size() - 1; i++)
138 {
139 for (int j = 0; j < HexapodConst::kLegNum; j++)
140 {
141 // 現在と次のノードがともに可動範囲内にない場合,スキップする.
142 HexapodJointState current_joint_state = joint_calculator_->CalculateJointState(j, nodes[i].leg_pos[j]);
143 HexapodJointState next_joint_state = joint_calculator_->CalculateJointState(j, nodes[i + 1].leg_pos[j]);
144
145 if (!joint_calculator_->IsValidJointState(j, nodes[i].leg_pos[j], current_joint_state) &&
146 !joint_calculator_->IsValidJointState(j, nodes[i + 1].leg_pos[j], next_joint_state))
147 {
148 continue;
149 }
150
151 // 補完するノードを生成する.
152 InterpolatedNodeCreator creator(converter_);
153
154 std::vector<RobotStateNode> interpolated_nodes = creator.CreateInterpolatedNode(nodes[i], nodes[i + 1]);
155
156 // 補完ノードの中で可動範囲外になるノードを探す.
157 bool error = false;
158
159 for (int k = 0; k < interpolated_nodes.size(); k++)
160 {
161 HexapodJointState joint_state = joint_calculator_->CalculateJointState(j, interpolated_nodes[k].leg_pos[j]);
162
163 if (!joint_calculator_->IsValidJointState(j, interpolated_nodes[k].leg_pos[j], joint_state))
164 {
165 error = true;
166
167 break;
168 }
169 }
170
171 // 可動範囲外になるノードがあった場合,エラーとして記録する.
172 if (error)
173 {
174 const auto length = nodes[i].leg_pos[j].ProjectedXY().GetLength();
175 error_first.push_back({ length, nodes[i].leg_pos[j].z });
176
177 const auto length3 = nodes[i + 1].leg_pos[j].ProjectedXY().GetLength();
178 error_end.push_back({ length3, nodes[i + 1].leg_pos[j].z });
179
180 Vector2 second;
181 if (length < length3)
182 {
183 second = Vector2{ error_first.back().x, error_end.back().y };
184 }
185 else
186 {
187 second = Vector2{ error_end.back().x, error_first.back().y };
188 }
189
190 error_second.push_back(second);
191 }
192 }
193 }
194
195 // 結果をファイルに書き込む.
196
197 // ファイルを開く.
198 std::ofstream ofs(output_path);
199
200 {
201 ofs << " x_position += [";
202
203 for (int i = 0; i < error_leg_pos.size(); i++)
204 {
205 ofs << error_leg_pos[i].x << ", ";
206 }
207
208 ofs << "]" << std::endl;
209 ofs << " z_position += [";
210
211 for (int i = 0; i < error_leg_pos.size(); i++)
212 {
213 ofs << error_leg_pos[i].y << ", ";
214 }
215
216 ofs << "]" << std::endl;
217 }
218
219 // 中継点を出力する.
220 {
221 ofs << " x_position_first += [";
222
223 for (int i = 0; i < error_leg_pos.size(); i++)
224 {
225 ofs << error_first[i].x << ", ";
226 }
227
228 ofs << "]" << std::endl;
229 ofs << " z_position_first += [";
230
231 for (int i = 0; i < error_leg_pos.size(); i++)
232 {
233 ofs << error_first[i].y << ", ";
234 }
235
236 ofs << "]" << std::endl;
237 }
238
239 {
240 ofs << " x_position_second += [";
241
242 for (int i = 0; i < error_leg_pos.size(); i++)
243 {
244 ofs << error_second[i].x << ", ";
245 }
246
247 ofs << "]" << std::endl;
248 ofs << " z_position_second += [";
249
250 for (int i = 0; i < error_leg_pos.size(); i++)
251 {
252 ofs << error_second[i].y << ", ";
253 }
254
255 ofs << "]" << std::endl;
256 }
257
258 {
259 ofs << " x_position_end += [";
260
261 for (int i = 0; i < error_leg_pos.size(); i++)
262 {
263 ofs << error_end[i].x << ", ";
264 }
265
266 ofs << "]" << std::endl;
267 ofs << " z_position_end += [";
268
269 for (int i = 0; i < error_leg_pos.size(); i++)
270 {
271 ofs << error_end[i].y << ", ";
272 }
273
274 ofs << "]" << std::endl;
275 }
276}
277
278} // namespace designlab
static void Output(const std::string &str, OutputDetail detail)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
static void WaitAnyKey(const std::string &str="Waiting for input.")
入力待ちをする関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static void OutputNewLine(int num, OutputDetail detail)
コマンドラインで改行をする関数.
static void OutputTitle(const std::string &title_name, bool output_copy_right=false)
コマンドラインにこのソフトのタイトルを出力する関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static void OutputHorizontalLine(const std::string &line_visual, OutputDetail detail)
コマンドラインに水平線を出力する関数.
static bool InputYesNo(const std::string &str="Are you sure?")
yesかnoを入力させる関数.返り値で yes なら true,noなら falseを返す. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
ファイルツリーを作成するクラス.
Definition file_tree.h:21
bool SelectFile(const std::string &path, int max_depth, const std::string &extension, const std::string keyword, std::string *output) const
ディレクトリの中から,ファイルを一つ選択する.
Definition file_tree.cpp:27
static constexpr int kLegNum
マップを表すクラス.
Definition map_state.h:32
static const std::string kNodeListName
ノードリストのファイル名 (プログラムの読み込み用)
static const std::string kDirectoryPath
出力先ディレクトリ(フォルダ)名.
bool ImportNodeListAndMapState(const std::string &file_path, std::vector< RobotStateNode > *node_list, MapState *map_state) const
ノードリストとマップの状態をファイルから読み込む.
SystemMainResultViewer(const std::shared_ptr< GraphicDataBroker > &broker_ptr, const std::shared_ptr< const ApplicationSettingRecord > setting_ptr, const std::shared_ptr< const IHexapodJointCalculator > joint_calculator, const std::shared_ptr< const IHexapodCoordinateConverter > converter)
void Main() override
主要な処理を行う関数.
OutputDetail
コマンドラインに文字を出力する際に,その詳細を指定するための列挙体.
@ kSystem
システムメッセージ,常に出力する.
@ kError
エラーメッセージ.