Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
merstoffe14 committed Oct 5, 2022
0 parents commit 96b370f
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
Binary file added __pycache__/electronicsToolkit.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/phasor.cpython-310.pyc
Binary file not shown.
70 changes: 70 additions & 0 deletions electronicsToolkit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import math
from math import radians
from mimetypes import init

from phasor import Phasor


class EEToolkit:

import numpy as np
from phasor import Phasor


# I am not sure if you would use RMS or PEAK value for phasors, I need to check my handbook
# In my head it would be PEAK value because then the phasor will draw the correct sinusoidal wave
# But if I remember correctly we always use RMS
# If we calculate a power, we use phasors using RMS (logical because for ac power calculations you need to use RMS)
def __init__(self) -> None:

self.standardValues = {
"mains_us": Phasor.fromComplex(120, 0, "V", 60),
"mains": Phasor.fromComplex(230, 0, "V", 50),
}


def addPhasor(self, p1: Phasor, p2: Phasor):

real = p1.realComponent + p2.realComponent
imaginary = p1.imaginaryComponent + p2.imaginaryComponent
p12 = Phasor("_","_", real, imaginary)
return p12


#Is er een betere manier voor deze optional argumetns
def subtractPhasor(self, p1: Phasor, p2: Phasor):

real = p1.realComponent - p2.realComponent
imaginary = p1.imaginaryComponent - p2.imaginaryComponent
p12 = Phasor("_","_", real, imaginary)
return p12

def multiplyPhasor(self, p1: Phasor, p2: Phasor):

argument = p1.argument * p2.argument
magnitude = p1.magnitude + p2.magnitude
p12 = Phasor(magnitude, argument)
return p12

def dividePhasor(self, p1: Phasor, p2: Phasor):

argument = p1.argument / p2.argument
magnitude = p1.magnitude - p2.magnitude
p12 = Phasor(magnitude, argument)
return p12

def activePower(self):
pass

def reactivePower(self):
pass

def apparentPower(self):
pass

def calculateImpedance(self, R, L ,C , omega):
pass




57 changes: 57 additions & 0 deletions phasor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import math
from math import radians, degrees

class Phasor:

#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

def __init__(self, magnitude, argument, realComponent, imaginaryComponent, frequency=None, unit=None) -> None:

if frequency == None:
self.isImpedance = True
self.unit = "Ohm"
else:
self.isImpedance = False
self.unit = unit

self.magnitude = magnitude
self.argument = argument
self.realComponent = realComponent
self.imaginaryComponent = imaginaryComponent
self.frequency = frequency
self.angularFrequency = frequency*math.pi*2
self.period = 1/frequency
self.complexNotation = [self.realComponent, self.imaginaryComponent]


@classmethod
def fromComplex(cls, realComponent: float, imaginaryComponent: float, unit: str = None, frequency: float = None):
magnitude = math.sqrt(realComponent**2 + imaginaryComponent**2)
argument = degrees(math.atan(imaginaryComponent/realComponent))
return cls(magnitude, argument, realComponent, imaginaryComponent, frequency, unit)

@classmethod
def fromPolar(cls, magnitude: float, argument: float, unit: str = None, frequency: float = None):
realComponent = math.cos(radians(argument))*magnitude
imaginaryComponent = math.sin(radians(argument))*magnitude
return cls(magnitude, argument, realComponent, imaginaryComponent, frequency, unit)


def printString(self, notation):

if notation == "complex":
if self.imaginaryComponent < 0:
string = f"{round(self.realComponent,2)} - j{round(abs(self.imaginaryComponent), 2)} {self.unit}"
else:
string = f"{round(self.realComponent, 2)} + j{round(abs(self.imaginaryComponent), 2)} {self.unit}"
return string

if notation == "polar":
string = f"{round(self.magnitude, 2)}{round(self.argument, 2)}º {self.unit}"
return string
else:
return "notation unknown"
14 changes: 14 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from electronicsToolkit import EEToolkit
from phasor import Phasor

eet = EEToolkit()

v1 = Phasor.fromComplex(30, 10, "V", 50)
i1 = Phasor.fromPolar(25,45, "A", 50)
z1 = Phasor.fromComplex(30, 20)

print(v1.printString("complex"))
print(v1.printString("polar"))

print(i1.printString("complex"))
print(i1.printString("polar"))

0 comments on commit 96b370f

Please sign in to comment.