-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataFunctions.h
260 lines (203 loc) · 10.2 KB
/
DataFunctions.h
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
//==============================================
/*
'dataFunctions.h'
This takes the recieved CAN messages and processes them based on defined scaling factors and offsets,
then stores them inside the data structs.
*/
//==============================================
#pragma once
#include "wifi.h"
#include "display.h"
#include "pinMap.h"
#include "SensorData.h"
#include "config.h"
#include "led.h"
#include "nvs.h"
// Configure how test data increments
int lastTestIncrement = 0;
int testTime = 25;
float testIncrementValue = 1.01;
int warningLimit = 1;
bool INC = true;
bool ALERT = false;
// ========================================================== Functions =========================================================================
void incrementTestData()
{
// Increment test data
if ((lastTestIncrement + testTime) < millis())
{
lastTestIncrement = millis();
if (INC == true)
{
testData.scaledValue = testData.scaledValue + testIncrementValue;
}
else
{
testData.scaledValue = testData.scaledValue - testIncrementValue;
}
if (testData.scaledValue > 110)
{
INC = false;
}
if (testData.scaledValue < -110)
{
INC = true;
}
#ifdef DEBUG_SERIAL
// Serial.print("Incremented Test data : ");
// Serial.println(testData.scaledValue);
#endif
}
}
void checkAlert()
{
if (warningCounter > warningLimit || checkEngine > 0)
{
ALERT = TRUE;
}
else
{
ALERT = FALSE;
}
}
bool sensorAlertCheck(float min, float max, float val)
{
if (val < min)
{
return true;
}
if (val > max)
{
return true;
}
return false;
}
void SAVE_CAN_DATA(twai_message_t CANmsg)
{
// Check the ID of the recieved CAN message, scale and store the sensor values
// Some data is sent in two bytes and is combined into a word before scaling
switch (CANmsg.identifier)
{
// FOME CAN
case (512):
warningCounter = ((int)word(CANmsg.data[1], CANmsg.data[0]));
lastError = ((int)word(CANmsg.data[3], CANmsg.data[2]));
revLimit = ((int)CANmsg.data[4]) & 0x01;
mainRelay = ((int)CANmsg.data[4]) & 0x02;
fuelPump = ((int)CANmsg.data[4]) & 0x04;
checkEngine = ((int)CANmsg.data[4]) & 0x08;
egoHeater = ((int)CANmsg.data[4]) & 0x10;
gear.scaledValue = ((int)CANmsg.data[5]);
break;
case (513):
rpm.scaledValue = ((((int)word(CANmsg.data[1], CANmsg.data[0])) * (rpm.scaleMultiplier)) + rpm.offset);
ignitionTiming.scaledValue = ((((float)word(CANmsg.data[3], CANmsg.data[2])) * (ignitionTiming.scaleMultiplier)) + ignitionTiming.offset);
injectorDuty.scaledValue = ((((float)word(CANmsg.data[5], CANmsg.data[4])) * (injectorDuty.scaleMultiplier)) + injectorDuty.offset);
vss.scaledValue = ((((float)(CANmsg.data[6])) * (vss.scaleMultiplier)) + vss.offset);
vss_mph.scaledValue = ((((float)(CANmsg.data[6])) * (vss_mph.scaleMultiplier)) + vss_mph.offset);
ethanol.scaledValue = ((((float)(CANmsg.data[7])) * (ethanol.scaleMultiplier)) + ethanol.offset);
break;
case (514):
accelerator.scaledValue = ((((short)word(CANmsg.data[1], CANmsg.data[0])) * (accelerator.scaleMultiplier)) + accelerator.offset);
throttle1.scaledValue = ((((short)word(CANmsg.data[3], CANmsg.data[2])) * (throttle1.scaleMultiplier)) + throttle1.offset);
throttle2.scaledValue = ((((short)word(CANmsg.data[5], CANmsg.data[4])) * (throttle2.scaleMultiplier)) + throttle2.offset);
wastegate.scaledValue = ((((short)word(CANmsg.data[7], CANmsg.data[6])) * (wastegate.scaleMultiplier)) + wastegate.offset);
break;
case (515):
manifoldPressure.scaledValue = ((((float)word(CANmsg.data[1], CANmsg.data[0])) * (manifoldPressure.scaleMultiplier)) + manifoldPressure.offset);
boost_kpa.scaledValue = manifoldPressure.scaledValue + boost_kpa.offset;
manifoldPressure_psi.scaledValue = ((((float)word(CANmsg.data[1], CANmsg.data[0])) * (manifoldPressure_psi.scaleMultiplier)) + manifoldPressure_psi.offset);
boost_psi.scaledValue = manifoldPressure_psi.scaledValue + boost_psi.offset;
fuelPressDelta_kpa.scaledValue = fuelPressureLow.scaledValue - manifoldPressure.scaledValue;
//fuelPressDelta_psi.scaledValue = fuelPressureLow_psi.scaledValue - manifoldPressure_psi.scaledValue;
coolantTemperature.scaledValue = ((((float)(CANmsg.data[2])) * (coolantTemperature.scaleMultiplier)) + coolantTemperature.offset);
coolantTemperature_f.scaledValue = ((coolantTemperature.scaledValue * (coolantTemperature_f.scaleMultiplier)) + coolantTemperature_f.offset);
intakeTemperature.scaledValue = ((((float)(CANmsg.data[3])) * (intakeTemperature.scaleMultiplier)) + intakeTemperature.offset);
intakeTemperature_f.scaledValue = ((intakeTemperature.scaledValue * intakeTemperature_f.scaleMultiplier) + intakeTemperature_f.offset);
auxTemp1.scaledValue = ((((float)(CANmsg.data[4])) * (auxTemp1.scaleMultiplier)) + auxTemp1.offset);
auxTemp2.scaledValue = ((((float)(CANmsg.data[5])) * (auxTemp2.scaleMultiplier)) + auxTemp2.offset);
auxTemp1_f.scaledValue = ((auxTemp1.scaledValue * auxTemp1_f.scaleMultiplier) + auxTemp1_f.offset);
auxTemp2_f.scaledValue = ((auxTemp2.scaledValue * auxTemp2_f.scaleMultiplier) + auxTemp2_f.offset);
mcuTemp.scaledValue = ((((float)(CANmsg.data[6])) * (mcuTemp.scaleMultiplier)) + mcuTemp.offset);
fuel.scaledValue = ((((float)(CANmsg.data[7])) * (fuel.scaleMultiplier)) + fuel.offset);
break;
case (516):
//afr.scaledValue = ((((float)word(CANmsg.data[1], CANmsg.data[0])) * (afr.scaleMultiplier)) + afr.offset);
oilPressure_psi.scaledValue = ((((float)word(CANmsg.data[3], CANmsg.data[2])) * (oilPressure_psi.scaleMultiplier)) + oilPressure_psi.offset);
oilPressure.scaledValue = ((((float)word(CANmsg.data[3], CANmsg.data[2])) * (oilPressure.scaleMultiplier)) + oilPressure.offset);
oilTemperature.scaledValue = (((float)(CANmsg.data[4]) * (oilTemperature.scaleMultiplier)) + oilTemperature.offset);
oilTemperature_f.scaledValue = ((oilTemperature.scaledValue * (oilTemperature_f.scaleMultiplier)) + oilTemperature_f.offset);
fuelTemperature.scaledValue = (((float)(CANmsg.data[5]) * (fuelTemperature.scaleMultiplier)) + fuelTemperature.offset);
fuelTemperature_f.scaledValue = ((fuelTemperature.scaledValue * fuelTemperature_f.scaleMultiplier) + fuelTemperature_f.offset);
//vvtPosition.scaledValue = ((((float)word(CANmsg.data[5], CANmsg.data[4])) * (vvtPosition.scaleMultiplier)) + vvtPosition.offset);
batteryVoltage.scaledValue = ((((float)word(CANmsg.data[7], CANmsg.data[6])) * (batteryVoltage.scaleMultiplier)) + batteryVoltage.offset);
break;
case (517):
airMass.scaledValue = ((((float)word(CANmsg.data[1], CANmsg.data[0])) * (airMass.scaleMultiplier)) + airMass.offset);
estimatedAirflow.scaledValue = ((((float)word(CANmsg.data[3], CANmsg.data[2])) * (estimatedAirflow.scaleMultiplier)) + estimatedAirflow.offset);
injectorPulse.scaledValue = ((((float)word(CANmsg.data[5], CANmsg.data[4])) * (injectorPulse.scaleMultiplier)) + injectorPulse.offset);
break;
case (518):
fuelConsumed.scaledValue = ((((float)word(CANmsg.data[1], CANmsg.data[0])) * (fuelConsumed.scaleMultiplier)) + fuelConsumed.offset);
fuelConsumption.scaledValue = ((((float)word(CANmsg.data[3], CANmsg.data[2])) * (fuelConsumption.scaleMultiplier)) + fuelConsumption.offset);
fuelTrim.scaledValue = ((((short)word(CANmsg.data[5], CANmsg.data[4])) * (fuelTrim.scaleMultiplier)) + fuelTrim.offset);
break;
case (519):
lambda1.scaledValue = ((((float)word(CANmsg.data[1], CANmsg.data[0])) * (lambda1.scaleMultiplier)) + lambda1.offset);
lambda2.scaledValue = ((((float)word(CANmsg.data[3], CANmsg.data[2])) * (lambda2.scaleMultiplier)) + lambda2.offset);
afr.scaledValue = lambda1.scaledValue * STOICH_RATIO;
fuelPressureLow.scaledValue = ((((float)word(CANmsg.data[5], CANmsg.data[4])) * (fuelPressureLow.scaleMultiplier)) + fuelPressureLow.offset);
fuelPressureHigh.scaledValue = ((((float)word(CANmsg.data[7], CANmsg.data[6])) * (fuelPressureHigh.scaleMultiplier)) + fuelPressureHigh.offset);
break;
case (520):
vvtIntake1.scaledValue = (float) CANmsg.data[0] * vvtIntake1.scaleMultiplier + vvtIntake1.offset;
vvtIntake2.scaledValue = (float) CANmsg.data[4] * vvtIntake2.scaleMultiplier + vvtIntake2.offset;
vvtExhaust1.scaledValue = (float) CANmsg.data[2] * vvtExhaust1.scaleMultiplier + vvtExhaust1.offset;
vvtExhaust2.scaledValue = (float) CANmsg.data[6] * vvtExhaust2.scaleMultiplier + vvtExhaust2.offset;
break;
// MS CAN
case (1512):
manifoldPressure.scaledValue = (((float)word(CANmsg.data[0], CANmsg.data[1])) / 10);
boost_kpa.scaledValue = manifoldPressure.scaledValue + boost_kpa.offset;
manifoldPressure_psi.scaledValue = ((manifoldPressure.scaledValue) * (0.1450377377));
boost_psi.scaledValue = manifoldPressure_psi.scaledValue + boost_psi.offset;
rpm.scaledValue = (((int)word(CANmsg.data[2], CANmsg.data[3])));
coolantTemperature_f.scaledValue = ((((int)word(CANmsg.data[4], CANmsg.data[5])) / 10));
coolantTemperature.scaledValue = (coolantTemperature_f.scaledValue - 32) / 1.8;
throttle1.scaledValue = ((((short)word(CANmsg.data[6], CANmsg.data[7])) / 10));
break;
case (1513):
// PW1
injectorPulse.scaledValue = (((float)word(CANmsg.data[0], CANmsg.data[1])) / 1000);
// mat
intakeTemperature_f.scaledValue = ((float)word((CANmsg.data[4]), (CANmsg.data[5]))) / 10;
intakeTemperature.scaledValue = (intakeTemperature_f.scaledValue-32)/1.8;
// adv_deg
ignitionTiming.scaledValue = ((float)word(CANmsg.data[6], CANmsg.data[7])) / 10;
break;
case (1514):
// b1 afr target
// AFR1
afr.scaledValue = (((float)(CANmsg.data[1]) / 10));
// EGO corr 1
fuelTrim.scaledValue = (((float)word(CANmsg.data[2], CANmsg.data[3])) / 10);
// EGT 1
// pwseq1
break;
case (1515):
// Battery
batteryVoltage.scaledValue = (((float)word(CANmsg.data[0], CANmsg.data[1])) / 10);
// Sensor 1
auxTemp1.scaledValue = (((float)word(CANmsg.data[2], CANmsg.data[3])) / 100);
// Sensor 2
auxTemp2.scaledValue = (((float)word(CANmsg.data[4], CANmsg.data[5])) / 100);
// knock retard
break;
case (1516):
// Vss 1
vss.scaledValue = (((float)word(CANmsg.data[0], CANmsg.data[1])) / 10);
// tc_rtd
// Launch time
break;
}
}