Skip to content

Commit

Permalink
Added a scope view
Browse files Browse the repository at this point in the history
Added a scope view [WIP].
I still need to add a legend.
  • Loading branch information
merstoffe14 committed Oct 11, 2022
1 parent 9e359c1 commit e65077b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
Empty file added 3phase.py
Empty file.
Binary file modified __pycache__/eecomplex.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/electronicsToolkit.cpython-39.pyc
Binary file not shown.
36 changes: 22 additions & 14 deletions eecomplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@

class EEComplex:

# In physics and engineering, a phasor (a portmanteau of phase vector)
# is a complex number representing a sinusoidal function whose
# amplitude (A), angular frequency (ω), and initial phase (θ) are time-invariant.

# Impedance values can also be saved in the form of phasors, altough they do not have an angular frequency
# Admitances as well

# To capture all of these in 1 simple object, we use the EEComplex.
# It is recomended to define a "Global" frequency variable, and set it at the top of your worksheet.
'''
In physics and engineering, a phasor (a portmanteau of phase vector)
is a complex number representing a sinusoidal function whose
amplitude (A), angular frequency (ω), and initial phase (θ) are time-invariant.
Impedance values can also be saved in the form of phasors, altough they do not have an angular frequency
Admitances as well
To capture all of these in 1 simple object, we use the EEComplex.
'''

def __init__(self, complexNumber: complex, units: list[list[str],list[str]], frequency: float = None) -> None:

self.units = units
self.complex = complexNumber
self.frequency = frequency
self.roundTo = 2

self.eecomplexChecker()
self.unitChecker()
Expand Down Expand Up @@ -90,8 +92,7 @@ def unitChecker(self):
nominator.append("Ohm")
# V = A/S, A = V/S, S = V/A
# Might add more later



def getRealComponent(self):
return self.complex.real

Expand All @@ -104,6 +105,14 @@ def getMagnitude(self):
def getArgument(self):
return degrees(math.atan(self.getImaginaryComponent()/self.getRealComponent()))


# Is this neccecary? I can check the unit using [nominator,denominator]
def unitCheck(self, unitToCheck: str) -> bool:
if not (self.units[0].count(unitToCheck) and len(self.units[0]) == 1) and not self.units[1]:
return False
else:
return True

def __str__(self):
nominatorString = ""
denominatorString = ""
Expand All @@ -118,9 +127,8 @@ def __str__(self):
unitsstring = "1/" + denominatorString
else:
unitsstring = nominatorString + "/" + denominatorString


return f"{self.getRealComponent()} + j{self.getImaginaryComponent()} {unitsstring} | {self.getMagnitude()}{self.getArgument()}° {unitsstring}"

return f"{round(self.getRealComponent(),self.roundTo)} + j{round(self.getImaginaryComponent(),self.roundTo)} {unitsstring} | {round(self.getMagnitude(), self.roundTo)}{round(self.getArgument(),self.roundTo)}° {unitsstring}"

def __add__(self, other: "EEComplex") -> "EEComplex":

Expand Down
23 changes: 19 additions & 4 deletions electronicsToolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def __init__(self) -> None:

def getAdmitanceFromZ(self, z: EEComplex) -> EEComplex:
#check if z is an impedance
if not (z.units[0].count("Ohm") and len(z.units[0]) == 1) and not z.units[1]:
if not z.unitCheck("Ohm"):
raise ValueError("Z must be an impedance!")
magnitude = 1/(z.getMagnitude())
argument = -z.getArgument()
return EEComplex.fromPolar(magnitude, argument, "S")

def getImpedanceFromY(self, y: EEComplex) -> EEComplex:
#check if y is an admitance
if not (y.units[0].count("S") and len(y.units[0]) == 1) and not y.units[1]:
if not y.unitCheck("S"):
raise ValueError("Y must be an admitance!")
magnitude = 1/(y.getMagnitude())
argument = -y.getArgument()
Expand Down Expand Up @@ -92,5 +92,20 @@ def parallelImpedance(self, values: list[EEComplex]) -> EEComplex:
return self.getImpedanceFromY(sum(y))

# Time in ms
def drawScopeView(self, phasors: list[EEComplex], time: int = 100):
pass
def drawScopeView(self, phasors: list[EEComplex], time: int = 100, sampleRate: int = 1000):
x = np.linspace(0, time/1000, 1000)
for p in phasors:
#Check if phasor has a unit of V or A
if not p.unitCheck("V") and not p.unitCheck("A"):
raise ValueError("Phasor must be a voltage or current source!")
else:
# Multiplied by a factor of sqrt(2), to convert from RMS to peak, since phasors are always pure sinusoidal waves
y = np.sqrt(2) * p.getMagnitude() * np.sin(2 * np.pi * p.frequency * x + radians(p.getArgument()))
plt.plot(x,y)

plt.xlabel('Time (s)')
plt.ylabel('Voltage (V)/ Current (A)')
plt.title('Scope View')
# add function labels

plt.show()
9 changes: 5 additions & 4 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

eet = EEToolkit()

z_rlc = eet.calculateImpedance(R = 10, L = 0.1, C = 0.0001, frequency = 50)
print(z_rlc)
z1 = EEComplex.fromPolar(100,0,"Ohm")
z2 = EEComplex.fromPolar(50,0,"Ohm")
z3 = EEComplex.fromPolar(25,0,"Ohm")

v1 = EEComplex.fromPolar(230,0,"V")
v2 = EEComplex.fromPolar(230,0,"V")
v1 = EEComplex.fromPolar(230,0,"V", 50)
v2 = EEComplex.fromComplex(230,100,"V", 50)

print(eet.parallelImpedance([z1,z2,z3]))

eet.drawPhasor(v1)

eet.drawScopeView([v1,v2])



Expand Down

0 comments on commit e65077b

Please sign in to comment.