-
Notifications
You must be signed in to change notification settings - Fork 100
/
selftest.c
142 lines (109 loc) · 3.82 KB
/
selftest.c
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
/*
Copyright 2021 Benjamin Vedder [email protected]
This file is part of the VESC BMS firmware.
The VESC BMS firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC BMS firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "selftest.h"
#include "terminal.h"
#include "commands.h"
#include "ltc6813.h"
#include "bms_if.h"
#include "main.h"
#include "ch.h"
#include "hal.h"
#include "buffer.h"
typedef struct {
float no_bal;
float bal;
} cell_test_voltage_t;
// Private functions
static void terminal_st(int argc, const char **argv);
static int32_t balancing_selftest(cell_test_voltage_t *p_test_v);
void selftest_init(void) {
terminal_register_command_callback(
"hw_st",
"Run hardware self test",
0,
terminal_st);
}
int32_t selftest_serialize_result(uint8_t *buffer, uint32_t buffer_size) {
cell_test_voltage_t cell_v[LTC_MAX_NBR_CELLS];
int32_t len = 0;
bool res_ok = false;
if (buffer_size < (sizeof(uint16_t) +
((backup.config.cell_num - backup.config.cell_first_index) * sizeof(float))))
return 0;
res_ok = ltc_self_test();
buffer_append_int16(buffer, res_ok, &len);
if (balancing_selftest(cell_v) < 0)
return len;
for (int i = backup.config.cell_first_index;
i < (backup.config.cell_first_index + backup.config.cell_num);i++) {
buffer_append_float32_auto(buffer, cell_v[i].no_bal, &len);
buffer_append_float32_auto(buffer, cell_v[i].bal, &len);
}
return len;
}
static int32_t balancing_selftest(cell_test_voltage_t *p_test_v) {
if ((backup.config.cell_first_index + backup.config.cell_num) >= LTC_MAX_NBR_CELLS)
return -1;
for (int i = backup.config.cell_first_index;
i < (backup.config.cell_first_index + backup.config.cell_num);i++) {
bms_if_set_balance_override(i, 1);
}
chThdSleepMilliseconds(500);
for (int i = backup.config.cell_first_index;
i < (backup.config.cell_first_index + backup.config.cell_num);i++) {
bms_if_set_balance_override(i, 2);
ltc_set_dsc(i, 1);
chThdSleepMilliseconds(200);
p_test_v[i].no_bal = ltc_last_cell_voltage(i);
p_test_v[i].bal = ltc_last_cell_voltage_no_mute(i);
bms_if_set_balance_override(i, 1);
ltc_set_dsc(i, 0);
}
for (int i = backup.config.cell_first_index;
i < (backup.config.cell_first_index + backup.config.cell_num);i++) {
bms_if_set_balance_override(i, 0);
}
return 0;
}
static void terminal_st(int argc, const char **argv) {
(void)argc; (void)argv;
cell_test_voltage_t cell_v[LTC_MAX_NBR_CELLS];
bool res_ok = ltc_self_test();
commands_printf("Testing LTC6813... %s\n", res_ok ? "Ok" : "Failed");
balancing_selftest(cell_v);
commands_printf("Testing balancing and open connections...\n");
commands_printf("Cell NoBal Bal Diff Result");
commands_printf("===================================");
for (int i = backup.config.cell_first_index;
i < (backup.config.cell_first_index + backup.config.cell_num);i++) {
float diff = fabsf(cell_v[i].bal - cell_v[i].no_bal);
bool ok = (diff / cell_v[i].no_bal > 0.03 && cell_v[i].bal > 2.0);
#ifdef HW_NO_CH0_TEST
if (i == 0) {
ok = cell_v[i].bal > 2.0;
}
#endif
commands_printf("%02d %.4f %.4f %.4f %s",
i + 1,
cell_v[i].no_bal,
cell_v[i].bal,
diff,
ok ? "Ok" : "Failed");
if (!ok) {
res_ok = false;
}
}
commands_printf(res_ok ? "\nAll tests passed!\n" : "\nOne or more tests failed...\n");
}