Skip to content

Commit 6cb523f

Browse files
MarkusMarkus
Markus
authored and
Markus
committed
pressureNormalization, injectControl PID
1 parent 62a2b1e commit 6cb523f

File tree

4 files changed

+79
-32
lines changed

4 files changed

+79
-32
lines changed

main/.vscode/c_cpp_properties.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
"macFrameworkPath": [
1616
"/System/Library/Frameworks",
1717
"/Library/Frameworks"
18-
]
18+
],
19+
"compilerPath": "/usr/bin/clang",
20+
"cStandard": "c11",
21+
"cppStandard": "c++17"
1922
}
2023
],
21-
"version": 3
24+
"version": 4
2225
}

main/calc.cpp

+35-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,31 @@ int initEVoltage = analogRead(exhaustPresPin) * 5.0;
1818

1919
// Calculation helpers
2020

21+
int pressureNormalization(int givenPressure)
22+
{
23+
struct SensorVals sensor = readSensors();
24+
int targetVoltage = 12000;
25+
float pressureModifier = targetVoltage / sensor.curBattery;
26+
if (sensor.curBattery < targetVoltage)
27+
{
28+
Serial.println("Battery voltage too low, target pressure cannot be reached.");
29+
}
30+
31+
int normalizedPressure = pressureModifier * givenPressure;
32+
if (normalizedPressure > 100)
33+
{
34+
normalizedPressure = 100;
35+
Serial.println("Pressure high compensation reached. Low battery?")
36+
}
37+
else if (normalizedPressure < 0)
38+
{
39+
normalizedPressure = 0;
40+
Serial.println("Pressure low compensation reached. Too high voltage?")
41+
}
42+
43+
return normalizedPressure;
44+
}
45+
2146
// Mapping throttle position sensor voltage to percentage
2247
int readTPSVoltage(int voltage)
2348
{
@@ -34,14 +59,14 @@ int readTPSVoltage(int voltage)
3459
// Mapping boost sensor voltage to percentage
3560
int readBoostVoltage(int voltage)
3661
{
37-
int result = voltage * 700/2.95;
62+
int result = voltage * 700 / 2.95;
3863
//int result = map(voltage, initBVoltage, 3100, 0, 3000); // NXP MPX5700AP (range 0-700kPa)
3964
return result;
4065
}
4166

4267
int readExPresVoltage(int voltage)
4368
{
44-
int result = voltage * 700/4.6;
69+
int result = voltage * 700 / 4.6;
4570
//int result = map(voltage, initEVoltage, 5000, 0, 3000); // NXP MPX5700AP (range 0-700kPa)
4671
return result;
4772
}
@@ -147,7 +172,6 @@ int readTempMapInverted(const int theMap[14][2], int y)
147172
return betweenL1;
148173
}
149174

150-
151175
int readPercentualMap(const int theMap[14][12], int x, int y)
152176
{
153177

@@ -289,8 +313,14 @@ int readPercentualMap(const int theMap[14][12], int x, int y)
289313
}
290314
}
291315
calculatedPoint = calculatedPoint * config.transSloppy;
292-
if ( calculatedPoint > 100 ) { calculatedPoint = 100; }
293-
if ( calculatedPoint < 1 ) { calculatedPoint = 0; }
316+
if (calculatedPoint > 100)
317+
{
318+
calculatedPoint = 100;
319+
}
320+
if (calculatedPoint < 1)
321+
{
322+
calculatedPoint = 0;
323+
}
294324
return calculatedPoint;
295325
}
296326

main/core.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ void doShift()
140140
Serial.println(F("[switchGearStart->switchGearStart] SPC low limit hit."));
141141
}
142142
}
143-
spcSetVal = (100 - spcPercentVal) * 2.55; // these are calculated twice to make sure if there is changes they are noted.
144-
mpcSetVal = (100 - mpcPercentVal) * 2.55;
143+
spcPressureNormalized = pressureNormalization(spcPercentVal);
144+
mpcPressureNormalized = pressureNormalization(mpcPercentVal);
145+
onPressureNormalized = pressureNormalization(100);
146+
147+
spcSetVal = (100 - spcPressureNormalized) * 2.55; // these are calculated twice to make sure if there is changes they are noted.
148+
mpcSetVal = (100 - mpcPressureNormalized) * 2.55;
145149

146150
shiftStartTime = millis(); // Beginning to count shiftStartTime
147151
// pinmode change is due the fact how n2/n3 speed sensors change during the shift.
@@ -150,7 +154,7 @@ void doShift()
150154
analogWrite(tcc, 0);
151155
analogWrite(spc, spcSetVal);
152156
analogWrite(mpc, mpcSetVal);
153-
analogWrite(cSolenoidEnabled, 255); // Beginning of gear change
157+
analogWrite(cSolenoidEnabled, onPressureNormalized); // Beginning of gear change
154158

155159
if (debugEnabled)
156160
{

main/input.cpp

+32-22
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,27 @@ byte wantedGear = 100;
1414

1515
// INPUT
1616

17-
// Pid tuning parameters
18-
const double Kp = 7; //80,21 Pid Proporional Gain. Initial ramp up i.e Spool, Lower if over boost
19-
double Ki = 20; //40,7 Pid Integral Gain. Overall change while near Target Boost, higher value means less change, possible boost spikes
20-
const double Kd = 0; //100, 1 Pid Derivative Gain.
17+
// Pid tuning parameters, for boostCtrl
18+
const double boostKp = 7; //80,21 Pid Proporional Gain. Initial ramp up i.e Spool, Lower if over boost
19+
double boostKi = 20; //40,7 Pid Integral Gain. Overall change while near Target Boost, higher value means less change, possible boost spikes
20+
const double boostKd = 0; //100, 1 Pid Derivative Gain.
21+
double pidBoost, boostPWM, pidBoostLim;
22+
//Load PID controller
23+
AutoPID boostPID(&pidBoost, &pidBoostLim, &boostPWM, 0, 255, boostKp, boostKi, boostKd);
24+
25+
#ifdef ECU
26+
// Pid tuning parameters, for injectionCtrl
27+
const double injectKp = 7; //80,21 Pid Proporional Gain. Initial ramp up, Lower if over
28+
double injectKi = 20; //40,7 Pid Integral Gain. Overall change while near Target
29+
const double injectKd = 0; //100, 1 Pid Derivative Gain.
30+
double pidInject, injectPWM, pidInjectLim;
31+
//Load PID controller
32+
AutoPID injectPID(&pidInject, &pidInjectLim, &injectPWM, 0, 255, injectKp, injectKi, injectKd);
33+
#endif
34+
2135
boolean garageShift, garageShiftMove, tpsConfigMode, tpsInitPhase1, tpsInitPhase2 = false;
22-
double garageTime, lastShift, lastInput;
36+
double garageTime, lastShift, lastInput, hornPressTime;
2337
int lockVal = 0;
24-
/*
25-
const double Kp = 200;
26-
double Ki = 100;
27-
const double Kd = 25; //This is not necessarily good idea.
28-
*/
29-
double pidBoost, boostPWM, pidBoostLim, hornPressTime;
30-
31-
//Load PID controller
32-
AutoPID myPID(&pidBoost, &pidBoostLim, &boostPWM, 0, 255, Kp, Ki, Kd);
3338

3439
// Polling for stick control
3540
// This is W202 electronic gear stick, should work on any pre-canbus sticks.
@@ -211,8 +216,8 @@ void boostControl(Task *me)
211216
struct SensorVals sensor = readSensors();
212217
pidBoost = sensor.curBoost;
213218
pidBoostLim = sensor.curBoostLim;
214-
myPID.setBangBang(100, 50);
215-
myPID.setTimeStep(100);
219+
boostPID.setBangBang(100, 50);
220+
boostPID.setTimeStep(100);
216221

217222
if (shiftBlocker && !slipFault && boostLimitShift)
218223
{
@@ -225,18 +230,18 @@ void boostControl(Task *me)
225230
{
226231
pidBoostLim = 0;
227232
}
228-
Ki = 5; // New integral gain value; we want change of pressure to be aggressive here.
233+
boostKi = 5; // New integral gain value; we want change of pressure to be aggressive here.
229234
}
230235
else
231236
{
232237
pidBoostLim = sensor.curBoostLim;
233-
Ki = 20; // New integral gain value; we want change of pressure to be more modest.
238+
boostKi = 20; // New integral gain value; we want change of pressure to be more modest.
234239
}
235240

236241
// Just a sanity check to make sure PID library is not doing anything stupid.
237242
if (sensor.curBoostLim > 0 && !slipFault && truePower)
238243
{
239-
myPID.run();
244+
boostPID.run();
240245
analogWrite(boostCtrl, boostPWM);
241246
// if (debugEnabled) { Serial.print("BoostPWM = "); Serial.println(boostPWM); }
242247
}
@@ -555,21 +560,26 @@ int adaptSPC(int mapId, int xVal, int yVal)
555560

556561
void injectionControl(Task *me)
557562
{
563+
#ifdef ECU
558564
struct SensorVals sensor = readSensors();
559565
int fuelRequire = sensor.curLoad / sensor.curLambda; // eg. 100% load / 100% lambda = 1x fueling, 100% load / 10% lambda = 10x fueling
560566
int fuelAmount = readMap(injectionMap, fuelRequire, sensor.curRPM);
561567
fuelAmount = fuelAmount * 2.55;
562-
analogWrite(injectionPin, fuelAmount);
563-
568+
injectPID.setBangBang(100, 50);
569+
injectPID.setTimeStep(100);
570+
// Read injectionpump travel
571+
injectPID.run();
572+
analogWrite(injectionPin, injectPWM);
564573
if (debugEnabled)
565574
{
566575
Serial.print("Fueling quantity with load/lambda: ")
567-
Serial.print(sensor.curLoad);
576+
Serial.print(sensor.curLoad);
568577
Serial.print("/");
569578
Serial.print(sensor.curLambda);
570579
Serial.print(" is ");
571580
Serial.print(fuelAmount);
572581
}
582+
#endif
573583
}
574584

575585
void radioControl()

0 commit comments

Comments
 (0)