Skip to content
This repository was archived by the owner on Dec 29, 2024. It is now read-only.

Commit 97f0c34

Browse files
committed
Fixing decoder objects; "generic" tested and working
I fixed some errors in the ESU Object and I overhauled the generic fallback object so that it works. All decoders should work at least in a limited form now.
1 parent bcded9d commit 97f0c34

File tree

2 files changed

+118
-82
lines changed

2 files changed

+118
-82
lines changed

cfg/bundles.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ bundles.locomotives = {
113113
train.all[trainPosition].prototype.realtime.amps = (currentTE / maxTE) * maxAmps;
114114
}
115115
},
116+
117+
wheelSlip:{//this contains all the wheel slip functions and calculations. ultimately the TE calculator will draw from this, and so will the brakes (no braking effort if wheels are slipping)
118+
internalForces:0,
119+
externalForces:0,
120+
calcInternalForces: function(trainPosition){
121+
//add up TE and locomotive brakes
122+
train.all[trainPosition].prototype.wheelSlip.internalForces = train.all[trainPosition].prototype.realtime.te + train.all[trainPosition].prototype.brake.brakingForce;
123+
return train.all[trainPosition].prototype.wheelSlip.internalForces;
124+
},
125+
calcExternalForces: function(trainPosition){
126+
//this is a shorter, more efficient way to add up all the net forces of every element in the train EXCEPT this one
127+
var extForces = Math.abs(train.total.netForce - train.all[trainPosition].prototype.realtime.netForce);
128+
train.all[trainPosition].prototype.wheelSlip.externalForces = extForces;
129+
return extForces;
130+
}
131+
},
116132

117133
air: //holds static and realtime data about pneumatics
118134
{
@@ -342,8 +358,7 @@ bundles.rollingstock = {
342358
releaseRate: 0.001, //psi per millisecond rate of release. Should be tiny, but larger than the charge rate
343359
},
344360
coeff: {
345-
rollingResistance: 0.005, //rolling resistance
346-
genResistance: 0, //arbitrary other resistance value that is left to account for friction bearings/roller bearings etc.
361+
rollingResistance: 0.009, //rolling resistance
347362
},
348363
tmp: { //junk for intervals and such to use as storage
349364
}

cfg/decoders.js

Lines changed: 101 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,56 @@ foo, WebSocket, $, Materialize, console, cfg, train, jmri, ui, air, sim
1414

1515
var decoders = {
1616
//product "LokSound Select"
17-
"ESU LokSound Blardy Blar Select": {
17+
"ESU LokSound Select": {
1818
//sound project "emd567"
19-
"LokSound Select Blardy Blar EMD 567" : function (address, trainPosition) {
19+
"LokSound Select EMD 567": function(address, trainPosition) {
2020
//ESU LokSound Select V4
2121
//decoder object for ESU official EMD 567 Sound project
2222
//By Hampton Morgan - k4kfh@github - Originally written in May 2015
2323
//evilgeniustech.com
24-
'use strict';
24+
console.debug("DECODER: Using 'LokSound Select EMD 567' for " + trainPosition)
2525
train.all[trainPosition].throttle = new jmri.throttle(address, jmri.throttleName.generate()); //we use the train position as the throttle name for future lookup purposes
2626

2727
//FUNCTIONS
2828
this.f = {};
2929
//light
3030
this.f.headlight = {};
31-
this.f.headlight.set = function (state) {
32-
train.all[trainPosition].throttle.f.set({"F0": state});
31+
this.f.headlight.set = function(state) {
32+
train.all[trainPosition].throttle.f.set({
33+
"F0": state
34+
});
3335
train.all[trainPosition].dcc.f.headlight.state = state;
3436
console.log("DCC: Setting headlight to " + state + " on Train#" + trainPosition)
3537
};
3638
//bell
3739
this.f.bell = {};
38-
this.f.bell.set = function (state) {
39-
train.all[trainPosition].throttle.f.set({"F1": state});
40-
train.all[trainPosition].dcc.f.bell.state = state;
41-
console.log("DCC: Setting bell to " + state + " on Train#" + trainPosition)
40+
this.f.bell.set = function(state) {
41+
train.all[trainPosition].throttle.f.set({
42+
"F1": state
43+
});
44+
train.all[trainPosition].dcc.f.bell.state = state;
45+
console.log("DCC: Setting bell to " + state + " on Train#" + trainPosition)
4246
};
4347
this.f.bell.state = false;
4448

4549
//horn
4650
this.f.horn = {};
47-
this.f.horn.set = function (state) {
48-
train.all[trainPosition].throttle.f.set({"F2": state});
49-
train.all[trainPosition].dcc.f.horn.state = state;
50-
console.log("DCC: Setting headlight to " + state + " on Train#" + trainPosition)
51+
this.f.horn.set = function(state) {
52+
train.all[trainPosition].throttle.f.set({
53+
"F2": state
54+
});
55+
train.all[trainPosition].dcc.f.horn.state = state;
56+
console.log("DCC: Setting headlight to " + state + " on Train#" + trainPosition)
5157
};
5258
this.f.horn.state = false;
5359

5460
//compressor
5561
this.f.compressor = {};
56-
this.f.compressor.set = function (state) {
62+
this.f.compressor.set = function(state) {
5763
if (state != train.all[trainPosition].dcc.f.compressor.state) {
58-
train.all[trainPosition].throttle.f.set({"F20":state});
64+
train.all[trainPosition].throttle.f.set({
65+
"F20": state
66+
});
5967
train.all[trainPosition].dcc.f.compressor.state = state;
6068
console.log("DCC: Setting compressor to " + state + " on Train#" + trainPosition)
6169
}
@@ -64,37 +72,41 @@ var decoders = {
6472

6573
//air release
6674
this.f.airDump = {};
67-
this.f.airDump.set = function (state) {
68-
train.all[trainPosition].throttle.f.set({"F19":state});
69-
console.log("DCC: Setting airDump to " + state + " on Train#" + trainPosition)
75+
this.f.airDump.set = function(state) {
76+
train.all[trainPosition].throttle.f.set({
77+
"F19": state
78+
});
79+
console.log("DCC: Setting airDump to " + state + " on Train#" + trainPosition)
7080
};
7181

7282
//dyn brake fans
7383
this.f.dynBrakes = {};
74-
this.f.dynBrakes.set = function (state) {
84+
this.f.dynBrakes.set = function(state) {
7585

7686
};
7787
this.f.dynBrakes.state = false;
7888

7989
//engine on/off
8090
this.f.engine = {};
81-
this.f.engine.set = function (state) {
91+
this.f.engine.set = function(state) {
8292
//This IF makes the entire function useless if you're out of fuel, or if the state argument is no different than the current actual state
8393
if (state !== train.all[trainPosition].dcc.f.engine.state) {
84-
train.all[trainPosition].throttle.f.set({"F8":state});
94+
train.all[trainPosition].throttle.f.set({
95+
"F8": state
96+
});
8597
train.all[trainPosition].dcc.f.engine.state = state;
8698
console.log("DCC: Setting engine to " + state + " on Train#" + trainPosition)
87-
//This code sets engineRunning to 0 or 1 depending on the state
99+
//This code sets engineRunning to 0 or 1 depending on the state
88100
if (state === true) {
89101
train.all[trainPosition].prototype.engineRunning = 1;
90-
}
91-
else if (state === false) {
102+
} else if (state === false) {
92103
train.all[trainPosition].prototype.engineRunning = 0;
93104
}
94-
}
95-
else {
105+
} else {
96106
//This code means that if you're out of fuel, regardless of what state you fed into this function it will turn the engine off.
97-
train.all[trainPosition].throttle.f.set({"F8":false});
107+
train.all[trainPosition].throttle.f.set({
108+
"F8": false
109+
});
98110
train.all[trainPosition].prototype.engineRunning = 0;
99111
train.all[trainPosition].dcc.f.engine.state = false;
100112
}
@@ -103,30 +115,42 @@ var decoders = {
103115

104116
//notch sound stuff.
105117
this.f.notch = {
106-
up: function () {
118+
up: function() {
107119
//Notch up code
108120
//This is inside an IF statement to make sure we don't try to notch OVER 8. If that happens, ESU decoders get confused.
109121
var newNotch = (train.all[trainPosition].dcc.f.notch.state + 1);
110122
if (newNotch <= 8) {
111123
train.all[trainPosition].dcc.f.notch.state++; //THIS HAS TO RUN INSTANTLY OR SIM.JS IS STUPID
112124
console.log("DCC: Increasing notch on Train#" + trainPosition)
113-
setTimeout(function () {
114-
train.all[trainPosition].throttle.f.set({"F9":true});
125+
setTimeout(function() {
126+
train.all[trainPosition].throttle.f.set({
127+
"F9": true
128+
});
115129
}, 500);
116-
setTimeout(function () {
117-
train.all[trainPosition].throttle.f.set({"F9":false});
130+
setTimeout(function() {
131+
train.all[trainPosition].throttle.f.set({
132+
"F9": false
133+
});
118134
}, 1750);
119135
}
120136
},
121-
down: function () {
137+
down: function() {
122138
//Notch down code
123139
//This is inside an IF statement to make sure we don't try to notch LESS THAN idle. If that happens, ESU decoders get confused.
124140
var newNotch = (train.all[trainPosition].dcc.f.notch.state - 1);
125141
if (newNotch >= 0) {
126142
train.all[trainPosition].dcc.f.notch.state--; //THIS MUST RUN INSTANTLY OR SIM.JS DOES WEIRD STUFF
127143
console.log("DCC: Decreasing notch on Train#" + trainPosition)
128-
setTimeout(function () { train.all[trainPosition].throttle.f.set({"F10":true});}, 500);
129-
setTimeout(function () { train.all[trainPosition].throttle.f.set({"F10":false});}, 1750);
144+
setTimeout(function() {
145+
train.all[trainPosition].throttle.f.set({
146+
"F10": true
147+
});
148+
}, 500);
149+
setTimeout(function() {
150+
train.all[trainPosition].throttle.f.set({
151+
"F10": false
152+
});
153+
}, 1750);
130154
}
131155
},
132156
state: 0 //This should reflect the current notching state of the sound decoder. You should increment this up or down 1 when your up() and down() functions finish, or sim.js's functions will be horribly confused and mess up your sounds.
@@ -136,20 +160,20 @@ var decoders = {
136160
//SPEED SETTING
137161
this.speed = {};
138162
this.speed.state = 0;
139-
this.speed.set = function (speed) {
163+
this.speed.set = function(speed) {
140164
train.all[trainPosition].throttle.speed.set(speed);
141165
train.all[trainPosition].dcc.speed.state = speed;
142166
};
143-
this.speed.setMPH = function (mph) {
167+
this.speed.setMPH = function(mph) {
144168
var speed = train.all[trainPosition].model.speed(mph);
145169
train.all[trainPosition].dcc.speed.set(speed);
146170
};
147171
}
148172
},
149173

150174
//GENERIC FALLBACK SCRIPT - DO NOT REMOVE!!
151-
"generic":{
152-
"generic" : function (address, trainPosition) {
175+
"generic": {
176+
"generic": function(address, trainPosition) {
153177
'use strict';
154178
//GENERIC FALLBACK
155179
train.all[trainPosition].throttle = new jmri.throttle(address, jmri.throttleName.generate());
@@ -159,104 +183,101 @@ var decoders = {
159183

160184
//light
161185
this.f.headlight = {};
162-
this.f.headlight.set = function (state) {
163-
train.all[trainPosition].throttle.f.set({"F0":state});
186+
this.f.headlight.set = function(state) {
187+
train.all[trainPosition].throttle.f.set({
188+
"F0": state
189+
});
164190
train.all[trainPosition].dcc.f.headlight.state = state;
165191
};
166192
this.f.headlight.state = false;
167193

168194
//bell
169195
this.f.bell = {};
170-
this.f.bell.set = function (state) {
171-
train.all[trainPosition].throttle.f.set({"F1":state});
196+
this.f.bell.set = function(state) {
197+
train.all[trainPosition].throttle.f.set({
198+
"F1": state
199+
});
172200
train.all[trainPosition].dcc.f.bell.state = state;
173201
};
174202
this.f.bell.state = false;
175203

176204
//horn
177205
this.f.horn = {};
178-
this.f.horn.set = function (state) {
179-
train.all[trainPosition].throttle.f.set({"F2":state});
206+
this.f.horn.set = function(state) {
207+
train.all[trainPosition].throttle.f.set({
208+
"F2": state
209+
});
180210
train.all[trainPosition].dcc.f.horn.state = state;
181211
};
182212
this.f.horn.state = false;
183213

184214
//compressor
185215
this.f.compressor = {};
186-
this.f.compressor.set = function (state) {
216+
this.f.compressor.set = function(state) {
187217
//there's no compressor assumed on these generic mystery decoders
188218
train.all[trainPosition].dcc.f.compressor.state = state;
189219
};
190220
this.f.compressor.state = false;
191221

192222
//air release
193-
this.f.airdump = {};
194-
this.f.airdump.set = function (state) {
223+
this.f.airDump = {};
224+
this.f.airDump.set = function(state) {
195225

196226
};
197-
this.f.airdump.state = false;
227+
this.f.airDump.state = false;
198228

199229
//dyn brake fans
200-
this.f.dynbrakes = {};
201-
this.f.dynbrakes.set = function (state) {
230+
this.f.dynBrakes = {};
231+
this.f.dynBrakes.set = function(state) {
202232

203233
};
204-
this.f.dynbrakes.state = false;
234+
this.f.dynBrakes.state = false;
205235

206236
//engine on/off
207237
this.f.engine = {};
208-
this.f.engine.set = function (state) {
238+
this.f.engine.set = function(state) {
209239
//This function is almost exactly the same as the one in my ESU LokSound EMD 567 decoder constructor, the difference is this one never actually sends a DCC command (it's basically dummy function that the physics engine thinks is legit)
210240

211-
//This IF makes the entire function useless if you're out of fuel.
212-
if (train.all[trainPosition].prototype.realtime.fuel.status !== 0){
213-
train.all[trainPosition].dcc.f.engine.state = state;
214-
//This code sets engineRunning to 0 or 1 depending on the state
215-
if (state === true) {
216-
train.all[trainPosition].prototype.engineRunning = 1;
217-
}
218-
else if (state === false) {
219-
train.all[trainPosition].prototype.engineRunning = 0;
220-
}
221-
}
222-
else {
223-
//This code means that if you're out of fuel, regardless of what state you fed into this function it will turn the engine off.
241+
train.all[trainPosition].dcc.f.engine.state = state;
242+
//This code sets engineRunning to 0 or 1 depending on the state
243+
if (state === true) {
244+
train.all[trainPosition].prototype.engineRunning = 1;
245+
} else if (state === false) {
224246
train.all[trainPosition].prototype.engineRunning = 0;
225-
train.all[trainPosition].dcc.f.engine.state = false;
226247
}
227248
};
228249
this.f.engine.state = false;
229250

230251
//notch sound stuff.
231252
this.f.notch = {
232-
up : function () {
253+
up: function() {
233254
//Notch up code
234255
//This is inside an IF statement to make sure we don't try to notch OVER 8.
235256
var newNotch = (train.all[trainPosition].dcc.f.notch.state + 1);
236257
if (newNotch <= 8) {
237-
train.all[trainPosition].dcc.f.notch.state++; //THIS HAS TO RUN INSTANTLY OR SIM.JS IS STUPID
258+
train.all[trainPosition].dcc.f.notch.state++; //THIS HAS TO RUN INSTANTLY OR SIM.JS IS STUPID
238259
}
239260
},
240-
down : function () {
261+
down: function() {
241262
//Notch down code
242263
//This is inside an IF statement to make sure we don't try to notch LESS THAN idle.
243264
var newNotch = (train.all[trainPosition].dcc.f.notch.state - 1);
244265
if (newNotch >= 0) {
245266
train.all[trainPosition].dcc.f.notch.state--; //THIS MUST RUN INSTANTLY OR SIM.JS DOES WEIRD STUFF
246267
}
247268
},
248-
state : 0 //This should reflect the current notching state of the sound decoder. You should increment this up or down 1 when your up() and down() functions finish, or sim.js's functions will be horribly confused and mess up your sounds.
269+
state: 0 //This should reflect the current notching state of the sound decoder. You should increment this up or down 1 when your up() and down() functions finish, or sim.js's functions will be horribly confused and mess up your sounds.
249270
};
250271

251272

252273
//SPEED SETTING
253274
this.speed = {};
254275
this.speed.state = 0;
255-
this.speed.set = function (speed) {
276+
this.speed.set = function(speed) {
256277
train.all[trainPosition].throttle.speed.set(speed);
257278
train.all[trainPosition].dcc.speed.state = speed;
258279
};
259-
this.speed.setMPH = function (mph) {
280+
this.speed.setMPH = function(mph) {
260281
var speed = train.all[trainPosition].model.speed(mph);
261282
train.all[trainPosition].dcc.speed.set(speed);
262283
};
@@ -265,14 +286,14 @@ var decoders = {
265286
//SPEED SETTING
266287
this.speed = {};
267288
this.speed.state = 0;
268-
this.speed.set = function (speed) {
269-
train[trainPosition].throttle.speed.set(speed);
270-
train[trainPosition].dcc.speed.state = speed;
289+
this.speed.set = function(speed) {
290+
train.all[trainPosition].throttle.speed.set(speed);
291+
train.all[trainPosition].dcc.speed.state = speed;
271292
};
272-
this.speed.setMPH = function (mph) {
273-
var speed = train[trainPosition].model.speed(mph);
274-
train[trainPosition].dcc.speed.set(speed);
293+
this.speed.setMPH = function(mph) {
294+
var speed = train.all[trainPosition].model.speed(mph);
295+
train.all[trainPosition].dcc.speed.set(speed);
275296
};
276297
}
277298
}
278-
};
299+
};

0 commit comments

Comments
 (0)