forked from bobsayshilol/engine-sim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload_simulation_cluster.cpp
More file actions
270 lines (227 loc) · 9.72 KB
/
load_simulation_cluster.cpp
File metadata and controls
270 lines (227 loc) · 9.72 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
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
#include "../include/load_simulation_cluster.h"
#include "../include/engine_sim_application.h"
#include "../include/ui_utilities.h"
#include <sstream>
#include <iomanip>
LoadSimulationCluster::LoadSimulationCluster() {
m_torqueGauge = nullptr;
m_hpGauge = nullptr;
m_simulator = nullptr;
m_clutchPressureGauge = nullptr;
m_filteredHorsepower = 0.0;
m_filteredTorque = 0.0;
m_peakHorsepower = 0.0;
m_peakTorque = 0.0;
m_peakHorsepowerRpm = 0.0;
m_peakTorqueRpm = 0.0;
memset(m_systemStatusLights, 0, sizeof(double) * 4);
}
LoadSimulationCluster::~LoadSimulationCluster() {
/* void */
}
void LoadSimulationCluster::initialize(EngineSimApplication *app) {
UiElement::initialize(app);
constexpr float shortenAngle = (float)units::angle(1.0, units::deg);
m_dynoSpeedGauge = addElement<LabeledGauge>();
m_dynoSpeedGauge->m_title = "DYNO. SPEED";
m_dynoSpeedGauge->m_unit = "RPM";
m_dynoSpeedGauge->m_precision = 0;
m_dynoSpeedGauge->setLocalPosition({ 0, 0 });
m_dynoSpeedGauge->m_gauge->m_min = 0;
m_dynoSpeedGauge->m_gauge->m_max = 100;
m_dynoSpeedGauge->m_gauge->m_minorStep = 500;
m_dynoSpeedGauge->m_gauge->m_majorStep = 1000;
m_dynoSpeedGauge->m_gauge->m_maxMinorTick = INT_MAX;
m_dynoSpeedGauge->m_gauge->m_thetaMin = (float)constants::pi * 0.8f;
m_dynoSpeedGauge->m_gauge->m_thetaMax = (float)constants::pi * 0.2f;
m_dynoSpeedGauge->m_gauge->m_needleWidth = 4.0f;
m_dynoSpeedGauge->m_gauge->m_gamma = 1.0f;
m_dynoSpeedGauge->m_gauge->m_needleKs = 1000.0f;
m_dynoSpeedGauge->m_gauge->m_needleKd = 5.0f;
m_dynoSpeedGauge->m_gauge->setBandCount(0);
m_torqueGauge = addElement<LabeledGauge>();
m_torqueGauge->m_title = "TORQUE";
m_torqueGauge->m_unit = "LB-FT";
m_torqueGauge->m_precision = 0;
m_torqueGauge->setLocalPosition({ 0, 0 });
m_torqueGauge->m_gauge->m_min = 0;
m_torqueGauge->m_gauge->m_max = 1000;
m_torqueGauge->m_gauge->m_minorStep = 50;
m_torqueGauge->m_gauge->m_majorStep = 100;
m_torqueGauge->m_gauge->m_maxMinorTick = INT_MAX;
m_torqueGauge->m_gauge->m_thetaMin = (float)constants::pi * 0.8f;
m_torqueGauge->m_gauge->m_thetaMax = (float)constants::pi * 0.2f;
m_torqueGauge->m_gauge->m_needleWidth = 4.0f;
m_torqueGauge->m_gauge->m_gamma = 1.0f;
m_torqueGauge->m_gauge->m_needleKs = 1000.0f;
m_torqueGauge->m_gauge->m_needleKd = 5.0f;
m_torqueGauge->m_gauge->setBandCount(0);
m_hpGauge = addElement<LabeledGauge>();
m_hpGauge->m_title = "HORSEPOWER";
m_hpGauge->m_unit = "HP";
m_hpGauge->m_precision = 0;
m_hpGauge->setLocalPosition({ 0, 0 });
m_hpGauge->m_gauge->m_min = 0;
m_hpGauge->m_gauge->m_max = 1000;
m_hpGauge->m_gauge->m_minorStep = 50;
m_hpGauge->m_gauge->m_majorStep = 100;
m_hpGauge->m_gauge->m_maxMinorTick = INT_MAX;
m_hpGauge->m_gauge->m_thetaMin = (float)constants::pi * 0.8f;
m_hpGauge->m_gauge->m_thetaMax = (float)constants::pi * 0.2f;
m_hpGauge->m_gauge->m_needleWidth = 4.0f;
m_hpGauge->m_gauge->m_gamma = 1.0f;
m_hpGauge->m_gauge->m_needleKs = 1000.0f;
m_hpGauge->m_gauge->m_needleKd = 5.0f;
m_hpGauge->m_gauge->setBandCount(0);
m_clutchPressureGauge = addElement<LabeledGauge>();
m_clutchPressureGauge->m_title = "CLUTCH";
m_clutchPressureGauge->m_unit = "";
m_clutchPressureGauge->m_spaceBeforeUnit = false;
m_clutchPressureGauge->m_precision = 0;
m_clutchPressureGauge->setLocalPosition({ 0, 0 });
m_clutchPressureGauge->m_gauge->m_min = 0;
m_clutchPressureGauge->m_gauge->m_max = 100;
m_clutchPressureGauge->m_gauge->m_minorStep = 10;
m_clutchPressureGauge->m_gauge->m_majorStep = 50;
m_clutchPressureGauge->m_gauge->m_maxMinorTick = 200;
m_clutchPressureGauge->m_gauge->m_thetaMin = (float)constants::pi * 0.8f;
m_clutchPressureGauge->m_gauge->m_thetaMax = (float)constants::pi * 0.2f;
m_clutchPressureGauge->m_gauge->m_needleWidth = 4.0f;
m_clutchPressureGauge->m_gauge->m_gamma = 1.0f;
m_clutchPressureGauge->m_gauge->m_needleKs = 1000.0f;
m_clutchPressureGauge->m_gauge->m_needleKd = 5.0f;
m_clutchPressureGauge->m_gauge->m_needleMaxVelocity = 10.0f;
m_clutchPressureGauge->m_gauge->setBandCount(0);
}
void LoadSimulationCluster::destroy() {
/* void */
}
void LoadSimulationCluster::update(float dt) {
UiElement::update(dt);
const float systemStatuses[] = {
m_simulator->getEngine()->getIgnitionModule()->m_enabled ? 1.0f : 0.01f,
m_simulator->m_starterMotor.m_enabled ? 1.0f : 0.01f,
m_simulator->m_dyno.m_enabled ? 1.0f : 0.01f,
m_simulator->m_dyno.m_hold ? (m_simulator->m_dyno.m_enabled ? 1.0f : 0.25f) : 0.01f
};
constexpr float RC = 0.08f;
const float alpha = dt / (dt + RC);
for (int i = 0; i < 4; ++i) {
const float next = systemStatuses[i];
m_systemStatusLights[i] = (1 - alpha) * m_systemStatusLights[i] + alpha * next;
}
if (m_app->getEngine()->ProcessKeyDown(ysKey::Code::I)) {
std::stringstream ss;
ss << std::setprecision(0) << std::fixed;
ss << m_peakHorsepower << "HP @ " << m_peakHorsepowerRpm << "rpm"
<< " | "
<< m_peakTorque << "lb-ft @ " << m_peakTorqueRpm << "rpm";
m_app->getInfoCluster()->setLogMessage(ss.str());
}
updateHpAndTorque(dt);
}
void LoadSimulationCluster::render() {
Grid grid;
grid.h_cells = 3;
grid.v_cells = 2;
const Bounds gearBounds = grid.get(m_bounds, 2, 0);
drawCurrentGear(gearBounds);
const Bounds clutchBounds = grid.get(m_bounds, 1, 0);
drawClutchPressureGauge(clutchBounds);
const Bounds systemStatusBounds = grid.get(m_bounds, 0, 0);
drawSystemStatus(systemStatusBounds);
const Bounds dynoSpeedBounds = grid.get(m_bounds, 0, 1);
m_dynoSpeedGauge->m_gauge->m_value =
(float)units::toRpm(m_simulator->m_dyno.m_rotationSpeed);
m_dynoSpeedGauge->m_bounds = dynoSpeedBounds;
constexpr float shortenAngle = (float)units::angle(1.0, units::deg);
const double redline = units::toRpm(m_simulator->getEngine()->getRedline());
const double maxRpm = std::floor(redline / 500.0) * 500.0;
m_dynoSpeedGauge->m_gauge->m_max = (int)(maxRpm);
m_dynoSpeedGauge->m_gauge->setBandCount(1);
m_dynoSpeedGauge->m_gauge->setBand(
{ m_app->getRed(), (float)redline, (float)maxRpm, 3.0f, 6.0f, shortenAngle, -shortenAngle }, 0);
const Bounds torqueBounds = grid.get(m_bounds, 1, 1);
m_torqueGauge->m_gauge->m_value = m_simulator->m_dyno.m_enabled
? (float)m_filteredTorque
: (float)m_peakTorque;
m_torqueGauge->m_bounds = torqueBounds;
const Bounds horsepowerBounds = grid.get(m_bounds, 2, 1);
m_hpGauge->m_gauge->m_value = m_simulator->m_dyno.m_enabled
? (float)m_filteredHorsepower
: (float)m_peakHorsepower;
m_hpGauge->m_bounds = horsepowerBounds;
UiElement::render();
}
void LoadSimulationCluster::drawCurrentGear(const Bounds &bounds) {
const Bounds insetBounds = bounds.inset(10.0f);
const Bounds title = insetBounds.verticalSplit(0.9f, 1.0f);
const Bounds body = insetBounds.verticalSplit(0.0f, 0.9f);
drawFrame(bounds, 1.0f, m_app->getWhite(), m_app->getBackgroundColor());
drawCenteredText("Gear", title.inset(10.0f), 24.0f);
const int gear = m_simulator->getTransmission()->getGear();
std::stringstream ss;
if (gear != -1) ss << (gear + 1);
else ss << "N";
drawCenteredText(ss.str(), body, 64.0f, Bounds::center);
}
void LoadSimulationCluster::drawClutchPressureGauge(const Bounds &bounds) {
m_clutchPressureGauge->m_bounds = bounds;
m_clutchPressureGauge->m_gauge->m_value =
(float)m_simulator->getTransmission()->getClutchPressure() * 100.0f;
}
void LoadSimulationCluster::drawSystemStatus(const Bounds &bounds) {
const Bounds left = bounds.horizontalSplit(0.0f, 0.6f);
const Bounds right = bounds.horizontalSplit(0.6f, 1.0f);
drawFrame(bounds, 1.0f, m_app->getWhite(), m_app->getBackgroundColor());
Grid grid;
grid.v_cells = 4;
grid.h_cells = 1;
drawText(
"Ignition",
grid.get(left, 0, 0).inset(10.0f),
20.0,
Bounds::lm);
drawText(
"Starter",
grid.get(left, 0, 1).inset(10.0f),
20.0,
Bounds::lm);
drawText(
"Dyno.",
grid.get(left, 0, 2).inset(10.0f),
20.0,
Bounds::lm);
drawText(
"Hold",
grid.get(left, 0, 3).inset(10.0f),
20.0,
Bounds::lm);
for (int i = 0; i < 4; ++i) {
const Bounds rawBounds = grid.get(right, 0, i);
const float width = std::fmax(rawBounds.width(), rawBounds.height());
const Bounds squareBounds(width - 20.0f, 5.0f, rawBounds.getPosition(Bounds::center), Bounds::center);
drawFrame(
squareBounds,
1.0f,
mix(m_app->getBackgroundColor(), m_app->getWhite(), 0.001f),
mix(m_app->getBackgroundColor(), m_app->getRed(), m_systemStatusLights[i])
);
}
}
void LoadSimulationCluster::updateHpAndTorque(float dt) {
constexpr double RC = 1.0;
const double alpha = dt / (dt + RC);
const double torque = units::convert(m_simulator->getFilteredDynoTorque(), units::ft_lb);
const double hp = torque * m_simulator->getEngine()->getRpm() / 5252.0;
m_filteredTorque = (1 - alpha) * m_filteredTorque + alpha * torque;
m_filteredHorsepower = (1 - alpha) * m_filteredHorsepower + alpha * hp;
if (m_filteredTorque > m_peakTorque) {
m_peakTorque = m_filteredTorque;
m_peakTorqueRpm = m_simulator->getEngine()->getRpm();
}
if (m_filteredHorsepower > m_peakHorsepower) {
m_peakHorsepower = std::fmax(m_peakHorsepower, m_filteredHorsepower);
m_peakHorsepowerRpm = m_simulator->getEngine()->getRpm();
}
}