main.js entry 1.8 KB
Copy main.js
SimKit.define({
title: "Half-wave rectifier with a smoothing capacitor",
dt: 5e-5, speed: 0.03, height: 300,
params: {
V: { label: "Peak voltage", unit: "V", min: 2, max: 24, value: 10, step: 0.5 },
f: { label: "Mains frequency", unit: "Hz", min: 20, max: 200, value: 50, log: true },
R: { label: "Load resistance", unit: "Ω", min: 50, max: 10000, value: 1000, log: true },
C: { label: "Filter capacitor", unit: "F", min: 1e-6, max: 1e-3, value: 1e-4, log: true },
filter: { type: "checkbox", label: "Use the filter capacitor", value: true }
},
init(P, S) {
const c = new SimKit.Circuit();
const a = c.node(), b = c.node();
Object.assign(S, { c, a, b });
S.src = c.V(a, 0, 0);
S.d = c.D(a, b);
S.load = c.R(b, 0, P.R);
S.cap = c.C(b, 0, P.C);
},
step(dt, P, S, t) {
S.src.value = P.V * Math.sin(2 * Math.PI * P.f * t);
S.load.value = P.R;
S.cap.value = P.filter ? P.C : 1e-12;
S.c.step(dt);
},
draw(g, W, H, P, S) {
const sch = SimKit.sch, v = S.c.v, col = n => SimKit.voltColor(v[n], P.V);
const x0 = W * 0.14, x1 = W * 0.42, x2 = W * 0.62, x3 = W * 0.86, yt = H * 0.3, yb = H * 0.8;
sch.wire(g, x0, yb, x3, yb, "#555");
sch.vsource(g, x0, yb, x0, yt, col(S.a));
sch.diode(g, x0, yt, x1, yt, col(S.a));
sch.wire(g, x1, yt, x3, yt, col(S.b));
sch.capacitor(g, x2, yt, x2, yb, col(S.b));
sch.resistor(g, x3, yt, x3, yb, col(S.b));
sch.dot(g, x2, yt, col(S.b));
sch.ground(g, W / 2, yb);
sch.label(g, "D", (x0 + x1) / 2, yt - 18);
sch.label(g, "Vout = " + SimKit.fmt(v[S.b], "V") + " diode current " + SimKit.fmt(S.d.i, "A"), W / 2, H - 12, "#444");
},
scope: {
span: P => 3 / P.f, unit: "V",
traces: [
{ label: "Input", color: "#5aa0ff", get: S => S.c.v[S.a] },
{ label: "Output", color: "#ff6060", get: S => S.c.v[S.b] }
]
}
});