GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
base.hpp
浏览该文件的文档.
1
5#pragma once
6
7#include <any>
8#include <chrono>
9#include <concepts>
10#include <functional>
11#include <memory>
12#include <string>
13#include <string_view>
14
15#include "utils/pid.h"
16#include "utils/utils.hpp"
17
18namespace roboctrl::device {
19using namespace std::chrono_literals;
20
23public:
25 struct info_type {
26 using owner_type = gimbal_base;
27 std::string imu_key {"imu"};
28 std::string yaw_motor_key {"gimbal_yaw_motor"};
29 std::string pitch_motor_key {"gimbal_pitch_motor"};
30 std::string yaw_motor_type {"dji"}, pitch_motor_type {"dji"};
31 utils::rad_pid::params_type yaw_angle_pid {}, pitch_angle_pid {};
32 utils::rad_pid::params_type yaw_relative_pid {};
33 utils::linear_pid::params_type yaw_rate_pid {}, pitch_rate_pid {};
34 fp32 yaw_zero {};
35 fp32 yaw_angle_direction {1.0f}, yaw_recenter_direction {1.0f};
36 bool yaw_zero_calibrated {false};
37 bool yaw_only {false};
38 bool yaw_current_control {true}, pitch_current_control {true};
39 bool recenter_on_enable {true};
40 fp32 init_tolerance {0.1f}, init_pitch {};
41 std::chrono::steady_clock::duration init_settle_time {2000ms};
42 fp32 yaw_direction {1.0f}, pitch_direction {1.0f};
43 fp32 pitch_min {-0.3f}, pitch_max {0.3f};
44 std::chrono::steady_clock::duration control_time {1ms};
45 };
46
47 virtual ~gimbal_base() = default;
48 virtual void start() = 0;
50 virtual fp32 yaw() const = 0;
52 virtual fp32 pitch() const = 0;
54 virtual fp32 relative_yaw() const = 0;
55 virtual bool relative_yaw_valid() const = 0;
56 virtual bool online() const = 0;
57 virtual bool initialized() const = 0;
59 virtual void set_recentering(bool) = 0;
60 virtual void hold() = 0;
61 virtual fp32 yaw_rate() const = 0;
62 virtual fp32 target_yaw() const = 0;
63 virtual fp32 target_pitch() const = 0;
65 virtual void set_target_yaw(fp32) = 0;
67 virtual void set_target_pitch(fp32) = 0;
69 virtual void add_yaw(fp32) = 0;
71 virtual void add_pitch(fp32) = 0;
73 virtual void set_enabled(bool) = 0;
74};
75
77template<typename T>
78concept gimbal = std::derived_from<T, gimbal_base>;
79
82public:
83 using factory = std::function<std::unique_ptr<gimbal_base>(const std::any&)>;
85 static bool register_type(std::string type, factory creator);
87 static gimbal_base* create(std::string_view type, const std::any& info);
89 static gimbal_base* current();
91 static bool init(std::string_view type, const std::any& info);
92};
93
94#ifndef ROBOCTRL_DETAIL_CAT
95#define ROBOCTRL_DETAIL_CAT_IMPL(a, b) a##b
96#define ROBOCTRL_DETAIL_CAT(a, b) ROBOCTRL_DETAIL_CAT_IMPL(a, b)
97#endif
98
99#define ROBOCTRL_REGISTER_GIMBAL(TYPE_NAME, CLASS_NAME) \
100 namespace { [[maybe_unused]] const bool ROBOCTRL_DETAIL_CAT(roboctrl_gimbal_registered_, __LINE__) = [] { \
101 return ::roboctrl::device::gimbal_registry::register_type( \
102 (TYPE_NAME), [](const std::any& value) -> std::unique_ptr<::roboctrl::device::gimbal_base> { \
103 const auto& info = std::any_cast<const CLASS_NAME::info_type&>(value); \
104 auto result = std::make_unique<CLASS_NAME>(); \
105 if (!result->init(info)) return nullptr; \
106 return result; \
107 }); \
108 }(); }
109
110} // namespace roboctrl::device
云台的统一姿态目标接口。
Definition base.hpp:22
virtual void set_enabled(bool)=0
启用或禁用云台输出。
virtual fp32 yaw() const =0
获取当前 yaw 姿态,单位 rad。
virtual void set_target_yaw(fp32)=0
设置 yaw 绝对目标,单位 rad。
virtual void add_yaw(fp32)=0
增加 yaw 目标量,单位 rad。
virtual void set_recentering(bool)=0
virtual void add_pitch(fp32)=0
增加 pitch 目标量,单位 rad。
virtual fp32 pitch() const =0
获取当前 pitch 姿态,单位 rad。
virtual void set_target_pitch(fp32)=0
设置 pitch 绝对目标,单位 rad。
virtual fp32 relative_yaw() const =0
具体云台实现的运行时工厂注册表。
Definition base.hpp:81
static gimbal_base * current()
获取当前已初始化的云台。
Definition base.cpp:33
static gimbal_base * create(std::string_view type, const std::any &info)
按类型名创建并初始化云台。
Definition base.cpp:37
static bool init(std::string_view type, const std::any &info)
按类型名初始化当前云台。
Definition base.cpp:54
static bool register_type(std::string type, factory creator)
注册一个类型名及其工厂;重复类型名返回 false。
Definition base.cpp:28
判断类型是否实现云台抽象。
Definition base.hpp:78
通用 PID 控制器实现。
云台初始化参数,包括 IMU、电机和角度限制。
Definition base.hpp:25
统一的参数封装,方便序列化。
Definition pid.h:36
常用数值与字节工具集合。
float fp32
单精度浮点别名。
Definition utils.hpp:22