public TristateScanner(double resHigh, double resLow, params double[] resPins) { if (resPins == null || resPins.Length == 0) { throw new ArgumentNullException("resPins"); } rValues = new[] { resHigh, resLow }.Concat(resPins).ToArray(); ground = new Node(); outputNode = new Node(); var node5V = new Node(); var source5V = new VoltageSource(5, node5V, ground); var rPlus = new Resistor(rValues[0], node5V, outputNode); var rMinus = new Resistor(rValues[1], ground, outputNode); pins = new KeyValuePair <VoltageSource, Resistor> [rValues.Length - 2]; for (int i = 0; i < pins.Length; i++) { var n = new Node(); var s = new VoltageSource(5, n, ground); var r = new Resistor(rValues[i + 2], n, outputNode); pins[i] = new KeyValuePair <VoltageSource, Resistor>(s, r); } stateCount = 1; for (int i = 0; i < pins.Length; i++) { stateCount *= 3; } InitFastNetwork(ground); }
// private VoltageSource AddVoltageSource(Circuit ckt) // { // VoltageSource source = new VoltageSource("PS", "PSPOS", "0", 0); // for (int i = 0; i < _powerSupply.transform.childCount; i++) // { // var child = _powerSupply.transform.GetChild(i); // if (child.CompareTag("BBSlot")) // { // if (child.GetComponent<Slot>().itemPlaced != null && !child.GetComponent<Slot>().slotChecked) // { // AttachPowerSupply(child, ckt); // } // } // } // return source; // } private void AddPowerSupplyConnection(string pos, string neg, Circuit ckt) { bool isGroundConnected = false; if (Globals.PSConnections.TryGetValue(Globals.PowerSupplyInput.ground, out Globals.BananaPlugs groundConnection)) { if (!groundConnection.Equals(Globals.BananaPlugs.noConnection)) { isGroundConnected = true; } } if (pos == Globals.PowerSupplyInput.ground.ToString()) { pos = "0"; } else if (pos == Globals.PowerSupplyInput.voltageSource.ToString() && isGroundConnected) { Debug.Log("Adding PS Voltage Source: " + pos + "PS" + " " + pos + " " + "0" + " " + _powerSupplyMachine.GetComponent <PSSelect>().voltage); VoltageSource voltSource = new VoltageSource(pos + "PS", pos, "0", _powerSupplyMachine.GetComponent <PSSelect>().voltage); voltageSource = voltSource; ckt.Add(voltSource); } else if (pos == Globals.PowerSupplyInput.currentSource.ToString() && isGroundConnected) { Debug.Log("Adding PS Current Source: " + pos + "PS" + " " + pos + " " + "0" + " " + _powerSupplyMachine.GetComponent <PSSelect>().current); CurrentSource currSource = new CurrentSource(pos + "PS", pos, "0", _powerSupplyMachine.GetComponent <PSSelect>().current); currentSource = currSource; ckt.Add(currSource); } Debug.Log("Adding Wire: " + pos + " " + pos + " " + neg + " " + 0); ckt.Add(new Resistor(pos, pos, neg, 0)); }
public void Test3() { var head = new Node { Name = "Head?" }; var node1 = new Node { Name = "N1" }; var refnode = new Node { IsReferenceNode = true, Name = "Reference" }; var node2 = new Node { Name = "N2" }; var nodes = new List <INode> { head, node1, refnode, node2 }; var vs = new VoltageSource(12) { Name = "Vin", Node1 = head, Node2 = refnode }; var r1 = new Resistor(100) { Name = "R1", Node1 = head, Node2 = refnode }; var r2 = new Resistor(100) { Name = "R2", Node1 = node1, Node2 = refnode }; var r3 = new Resistor(220) { Name = "R3", Node1 = head, Node2 = node2 }; var r4 = new Resistor(1000) { Name = "R4", Node1 = node1, Node2 = refnode }; var sw = new Switch { IsClosed = true, Name = "LOL I'm a switch", Node1 = node1, Node2 = node2 }; var components = new List <IComponent> { vs, r1, r2, r3, r4, sw }; Simulator.AnalyzeAndUpdate(nodes, components); Assert.Equal(12, head.Voltage, 3); Assert.Equal(3.509, node1.Voltage, 3); Assert.Equal(0.159, vs.Current, 3); Assert.Equal(12, r1.Voltage, 3); Assert.Equal(0.12, r1.Current, 3); Assert.Equal(3.509, r2.Voltage, 3); Assert.Equal(0.035, r2.Current, 3); Assert.Equal(8.491, r3.Voltage, 3); Assert.Equal(0.039, r3.Current, 3); Assert.Equal(3.509, r4.Voltage, 3); Assert.Equal(0.004, r4.Current, 3); }
public MatValueSource(VoltageSource source) { if (source == null) { throw new ArgumentNullException("source"); } this.source = source; }
public void Test5() { var head = new Node { Name = "Head" }; var node1 = new Node { Name = "N1" }; var refnode = new Node { IsReferenceNode = true, Name = "Ref" }; var nodes = new List <INode> { head, node1, refnode }; var vs = new VoltageSource(12) { Node1 = head, Node2 = refnode, Name = "Vin1" }; var r1 = new Resistor(100) { Node1 = head, Node2 = refnode, Name = "R1" }; var r2 = new Resistor { Current = 0.035087719298245612, Node1 = node1, Node2 = refnode, Name = "R2" }; var r3 = new Resistor(220) { Node1 = head, Node2 = node1, Name = "R3" }; var r4 = new Resistor(1000) { Node1 = node1, Node2 = refnode, Name = "R4" }; var components = new List <IComponent> { vs, r1, r2, r3, r4 }; Simulator.AnalyzeAndUpdate(nodes, components); Assert.Equal(100, r2.Resistance, 0); Assert.Equal(12, head.Voltage, 3); Assert.Equal(3.509, node1.Voltage, 3); Assert.Equal(0.159, vs.Current, 3); Assert.Equal(12, r1.Voltage, 3); Assert.Equal(0.12, r1.Current, 3); Assert.Equal(3.509, r2.Voltage, 3); //Assert.Equal(0.035, r2.Current, 3); Assert.Equal(8.491, r3.Voltage, 3); Assert.Equal(0.039, r3.Current, 3); Assert.Equal(3.509, r4.Voltage, 3); Assert.Equal(0.004, r4.Current, 3); }
/// <summary> /// Reads <see cref="Control"/> statement and modifies the context. /// </summary> /// <param name="statement">A statement to process.</param> /// <param name="context">A context to modify.</param> public override void Read(Control statement, ICircuitContext context) { if (statement.Parameters.Count != 2) { context.Result.Validation.Add(new ValidationEntry(ValidationEntrySource.Reader, ValidationEntryLevel.Warning, "Wrong parameter count for .CONNECT", statement.LineInfo)); return; } string nodeA = statement.Parameters.Get(0).Image; string nodeB = statement.Parameters.Get(1).Image; var vs = new VoltageSource($"Voltage connector: {nodeA} <-> {nodeB}"); context.CreateNodes(vs, statement.Parameters); context.Result.AddEntity(vs); }
public void Test7() { var n1 = new Node { Name = "Center" }; var n2 = new Node { Name = "Out" }; var n3 = new Node { Name = "Gnd", IsReferenceNode = true }; var nodes = new List <INode> { n1, n2, n3 }; var vs1 = new VoltageSource(12) { Name = "Vin1", Node1 = n2, Node2 = n1 }; var vs2 = new VoltageSource(5) { Name = "Vin2", Node1 = n1, Node2 = n3 }; var r1 = new Resistor(20) { Name = "R1", Node1 = n2, Node2 = n3 }; var components = new List <IComponent> { vs1, vs2, r1 }; Simulator.AnalyzeAndUpdate(nodes, components); Assert.Equal(n1.Voltage, 5, 3); Assert.Equal(n2.Voltage, 17, 3); Assert.Equal(vs1.Current, 0.85, 3); Assert.Equal(vs2.Current, 0.85, 3); Assert.Equal(r1.Current, 0.85, 3); Assert.Equal(r1.Voltage, 17, 3); }
public void AddSource(VoltageSource s) { sources.Add(s); }
public void AddSource(VoltageSource s) { sources.Add(s); }
public void SpiceSharpCalculation() { Debug.Log("*********************"); Debug.Log("Starting calculation "); ClearAllSlots(); voltageSource = new VoltageSource("a"); currentSource = new CurrentSource("b"); var ckt = new Circuit(); foreach (KeyValuePair <Globals.AgilentInput, Globals.BananaPlugs> entry in Globals.AgilentConnections) { if (entry.Value != Globals.BananaPlugs.noConnection) { ActivateBananaSlot(entry.Value.ToString()); } } foreach (KeyValuePair <Globals.FlukeInput, Globals.BananaPlugs> entry in Globals.FlukeConnections) { if (entry.Value != Globals.BananaPlugs.noConnection) { ActivateBananaSlot(entry.Value.ToString()); } } foreach (KeyValuePair <Globals.PowerSupplyInput, Globals.BananaPlugs> entry in Globals.PSConnections) { if (entry.Value != Globals.BananaPlugs.noConnection) { AddPowerSupplyConnection(entry.Key.ToString(), entry.Value.ToString(), ckt); ActivateBananaSlot(entry.Value.ToString()); } } //Handle Self BananaPlug Connections for (int i = 0; i < 5; i++) { if (bananaPlugActive[i]) { HandleSelfBananaConnections(i, ckt); } } //INSERT POTENTIOMETER VAL HERE if (bananaPlugActive[4]) { if (_potentioMeterTextBox.text.Length == 0) { ckt.Add(new Resistor("PotentioMeterBanana", "B4", "PotentioMeter", 0)); Debug.Log("Adding Wire: PotentioMeterBanana B4 PotentioMeter 0"); } else { ckt.Add(new Resistor("PotentioMeterBanana", "B4", "PotentioMeter", int.Parse(_potentioMeterTextBox.text))); Debug.Log("Adding Wire: PotentioMeterBanana B4 PotentioMeter " + _potentioMeterTextBox.text); } ckt.Add(new Resistor("PotentioMeterBlack", "PotentioMeter", "C67", 0)); ckt.Add(new Resistor("PotentioMeterBlue", "PotentioMeter", "C70", 0)); ckt.Add(new Resistor("PotentioMeterRed", "PotentioMeter", "C72", 0)); Debug.Log("Adding Wire: PotentioMeterBlack PotentioMeter C67 0"); Debug.Log("Adding Wire: PotentioMeterBlue PotentioMeter C70 0"); Debug.Log("Adding Wire: PotentioMeterRed PotentioMeter C82 0"); } if (bananaPlugActive[2] || bananaPlugActive[4]) { ckt.Add(new Inductor("B2B4Inductor", "B2", "B4", 0.020f)); Debug.Log("Adding Inductor: PotentioMeterInductor B2 B4 0.020H"); } // ckt.Add(new Resistor("PotentioMeterBanana", "B4", "PotentioMeter", 500)); // ckt.Add(new Resistor("PotentioMeterBlack", "PotentioMeter", "C17", 0)); // ckt.Add(new Resistor("PotentioMeterBlue", "PotentioMeter", "C18", 0)); // ckt.Add(new Resistor("PotentioMeterRed", "PotentioMeter", "C19", 0)); // ckt.Add(new Resistor("ShortCircuitInductor", "B2", "B4", 0)); // ckt.Add(new Inductor("ShortCircuitInductor","B2", "B4",0.020f)); //handle permanent connections for (int i = 0; i < _bananaPlugs.transform.childCount; i++) { if (bananaPlugActive[i]) { var child = _bananaPlugs.transform.GetChild(i); if (child.CompareTag("BBSlot")) { AddElectricalElement(child.GetComponent <Slot>(), ckt); child.GetComponent <Slot>().slotChecked = true; child.GetComponent <Slot>().slotPair.GetComponent <Slot>().slotChecked = true; } } } for (int i = 0; i < _breadboardUI.transform.childCount; i++) { var child = _breadboardUI.transform.GetChild(i); if (child.CompareTag("BBSlot")) { if (child.GetComponent <Slot>().itemPlaced != null && !child.GetComponent <Slot>().slotChecked&& !child.GetComponent <Slot>().ignoreThisSlot) { if (child.GetComponent <Slot>().slotPair.GetComponent <Slot>().slotType == Globals.SlotType.defaultSlot) { AddElectricalElement(child.GetComponent <Slot>(), ckt); child.GetComponent <Slot>().slotChecked = true; child.GetComponent <Slot>().slotPair.GetComponent <Slot>().slotChecked = true; } } } // if (breadboardSlots[i].GetComponent<Slot>().itemPlaced != null && !breadboardSlots[i].GetComponent<Slot>().slotChecked) // { // if (breadboardSlots[i].GetComponent<Slot>().slotPair.GetComponent<Slot>().slotType == Globals.SlotType.defaultSlot) // { // AddElectricalElement(breadboardSlots[i].GetComponent<Slot>(), ckt); // breadboardSlots[i].GetComponent<Slot>().slotChecked = true; // breadboardSlots[i].GetComponent<Slot>().slotPair.GetComponent<Slot>().slotChecked = true; // } // } } // VoltageSource powerSupply = AddVoltageSource(ckt); // ckt.Add(powerSupply); // Create a DC sweep and register to the event for exporting simulation data //var dc = new DC("dc", "voltageSourcePS", _powerSupply.GetComponent<PowerSupply>().powerReading, _powerSupply.GetComponent<PowerSupply>().powerReading, 1); //var dc = new DC("dc", "voltageSourcePS", _powerSupplyMachine.GetComponent<PSSelect>().voltage, _powerSupplyMachine.GetComponent<PSSelect>().voltage, 1); var dc = new DC("dc"); if (voltageSource.Name != "a" && currentSource.Name != "b") { dc = new DC("dc", new[] { new ParameterSweep(voltageSource.Name, new LinearSweep(_powerSupplyMachine.GetComponent <PSSelect>().voltage, _powerSupplyMachine.GetComponent <PSSelect>().voltage, 1)), new ParameterSweep(currentSource.Name, new LinearSweep(_powerSupplyMachine.GetComponent <PSSelect>().current, _powerSupplyMachine.GetComponent <PSSelect>().current, 1)) }); } else if (voltageSource.Name != "a") { dc = new DC("dc", new[] { new ParameterSweep(voltageSource.Name, new LinearSweep(_powerSupplyMachine.GetComponent <PSSelect>().voltage, _powerSupplyMachine.GetComponent <PSSelect>().voltage, 1)) }); } else if (currentSource.Name != "b") { dc = new DC("dc", new[] { new ParameterSweep(currentSource.Name, new LinearSweep(_powerSupplyMachine.GetComponent <PSSelect>().current, _powerSupplyMachine.GetComponent <PSSelect>().current, 1)) }); } //GetCircuitToolsReading(dc); ManageAgilentReadingsConnections(dc); ManageFlukeReadingsConnections(dc); // Run the simulation try { dc.Run(ckt); } catch (ValidationFailedException e) { Debug.Log("Error is: " + e.ToString()); } }
public void Test2() { var head = new Node { Name = "Head" }; var negativeHead = new Node { Name = "I'm Negative" }; var node1 = new Node { Name = "N1" }; var node2 = new Node { Name = "N2" }; var node3 = new Node { Name = "N3" }; var node4 = new Node { Name = "N4" }; var node5 = new Node { Name = "N5" }; var refnode = new Node { IsReferenceNode = true }; var nodes = new List <INode> { head, negativeHead, node1, node2, node3, node4, node5, refnode }; var vs1 = new VoltageSource(15) { Name = "Vin1", Node1 = head, Node2 = refnode }; var vs2 = new VoltageSource(25) { Name = "Vin2", Node1 = node5, Node2 = negativeHead }; var r1 = new Resistor(120) { Name = "R1", Node1 = head, Node2 = node1 }; var r2 = new Resistor(170) { Name = "R2", Node1 = node1, Node2 = node2 }; var r3 = new Resistor(150) { Name = "R3", Node1 = node2, Node2 = refnode }; var r4 = new Resistor(70) { Name = "R4", Node1 = node1, Node2 = node3 }; var r5 = new Resistor(100) { Name = "R5", Node1 = node3, Node2 = node4 }; var r6 = new Resistor(1000) { Name = "R6", Node1 = node4, Node2 = node2 }; var r7 = new Resistor(300) { Name = "R7", Node1 = node4, Node2 = node2 }; var r8 = new Resistor(1700) { Name = "R8", Node1 = node3, Node2 = negativeHead }; var r9 = new Resistor(1500) { Name = "R9", Node1 = negativeHead, Node2 = node5 }; var r10 = new Resistor(100) { Name = "R10", Node1 = node5, Node2 = node4 }; var r11 = new Resistor(400) { Name = "R11", Node1 = node5, Node2 = node4 }; var components = new List <IComponent> { vs1, vs2, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11 }; Simulator.AnalyzeAndUpdate(nodes, components); Assert.Equal(15, head.Voltage, 3); Assert.Equal(10.25, node1.Voltage, 2); Assert.Equal(5.94, node2.Voltage, 2); Assert.Equal(9.25, node3.Voltage, 2); Assert.Equal(9.23, node4.Voltage, 2); Assert.Equal(10.35, node5.Voltage, 2); Assert.Equal(-14.65, negativeHead.Voltage, 2); Assert.Equal(0.0396, vs1.Current, 4); Assert.Equal(0.03072, vs2.Current, 5); Assert.Equal(120, r1.Resistance, 0); Assert.Equal(4.75, r1.Voltage, 2); Assert.Equal(0.0396, r1.Current, 4); Assert.Equal(170, r2.Resistance, 0); Assert.Equal(4.31, r2.Voltage, 2); Assert.Equal(0.02534, r2.Current, 5); Assert.Equal(150, r3.Resistance, 0); Assert.Equal(5.94, r3.Voltage, 2); Assert.Equal(0.0396, r3.Current, 4); Assert.Equal(70, r4.Resistance, 0); Assert.Equal(0.998, r4.Voltage, 3); Assert.Equal(0.01426, r4.Current, 5); Assert.Equal(100, r5.Resistance, 0); Assert.Equal(0.02009, r5.Voltage, 5); Assert.Equal(0.00020086, r5.Current, 8); Assert.Equal(1000, r6.Resistance, 0); Assert.Equal(3.29, r6.Voltage, 2); Assert.Equal(0.00329, r6.Current, 5); Assert.Equal(300, r7.Resistance, 0); Assert.Equal(3.29, r7.Voltage, 2); Assert.Equal(0.01097, r7.Current, 5); Assert.Equal(1700, r8.Resistance, 0); Assert.Equal(23.9, r8.Voltage, 1); Assert.Equal(0.01406, r8.Current, 5); Assert.Equal(1500, r9.Resistance, 0); Assert.Equal(-25, r9.Voltage, 0); Assert.Equal(-0.01667, r9.Current, 5); Assert.Equal(100, r10.Resistance, 0); Assert.Equal(1.12, r10.Voltage, 2); Assert.Equal(0.01124, r10.Current, 5); Assert.Equal(400, r11.Resistance, 0); Assert.Equal(1.12, r11.Voltage, 2); Assert.Equal(0.00281, r11.Current, 5); }