GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
controller.hpp
浏览该文件的文档.
1
6#pragma once
7
8#include <utility>
9#include <functional>
10#include <memory>
11#include <vector>
12
13#include "utils/utils.hpp"
14
15namespace roboctrl::utils{
16
18template<typename T>
20public:
21 virtual ~control_stage() = default;
22 virtual T update(T input, fp32 dt) = 0;
23 virtual void reset() = 0;
24};
25
30template<typename T>
32public:
33 void add(std::unique_ptr<control_stage<T>> stage) {
34 if (stage) stages_.push_back(std::move(stage));
35 }
36
37 T update(T input, fp32 dt) {
38 for (auto& stage : stages_) input = stage->update(input, dt);
39 return input;
40 }
41
42 void reset() {
43 for (auto& stage : stages_) stage->reset();
44 }
45
46 [[nodiscard]] bool empty() const noexcept { return stages_.empty(); }
47
48private:
49 std::vector<std::unique_ptr<control_stage<T>>> stages_;
50};
51
56template<typename T>
57concept controller = requires (T c) {
58 typename T::state_type;
59 typename T::input_type;
60 typename T::params_type;
61 { T(std::declval<typename T::params_type>()) };
62 { c.update(std::declval<typename T::input_type>()) } -> std::same_as<void>;
63 { c.state() } -> std::same_as<typename T::state_type>;
64};
65
70template<controller... Cs>
72 static_assert(sizeof...(Cs) > 0);
73
74 template<std::size_t N>
75 using state_type_n = typename std::tuple_element<N, std::tuple<typename Cs::state_type...>>::type;
76
77 template<std::size_t N>
78 using input_type_n = typename std::tuple_element<N, std::tuple<typename Cs::input_type...>>::type;
79
80 using state_type = state_type_n<sizeof...(Cs) - 1>;
81 using input_type = input_type_n<0>;
82 using params_type = std::tuple<typename Cs::params_type...>;
83
84 std::tuple<Cs...> controllers_;
85
89 explicit control_chain(Cs&&... cs)
90 : controllers_(std::forward<Cs>(cs)...) {}
91
95 explicit control_chain(params_type&& params)
96 : controllers_(std::apply(
97 [](auto&&... ps){ return std::make_tuple(Cs(std::forward<decltype(ps)>(ps))...); },
98 std::move(params)))
99 {}
100
101
106 void update(const input_type& input) {
107 auto current_input = input;
108 std::apply([&](auto&... ctrls){
109 ((ctrls.update(current_input),
110 current_input = ctrls.state()), ...);
111 }, controllers_);
112 }
113
117 state_type state() const {
118 return std::get<sizeof...(Cs) - 1>(controllers_).state();
119 }
120};
121
122template<controller controller_type>
123controller_type make_controller(const typename controller_type::info_type& info){
124 return {info};
125}
126
127}
控制器概念,约束 update/state/构造能力。
用于存放工具函数的命名空间。
Definition ballistics.hpp:6
控制链,将多个控制器串联组合。
state_type state() const
读取链路末尾控制器的状态。
void update(const input_type &input)
推动控制链一次,依次更新所有控制器。
control_chain(params_type &&params)
使用各控制器的参数构造实例。
control_chain(Cs &&... cs)
直接以控制器实例初始化。
常用数值与字节工具集合。
float fp32
单精度浮点别名。
Definition utils.hpp:22