UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
SteerOptimizer.hpp
[詳解]
1//
2// 独立ステアリング機構最適化クラス
3//
4// Copyright (c) 2022-2024 udonrobo
5//
6
7#pragma once
8
10#include <Udon/Types/Polar.hpp>
11#include <array>
12
13#include "Math.hpp"
14
15namespace Udon
16{
17
21 {
22
24 double offset;
25
27 Udon::Polar optimized;
28
29 public:
32 : offset()
33 , optimized()
34 {
35 }
36
39 void clear()
40 {
41 offset = 0;
42 optimized = {};
43 }
44
49 Udon::Polar operator()(const Udon::Polar& current, const Udon::Polar& target)
50 {
51 // タイヤのどれも動いていない -> 前回値
52 if (target.r == 0)
53 {
54 optimized.r = 0;
55 return optimized;
56 }
57
58 // atan2 等から求めた角度 [-π ~ π] から無限回転に変換
59 const auto infinitemized = WrapAngle(current, /*ref*/ offset, target);
60
61 // 旋回方向、ホイール回転方向最適化後
62 const auto reversibled = OptimizeSteeringAndWheelRotation(current, infinitemized);
63
64 // 最適化値更新
65 optimized = reversibled;
66
67 return optimized;
68 }
69
74 {
75 return (*this)(optimized, target);
76 }
77
78 private:
84 static Udon::Polar WrapAngle(const Udon::Polar& current, double& offsetRef, const Udon::Polar& target)
85 {
86 // 変化角
87 const auto dTheta = target.theta + offsetRef - current.theta;
88
89 // 変化量がいきなり半周を超えた -> 計算値が-π~π間を通過 -> 一周分オフセットを加減算
90 if (dTheta > Udon::Pi)
91 {
92 offsetRef -= Udon::Pi * 2;
93 }
94 else if (dTheta < -Udon::Pi)
95 {
96 offsetRef += Udon::Pi * 2;
97 }
98 return { target.r, target.theta + offsetRef };
99 }
100
105 static Udon::Polar OptimizeSteeringAndWheelRotation(const Polar& current, const Polar& target)
106 {
107 // 変化角
108 const auto dTheta = target.theta - current.theta;
109
110 // 90度以上回転するときはホイールを逆回転させ、半周旋回
111 if (dTheta > Udon::Pi / 2)
112 {
113 return { target.r * -1, target.theta - Udon::Pi };
114 }
115 else if (dTheta < -Udon::Pi / 2)
116 {
117 return { target.r * -1, target.theta + Udon::Pi };
118 }
119 else
120 {
121 return target;
122 }
123 }
124 };
125
128 template <size_t WheelCount = 4>
130 {
131
133 std::array<SteerModuleOptimizer, WheelCount> modules;
134
135 public:
137 SteerOptimizer() = default;
138
141 void clear()
142 {
143 for (auto&& module : modules)
144 {
145 module.clear();
146 }
147 }
148
153 inline auto operator()(
154 const std::array<Udon::Polar, WheelCount>& current,
155 const std::array<Udon::Polar, WheelCount>& target) -> std::array<Udon::Polar, WheelCount>
156 {
157 std::array<Udon::Polar, WheelCount> optimized;
158 for (size_t i = 0; i < WheelCount; ++i)
159 {
160 optimized.at(i) = modules.at(i)(current.at(i), target.at(i));
161 }
162 return optimized;
163 }
164
168 auto operator()(const std::array<Udon::Polar, WheelCount>& target) -> std::array<Udon::Polar, WheelCount>
169 {
170 std::array<Udon::Polar, WheelCount> optimized;
171 for (size_t i = 0; i < WheelCount; ++i)
172 {
173 optimized.at(i) = modules.at(i)(target.at(i));
174 }
175 return optimized;
176 }
177 };
178
179} // namespace Udon
独立ステアリング機構の各モジュール最適化クラス
Definition SteerOptimizer.hpp:21
void clear()
最適化値を消去する
Definition SteerOptimizer.hpp:39
SteerModuleOptimizer()
コンストラクタ
Definition SteerOptimizer.hpp:31
Udon::Polar operator()(const Udon::Polar &current, const Udon::Polar &target)
最適化を行う(実測値と比較する)
Definition SteerOptimizer.hpp:49
Udon::Polar operator()(const Udon::Polar &target)
最適化を行う(前回の制御値と比較する)
Definition SteerOptimizer.hpp:73
独立ステアリング機構最適化クラス
Definition SteerOptimizer.hpp:130
SteerOptimizer()=default
コンストラクタ
auto operator()(const std::array< Udon::Polar, WheelCount > &current, const std::array< Udon::Polar, WheelCount > &target) -> std::array< Udon::Polar, WheelCount >
最適化を行う(実測値と比較する)
Definition SteerOptimizer.hpp:153
void clear()
最適化値を消去する
Definition SteerOptimizer.hpp:141
auto operator()(const std::array< Udon::Polar, WheelCount > &target) -> std::array< Udon::Polar, WheelCount >
最適化を行う(前回の制御値と比較する)
Definition SteerOptimizer.hpp:168
Definition Bit.hpp:12
constexpr double Pi
π
Definition Math.hpp:26
極座標系
Definition Polar.hpp:21
ValueType theta
角度
Definition Polar.hpp:29
ValueType r
中心からの距離
Definition Polar.hpp:26