-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.js
More file actions
201 lines (170 loc) · 6.76 KB
/
simulator.js
File metadata and controls
201 lines (170 loc) · 6.76 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
// simulator.js
import OscilloscopeDisplay from './display.js';
import AudioPlayer from './audioPlayer.js';
import SoundGenerator from './generator.js';
import VerticalLinearBar from './VerticalLinearBar.js';
class TrainSimulator {
constructor(speedDisplay, canvas, ctx) {
this.speedDisplay = speedDisplay;
this.canvas = canvas;
this.ctx = ctx;
this.trainSpeed = 0; // Start at 0 km/h
this.powerLevel = 0;
this.brakingLevel = 0;
this.spwmConfig = [];
this.maxAcceleration = 2; // Default max acceleration
this.maxSpeed = 400; // Updated max speed
// Audio Sample Config
this.sampleRate = 44100; // Default sample rate
this.bufferSize = 2048;
this.audioPlayer = new AudioPlayer(this.sampleRate, this.bufferSize);
this.oscilloscope = new OscilloscopeDisplay(canvas, ctx);
this.startTime = performance.now(); // Initialize start time
this.soundGeneratorForAudio = null; // Separate instance for audio
this.soundGeneratorForOscilloscope = null; // Separate instance for oscilloscope
// System time variables
this.lastUpdateTime = performance.now();
// Speedometer and Tractive Effort Bar
this.speedometer = null;
this.tractiveEffortBar = null;
}
async loadConfig(configPath) {
// Reset params
this.trainSpeed = 0; // Start at 0 km/h
this.powerLevel = 0;
this.brakingLevel = 0;
// Add a cache-busting query parameter
const cacheBust = `?t=${new Date().getTime()}`;
const response = await fetch(configPath + cacheBust);
const config = await response.json();
// this.spwmConfig = config.speedRanges;
this.maxAcceleration = parseFloat(config.maxAcceleration_kmh_s);
this.maxSpeed = config.maxSpeed_kmh;
this.soundGeneratorForAudio = new SoundGenerator(config, this.sampleRate);
this.soundGeneratorForOscilloscope = new SoundGenerator(config, this.sampleRate);
// Set the sound generator for audio
this.audioPlayer.setGenerator({
generateSample: () => {
return this.soundGeneratorForAudio.generateSample(this.trainSpeed, this.sampleRate).soundSample;
}
});
// Initialize or update the speedometer and tractive effort bar
this.initializeBars();
}
initializeBars() {
const speedometerCanvas = document.getElementById('speedometer');
const tractiveEffortCanvas = document.getElementById('tractiveEffort');
const speedometerOptions = {
verticalOffset: 75,
width: speedometerCanvas.width / 2.5,
height: speedometerCanvas.height - 75,
marginTop: 20,
maxValue: this.maxSpeed, // Use the loaded max speed
unit: 'km/h',
color: '#22aaff',
centered: false,
positiveOnly: true
};
const tractiveEffortOptions = {
verticalOffset: 75,
width: tractiveEffortCanvas.width / 2.5,
height: tractiveEffortCanvas.height - 75,
marginTop: 20,
maxValue: 100, // Example max tractive effort in kN
unit: '%TE',
color: '#22aaff',
centered: true,
positiveOnly: false
};
const speedometerConfig = {
graduationStep: this.maxSpeed / 10 // Adjust based on max speed
};
const tractiveEffortConfig = {
graduationStep: 25 // Example graduation step for tractive effort
};
this.speedometer = new VerticalLinearBar(speedometerCanvas, speedometerOptions, speedometerConfig);
this.tractiveEffortBar = new VerticalLinearBar(tractiveEffortCanvas, tractiveEffortOptions, tractiveEffortConfig);
}
updateSpeed() {
const currentTime = performance.now();
const deltaTime = (currentTime - this.lastUpdateTime) / 1000; // Convert to seconds
this.lastUpdateTime = currentTime;
if (this.powerLevel > 0) {
const acceleration = this.powerLevel * this.maxAcceleration;
this.trainSpeed += acceleration * deltaTime;
} else if (this.brakingLevel > 0) {
const deceleration = this.brakingLevel * this.maxAcceleration;
this.trainSpeed -= deceleration * deltaTime;
}
// Ensure speed is within 0 to maxSpeed
this.trainSpeed = Math.max(0, Math.min(this.trainSpeed, this.maxSpeed));
}
update() {
this.updateSpeed();
const bufferSize = this.oscilloscope.getWidth();
let soundData = new Float32Array(bufferSize);
let commandData = new Float32Array(bufferSize);
let carrierData = new Float32Array(bufferSize);
for (let i = 0; i < bufferSize; i++) {
const sample = this.soundGeneratorForOscilloscope.generateSample(this.trainSpeed, this.sampleRate);
soundData[i] = sample.soundSample;
commandData[i] = sample.commandSample;
carrierData[i] = sample.carrierSample;
}
this.soundGeneratorForOscilloscope.globalPhases = [0, 0]; // reset every frame to keep the oscope generation static
this.oscilloscope.drawOscilloscope([
{
data: commandData,
label: "Command Signal",
yMin: -1.1,
yMax: 1.1,
numXTicks: 25
},
{
data: soundData,
label: "Inverter Output",
yMin: -1.1,
yMax: 1.1,
numXTicks: 25
},
{
data: carrierData,
label: "Carrier Signal",
yMin: -0.1,
yMax: 1.1,
numXTicks: 25
}
], this.sampleRate);
// Draw the speedometer and tractive effort bar
this.speedometer.draw(this.trainSpeed);
this.tractiveEffortBar.draw(this.calculateTractiveEffort());
requestAnimationFrame(() => this.update());
}
calculateTractiveEffort() {
// Example calculation for tractive effort
// This should be replaced with the actual logic to calculate tractive effort based on train's state
if (this.brakingLevel > 0) {
return -this.brakingLevel * 20;
}
else if (this.powerLevel > 0) {
return this.powerLevel * (100/3);
}
return 0.;
}
setPower(level) {
this.powerLevel = level;
this.brakingLevel = 0;
}
setNeutral() {
this.powerLevel = 0;
this.brakingLevel = 0;
}
setBraking(level) {
this.brakingLevel = level;
this.powerLevel = 0;
}
playAudio() {
this.audioPlayer.play();
}
}
export default TrainSimulator;