-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCritical_Page.cpp
More file actions
195 lines (172 loc) · 4.92 KB
/
Critical_Page.cpp
File metadata and controls
195 lines (172 loc) · 4.92 KB
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
#include "Critical_Page.h"
#include <Arduino.h>
#include <Wire.h>
#include <MY17_Can_Library.h>
#include <LiquidCrystal.h>
// From VCU
static bool limpOn = false;
static bool aeroOn = false;
static bool tcOn = false;
static bool regenOn = false;
static bool lvLow = false;
// From Current Sensor
static int16_t power_cW = 0;
static int16_t voltage_V = 0;
// From BMS
static uint8_t soc = 0;
static uint8_t temp = 0;
static uint16_t min_cell_voltage = 0;
static bool dcdc_fault = false;
static String lastTop = "";
static String lastBottom = "";
static Can_Vcu_LimpState_T limp_state = CAN_LIMP_NORMAL;
void Critical_Page::act(Action_T action) {
if (action == TOUCH) {
Can_Dash_Request_T msg;
msg.type = CAN_DASH_REQUEST_LIMP_MODE_ENABLE;
Can_Dash_Request_Write(&msg);
}
}
void Critical_Page::process_Vcu_DashHeartbeat(Can_Vcu_DashHeartbeat_T *msg){
limpOn = msg->limp_mode;
aeroOn = msg->active_aero;
tcOn = msg->traction_control;
regenOn = msg->regen;
lvLow = msg->lv_warning;
limp_state = msg->limp_state;
}
void Critical_Page::process_CurrentSensor_Power(Can_CurrentSensor_Power_T *msg)
{
int32_t power = msg->power_W / 100;
// This cast is safe because power will never go above 100 kW, or 1000 cW
power_cW = (int16_t) power;
}
void Critical_Page::process_CurrentSensor_Voltage(Can_CurrentSensor_Voltage_T *msg)
{
voltage_V = msg->voltage_mV / 1000;
}
void Critical_Page::process_Bms_Heartbeat(Can_Bms_Heartbeat_T *msg) {
soc = msg->soc;
if (msg->dcdc_enable && msg->dcdc_fault) {
// Fault line is on when trying to enable, no bueno
dcdc_fault = true;
} else {
dcdc_fault = false;
}
}
void Critical_Page::process_Bms_CellTemps(Can_Bms_CellTemps_T *msg) {
// max_cell_temp is in deci-celsius
int16_t max_temp_C = msg->max_cell_temp / 10;
// Car temp should never go above 255C, so this cast is safe
temp = (uint8_t) max_temp_C;
}
void Critical_Page::process_Bms_PackStatus(Can_Bms_PackStatus_T *msg) {
// cell voltage is in mV
min_cell_voltage = msg->min_cell_voltage;
}
void Critical_Page::screen(String& top, String& bottom) {
top_line(top);
bottom_line(bottom);
}
void Critical_Page::top_line(String& line) {
num_to_three_char_string(voltage_V, line);
line.concat("V ");
if (limp_state == CAN_LIMP_NORMAL) {
line.concat(" ");
} else if (limp_state == CAN_LIMP_50) {
line.concat("LIMP50");
} else if (limp_state == CAN_LIMP_33) {
line.concat("LIMP33");
} else if (limp_state == CAN_LIMP_25) {
line.concat("LIMP25");
}
line.concat(" ");
line.concat(aeroOn ? "AERO" : " ");
}
void Critical_Page::bottom_line(String& line) {
// TODO when soc finished if ever
//displaySoc(line);
displayLowestCell(line);
displayTemp(line);
displayPower(line);
}
void Critical_Page::displayLowestCell(String& line) {
if (min_cell_voltage < 100) {
// Lowest cell is below 1 volt so something is very wrong
line.concat("RIP:(");
} else {
String voltage_string = String(min_cell_voltage);
line.concat(voltage_string.charAt(0));
line.concat(".");
line.concat(voltage_string.charAt(1));
line.concat(voltage_string.charAt(2));
line.concat("V");
line.concat(" ");
}
}
void Critical_Page::displaySoc(String& line) {
if (soc > 99) {
soc = 99;
}
line.concat("SOC");
String soc_string = String(soc);
bool two_digits = soc >= 10;
// We have asserted above that SOC is 99 or below
line.concat(two_digits ? soc_string.charAt(0) : '0');
line.concat(two_digits ? soc_string.charAt(1) : soc_string.charAt(0));
line.concat(" ");
}
void Critical_Page::displayTemp(String& line) {
if (temp > 99) {
temp = 99;
}
bool two_digits = temp >= 10;
String temp_string = String(temp);
line.concat(two_digits ? temp_string.charAt(0) : '0');
line.concat(two_digits ? temp_string.charAt(1) : temp_string.charAt(0));
line.concat("C");
}
void Critical_Page::displayPower(String& line) {
int16_t power_disp = power_cW;
if (power_cW < 0) {
power_disp = -1 * power_disp;
line.concat('-');
} else {
line.concat(' ');
}
if (power_cW > 999) {
power_disp = 999;
}
String power_string = String(power_disp);
if (power_disp >= 100) {
line.concat(power_string.charAt(0));
line.concat(power_string.charAt(1));
line.concat('.');
line.concat(power_string.charAt(2));
} else if (power_disp >= 10) {
line.concat('0');
line.concat(power_string.charAt(0));
line.concat('.');
line.concat(power_string.charAt(1));
} else {
line.concat('0');
line.concat('0');
line.concat('.');
line.concat(power_string.charAt(0));
}
line.concat("kW");
}
void Critical_Page::num_to_three_char_string(int32_t num, String& output) {
String num_string = String(num);
uint8_t len = num_string.length();
if (len > 3) {
output.concat("999");
return;
}
for (int i = 0; i < 3 - len; i++) {
output.concat(" ");
}
for (int i = 0; i < len; i++) {
output.concat(num_string.charAt(i));
}
}