GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
control_mapping.hpp
1#pragma once
2
3#include <algorithm>
4
5#include "device/controlpad.h"
6#include "utils/utils.hpp"
7
8namespace roboctrl::ctrl {
9
12 vectorf velocity {};
13 fp32 rotate_speed {};
14 fp32 yaw_delta {};
15 fp32 pitch_delta {};
16 fp32 pitch_target {};
17 bool use_pitch_target {false};
18 bool friction_enabled {false};
19 bool firing {false};
20 bool auto_aim {false};
21 bool arm_requested {false};
22};
23
29public:
32 constexpr std::int32_t key_d = 0x1;
33 constexpr std::int32_t key_a = 0x2;
34 constexpr std::int32_t key_s = 0x4;
35 constexpr std::int32_t key_w = 0x8;
36 constexpr std::int32_t key_r = 0x40;
37 constexpr std::int32_t key_f = 0x80;
38 constexpr std::int32_t s_up = 1;
39 constexpr std::int32_t s_down = 2;
40 constexpr std::int32_t roll_up = -660;
41 constexpr std::int32_t roll_down = 660;
42 constexpr fp32 rc_scale = 660.0f;
43
44 control_command command;
45 const auto pitch_wheel = input.gimbal_pitch_wheel();
46 const bool unlock_gesture =
47 input.s1 == s_down && input.s2 == s_down && pitch_wheel == roll_up;
48 if (unlock_gesture && !unlock_gesture_last_) {
49 armed_ = true;
50 command.arm_requested = true;
51 }
52 unlock_gesture_last_ = unlock_gesture;
53
54 if (input.key != 0) {
55 keyboard_mode_ = true;
56 }
57 if (!armed_) {
58 return command;
59 }
60
61 if (keyboard_mode_) {
62 command.velocity.x = static_cast<fp32>(bool(input.key & key_d))
63 - static_cast<fp32>(bool(input.key & key_a));
64 command.velocity.y = static_cast<fp32>(bool(input.key & key_w))
65 - static_cast<fp32>(bool(input.key & key_s));
66
67 const bool rotate_pressed = input.key & key_r;
68 if (rotate_pressed && !rotate_pressed_last_) {
69 rotate_enabled_ = !rotate_enabled_;
70 }
71 rotate_pressed_last_ = rotate_pressed;
72
73 const bool friction_pressed = input.key & key_f;
74 if (friction_pressed && !friction_pressed_last_) {
75 friction_enabled_ = !friction_enabled_;
76 }
77 friction_pressed_last_ = friction_pressed;
78
79 command.rotate_speed = rotate_enabled_ ? 1.0f : 0.0f;
80 command.friction_enabled = friction_enabled_;
81 command.auto_aim = input.mouse_r || (input.s1 == s_down && input.s2 == s_up);
82 command.firing = input.mouse_l || pitch_wheel == roll_down;
83 if (!command.auto_aim) {
84 command.yaw_delta = static_cast<fp32>(input.mouse_x) / 10000.0f;
85 command.pitch_delta = static_cast<fp32>(input.mouse_y) / 10000.0f;
86 }
87 return command;
88 }
89
90 command.velocity = {
91 .x = static_cast<fp32>(input.ch3) / rc_scale * 3.0f,
92 .y = static_cast<fp32>(input.ch2) / rc_scale * 3.0f
93 };
94 command.yaw_delta = static_cast<fp32>(input.ch0) / rc_scale / 200.0f;
95 command.pitch_target = std::clamp(
96 static_cast<fp32>(input.ch1) / rc_scale * 0.3f, -0.3f, 0.3f);
97 command.use_pitch_target = true;
98 command.rotate_speed = input.s1 == s_up ? 1.0f : 0.0f;
99 command.friction_enabled = input.s2 == s_up;
100 command.auto_aim = input.s1 == s_down && input.s2 == s_up;
101 command.firing = pitch_wheel == roll_down;
102 friction_enabled_ = command.friction_enabled;
103 return command;
104 }
105
107 [[nodiscard]] bool armed() const noexcept { return armed_; }
108 [[nodiscard]] bool keyboard_mode() const noexcept { return keyboard_mode_; }
109
114 void reset() noexcept {
115 armed_ = false;
116 keyboard_mode_ = false;
117 rotate_enabled_ = false;
118 friction_enabled_ = false;
119 rotate_pressed_last_ = false;
120 friction_pressed_last_ = false;
121 }
122
123private:
124 bool armed_ {false};
125 bool keyboard_mode_ {false};
126 bool rotate_enabled_ {false};
127 bool friction_enabled_ {false};
128 bool rotate_pressed_last_ {false};
129 bool friction_pressed_last_ {false};
130 bool unlock_gesture_last_ {false};
131};
132
133} // namespace roboctrl::ctrl
将遥控器快照映射为控制命令,并维护解锁状态。
bool armed() const noexcept
返回是否已完成解锁手势。
void reset() noexcept
清除解锁、按键边沿和摩擦轮状态。
control_command update(const device::control_pad_state &input)
将一个遥控器输入快照转换为控制命令。
遥控器串口设备与稳定输入快照。
已完成单位转换和解锁逻辑的控制层命令。
串口遥控输入的稳定内存表示。
Definition controlpad.h:22
std::int32_t gimbal_pitch_wheel() const noexcept
获取云台俯仰拨轮原始值。
Definition controlpad.h:49
常用数值与字节工具集合。
float fp32
单精度浮点别名。
Definition utils.hpp:22