Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
barreiroleo committed Sep 19, 2021
1 parent 53bdf75 commit de1696f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 5 additions & 4 deletions dimmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _v_sine(self, t):
omega = 2 * np.pi * self.frequency
return self.amplitud * np.sin(omega * t)

def v_dimmer(self, t):
def v_dimmer(self, t):
per = self.periode
tint = self.time_interrupt
# sourcery skip: flip-comparison
Expand Down Expand Up @@ -73,17 +73,17 @@ def convert_duty_rms(self, vrms_duty):
def solve_tint_for_duty(self, duty_perc):
# HACK: Se respalda el estado del time interrupt porque se utiliza calacular. Al final se restaura.
time_interrupt_backup = self.time_interrupt

def function(tint, bias=0):
self.time_interrupt = tint
vrms_max = np.sqrt(2) * 0.5 * self.amplitud
vrms_duty = self.vrms_simbolic() / vrms_max
return vrms_duty - bias
root = bisect(function, 0, self.periode * 0.5, args=(duty_perc / 100))

self.time_interrupt = time_interrupt_backup
return root

def print_dimmer_state(self):
# TODO: Ver si conviene agregar un atributo de vrms actual. Se repite el calculo en ocaciones.
vrms = self.vrms_simbolic()
Expand All @@ -92,6 +92,7 @@ def print_dimmer_state(self):
data = [[self.amplitud, self.frequency, self.time_interrupt, vrms, duty]]
print_table(header, data)


def print_table(header, data):
print("")
print(tabulate(tabular_data=data, headers=header, tablefmt="simple",
Expand Down
10 changes: 6 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ def do_tint_solve(val):

def do_tint_table_solve(val):
points = 256
for i in range(points + 1):
duty = i * 100 / points
tint = dimmer.solve_tint_for_duty(duty)
print(f"n: {i:<3} -> duty({tint*1e3:>13.10f} ms)= {duty:>6.2f}%")
for i in range(points):
duty = i * 100 / points
tint = dimmer.solve_tint_for_duty(duty)
tperc = tint / (dimmer.periode / 2)
print(f"n: {i:<3}; t%: {tperc:>.8f} ->", end=' ')
print(f"duty({tint*1e3:>8.5f} ms)= {duty:>6.2f}%")
dimmer.print_dimmer_state()

ax_frec = plt.axes([0.12, 0.10, 0.80, 0.03])
Expand Down

0 comments on commit de1696f

Please sign in to comment.