GKD.RoboCtrl
RoboMaster Linux 电控:异步 IO、设备驱动与机器人控制
载入中...
搜索中...
未找到
ramp.hpp
1
#pragma once
2
#include <cmath>
3
#include <concepts>
4
#include <chrono>
5
6
#include "
utils/controller.hpp
"
7
8
namespace
roboctrl::utils
{
9
10
37
template
<std::
float
ing_po
int
T>
38
class
ramp
{
39
public
:
44
struct
params_type
{
45
T
acc
{};
46
};
47
48
using
input_type
= T;
49
using
state_type
= T;
50
51
public
:
52
ramp
() =
default
;
53
60
explicit
ramp
(
const
params_type
& params)
61
: acc_{params.acc}, last_update_{std::chrono::steady_clock::
now
()} {}
62
76
inline
void
update
(T target, T dt)
noexcept
{
77
if
(dt <= T{0})
return
;
78
const
T diff = target - out_;
79
const
T max_step = std::max(T{0}, acc_) * dt;
80
if
(std::fabs(diff) <= max_step)
81
out_ = target;
82
else
83
out_ += std::copysign(max_step, diff);
84
}
85
87
inline
void
update
(T target)
noexcept
{
88
using namespace
std::chrono;
89
const
auto
now
= steady_clock::now();
90
const
std::chrono::duration<T> dur =
now
- last_update_;
91
last_update_ =
now
;
92
update
(target, dur.count());
93
}
94
99
inline
void
set_acc
(T new_acc)
noexcept
{ acc_ = new_acc; }
100
105
inline
void
reset
() noexcept { out_ = T{0}; }
106
111
inline
void
reset
(T value)
noexcept
{ out_ = value; }
112
117
inline
state_type
state
() const noexcept {
return
out_; }
118
119
private
:
120
T out_ = T{0};
121
T acc_ = T{0};
122
std::chrono::steady_clock::time_point last_update_{};
123
};
124
128
using
ramp_f
=
ramp<float>
;
129
133
using
ramp_d
=
ramp<double>
;
134
138
static_assert
(
utils::controller<ramp_f>
);
139
static_assert
(
utils::controller<ramp_d>
);
140
141
}
// namespace control
roboctrl::utils::ramp
一阶斜坡控制器 (Ramp)
Definition
ramp.hpp:38
roboctrl::utils::ramp::update
void update(T target, T dt) noexcept
更新输出值
Definition
ramp.hpp:76
roboctrl::utils::ramp::reset
void reset(T value) noexcept
设置输出初值
Definition
ramp.hpp:111
roboctrl::utils::ramp::ramp
ramp(const params_type ¶ms)
构造函数
Definition
ramp.hpp:60
roboctrl::utils::ramp::state
state_type state() const noexcept
获取当前输出状态
Definition
ramp.hpp:117
roboctrl::utils::ramp::update
void update(T target) noexcept
Definition
ramp.hpp:87
roboctrl::utils::ramp::input_type
T input_type
输入类型:目标值
Definition
ramp.hpp:48
roboctrl::utils::ramp::reset
void reset() noexcept
清零输出值
Definition
ramp.hpp:105
roboctrl::utils::ramp::state_type
T state_type
状态类型:当前输出值
Definition
ramp.hpp:49
roboctrl::utils::ramp::set_acc
void set_acc(T new_acc) noexcept
修改最大变化速率
Definition
ramp.hpp:99
roboctrl::utils::controller
控制器概念,约束 update/state/构造能力。
Definition
controller.hpp:57
controller.hpp
控制器概念与串联控制链。
roboctrl::utils
用于存放工具函数的命名空间。
Definition
ballistics.hpp:6
roboctrl::utils::now
auto now()
记录程序启动后的纳秒级时间戳。
Definition
utils.hpp:158
roboctrl::utils::ramp::params_type
参数结构体
Definition
ramp.hpp:44
roboctrl::utils::ramp::params_type::acc
T acc
最大速度变化率 (单位/秒)
Definition
ramp.hpp:45
include
utils
ramp.hpp
制作者
1.9.8