1
package com.sun.electric.tool.simulation.test;
4
* Assuming you have a PowerChannel with a resistor attached to the
5
* output and a VoltageReadable across the resistor, this will act
6
* like a PowerChannel+CurrentReadable that lets you set the voltage
7
* across the load as long as it maintains a reasonably stable current
10
* +--------------+ resistor +------+
11
* +--| PowerChannel |---------/\/\/\/\---------| load |---+
12
* | +--------------+ ^ ^ +------+ |
14
* | +- VoltageReadable -+ |
16
* +-------------------------------------------------------+
18
* @author md227184 (wrote FastProxEquipment, from which this is derived)
21
public class PowerChannelResistorVoltageReadable extends PowerChannel {
23
private boolean fastConvergence;
24
private PowerChannel pc;
26
private VoltageReadable vr;
27
private static float EPSILON_VOLTS = 0.01f;
29
public void setCurrent(float amps) { pc.setCurrent(amps); }
30
public float getCurrentSetpoint() { return pc.getCurrentSetpoint(); }
31
public float readCurrent() {
32
double pc_current = pc.readCurrent();
33
double vr_current = vr.readVoltage() / ohms;
34
if (Math.abs(pc_current - vr_current) > EPSILON_VOLTS * ohms)
35
throw new RuntimeException("PowerChannel and VoltageReadable disagree on current; perhaps you gave the wrong resistor value?\n" +
36
" PowerChannel says: " + pc_current + "\n" +
37
" VoltageReadable says: " + vr_current);
38
return (float)vr_current;
40
public float readVoltage() {
41
readCurrent(); // to force the sanity check
42
return pc.readVoltage() - vr.readVoltage();
44
public void setVoltageNoWait(float volts) { throw new RuntimeException("cannot do this in a "+this.getClass().getName()); }
45
public void waitForVoltage(float setVolts) { throw new RuntimeException("cannot do this in a "+this.getClass().getName()); }
46
public float getVoltageSetpoint() { throw new RuntimeException("cannot do this in a "+this.getClass().getName()); }
47
public void setVoltageWait(float v) {
48
readCurrent(); // to force the sanity check
50
double vs = pc.readVoltage();
51
double vread = vr.readVoltage();
52
double i = vread/1000;
53
double vl = (vs-ohms*i);
54
System.err.print("\r\033[0K\r");
55
System.err.print(this.getClass().getSimpleName()+
57
" desired/actual="+v+"/"+vl);
58
if (vl+EPSILON_VOLTS < v || vl-EPSILON_VOLTS > v) {
59
if (fastConvergence) {
60
double delta = v+i*ohms - vs;
66
pc.setVoltageWait((float)vs);
68
readCurrent(); // to force the sanity check
69
System.err.print("\r\033[0K\r");
75
public PowerChannelResistorVoltageReadable(PowerChannel pc,
77
VoltageReadable voltMeterAcrossResistor,
78
boolean fastConvergence) {
80
this.ohms = ohmsOfResistor;
81
this.vr = voltMeterAcrossResistor;
82
this.fastConvergence = fastConvergence;