feat: 开源母线电容计算器并接入独立自动部署
整理 Buck 与 PMSM 完整源码、离线构建、校核测试和使用说明,采用 MIT 许可。 增加页首开源链接,支持推送 main 后自动测试、构建并发布到原计算器网址。
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
'use strict';
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const vm = require('node:vm');
|
||||
const engine = require('../src/motor-bus-engine.js');
|
||||
const { normalize, electricalLimit, snapFrequency, caseSpectrum, exactSpectrum, scan, scanAsync } = engine;
|
||||
let passed = 0;
|
||||
function test(name, run) { run(); passed++; console.log('✓ ' + name); }
|
||||
function close(actual, expected, rel = 1e-9, abs = 1e-11) {
|
||||
assert.ok(Number.isFinite(actual));
|
||||
assert.ok(Math.abs(actual - expected) <= abs + rel * Math.max(Math.abs(actual), Math.abs(expected)), `实际 ${actual},预期 ${expected}`);
|
||||
}
|
||||
function rejects(run) { assert.throws(run, error => error instanceof Error && /[\u4e00-\u9fff]/.test(error.message)); }
|
||||
const { motor } = normalize();
|
||||
const Ib = Math.SQRT2 * 13.3;
|
||||
const cache = new Map();
|
||||
function point(fe, iqPeak, extra = {}) {
|
||||
const key = JSON.stringify([fe, iqPeak, extra]);
|
||||
if (!cache.has(key)) cache.set(key, caseSpectrum(motor, { fe, iqPeak, ...extra }));
|
||||
return cache.get(key);
|
||||
}
|
||||
|
||||
test('CommonJS/browser/worker global接口一致,依赖FFT加载明确', () => {
|
||||
assert.equal(globalThis.MotorBusEngine, engine);
|
||||
assert.ok(Object.isFrozen(engine));
|
||||
const browser = { setTimeout };
|
||||
vm.runInNewContext(fs.readFileSync(require.resolve('../src/buck-calculator-engine.js'), 'utf8'), browser);
|
||||
const source = fs.readFileSync(require.resolve('../src/motor-bus-engine.js'), 'utf8');
|
||||
vm.runInNewContext(source, browser);
|
||||
assert.equal(typeof browser.MotorBusEngine.scanAsync, 'function');
|
||||
close(browser.MotorBusEngine.caseSpectrum({}, { fe: 0, iqPeak: 49.04 }).peakLine.amplitude, 18.290380128839956);
|
||||
assert.throws(() => vm.runInNewContext(source, {}), /先加载 BuckRLC/);
|
||||
});
|
||||
|
||||
test('线间R/L折半,13.3ARMS基波与49.04Apk锁转假设分开', () => {
|
||||
close(motor.R, 0.065); close(motor.L, 44.985e-6);
|
||||
close(motor.alpha, motor.R / motor.L); close(motor.fePerRpm, 14 * 9.25 / 60);
|
||||
const normalized = normalize();
|
||||
assert.equal(normalized.options.phaseRms, 13.3); assert.equal(normalized.options.stallPeak, 49.04);
|
||||
assert.ok(engine.scope.includes('不是厂家已确认')); assert.ok(engine.scope.includes('不是连续域全局证明'));
|
||||
assert.equal(normalize({}, { maxBusW: '', maxRpm: '' }).options.maxBusW, 0);
|
||||
});
|
||||
|
||||
test('i_d0不弱磁可达边界随q正负/SPWM变化,边界相电压满足解析式', () => {
|
||||
const positive = electricalLimit(motor, Ib), negative = electricalLimit(motor, -Ib);
|
||||
assert.ok(negative.rpm > positive.rpm);
|
||||
close(positive.rpm, 295.10850140227); close(negative.rpm, 349.9719493142039);
|
||||
for (const pwm of ['svpwm', 'spwm']) for (const iqPeak of [-Ib, 0, Ib]) {
|
||||
const limit = electricalLimit(motor, iqPeak, pwm);
|
||||
assert.equal(limit.available, true);
|
||||
const rpm = limit.fe / motor.fePerRpm;
|
||||
const v = Math.hypot(motor.ke * rpm + motor.R * iqPeak, 2 * Math.PI * limit.fe * motor.L * iqPeak);
|
||||
close(v, limit.voltageLimit);
|
||||
}
|
||||
assert.ok(electricalLimit(motor, Ib, 'spwm').rpm < positive.rpm);
|
||||
const step = motor.fsw / 512;
|
||||
const last = Math.floor(positive.fe / step) * step;
|
||||
assert.ok(caseSpectrum(motor, { fe: last, iqPeak: Ib }).modulationRatio <= 1);
|
||||
rejects(() => caseSpectrum(motor, { fe: last + step, iqPeak: Ib }));
|
||||
});
|
||||
|
||||
test('有理电频公共周期保持真实freq=k*f0,不静默把任意rpm改网格', () => {
|
||||
const snapped = snapFrequency(motor, 310);
|
||||
assert.equal(snapped.fe, 312.5); close(snapped.rpm, 312.5 / motor.fePerRpm);
|
||||
const c = point(312.5, Ib);
|
||||
assert.equal(c.cycles, 64); assert.equal(c.M, 64 * 128);
|
||||
close(c.duration, 1 / c.f0); close(c.f0, motor.fsw / 64);
|
||||
assert.ok(c.k instanceof Uint32Array);
|
||||
for (const name of ['freq', 're', 'im']) assert.ok(c[name] instanceof Float64Array);
|
||||
assert.equal(c.freq.length, 640); assert.equal(c.freq.at(-1), 200000);
|
||||
for (let i = 0; i < c.freq.length; i++) { assert.equal(c.k[i], i + 1); assert.equal(c.freq[i], c.k[i] * c.f0); }
|
||||
rejects(() => caseSpectrum(motor, { fe: 310, iqPeak: Ib }));
|
||||
const fromRpm = caseSpectrum(motor, { rpm: c.rpm, iqPeak: Ib });
|
||||
close(fromRpm.dc, c.dc); close(fromRpm.peakLine.amplitude, c.peakLine.amplitude);
|
||||
});
|
||||
|
||||
test('已确认主谱线校核:额定143–148rpm约10.8A@40k,锁转18.29A@40k', () => {
|
||||
const running = point(312.5, Ib, { verifyLines: 12 });
|
||||
assert.ok(running.rpm > 143 && running.rpm < 148);
|
||||
assert.equal(running.peakLine.frequency, 40000); close(running.peakLine.amplitude, 10.8, 0.005);
|
||||
const locked = point(0, 49.04);
|
||||
assert.equal(locked.peakLine.frequency, 40000); close(locked.peakLine.amplitude, 18.29038012884, 1e-10);
|
||||
assert.equal(locked.M, 0); assert.equal(locked.freq.length, 10);
|
||||
assert.ok(running.verification.maxStrongRelativeError < 0.02);
|
||||
assert.ok(running.verification.maxAbsError < 0.01);
|
||||
});
|
||||
|
||||
test('实际DC可正取电/负回馈,q0有开关纹波且不篡改成理想功率公式', () => {
|
||||
const motoring = point(546.875, Ib), generating = point(546.875, -Ib), zero = point(546.875, 0);
|
||||
assert.ok(motoring.dc > 0); assert.ok(generating.dc < 0);
|
||||
assert.ok(zero.peakLine.amplitude > 0.05);
|
||||
assert.ok(Math.abs(zero.dc) > 1e-6); // held-duty tracking error is not overwritten.
|
||||
close(motoring.meanBusW, motor.Vdc * motoring.dc);
|
||||
close(generating.meanBusW, motor.Vdc * generating.dc);
|
||||
});
|
||||
|
||||
test('周期闭合/三相平衡与母线能量收支在理想模型内一致', () => {
|
||||
for (const c of [point(0, 49.04), point(312.5, Ib), point(546.875, -Ib), point(546.875, 0)]) {
|
||||
assert.ok(c.closure < 1e-9); assert.ok(c.balanceError < 1e-9);
|
||||
assert.ok(Math.abs(c.energyResidual) < 1e-8 + Math.abs(c.meanBusW) * 1e-9);
|
||||
close(c.meanBusW, c.copperW + c.electromagneticW, 1e-9);
|
||||
assert.ok(c.peakPhase > 0); assert.ok(c.phaseRms.every(x => x >= 0 && Number.isFinite(x)));
|
||||
}
|
||||
});
|
||||
|
||||
test('反向转速/转矩并将电角移π只交换相序,同工况母线复频谱一致', () => {
|
||||
const forward = point(312.5, Ib, { angle: 0.2 });
|
||||
const reverse = point(-312.5, -Ib, { angle: Math.PI - 0.2 });
|
||||
close(reverse.dc, forward.dc, 1e-9);
|
||||
assert.equal(reverse.freq.length, forward.freq.length);
|
||||
for (let i = 0; i < forward.freq.length; i++) {
|
||||
assert.equal(reverse.freq[i], forward.freq[i]);
|
||||
close(reverse.re[i], forward.re[i], 1e-7, 1e-8); close(reverse.im[i], forward.im[i], 1e-7, 1e-8);
|
||||
}
|
||||
});
|
||||
|
||||
test('精确开关边沿复积分校核采样峰值与相位,锁转用精确谱而非冻结相电流', () => {
|
||||
const c = point(312.5, Ib, { angle: 0.13 });
|
||||
const indices = Array.from({ length: c.freq.length }, (_, i) => i)
|
||||
.sort((a, b) => Math.hypot(c.re[b], c.im[b]) - Math.hypot(c.re[a], c.im[a])).slice(0, 12);
|
||||
const ex = exactSpectrum(motor, { fe: c.fe, iqPeak: Ib, angle: c.angle }, [0, ...indices.map(i => c.freq[i])]);
|
||||
close(ex.re[0], c.dc, 1e-9); close(ex.im[0], 0);
|
||||
for (let j = 0; j < indices.length; j++) {
|
||||
const i = indices[j], error = Math.hypot(c.re[i] - ex.re[j + 1], c.im[i] - ex.im[j + 1]);
|
||||
assert.ok(error < 0.01); assert.ok(error / Math.hypot(ex.re[j + 1], ex.im[j + 1]) < 0.03);
|
||||
}
|
||||
const locked = point(0, 49.04), lockedEx = exactSpectrum(motor, { fe: 0, iqPeak: 49.04 }, locked.freq);
|
||||
for (let i = 0; i < locked.freq.length; i++) { close(locked.re[i], lockedEx.re[i]); close(locked.im[i], lockedEx.im[i]); }
|
||||
});
|
||||
|
||||
test('提高spp的主谱线与精确积分收敛检查只声明数值误差', () => {
|
||||
const low = point(312.5, Ib, { samplesPerPwm: 64, verifyLines: 8 });
|
||||
const high = point(312.5, Ib, { samplesPerPwm: 256, verifyLines: 8 });
|
||||
assert.ok(high.verification.maxAbsError < low.verification.maxAbsError);
|
||||
close(high.peakLine.amplitude, point(312.5, Ib).peakLine.amplitude, 0.002);
|
||||
assert.ok(high.verification.scope.includes('不验证电机参数或硬件精度'));
|
||||
});
|
||||
|
||||
test('SVPWM/SPWM均支持中心PWM,SPWM改变波形但仍满足能量/平衡', () => {
|
||||
const c = point(312.5, Ib, { pwm: 'spwm', verifyLines: 4 });
|
||||
assert.equal(c.pwm, 'spwm'); assert.ok(c.modulationRatio < 1);
|
||||
assert.ok(Math.abs(c.energyResidual) < 1e-8); assert.ok(c.balanceError < 1e-9);
|
||||
assert.ok(c.verification.maxStrongRelativeError < 0.03);
|
||||
assert.notEqual(c.peakLine.amplitude, point(312.5, Ib).peakLine.amplitude);
|
||||
});
|
||||
|
||||
test('有限scan保留两组独立工况与signed q,锁转不伪扫转速;功率按实际DC过滤', () => {
|
||||
const options = { nSpeed: 2, nCurrent: 1, nAngle: 3, bands: 3, maxCycles: 128, samplesPerPwm: 64 };
|
||||
const result = scan(motor, options);
|
||||
assert.ok(result.rated.cases.length > 0 && result.locked.cases.length > 0);
|
||||
assert.ok(result.rated.cases.some(c => c.q === -1)); assert.ok(result.rated.cases.some(c => c.q === 0));
|
||||
assert.ok(result.rated.cases.some(c => c.q === 1));
|
||||
assert.ok(result.locked.cases.every(c => c.rpm === 0));
|
||||
assert.ok(result.locked.cases.some(c => c.iqPeak === 49.04));
|
||||
assert.ok(result.locked.cases.every(c => c.angle >= 0 && c.angle < Math.PI / 3));
|
||||
assert.equal(result.grid.lineAmplitudeThreshold, 0); assert.equal(result.grid.runningAngles, 1);
|
||||
assert.ok(result.scope.includes('不是连续域全局证明')); assert.ok(result.scope.includes('未扫描点'));
|
||||
const limited = scan(motor, { ...options, maxBusW: 50 });
|
||||
assert.ok(limited.skippedPower > 0);
|
||||
assert.ok([...limited.rated.cases, ...limited.locked.cases].every(c => Math.abs(motor.Vdc * c.dc) <= 50 * (1 + 1e-9)));
|
||||
const rpmLimited = scan(motor, { ...options, maxRpm: 100 });
|
||||
assert.ok(rpmLimited.rated.cases.every(c => c.rpm <= 100));
|
||||
});
|
||||
|
||||
test('非法输入、超调制和不支持公共周期拒绝而非偷偷夹限或epsilon替代', () => {
|
||||
for (const key of Object.keys(engine.defaults)) for (const value of [0, -1, NaN, Infinity, '1', null]) rejects(() => normalize({ [key]: value }));
|
||||
for (const options of [{ pwm: 'SVPWM' }, { bands: 11 }, { nSpeed: 0 }, { nCurrent: 0 },
|
||||
{ samplesPerPwm: 100 }, { maxCycles: 100 }, { maxBusW: -1 }, { maxRpm: -1 }, { phaseRms: 0 }, { stallPeak: 0 }]) rejects(() => normalize({}, options));
|
||||
rejects(() => caseSpectrum(motor, { fe: 100, iqPeak: Ib }));
|
||||
rejects(() => caseSpectrum(motor, { fe: 0, iqPeak: 1000 }));
|
||||
rejects(() => exactSpectrum(motor, { fe: 0, iqPeak: 1 }, [-1]));
|
||||
rejects(() => exactSpectrum(motor, { fe: 0, iqPeak: 1 }, new Array(257).fill(1)));
|
||||
});
|
||||
|
||||
async function asyncTests() {
|
||||
const options = { nSpeed: 1, nCurrent: 1, nAngle: 1, bands: 1, maxCycles: 64, samplesPerPwm: 64 };
|
||||
const sync = scan(motor, options); let progress = 0;
|
||||
const async = await scanAsync(motor, options, { onProgress(p) { progress++; assert.ok(p.evaluated > 0); } });
|
||||
assert.equal(async.evaluated, sync.evaluated); assert.equal(async.rated.cases.length, sync.rated.cases.length);
|
||||
close(async.rated.peak.amplitude, sync.rated.peak.amplitude); assert.equal(progress, async.evaluated);
|
||||
let count = 0;
|
||||
const cancelled = await scanAsync(motor, options, { shouldCancel: () => count >= 2, onProgress() { count++; } });
|
||||
assert.equal(cancelled.cancelled, true); assert.equal(count, 2);
|
||||
assert.equal(cancelled.rated.cases.length, 0);
|
||||
passed++; console.log('✓ async逐工况让出、进度与取消,结果不拼合半成品');
|
||||
console.log(`\n${passed} 项电机母线模型测试全部通过。`);
|
||||
}
|
||||
asyncTests().catch(error => { console.error(error); process.exitCode = 1; });
|
||||
Reference in New Issue
Block a user