-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
test_runner.cpp
157 lines (130 loc) · 5.55 KB
/
test_runner.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#define DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
#define DOCTEST_CONFIG_USE_STD_HEADERS
#define DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS
#define DOCTEST_CONFIG_NO_EXCEPTIONS
#define DOCTEST_CONFIG_NO_WINDOWS_SEH
#define DOCTEST_CONFIG_NO_POSIX_SIGNALS
// #define DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS
#include <doctest.h>
#include <communication/can_helpers.hpp>
using std::cout;
using std::endl;
TEST_SUITE("delta_enc") {
// Modulo (as opposed to remainder), per https://stackoverflow.com/a/19288271
int mod(int dividend, int divisor) {
int r = dividend % divisor;
return (r < 0) ? (r + divisor) : r;
}
int getDelta(int pos_abs, int count_in_cpr, int cpr) {
int delta_enc = pos_abs - count_in_cpr;
delta_enc = mod(delta_enc, cpr);
if (delta_enc > (cpr / 2))
delta_enc -= cpr;
return delta_enc;
}
TEST_CASE("mod") {
int cpr = 1000;
// Check moves around 0
CHECK(getDelta(1, 0, cpr) == 1);
CHECK(getDelta(0, 1, cpr) == -1);
CHECK(getDelta(999, 0, cpr) == -1);
CHECK(getDelta(50, 650, cpr) == 400);
CHECK(getDelta(650, 50, cpr) == -400);
CHECK(getDelta(50, 500, cpr) == -450);
CHECK(getDelta(500, 50, cpr) == 450);
// Test moving a distance larger than cpr / 2
CHECK(getDelta(950, 450, cpr) == 500);
CHECK(getDelta(451, 950, cpr) == -499);
CHECK(getDelta(450, 950, cpr) == 500);
// Test handling around mid-point
CHECK(getDelta(501, 499, cpr) == 2);
CHECK(getDelta(499, 501, cpr) == -2);
CHECK(getDelta(550, 450, cpr) == 100);
CHECK(getDelta(450, 550, cpr) == -100);
}
}
TEST_SUITE("velLimiter") {
// Velocity limiting in current mode
#include <algorithm>
using doctest::Approx;
auto limitVel(float vel_limit, float vel_estimate, float vel_gain, float Iq) {
float Imax = (vel_limit - vel_estimate) * vel_gain;
float Imin = (-vel_limit - vel_estimate) * vel_gain;
return std::clamp(Iq, Imin, Imax);
}
TEST_CASE("limit Vel") {
CHECK(limitVel(0, 0, 0, 0) == 0.0f);
CHECK(limitVel(1000.0f, 1.0f, 0.0f, 0.0f) == 0.0f);
CHECK(limitVel(1000.0f, 500.0f, 1.0f, 1.0f) == 1.0f);
CHECK(limitVel(1000.0f, 500.0f, 1.0f, -20.0f) == -20.0f);
CHECK(limitVel(1000.0f, 999.0f, 1.0f, 2.0f) == 1.0f);
CHECK(limitVel(1000.0f, 999.0f, 1.0f, -5.0f) == -5.0f);
CHECK(limitVel(1000.0f, -999.0f, 1.0f, -5.0f) == -1.0f);
CHECK(limitVel(1000.0f, -999.0f, 1.0f, 5.0f) == 5.0f);
CHECK(limitVel(1000.0f, 0.0f, 1.0f, 1.0f) == 1.0f);
CHECK(limitVel(1000.0f, 0.0f, 1.0f, -1.0f) == -1.0f);
}
TEST_CASE("Accelerating") {
CHECK(limitVel(200000.0f, 195000.0f, 5.0E-4f, 30.0f) == 2.5f);
CHECK(limitVel(200000.0f, 205000.0f, 5.0E-4f, 30.0f) == -2.5f);
CHECK(limitVel(200000.0f, -195000.0f, 5.0E-4, -30.0f) == -2.5f);
CHECK(limitVel(200000.0f, -205000.0f, 5.0E-4f, -30.0f) == 2.5f);
}
TEST_CASE("Decelerating") {
CHECK(limitVel(200000.0f, 195000.0f, 5.0E-4f, -30.0f) == -30.0f);
CHECK(limitVel(200000.0f, 205000.0f, 5.0E-4f, -30.0f) == -30.0f);
CHECK(limitVel(200000.0f, -195000.0f, 5.0E-4, 30.0f) == 30.0f);
CHECK(limitVel(200000.0f, -205000.0f, 5.0E-4f, 30.0f) == 30.0f);
}
TEST_CASE("Over-Center") {
CHECK(limitVel(20000.0f, 1000.0f, 5.0E-4f, 30.0f) == 9.5f);
CHECK(limitVel(20000.0f, -1000.0f, 5.0E-4f, 30.0f) == Approx(10.5f));
}
}
TEST_SUITE("vel_ramp") {
float vel_ramp_old(float input_vel_, float vel_setpoint_, float vel_ramp_rate) {
float max_step_size = 0.000125f * vel_ramp_rate;
float full_step = input_vel_ - vel_setpoint_;
float step;
if (std::abs(full_step) > max_step_size) {
step = std::copysignf(max_step_size, full_step);
} else {
step = full_step;
}
return step;
}
float vel_ramp_new(float input_vel_, float vel_setpoint_, float vel_ramp_rate) {
float max_step_size = 0.000125f * vel_ramp_rate;
float full_step = input_vel_ - vel_setpoint_;
return std::clamp(full_step, -max_step_size, max_step_size);
}
uint8_t parity(uint16_t v) {
v ^= v >> 8;
v ^= v >> 4;
v ^= v >> 2;
v ^= v >> 1;
return v & 1;
}
TEST_CASE("Equivalence") {
float vel_setpoint = 0.0f;
float vel_ramp_rate = 8000;
float input_vel = 0.0f;
CHECK(vel_ramp_old(input_vel, vel_setpoint, vel_ramp_rate) == vel_ramp_new(input_vel, vel_setpoint, vel_ramp_rate));
input_vel = 10.0f;
CHECK(vel_ramp_old(input_vel, vel_setpoint, vel_ramp_rate) == vel_ramp_new(input_vel, vel_setpoint, vel_ramp_rate));
input_vel = 10000.0f;
CHECK(vel_ramp_old(input_vel, vel_setpoint, vel_ramp_rate) == vel_ramp_new(input_vel, vel_setpoint, vel_ramp_rate));
input_vel = -10000.0f;
CHECK(vel_ramp_old(input_vel, vel_setpoint, vel_ramp_rate) == vel_ramp_new(input_vel, vel_setpoint, vel_ramp_rate));
input_vel = -0.1234f;
CHECK(vel_ramp_old(input_vel, vel_setpoint, vel_ramp_rate) == vel_ramp_new(input_vel, vel_setpoint, vel_ramp_rate));
input_vel = 0.1234f;
CHECK(vel_ramp_old(input_vel, vel_setpoint, vel_ramp_rate) == vel_ramp_new(input_vel, vel_setpoint, vel_ramp_rate));
}
TEST_CASE("Parity") {
CHECK(parity(0x0DDF & 0x7FFF) == 0);
CHECK(parity(0x8DDF & 0x7FFF) == 0);
CHECK(parity(0x5BFF & 0x7FFF) == 1);
}
}