-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuelSystem.cpp
More file actions
72 lines (60 loc) · 1.88 KB
/
FuelSystem.cpp
File metadata and controls
72 lines (60 loc) · 1.88 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
#include "FuelSystem.h"
#include "ECU.h"
#include <algorithm>
FuelSystem::FuelSystem()
: fuelRate_(0.0), pressure_(0.0), filterDelta_(0.0), temperature_(35.0), filterClog_(0.0),
fuelRemaining_(tankCapacity)
{
}
void FuelSystem::update(double dt, const ECU& ecu, const Sensors& sensors)
{
if (fuelRemaining_ <= 0.0) {
fuelRate_ = 0.0;
pressure_ = 0.0;
filterDelta_ = 0.1;
temperature_ += (25.0 - temperature_) * dt * 0.4;
temperature_ = std::clamp(temperature_, 25.0, 95.0);
filterClog_ = std::max(0.0, filterClog_ - dt * 0.0005);
return;
}
double baseFlow = std::clamp(ecu.fuelCommand().pulseWidthMs * 0.22, 0.0, 3.8);
double boostFactor = 1.0 + std::clamp(sensors.readings().boostPressure, 0.0, 2.0) * 0.15;
fuelRate_ = std::clamp(baseFlow * boostFactor, 0.05, 6.2);
double consumedLiters = fuelRate_ / 3600.0 * dt;
if (consumedLiters >= fuelRemaining_) {
fuelRate_ = fuelRemaining_ / dt * 3600.0;
fuelRemaining_ = 0.0;
} else {
fuelRemaining_ -= consumedLiters;
}
pressure_ = std::clamp(35.0 + fuelRate_ * 4.5 - filterClog_ * 2.5, 20.0, 80.0);
filterDelta_ = std::clamp(fuelRate_ * 0.08 + filterClog_ * 0.9, 0.1, 6.0);
temperature_ += ((35.0 + fuelRate_ * 5.0) - temperature_) * dt * 0.4;
temperature_ = std::clamp(temperature_, 25.0, 95.0);
filterClog_ += std::max(0.0, fuelRate_ * 0.0008 - dt * 0.0002);
filterClog_ = std::clamp(filterClog_, 0.0, 0.55);
}
double FuelSystem::fuelRate() const
{
return fuelRate_;
}
double FuelSystem::pressure() const
{
return pressure_;
}
double FuelSystem::filterDelta() const
{
return filterDelta_;
}
double FuelSystem::temperature() const
{
return temperature_;
}
double FuelSystem::fuelRemaining() const
{
return fuelRemaining_;
}
bool FuelSystem::hasFuel() const
{
return fuelRemaining_ > 0.0;
}