Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
*.swp
66 changes: 40 additions & 26 deletions control.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,34 @@

import numpy as np

import dyn_model as dm
import dyn_model as dm

import misc_utils as mu

import math

PWM_freq = 16000
PWM_cycle_time = (1./16000)
PWM_cycle_time = 1.0 / 16000
PWM_duty = 0.6
PWM_duty_time = PWM_cycle_time * PWM_duty

debug = False


#
#
# Sp setpoint, Y output
#
def run_hpwm_l_on_bipol(Sp, Y, t):
elec_angle = mu.norm_angle(Y[dm.ov_theta] * dm.NbPoles/2)
elec_angle = mu.norm_angle(Y[dm.ov_theta] * dm.NbPoles / 2)

U = np.zeros(dm.iv_size)

step = "none"

# switching pattern based on the "encoder"
# H PWM L ON pattern
if 0. <= elec_angle <= (math.pi * (1./6.)): # second half of step 1
if 0.0 <= elec_angle <= (math.pi * (1.0 / 6.0)): # second half of step 1
# U off
# V low
# W hpwm
Expand All @@ -59,7 +60,7 @@ def run_hpwm_l_on_bipol(Sp, Y, t):
hw = 0
lw = 0
step = "1b"
elif (math.pi * (1.0/6.0)) < elec_angle <= (math.pi * (3.0/6.0)): # step 2
elif (math.pi * (1.0 / 6.0)) < elec_angle <= (math.pi * (3.0 / 6.0)): # step 2
# U hpwm
# V low
# W off
Expand All @@ -73,7 +74,7 @@ def run_hpwm_l_on_bipol(Sp, Y, t):
hw = 0
lw = 0
step = "2 "
elif (math.pi * (3.0/6.0)) < elec_angle <= (math.pi * (5.0/6.0)): # step 3
elif (math.pi * (3.0 / 6.0)) < elec_angle <= (math.pi * (5.0 / 6.0)): # step 3
# U hpwm
# V off
# W low
Expand All @@ -87,7 +88,7 @@ def run_hpwm_l_on_bipol(Sp, Y, t):
hw = 0
lw = 1
step = "3 "
elif (math.pi * (5.0/6.0)) < elec_angle <= (math.pi * (7.0/6.0)): # step 4
elif (math.pi * (5.0 / 6.0)) < elec_angle <= (math.pi * (7.0 / 6.0)): # step 4
# U off
# V hpwm
# W low
Expand All @@ -101,7 +102,7 @@ def run_hpwm_l_on_bipol(Sp, Y, t):
hw = 0
lw = 1
step = "4 "
elif (math.pi * (7.0/6.0)) < elec_angle <= (math.pi * (9.0/6.0)): # step 5
elif (math.pi * (7.0 / 6.0)) < elec_angle <= (math.pi * (9.0 / 6.0)): # step 5
# U low
# V hpwm
# W off
Expand All @@ -115,7 +116,7 @@ def run_hpwm_l_on_bipol(Sp, Y, t):
hw = 0
lw = 0
step = "5 "
elif (math.pi * (9.0/6.0)) < elec_angle <= (math.pi * (11.0/6.0)): # step 6
elif (math.pi * (9.0 / 6.0)) < elec_angle <= (math.pi * (11.0 / 6.0)): # step 6
# U low
# V off
# W hpwm
Expand All @@ -129,7 +130,9 @@ def run_hpwm_l_on_bipol(Sp, Y, t):
hw = 0
lw = 0
step = "6 "
elif (math.pi * (11.0/6.0)) < elec_angle <= (math.pi * (12.0/6.0)): # first half of step 1
elif (
(math.pi * (11.0 / 6.0)) < elec_angle <= (math.pi * (12.0 / 6.0))
): # first half of step 1
# U off
# V low
# W hpwm
Expand All @@ -144,7 +147,7 @@ def run_hpwm_l_on_bipol(Sp, Y, t):
lw = 0
step = "1a"
else:
print 'ERROR: The electrical angle is out of range!!!'
print("ERROR: The electrical angle is out of range!!!")

# Assigning the scheme phase values to the simulator phases
# "Connecting the controller wires to the motor" ^^
Expand All @@ -157,32 +160,37 @@ def run_hpwm_l_on_bipol(Sp, Y, t):
U[dm.iv_lw] = lv

if debug:
print 'time {} step {} eangle {} switches {}'.format(t, step, mu.deg_of_rad(elec_angle), U)
print(
"time {} step {} eangle {} switches {}".format(
t, step, mu.deg_of_rad(elec_angle), U
)
)

return U


#
#
# Sp setpoint, Y output
#
def run_hpwm_l_on(Sp, Y, t):
elec_angle = mu.norm_angle(Y[dm.ov_theta] * dm.NbPoles/2)
elec_angle = mu.norm_angle(Y[dm.ov_theta] * dm.NbPoles / 2)

U = np.zeros(dm.iv_size)

step = "none"

# switching pattern based on the "encoder"
# H PWM L ON pattern bipolar
if 0. <= elec_angle <= (math.pi * (1./6.)): # second half of step 1
if 0.0 <= elec_angle <= (math.pi * (1.0 / 6.0)): # second half of step 1
# U off
# V low
# W hpwm
hu = 0
lu = 0
if math.fmod(t, PWM_cycle_time) <= PWM_duty_time:
hw = 1
lw = 0
lw = 0
hv = 0
lv = 1
else:
Expand All @@ -191,24 +199,24 @@ def run_hpwm_l_on(Sp, Y, t):
hv = 1
lv = 0
step = "1b"
elif (math.pi * (1.0/6.0)) < elec_angle <= (math.pi * (3.0/6.0)): # step 2
elif (math.pi * (1.0 / 6.0)) < elec_angle <= (math.pi * (3.0 / 6.0)): # step 2
# U hpwm
# V low
# W off
if math.fmod(t, PWM_cycle_time) <= PWM_duty_time:
hu = 1
lu = 0
lu = 0
hv = 0
lv = 1
else:
hu = 0
lu = 1
lu = 1
hv = 1
lv = 0
hw = 0
lw = 0
step = "2 "
elif (math.pi * (3.0/6.0)) < elec_angle <= (math.pi * (5.0/6.0)): # step 3
elif (math.pi * (3.0 / 6.0)) < elec_angle <= (math.pi * (5.0 / 6.0)): # step 3
# U hpwm
# V off
# W low
Expand All @@ -225,7 +233,7 @@ def run_hpwm_l_on(Sp, Y, t):
hv = 0
lv = 0
step = "3 "
elif (math.pi * (5.0/6.0)) < elec_angle <= (math.pi * (7.0/6.0)): # step 4
elif (math.pi * (5.0 / 6.0)) < elec_angle <= (math.pi * (7.0 / 6.0)): # step 4
# U off
# V hpwm
# W low
Expand All @@ -242,7 +250,7 @@ def run_hpwm_l_on(Sp, Y, t):
hw = 1
lw = 0
step = "4 "
elif (math.pi * (7.0/6.0)) < elec_angle <= (math.pi * (9.0/6.0)): # step 5
elif (math.pi * (7.0 / 6.0)) < elec_angle <= (math.pi * (9.0 / 6.0)): # step 5
# U low
# V hpwm
# W off
Expand All @@ -259,7 +267,7 @@ def run_hpwm_l_on(Sp, Y, t):
hw = 0
lw = 0
step = "5 "
elif (math.pi * (9.0/6.0)) < elec_angle <= (math.pi * (11.0/6.0)): # step 6
elif (math.pi * (9.0 / 6.0)) < elec_angle <= (math.pi * (11.0 / 6.0)): # step 6
# U low
# V off
# W hpwm
Expand All @@ -276,7 +284,9 @@ def run_hpwm_l_on(Sp, Y, t):
hu = 1
lu = 0
step = "6 "
elif (math.pi * (11.0/6.0)) < elec_angle <= (math.pi * (12.0/6.0)): # first half of step 1
elif (
(math.pi * (11.0 / 6.0)) < elec_angle <= (math.pi * (12.0 / 6.0))
): # first half of step 1
# U off
# V low
# W hpwm
Expand All @@ -294,7 +304,7 @@ def run_hpwm_l_on(Sp, Y, t):
lv = 0
step = "1a"
else:
print 'ERROR: The electrical angle is out of range!!!'
print("ERROR: The electrical angle is out of range!!!")

# Assigning the scheme phase values to the simulator phases
# "Connecting the controller wires to the motor" ^^
Expand All @@ -307,7 +317,11 @@ def run_hpwm_l_on(Sp, Y, t):
U[dm.iv_lw] = lv

if debug:
print 'time {} step {} eangle {} switches {}'.format(t, step, mu.deg_of_rad(elec_angle), U)
print(
"time {} step {} eangle {} switches {}".format(
t, step, mu.deg_of_rad(elec_angle), U
)
)

return U

Expand All @@ -317,5 +331,5 @@ def run_hpwm_l_on(Sp, Y, t):
# Sp setpoint, Y output
#
def run(Sp, Y, t):
#return run_hpwm_l_on(Sp, Y, t)
# return run_hpwm_l_on(Sp, Y, t)
return run_hpwm_l_on_bipol(Sp, Y, t)
Loading