GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
super_cap_protocol.hpp
1#pragma once
2
3#include <array>
4#include <bit>
5#include <cmath>
6#include <cstddef>
7#include <cstdint>
8#include <limits>
9#include <optional>
10#include <span>
11
12namespace roboctrl::device::super_cap_protocol {
13
14struct feedback {
15 uint8_t error;
16 float chassis_power;
17 uint16_t power_limit;
18 uint8_t energy;
19};
20
21inline std::optional<feedback> decode(std::span<const std::byte> data) {
22 if (data.size() != 8) return std::nullopt;
23 static_assert(sizeof(float) == 4 && std::numeric_limits<float>::is_iec559);
24 uint32_t raw = 0;
25 for (size_t i = 0; i < 4; ++i) raw |= uint32_t{std::to_integer<uint8_t>(data[i + 1])} << (8 * i);
26 const float power = std::bit_cast<float>(raw);
27 if (!std::isfinite(power)) return std::nullopt;
28 return feedback{std::to_integer<uint8_t>(data[0]), power,
29 static_cast<uint16_t>(std::to_integer<uint8_t>(data[5]) | uint16_t{std::to_integer<uint8_t>(data[6])} << 8),
30 std::to_integer<uint8_t>(data[7])};
31}
32
33inline std::array<std::byte, 8> encode(bool enabled, uint16_t power_limit, uint16_t buffer_target) {
34 return {static_cast<std::byte>(enabled), static_cast<std::byte>(power_limit & 0xff),
35 static_cast<std::byte>(power_limit >> 8), static_cast<std::byte>(buffer_target & 0xff),
36 static_cast<std::byte>(buffer_target >> 8), std::byte{0}, std::byte{0}, std::byte{0}};
37}
38
39inline bool output_enabled(bool requested, bool command_fresh, bool online, uint8_t error) {
40 return requested && command_fresh && online && error == 0;
41}
42
43} // namespace roboctrl::device::super_cap_protocol