-
Notifications
You must be signed in to change notification settings - Fork 0
/
Store_Controller.cpp
385 lines (349 loc) · 10.4 KB
/
Store_Controller.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "Store_Controller.h"
#include "Can_Ids.h"
#include "Dispatch_Controller.h"
#include "Logger.h"
// Value for indicating that there is not yet a reading
const int16_t SENTINAL = -32768;
const String NO_DIRECTION = "only";
bool verbose = false;
Store_Controller::Store_Controller()
// Fault logging
: hasFault(true)
// Can node logging
, analogThrottle(0)
, analogBrake(0)
, brakeThrottleConflict(false)
// Wheel logging
, speeds{SENTINAL, SENTINAL, SENTINAL, SENTINAL}
// Motor controller states
, responses{false, false}
, errors{false, false}
, motorRpm{SENTINAL, SENTINAL}
// Motor controller readings
, torqueCommands{SENTINAL, SENTINAL}
// BMS readings
, bmsTemp(SENTINAL)
, bmsVoltage(SENTINAL)
, soc(SENTINAL)
{
// No initialization required
}
/********************* Fault Logging *************************/
void Store_Controller::logHasFault(bool fault) {
hasFault = fault;
}
bool Store_Controller::readHasFault() {
return hasFault;
}
/********************* Can Node Logging **********************/
void Store_Controller::logAnalogThrottle(const uint8_t throttle) {
analogThrottle = throttle;
Onboard().logFour("throttle_position", NO_DIRECTION, throttle, "uint8_t");
}
uint8_t Store_Controller::readAnalogThrottle() {
return analogThrottle;
}
void Store_Controller::logAnalogBrake(const uint8_t brake) {
analogBrake = brake;
Onboard().logFour("brake_pressure", NO_DIRECTION, brake, "uint8_t");
}
uint8_t Store_Controller::readAnalogBrake() {
return analogBrake;
}
void Store_Controller::logBrakeThrottleConflict(const bool conflict) {
brakeThrottleConflict = conflict;
if (conflict) {
// Breaks pattern because error that we want to catch and filter
// Onboard().logFive("brake_throttle_conflict", analogThrottle, "throttle", analogBrake, "brake");
}
}
bool Store_Controller::readBrakeThrottleConflict() {
return brakeThrottleConflict;
}
/******************** Wheel Speed Logging *********************/
void Store_Controller::logSpeed(const Wheel wheel, const int16_t rpm) {
speeds[wheel] = rpm;
String wheelName = "";
switch(wheel) {
case FrontRightWheel:
wheelName = "front_right";
break;
case FrontLeftWheel:
wheelName = "front_left";
break;
case RearRightWheel:
wheelName = "rear_right";
break;
case RearLeftWheel:
wheelName = "rear_left";
break;
default:
//Should never happen
return;
}
Onboard().logFour("wheel_rpm", wheelName, rpm, "rpm");
}
int16_t Store_Controller::readSpeed(const Wheel wheel) {
return speeds[wheel];
}
/****************** Motor Controller Logging ******************/
void Store_Controller::logMotorResponse(Motor dir) {
responses[dir] = true;
}
bool Store_Controller::readMotorResponse(Motor dir) {
return responses[dir];
}
String motor_faults[16] = {
"parameter_damaged",
"igbt_error",
"should_never_happen",
"can_timeout",
"faulty_resolver",
"under_voltage",
"over_temp",
"over_current",
"current_out_of_tolerance",
"3x_over_current",
"raceaway",
"can_hardware_error",
"adc_error",
"faulty_encoder",
"software_error",
"ballast_overload"
};
void Store_Controller::logMotorErrors(Motor dir, uint16_t error_string) {
String motor_name = (dir == RightMotor) ? "right" : "left";
bool hasFault = false;
// First update the array of errors
for(int i = 0; i < 16; i++) {
if (bitRead(error_string, i)) {
String error_name = motor_faults[i];
if (error_name != "under_voltage") {
hasFault = true;
Onboard().logThree("motor_fault", motor_name, error_name);
}
}
}
if (errors[dir] != hasFault) {
// Record fault
errors[dir] = hasFault;
// Light goes on if either has fault
bool lightState = errors[LeftMotor] || errors[RightMotor];
// Write message
uint8_t faultValue = lightState ? 4 : 3;
Frame error_light_frame = {.id=VCU_ID, .body={faultValue}, .len=1};
CAN().write(error_light_frame);
}
}
String motor_warnings[16] = {
"inconsistent_identification",
"faulty_RUN_signal",
"inactive_RFE",
"should_never_happen",
"should_never_happen",
"missing_or_low_power_voltage",
"Motor_temp>87%",
"Device_temp>87%",
"OverVoltage>1.5xUN",
"2x_over_current",
"should_never_happen",
"should_never_happen",
"Overload>87%",
"should_never_happen",
"should_never_happen",
"Ballast_circuit_overload>87%"
};
void Store_Controller::logMotorWarnings(Motor dir, uint16_t warning_string){
String motor_name = (dir == RightMotor) ? "right" : "left";
for (int i = 0; i < 16; i++) {
if (bitRead(warning_string, i)) {
String warning_name = motor_warnings[i];
Onboard().logThree("motor_warning", motor_name, warning_name);
}
}
}
String motor_states[32] = {
"hardware_enable",
"speed_cmd_zero",
"lim_plus",
"lim_minus",
"drive_ok",
"curr_lim_cont",
"speed_lim_torque_mode",
"pos_control_ok",
"speed_control_ok",
"speed_near_zero",
"ref_switch_tripped",
"calib_active",
"calib_done",
"pos_within_tol",
"drive_ready",
"de_energized_brake",
"speed_inverted",
"speed_lim_ok",
"pos_speed_lim_ok",
"neg_speed_lim_ok",
"curr_lim_dig",
"curr_lim_actual",
"curr_lim_speed",
"curr_lim_igbt_temp",
"curr_lim_cont_igbt_temp",
"curr_lim_freq",
"curr_lim_motor_temp",
"curr_derate_analog",
"curr_at_peak",
"rfe_pulse",
"should_never_happen",
"hand_wheel_input"
};
void Store_Controller::logMotorState(Motor dir, uint32_t state_string){
String motor_name = (dir == RightMotor) ? "right" : "left";
String motor_message = "";
for (int i = 0; i < 32; i++) {
if (bitRead(state_string, i)){
motor_message += motor_states[i] + ", ";
}
}
// Onboard().logThree("motor_state", motor_name, motor_message);
}
void Store_Controller::logMotorTorqueCommand(Motor dir, int16_t torqueCommand) {
torqueCommands[dir] = torqueCommand;
String motor_name = (dir == RightMotor) ? "right" : "left";
if (Dispatcher().isEnabled()) {
// Onboard().logFour("motor_torque_cmd", motor_name, torqueCommand, "uint16_t");
}
}
int16_t Store_Controller::readMotorTorqueCommand(Motor controller) {
return torqueCommands[controller];
}
void Store_Controller::logMotorRpm(Motor dir, int16_t rpm) {
motorRpm[dir] = rpm;
String motor_name = (dir == RightMotor) ? "right" : "left";
if (Dispatcher().isEnabled()) {
Onboard().logFour("motor_rpm", motor_name, rpm, "motor_units");
}
}
int16_t Store_Controller::readMotorRpm(Motor controller) {
return motorRpm[controller];
}
void Store_Controller::logMotorCurrent(Motor dir, int16_t current) {
String motor_name = (dir == RightMotor) ? "right" : "left";
if (Dispatcher().isEnabled()) {
// Onboard().logFour("motor_current", motor_name, current, "motor_units");
(void)current;
}
}
void Store_Controller::logMotorCurrentAfterFilter(Motor dir, int16_t current) {
String motor_name = (dir == RightMotor) ? "right" : "left";
if (Dispatcher().isEnabled()) {
// Onboard().logFour("cur_after_filter", motor_name, current, "motor_units");
(void)current;
}
}
void Store_Controller::logMotorCurrentCommand(Motor dir, int16_t current) {
String motor_name = (dir == RightMotor) ? "right" : "left";
if (Dispatcher().isEnabled()) {
// Onboard().logFour("cur_command", motor_name, current, "motor_units");
(void)current;
}
}
void Store_Controller::logMotorAirTemp(Motor dir, int16_t temp) {
String motor_name = (dir == RightMotor) ? "right" : "left";
if (Dispatcher().isEnabled()) {
// Onboard().logFour("motor_air_temp", motor_name, temp, "motor_units");
(void)temp;
}
}
void Store_Controller::logMotorIgbtTemp(Motor dir, int16_t temp) {
String motor_name = (dir == RightMotor) ? "right" : "left";
if (Dispatcher().isEnabled()) {
// Onboard().logFour("motor_igbt_temp", motor_name, temp, "motor_units");
(void)temp;
}
}
void Store_Controller::logMotorCurrentLimit(Motor dir, int16_t limit) {
String motor_name = (dir == RightMotor) ? "right" : "left";
if (Dispatcher().isEnabled()) {
// Onboard().logFour("cur_limit", motor_name, limit, "motor_units");
(void)limit;
}
}
Motor Store_Controller::toMotor(uint16_t id) {
return id == RIGHT_MOTOR_ID ? RightMotor : LeftMotor;
}
Motor Store_Controller::otherMotor(Motor dir) {
return dir == RightMotor ? LeftMotor : RightMotor;
}
/************************ BMS Logging *************************/
String bms_faults[8] = {
"driving_off",
"interlock_tripped",
"communication_fault",
"charge_overcurrent",
"discharge_overcurrent",
"over_temp",
"under_voltage",
"over_voltage"
};
void Store_Controller::logBmsFaults(uint8_t fault_string) {
for(int i = 0; i < 8; i++) {
if (bitRead(fault_string, i)) {
String fault_name = bms_faults[i];
// Breaks pattern because error that we want to catch and filter
// Xbee().logTwo("bms_fault", fault_name);
Onboard().logTwo("bms_fault", fault_name);
}
}
}
String bms_warnings[8] = {
"low_voltage",
"high_voltage",
"charge_overcurrent",
"discharge_overcurrent",
"cold_temperature",
"hot_temperature",
"low_soh",
"isolation_fault"
};
void Store_Controller::logBmsWarnings(uint8_t warning_string) {
for(int i = 0; i < 8; i++) {
if (bitRead(warning_string, i)) {
String warning_name = bms_warnings[i];
// Breaks pattern because error that we want to catch and filter
// Xbee().logTwo("bms_warning", warning_name);
// Onboard().logTwo("bms_warning", warning_name);
}
}
}
void Store_Controller::logBmsTemp(const int16_t _bmsTemp) {
bmsTemp = _bmsTemp;
// Onboard().logFour("bms_temp", NO_DIRECTION, bmsTemp, "degrees");
}
int16_t Store_Controller::readBmsTemp() {
return bmsTemp;
}
void Store_Controller::logBmsVoltage(const int16_t _bmsVoltage) {
bmsVoltage = _bmsVoltage;
// Onboard().logFour("bms_voltage", NO_DIRECTION, bmsVoltage, "volts");
}
int16_t Store_Controller::readBmsVoltage() {
return bmsVoltage;
}
void Store_Controller::logBmsSoc(const int16_t _soc) {
soc = _soc;
// Onboard().logFour("bms_soc", NO_DIRECTION, soc, "percent");
}
int16_t Store_Controller::readBmsSoc() {
return soc;
}
/********************* Private Methods **************************/
Store_Controller* Store_Controller::instance = NULL;
Store_Controller& Store_Controller::readInstance() {
if(!instance) {
instance = new Store_Controller();
}
return *instance;
}
Store_Controller& Store() {
return Store_Controller::readInstance();
}