GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
power_manager.h
1#pragma once
2
3#include <array>
4#include <cstdint>
5
6#include "core/async.hpp"
7#include "utils/singleton.hpp"
8
9namespace roboctrl::device { class chassis_base; }
10
11namespace roboctrl::ctrl {
12
15 float requested_current {};
16 float measured_current {};
17 float angular_speed {};
18 float target_angular_speed {};
19 float max_current {};
20 bool online {false};
21};
22
25 bool referee_online {false};
26 float referee_power_limit {};
27 bool buffer_online {false};
28 float buffer_energy {};
29 bool cap_online {false};
30 float cap_power_limit {};
31 float cap_energy {}; // Legacy capacitor protocol index, 0..255; not joules.
32 float measured_power {}; // Capacitor input, W; no guessed referee field.
33 std::uint64_t measurement_sequence {};
34};
35
37 std::array<float, 4> current_limits {};
38 float power_limit {};
39 float requested_power {};
40 float allocated_power {};
41 float measured_power {};
42 float speed_loss {};
43 float torque_loss {};
44 bool limited {false};
45 bool motor_fault {false};
46 bool telemetry_offline {true};
47 bool budget_unachievable {false};
48 std::uint64_t rls_updates {};
49};
50
52class power_manager : public utils::singleton_base<power_manager> {
53public:
54 struct info_type {
56 bool enabled {true};
57 float configured_power_limit {35.0f};
58 float offline_power_limit {35.0f};
59 float max_cap_boost {0.0f};
60 // Algebraic conversion of the legacy M3508 rotor model to output shaft.
61 float torque_per_current {0.3f * 20.0f / 16384.0f};
62 float speed_loss {0.22f * (3591.0f / 187.0f)};
63 float torque_loss {1.2f * (187.0f / 3591.0f) * (187.0f / 3591.0f)};
64 float constant_loss {2.78f};
65 float error_weight_low {1.0f}; // rad/s, not the legacy mixed-unit error.
66 float error_weight_high {2.0f};
67 float cap_base_energy {100.0f};
68 float cap_full_energy {250.0f};
69 float buffer_base_energy {50.0f};
70 float buffer_full_energy {60.0f};
71 float energy_kp {50.0f};
72 float energy_kd {0.0002f}; // Legacy kd=0.2 per 1 ms -> seconds derivative.
73 bool rls_enabled {false};
74 float rls_forgetting_factor {0.9999f};
75 float rls_initial_covariance {0.00001f};
76 float rls_period {0.01f}; // Seconds; consume a measurement only once.
77 float max_speed_loss {20.0f};
78 float max_torque_loss {20.0f};
79 };
80
81 power_manager() = default;
82 static bool valid_configuration(const info_type& info);
83 bool init(const info_type& info);
84 bool configured() const { return configured_; }
85 bool enabled() const { return configured_ && info_.enabled; }
86 const power_allocation& status() const { return status_; }
87
89 power_allocation allocate(const std::array<power_wheel, 4>& wheels,
90 const power_feedback& feedback, bool output_allowed,
91 float dt_seconds);
93 void update(device::chassis_base& chassis, const power_feedback& feedback,
94 bool output_allowed, float dt_seconds);
96 awaitable<void> update(device::chassis_base& chassis, bool output_allowed, float dt_seconds);
97
98private:
99 float energy_limit(const power_feedback& feedback, float dt_seconds);
100 void update_model(const std::array<power_wheel, 4>& wheels,
101 const power_feedback& feedback, float dt_seconds);
102 void reset_energy();
103
104 info_type info_ {};
105 bool configured_ {false};
106 power_allocation status_ {};
107 float speed_loss_ {};
108 float torque_loss_ {};
109 std::array<std::array<double, 2>, 2> covariance_ {};
110 float previous_base_error_ {};
111 float previous_full_error_ {};
112 unsigned energy_source_ {};
113 float rls_elapsed_ {};
114 std::uint64_t last_measurement_sequence_ {};
115 std::uint64_t rls_updates_ {};
116};
117
118static_assert(utils::singleton<power_manager>);
119
120} // namespace roboctrl::ctrl
异步任务上下文组件。
void update(device::chassis_base &chassis, const power_feedback &feedback, bool output_allowed, float dt_seconds)
power_allocation allocate(const std::array< power_wheel, 4 > &wheels, const power_feedback &feedback, bool output_allowed, float dt_seconds)
提供单例模式的单例基类
Definition singleton.hpp:19