-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathservo_math.cpp
More file actions
45 lines (36 loc) · 954 Bytes
/
Copy pathservo_math.cpp
File metadata and controls
45 lines (36 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
#include "servo_math.h"
#include <algorithm>
#include <cmath>
namespace stackchan::motion {
int clampServoSpeed(int speed)
{
return std::clamp(speed, 0, 1000);
}
ServoSpringProfile calculateServoSpringProfile(int speed)
{
speed = clampServoSpeed(speed);
float kMin = 10.0f;
float kMax = 650.0f;
float normalizedSpeed = speed / 1000.0f;
float stiffness = kMin + (normalizedSpeed * normalizedSpeed) * (kMax - kMin);
float mass = 1.0f;
float damping = 2.0f * std::sqrt(mass * stiffness);
ServoSpringProfile profile{
.stiffness = stiffness,
.damping = damping,
.mass = mass,
.restSpeed = 0.1f,
.restDelta = 0.1f,
};
if (speed > 800) {
profile.restDelta = 0.5f;
profile.restSpeed = 0.5f;
}
return profile;
}
} // namespace stackchan::motion