Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 133 additions & 122 deletions src/mathchannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@ class MathChannels {

#getDefinitions() {
return [
{
id: 'est_power_kgh',
name: 'Estimated Power (HP) [Source: kg/h]',
description:
'Converts kg/h to g/s, then estimates HP. (MAF / 3.6 * Factor)',
inputs: [
{ name: 'maf', label: 'Air Mass Flow (kg/h)' },
{
name: 'factor',
label: 'Factor (Diesel ~1.35, Petrol ~1.25)',
isConstant: true,
defaultValue: 1.35,
},
],
formula: (values) => (values[0] / 3.6) * values[1],
},
{
id: 'est_power_gs',
name: 'Estimated Power (HP) [Source: g/s]',
description: 'Direct g/s calculation. (MAF * Factor)',
inputs: [
{ name: 'maf', label: 'Air Mass Flow (g/s)' },
{
name: 'factor',
label: 'Factor (Diesel ~1.35, Petrol ~1.25)',
isConstant: true,
defaultValue: 1.35,
},
],
formula: (values) => values[0] * values[1],
},
{
id: 'power_from_torque',
name: 'Calculated Power (HP) [Source: Torque]',
description:
'Calculates HP from Torque (Nm) and RPM. (Torque * RPM / 7127)',
inputs: [
{ name: 'torque', label: 'Torque (Nm)' },
{ name: 'rpm', label: 'Engine RPM' },
],
formula: (values) => (values[0] * values[1]) / 7127,
},
{
id: 'acceleration',
name: 'Acceleration (m/s²) [0-100 Logic]',
Expand All @@ -37,13 +79,9 @@ class MathChannels {
if (dt <= 0) continue;

const dv = (p2.y - p1.y) / 3.6;

const accel = dv / dt;

result.push({
x: p2.x,
y: accel,
});
result.push({ x: p2.x, y: accel });
}
return result;
},
Expand Down Expand Up @@ -76,56 +114,11 @@ class MathChannels {
}
}

smoothed.push({
x: sourceData[i].x,
y: sum / count,
});
smoothed.push({ x: sourceData[i].x, y: sum / count });
}
return smoothed;
},
},
{
id: 'est_power_kgh',
name: 'Estimated Power (HP) [Source: kg/h]',
description:
'Converts kg/h to g/s, then estimates HP. (MAF / 3.6 * Factor)',
inputs: [
{ name: 'maf', label: 'Air Mass Flow (kg/h)' },
{
name: 'factor',
label: 'Factor (Diesel ~1.35, Petrol ~1.25)',
isConstant: true,
defaultValue: 1.35,
},
],
formula: (values) => (values[0] / 3.6) * values[1],
},
{
id: 'est_power_gs',
name: 'Estimated Power (HP) [Source: g/s]',
description: 'Direct g/s calculation. (MAF * Factor)',
inputs: [
{ name: 'maf', label: 'Air Mass Flow (g/s)' },
{
name: 'factor',
label: 'Factor (Diesel ~1.35, Petrol ~1.25)',
isConstant: true,
defaultValue: 1.35,
},
],
formula: (values) => values[0] * values[1],
},
{
id: 'power_from_torque',
name: 'Calculated Power (HP) [Source: Torque]',
description:
'Calculates HP from Torque (Nm) and RPM. (Torque * RPM / 7127)',
inputs: [
{ name: 'torque', label: 'Torque (Nm)' },
{ name: 'rpm', label: 'Engine RPM' },
],
formula: (values) => (values[0] * values[1]) / 7127,
},
{
id: 'boost',
name: 'Boost Pressure (Bar)',
Expand Down Expand Up @@ -214,7 +207,6 @@ class MathChannels {
throw new Error(`Signal '${signalName}' not found in file.`);

sourceSignals.push({ isConstant: false, data: signalData });

if (!masterTimeBase) masterTimeBase = signalData;
}
});
Expand All @@ -223,9 +215,6 @@ class MathChannels {
throw new Error('At least one input must be a signal.');

const resultData = [];
let min = Infinity;
let max = -Infinity;

for (let i = 0; i < masterTimeBase.length; i++) {
const currentTime = masterTimeBase[i].x;
const currentValues = [];
Expand All @@ -240,37 +229,17 @@ class MathChannels {
});

const calculatedY = definition.formula(currentValues);

if (calculatedY < min) min = calculatedY;
if (calculatedY > max) max = calculatedY;

resultData.push({
x: currentTime,
y: calculatedY,
});
}

const finalName = newChannelName || `Math: ${definition.name}`;

file.signals[finalName] = resultData;

if (!file.metadata) file.metadata = {};
file.metadata[finalName] = {
min: min,
max: max,
unit: 'Math',
};

if (!file.availableSignals.includes(finalName)) {
file.availableSignals.push(finalName);
file.availableSignals.sort();
resultData.push({ x: currentTime, y: calculatedY });
}

return finalName;
return this.#finalizeChannel(
file,
resultData,
newChannelName || `Math: ${definition.name}`
);
}

#executeCustomProcess(file, definition, inputMapping, newChannelName) {
// Extract inputs strictly for custom processor
const signals = [];
const constants = [];

Expand All @@ -288,14 +257,9 @@ class MathChannels {
}
});

// For smoothing, we assume 1 signal input.
// Generalizing this would require more complex mapping, but for now:
if (signals.length === 0)
throw new Error('Custom process requires at least one signal.');

// Run the custom processor logic defined in #getDefinitions
const resultData = definition.customProcess(signals[0], constants);

return this.#finalizeChannel(
file,
resultData,
Expand Down Expand Up @@ -325,52 +289,42 @@ class MathChannels {
file.availableSignals.push(finalName);
file.availableSignals.sort();
}

return finalName;
}

#interpolate(data, targetTime) {
if (!data || data.length === 0) return 0;

if (targetTime <= data[0].x) return parseFloat(data[0].y);
if (targetTime >= data[data.length - 1].x)
return parseFloat(data[data.length - 1].y);

let left = 0;
let right = data.length - 1;

while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (data[mid].x < targetTime) {
left = mid + 1;
} else {
right = mid - 1;
}
if (data[mid].x < targetTime) left = mid + 1;
else right = mid - 1;
}

const p1 = data[left - 1];
const p2 = data[left];

if (!p1) return parseFloat(p2.y);
if (!p2) return parseFloat(p1.y);

const t1 = p1.x;
const t2 = p2.x;
const y1 = parseFloat(p1.y);
const y2 = parseFloat(p2.y);

if (t2 === t1) return y1;

const fraction = (targetTime - t1) / (t2 - t1);
return y1 + (y2 - y1) * fraction;
return y1 + (y2 - y1) * ((targetTime - t1) / (t2 - t1));
}

#openModal() {
if (AppState.files.length === 0) {
alert('Please load a log file first.');
return;
}

const modal = document.getElementById('mathModal');
if (modal) modal.style.display = 'flex';

Expand Down Expand Up @@ -407,37 +361,97 @@ class MathChannels {
return;
}
const file = AppState.files[0];
const signalOptions = file.availableSignals
.map((s) => `<option value="${s}">${s}</option>`)
.join('');

definition.inputs.forEach((input, idx) => {
const wrapper = document.createElement('div');
wrapper.style.marginBottom = '10px';
wrapper.style.marginBottom = '15px';

wrapper.innerHTML = `<label style="font-size:0.85em; font-weight:bold; display:block; margin-bottom:4px;">${input.label}</label>`;

let inputHtml = '';
if (input.isConstant) {
inputHtml = `<input type="number" id="math-input-${idx}" value="${input.defaultValue}" class="template-select" style="width:100%">`;
const inputEl = document.createElement('input');
inputEl.type = 'number';
inputEl.id = `math-input-${idx}`;
inputEl.value = input.defaultValue;
inputEl.className = 'template-select';
inputEl.style.width = '100%';
wrapper.appendChild(inputEl);
} else {
inputHtml = `<select id="math-input-${idx}" class="template-select" style="width:100%">${signalOptions}</select>`;
const searchableSelect = this.#createSearchableSelect(
idx,
file.availableSignals,
input.name
);
wrapper.appendChild(searchableSelect);
}

wrapper.innerHTML = `
<label style="font-size:0.85em; font-weight:bold; display:block; margin-bottom:4px;">${input.label}</label>
${inputHtml}
`;
container.appendChild(wrapper);
});
}

if (!input.isConstant) {
const sel = wrapper.querySelector('select');
for (let opt of sel.options) {
if (opt.value.toLowerCase().includes(input.name.toLowerCase())) {
sel.value = opt.value;
break;
}
}
#createSearchableSelect(idx, signals, inputFilterName) {
const wrapper = document.createElement('div');
wrapper.className = 'searchable-select-wrapper';

const input = document.createElement('input');
input.type = 'text';
input.id = `math-input-${idx}`;
input.className = 'searchable-input';
input.placeholder = 'Search or Select Signal...';
input.autocomplete = 'off';

const resultsList = document.createElement('div');
resultsList.className = 'search-results-list';

const defaultSignal = signals.find((s) =>
s.toLowerCase().includes(inputFilterName.toLowerCase())
);
if (defaultSignal) input.value = defaultSignal;

const renderOptions = (filterText = '') => {
resultsList.innerHTML = '';
const lowerFilter = filterText.toLowerCase();

const filtered = signals.filter((s) =>
s.toLowerCase().includes(lowerFilter)
);

if (filtered.length === 0) {
resultsList.innerHTML =
'<div class="search-option" style="color:#999; cursor:default;">No signals found</div>';
} else {
filtered.forEach((sig) => {
const opt = document.createElement('div');
opt.className = 'search-option';
opt.textContent = sig;
opt.onclick = () => {
input.value = sig;
resultsList.style.display = 'none';
};
resultsList.appendChild(opt);
});
}
};

input.addEventListener('focus', () => {
renderOptions(input.value);
resultsList.style.display = 'block';
});

input.addEventListener('input', (e) => {
renderOptions(e.target.value);
resultsList.style.display = 'block';
});

document.addEventListener('click', (e) => {
if (!wrapper.contains(e.target)) {
resultsList.style.display = 'none';
}
});

wrapper.appendChild(input);
wrapper.appendChild(resultsList);

return wrapper;
}

#executeCreation() {
Expand All @@ -464,12 +478,9 @@ class MathChannels {
inputMapping,
newName
);

this.#closeModal();

if (typeof UI.renderSignalList === 'function') {
UI.renderSignalList();

setTimeout(() => {
const checkbox = document.querySelector(
`input[data-key="${createdName}"]`
Expand Down
Loading
Loading