GKD.RoboCtrl
载入中...
搜索中...
未找到
base.hpp
浏览该文件的文档.
1
12#pragma once
13#include <cstddef>
14#include <cstdint>
15#include <type_traits>
16
17#include "core/async.hpp"
18#include "core/logger.h"
19#include "core/multiton.hpp"
20#include "device/base.hpp"
21#include "utils/controller.hpp"
22#include "utils/utils.hpp"
23#include "io/base.hpp"
24
25namespace roboctrl::device{
26
28namespace details{
29
30 //通用电机测量数据。
31struct motor_measure{
32 uint16_t ecd = 0;
33 int16_t speed_rpm = 0;
34 int16_t given_current = 0;
35 uint8_t temperate = 0;
36};
37
38struct motor_upload_pkg {
39 uint8_t angle_h : 1;
40 uint8_t angle_l : 1;
41 uint8_t speed_h : 1;
42 uint8_t speed_l : 1;
43 uint8_t current_h : 1;
44 uint8_t current_l : 1;
45 uint8_t temperature : 1;
46 uint8_t unused : 1;
47} __attribute__((packed));
48
49inline motor_measure parse_motor_upload_pkg(io::byte_span data)
50{
51 motor_upload_pkg pkg = utils::from_bytes<motor_upload_pkg>(data);
52 return {
53 .ecd = utils::make_u16(pkg.angle_h, pkg.angle_l),
54 .speed_rpm = utils::make_i16(pkg.speed_h, pkg.speed_l),
55 .given_current = utils::make_i16(pkg.current_h, pkg.current_l),
56 .temperate = static_cast<uint8_t>(pkg.temperature)
57 };
58}
59
60}
62
63struct motor_base : public device_base {
64protected:
65 float angle_ {}; //rad
66 float angle_speed_ {}; //rad/s
67 float torque_ {}; // A
68 float radius_ {}; // m
69
70public:
76 inline fp32 angle() const { return angle_; }
77
83 inline fp32 angle_speed() const { return angle_speed_; }
84
90 inline fp32 rpm() const { return angle_speed_ * 60.f / (2.f * Pi_f); }
91
97 inline fp32 torque() const { return torque_; }
98
104 inline fp32 linear_speed() const { return angle_speed_ * radius_; }
105
110 inline explicit motor_base(const std::chrono::nanoseconds offline_timeout,fp32 radius) : device_base{offline_timeout},radius_{radius}{}
111};
112
113template <typename T>
114concept motor =
116 std::derived_from<T, motor_base> and
117 requires(T t) {
118 { t.set(std::declval<float>()) } -> std::same_as<awaitable<void>>;
119 { t.enable() } -> std::same_as<awaitable<void>>;
120 { t.task() } -> std::same_as<awaitable<void>>;
121 };
122
123
139template<motor motor_type,utils::controller controller_type>
140requires (std::is_convertible_v<float, typename controller_type::input_type> &&
141 std::is_convertible_v<typename controller_type::state_type, float >)
143 using motor_key_type = typename motor_type::info_type::key_type;
144
145 controller_type controller;
146 motor_key_type name;
147
149 motor_key_type key;
150 typename controller_type::params_type controller_params;
151 };
152
153 controlled_motor() = default;
154
155 controlled_motor(const motor_key_type& name,const typename controller_type::params_type& controller_params):
156 name{name},controller{controller_params}{}
157
163 awaitable<void> set(float state){
164 controller.update(state);
165 co_await roboctrl::get<motor_type>(name).set(controller.state());
166 }
167
173 inline motor_type& motor() const {return roboctrl::get<motor_type>(name); }
174
180 inline fp32 angle() const { return motor().angle(); }
181
187 inline fp32 angle_speed() const { return motor().angle_speed(); }
188
194 inline fp32 torque() const { return motor().torque(); }
195
201 inline fp32 linear_speed() const { return motor().linear_speed(); }
202};
203
216template<motor motor_type>
217awaitable<void> set_motor(const typename motor_type::info_type::key_type& key ,fp32 value){
218 co_await roboctrl::get<motor_type>(key).set(value);
219}
220
221} // namespace dev
222
223namespace motor {
224enum class dir : int {
225 forward = 1,
226 reverse = -1,
227};
228
229}
异步任务上下文组件。
控制器概念与串联控制链。
设备抽象基类与通用概念。
IO的基础组件。
用于日志输出的组件。
用于实现多例模式的通用组件。
asio::awaitable< T > awaitable
协程任务类型。
Definition async.hpp:42
设备模块
Definition base.hpp:25
awaitable< void > set_motor(const typename motor_type::info_type::key_type &key, fp32 value)
设置电机目标状态
Definition base.hpp:217
被控制算法(例如PID)控制的电机
Definition base.hpp:142
fp32 angle_speed() const
获取电机角速度(单位为rad/s)
Definition base.hpp:187
fp32 linear_speed() const
获取电机线速度(单位为m/s)
Definition base.hpp:201
fp32 torque() const
获取电机扭矩(单位为A)
Definition base.hpp:194
fp32 angle() const
获取电机角度(单位为rad)
Definition base.hpp:180
motor_type & motor() const
获取电机对象
Definition base.hpp:173
awaitable< void > set(float state)
设置电机目标状态
Definition base.hpp:163
设备基础类,提供判断设备离线的基础功能
Definition base.hpp:38
motor_base(const std::chrono::nanoseconds offline_timeout, fp32 radius)
Definition base.hpp:110
fp32 linear_speed() const
获取电机线速度(单位为m/s)
Definition base.hpp:104
fp32 angle_speed() const
获取电机角速度(单位为rad/s)
Definition base.hpp:83
fp32 torque() const
获取电机扭矩(单位为A)
Definition base.hpp:97
fp32 angle() const
获取电机角度(单位为rad)
Definition base.hpp:76
fp32 rpm() const
获取电机转速(单位为rpm)
Definition base.hpp:90
常用数值与字节工具集合。
constexpr auto Pi_f
单精度浮点类型的 Pi 常量。
Definition utils.hpp:34
float fp32
单精度浮点别名。
Definition utils.hpp:22