Last active
September 11, 2022 03:00
-
-
Save Jesse-Millwood/aecb27116e8a8aac3d1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
File: Electrical_Components.py | |
Classes and methods to aid in electical calcualtaions | |
in python | |
Jesse Millwood 2014 | |
''' | |
import re | |
import sys | |
import numpy as np | |
SIunits = {'G':1e9,'M':1e6,'k':1e3,'m':1e-3,'u':1e-6,'n':1e-9} | |
# Source Object | |
class Source (object): | |
def __init__(self, Type='DC', Hz=0, A=1,Phase=0): | |
self.Type = Type | |
self.Hz = Hz | |
self.A = A | |
self.Phase = Phase | |
self.w = np.pi*2*self.Hz | |
def createSignal(self, t): | |
self.signal = self.A*np.cos(self.w*t + self.Phase) | |
class Component(object): | |
def __init__(self, Value='10'): | |
self.Value = Value | |
self.value = self.SIconvert() | |
self.V = 0 | |
self.I = 0 | |
def SIconvert(self): | |
if type(self.Value) is not str: | |
print 'Enter Value of Component as String' | |
print 'Exiting Program .....' | |
sys.exit() | |
val_num = map(float,re.findall(r'\d+',self.Value)) | |
for token in re.split(r'(\D)',self.Value): | |
if token in SIunits: | |
return val_num[0] * SIunits[token] | |
else: | |
return val_num[0] | |
class Inductor(Component): | |
def __init__(self, Value='1uH'): | |
Component.__init__(self, Value) | |
self.Device = Inductor | |
self.Unit = 'H' | |
def Reactance (self, w=0): | |
self.XL = w * self.value * 1j | |
# TODO See if this is good here | |
def CalcV(self,I): | |
self.V = self.XL*I | |
class Capacitor(Component): | |
def __init__(self, Value='1uF'): | |
Component.__init__(self, Value) | |
self.Unit = 'F' | |
def Reactance (self, w=0): | |
self.XC = 1j/(w * self.value) | |
class Resistor(Component): | |
def __init__(self, Value='1 kO'): | |
Component.__init__(self, Value) | |
self.Unit = 'Ohm' | |
def Resistance(self): | |
self.R = self.value | |
class Phasor(object): | |
def __init__(self, rect=0+0j): | |
self.rect = rect | |
self.theta=[np.angle(self.rect),np.angle(self.rect)] | |
self.mag =np.array([0, np.absolute(self.rect)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Given the component values for a Series R-L-C Circuit | |
Draw the phasor diagram. Calculations are based on ideal | |
values | |
Tested on | |
Xubuntu 14.04 with | |
Python 2.7, | |
Matplotlib 1.3.1, | |
Numpy 1.8.1 | |
Jesse M | |
''' | |
# Import Modules | |
import matplotlib.widgets as mw | |
import matplotlib.pyplot as plt | |
import matplotlib | |
import numpy as np | |
from Electrical_Components import * | |
Vs = Source(Type='AC', Hz=60, A=110) | |
R = Resistor(Value='100 O') | |
L = Inductor(Value='500mH') | |
C = Capacitor(Value='10uF') | |
L.Reactance(w=Vs.w) | |
C.Reactance(w=Vs.w) | |
R.Resistance() | |
Z = Phasor(rect=(R.R + (L.XL-C.XC))) | |
I = Vs.A/Z.rect | |
L.V = I*L.XL | |
C.V = I*-1*C.XC | |
R.V = I*R.R | |
# TODO : Bring the phasor object into the components | |
IP = Phasor(rect=I) | |
LV = Phasor(rect=L.V) | |
CV = Phasor(rect=C.V) | |
RV = Phasor(rect=R.V) | |
VS = Phasor(rect=Vs.A) | |
fig = plt.figure() | |
ax = fig.add_subplot(111, polar=True) | |
ax.plot(IP.theta,IP.mag*180,label=r'$I$') # Scaled by 180 because it is visible then | |
ax.arrow(0,0,LV.theta[1],LV.mag[1], length_includes_head=True) | |
#ax.plot(LV.theta,LV.mag,label=r'$V_{IND}$') | |
ax.plot(CV.theta,CV.mag,label=r'$V_{CAP}$') | |
ax.plot(RV.theta,RV.mag,label=r'$V_{RES}$') | |
ax.plot(VS.theta,VS.mag,label=r'$V_{SRC}$') | |
ax.set_yticklabels([]) | |
string = 'Series RLC Circuit:\nR:{}\nL:{}\nC:{}'.format(R.Value,L.Value,C.Value) | |
# these are matplotlib.patch.Patch properties | |
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) | |
ax.text(0.02,0.84,string,transform=fig.transFigure,bbox=props) | |
ax.legend(bbox_to_anchor=(0,0,1,1), bbox_transform=fig.transFigure) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment