Skip to content
Open
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
34 changes: 26 additions & 8 deletions src/com/lushprojects/circuitjs1/client/RelayElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class RelayElm extends CircuitElm {
int poleCount;
int openhs, dflip;
boolean onState;

// track time current has been above/below threshold to filter transients
double onThresholdTime, offThresholdTime;
final int nSwitch0 = 0;
final int nSwitch1 = 1;
final int nSwitch2 = 2;
Expand Down Expand Up @@ -328,6 +331,7 @@ void reset() {
for (i = 0; i != poleCount; i++)
switchCurrent[i] = switchCurCount[i] = 0;
d_position = i_position = 0;
onThresholdTime = offThresholdTime = 0;

// preserve onState because if we don't, Relay Flip-Flop gets left in a weird state on reset.
// onState = false;
Expand Down Expand Up @@ -361,30 +365,44 @@ void startIteration() {
}
ind.startIteration(volts[nCoil1]-volts[nCoil3]);
double absCurrent = Math.abs(coilCurrent);


// require current to be sustained above/below threshold for a minimum
// time before switching, to filter brief transients like back-EMF spikes
double minHoldTime = switchingTime * 0.5;

if (onState) {
// on or turning on. check if we need to turn off
if (absCurrent < offCurrent) {
// turning off, set switch to intermediate position
onState = false;
i_position = 2;
offThresholdTime += sim.timeStep;
onThresholdTime = 0;
if (offThresholdTime >= minHoldTime) {
// turning off, set switch to intermediate position
onState = false;
i_position = 2;
}
} else {
offThresholdTime = 0;
d_position += sim.timeStep/switchingTime;
if (d_position >= 1)
d_position = i_position = 1;
}
} else {
// off or turning off. check if we need to turn on
if (absCurrent > onCurrent) {
// turning on, set switch to intermediate position
onState = true;
i_position = 2;
onThresholdTime += sim.timeStep;
offThresholdTime = 0;
if (onThresholdTime >= minHoldTime) {
// turning on, set switch to intermediate position
onState = true;
i_position = 2;
}
} else {
onThresholdTime = 0;
d_position -= sim.timeStep/switchingTime;
if (d_position <= 0)
d_position = i_position = 0;
}

}
}

Expand Down
Loading