GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
phantomx_renderer_simple.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 <array>
11
12#include "dxlib_util.h"
13#include "math_util.h"
14#include "leg_state.h"
15#include "phantomx_mk2_const.h"
16
17
18namespace designlab
19{
20
22 const std::shared_ptr<const IHexapodCoordinateConverter>& converter_ptr,
23 const std::shared_ptr<const IHexapodJointCalculator>& calculator_ptr,
24 DisplayQuality display_quality
25) :
26 kColorBody(GetColor(23, 58, 235)),
27 kColorLeg(GetColor(23, 58, 235)),
28 kColorLiftedLeg(GetColor(240, 30, 60)),
29 kColorJoint(GetColor(100, 100, 200)),
30 kColorLiftedJoint(GetColor(200, 100, 100)),
31 kColorLegBase(GetColor(100, 200, 100)),
32 kColorErrorText(GetColor(32, 32, 32)),
33 kColorErrorJoint(GetColor(180, 180, 64)),
34 kCapsuleDivNum(6),
35 kSphereDivNum(16),
36 kLegRadius(10.0f),
37 kJointRadius(20.0f),
38 converter_ptr_(converter_ptr),
39 calculator_ptr_(calculator_ptr),
40 display_quality_(display_quality)
41{
43}
44
45
47{
48 draw_node_ = node;
49
50 if (!calculator_ptr_) { return; } //計算器がないならば何もしない
51
52 draw_joint_state_ = calculator_ptr_->CalculateAllJointState(node);
53}
54
55
57{
58 // ロボットの描画
59 DrawHexapodNormal();
60}
61
62
63void PhantomXRendererSimple::DrawHexapodNormal() const
64{
65 //胴体を描画する.
66 std::array<VECTOR, HexapodConst::kLegNum> vertex;
67
68 for (int i = 0; i < HexapodConst::kLegNum; i++)
69 {
71 converter_ptr_->ConvertLegToGlobalCoordinate(
72 draw_joint_state_[i].joint_pos_leg_coordinate[0],
73 i,
75 draw_node_.posture,
76 true
77 )
78 );
79 }
80
82
83 //重心の描画
84 DrawSphere3D(dxlib_util::ConvertToDxlibVec(draw_node_.center_of_mass_global_coord), kJointRadius * 1.5f, kSphereDivNum, kColorJoint, kColorJoint, TRUE);
85
86 //脚を描画する.
87 for (int i = 0; i < HexapodConst::kLegNum; i++)
88 {
89 //脚の色を遊脚・接地で変更する.
90 const unsigned int kLegBaseColor = leg_func::IsGrounded(draw_node_.leg_state, i) ? kColorLeg : kColorLiftedLeg;
91 const unsigned int kJointColor = leg_func::IsGrounded(draw_node_.leg_state, i) ? kColorJoint : kColorLiftedJoint;
92
93 if (draw_joint_state_[i].joint_pos_leg_coordinate.size() != 4) { continue; }
94 if (draw_joint_state_[i].joint_angle.size() != 3) { continue; }
95
96 //各脚の描画
97 for (int j = 0; j < 3; j++)
98 {
99 const VECTOR start = dxlib_util::ConvertToDxlibVec(
100 converter_ptr_->ConvertLegToGlobalCoordinate(
101 draw_joint_state_[i].joint_pos_leg_coordinate[j],
102 i,
104 draw_node_.posture,
105 true
106 )
107 );
108
109 const VECTOR end = dxlib_util::ConvertToDxlibVec(
110 converter_ptr_->ConvertLegToGlobalCoordinate(
111 draw_joint_state_[i].joint_pos_leg_coordinate[j + 1],
112 i,
114 draw_node_.posture,
115 true
116 )
117 );
118
119 if (draw_joint_state_[i].is_in_range)
120 {
121 DrawCapsule3D(start, end, kLegRadius, kCapsuleDivNum, kLegBaseColor, kLegBaseColor, TRUE);
122 }
123 else
124 {
125 DrawCapsule3D(start, end, kLegRadius, kCapsuleDivNum, kColorErrorJoint, kColorErrorJoint, TRUE);
126 }
127 }
128
129
130 //間接の描画
131 for (int j = 0; j < 4; j++)
132 {
133 unsigned int color = kJointColor;
134
135 if (j == 0 && !PhantomXMkIIConst::IsValidCoxaAngle(i, draw_joint_state_[i].joint_angle[0])) { color = kColorErrorJoint; }
136 if (j == 1 && !PhantomXMkIIConst::IsValidFemurAngle(draw_joint_state_[i].joint_angle[1])) { color = kColorErrorJoint; }
137 if (j == 2 && !PhantomXMkIIConst::IsValidTibiaAngle(draw_joint_state_[i].joint_angle[2])) { color = kColorErrorJoint; }
138
140 converter_ptr_->ConvertLegToGlobalCoordinate(
141 draw_joint_state_[i].joint_pos_leg_coordinate[j],
142 i,
144 draw_node_.posture,
145 true
146 )
147 );
148
149 DrawSphere3D(pos, kJointRadius, kSphereDivNum, color, color, TRUE);
150 }
151
152 DrawSphere3D(
153 dxlib_util::ConvertToDxlibVec(converter_ptr_->ConvertLegToGlobalCoordinate(draw_node_.leg_pos[i], i, draw_node_.center_of_mass_global_coord, draw_node_.posture, true)),
154 kJointRadius / 2,
155 kSphereDivNum,
156 kJointColor,
157 kJointColor,
158 TRUE
159 );
160
161 //脚の接地の基準地点の描画
162 DrawSphere3D(
163 dxlib_util::ConvertToDxlibVec(converter_ptr_->ConvertLegToGlobalCoordinate(draw_node_.leg_reference_pos[i], i, draw_node_.center_of_mass_global_coord, draw_node_.posture, true)),
164 kJointRadius / 3,
165 kSphereDivNum,
166 kColorLegBase,
167 kColorLegBase,
168 TRUE
169 );
170 }
171}
172
173} // namespace designlab
static constexpr int kLegNum
static constexpr bool IsValidFemurAngle(const float angle) noexcept
第2関節の角度が有効な範囲内かどうかを判定する.
static constexpr bool IsValidTibiaAngle(const float angle) noexcept
第3関節の角度が有効な範囲内かどうかを判定する.
static constexpr float kBodyHeight
static constexpr bool IsValidCoxaAngle(const int leg_index, const float angle) noexcept
第1関節の角度が有効な範囲内かどうかを判定する.
PhantomXRendererSimple(const std::shared_ptr< const IHexapodCoordinateConverter > &converter_ptr, const std::shared_ptr< const IHexapodJointCalculator > &calculator_ptr, DisplayQuality display_quality)
void Draw() const override
描画処理を行う. const 関数にしているのは, 描画処理の中でメンバ変数を変更しないようにするため.
void SetNode(const RobotStateNode &node) override
ノードをセットする.
VECTOR ConvertToDxlibVec(const Vector3 &vec)
Dxlibの座標を示すVECTORと,このプログラムで使用しているVectorを変換する. ロボット座標系は右手座標系, Dxlibは左手座標系(工学は右手・ゲームライブラリは左手が多い)なのでyを...
Definition dxlib_util.h:35
void DrawHexagonalPrism(const std::array< VECTOR, 6 > &vertex, const float height, const unsigned int color)
3D空間に六角柱を描画する.
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
DisplayQuality
描画の品質設定を示す列挙体.
グラフ構造のためのノード(頂点).
std::array< Vector3, HexapodConst::kLegNum > leg_pos
[4 * 3 * 6 = 72 byte] 脚先の座標.(coxa(脚の付け根)を原点とする)
leg_func::LegStateBit leg_state
[4 byte] 脚状態,重心パターンを bitで表す.旧名 leg_con.
Vector3 center_of_mass_global_coord
[4 * 3 = 12byte] グローバル座標系における重心の位置.旧名 GCOM
Quaternion posture
[4 * 4 = 16byte] 姿勢を表すクォータニオン.
std::array< Vector3, HexapodConst::kLegNum > leg_reference_pos