Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test for classic controllers, dc motor, dcExternallyExcited motor #261

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
156 changes: 78 additions & 78 deletions examples/classic_controllers/classic_controllers.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@
env.reset()
controller.reset()

motor_dashboard.show_and_hold()
motor_dashboard.show()
env.close()
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@
if terminated:
env.reset()
controller.reset()
motor_dashboard.show_and_hold()
motor_dashboard.show()
env.close()
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@
env.reset()
controller.reset()

motor_dashboard.show_and_hold()
motor_dashboard.show()
env.close()
30 changes: 15 additions & 15 deletions examples/classic_controllers/controllers/cascaded_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ def __init__(
):
self.env = environment
self.visualization = visualization
self.action_space = environment.action_space
self.state_space = environment.unwrapped.physical_system.state_space
self.state_names = environment.unwrapped.state_names

self.i_e_idx = environment.unwrapped.physical_system.CURRENTS_IDX[-1]
self.i_a_idx = environment.unwrapped.physical_system.CURRENTS_IDX[0]
self.u_idx = environment.unwrapped.physical_system.VOLTAGES_IDX[-1]
self.omega_idx = environment.unwrapped.state_names.index("omega")
self.torque_idx = environment.unwrapped.state_names.index("torque")
self.action_space = environment.get_wrapper_attr('action_space')
self.state_space = environment.get_wrapper_attr('physical_system').state_space
self.state_names = environment.get_wrapper_attr('state_names')

self.i_e_idx = environment.get_wrapper_attr('physical_system').CURRENTS_IDX[-1]
self.i_a_idx = environment.get_wrapper_attr('physical_system').CURRENTS_IDX[0]
self.u_idx = environment.get_wrapper_attr('physical_system').VOLTAGES_IDX[-1]
self.omega_idx = environment.get_wrapper_attr('state_names').index("omega")
self.torque_idx = environment.get_wrapper_attr('state_names').index("torque")
self.ref_idx = np.where(ref_states != "i_e")[0][0]
self.ref_state_idx = [
self.i_a_idx,
environment.unwrapped.state_names.index(ref_states[self.ref_idx]),
environment.get_wrapper_attr('state_names').index(ref_states[self.ref_idx]),
]

self.limit = environment.unwrapped.physical_system.limits[environment.unwrapped.state_filter]
self.nominal_values = environment.unwrapped.physical_system.nominal_state[environment.unwrapped.state_filter]
self.limit = environment.get_wrapper_attr('physical_system').limits[environment.get_wrapper_attr('state_filter')]
self.nominal_values = environment.get_wrapper_attr('physical_system').nominal_state[environment.get_wrapper_attr('state_filter')]

self.control_e = isinstance(environment.unwrapped.physical_system.electrical_motor, DcExternallyExcitedMotor)
self.control_e = isinstance(environment.get_wrapper_attr('physical_system').electrical_motor, DcExternallyExcitedMotor)
self.control_omega = 0

mp = environment.unwrapped.physical_system.electrical_motor.motor_parameter
mp = environment.get_wrapper_attr('physical_system').electrical_motor.motor_parameter
self.psi_e = mp.get("psie_e", False)
self.l_e = mp.get("l_e_prime", False)
self.r_e = mp.get("r_e", None)
Expand Down Expand Up @@ -110,7 +110,7 @@ def __init__(

# Set up the plots
self.external_ref_plots = external_ref_plots
internal_refs = np.array([environment.unwrapped.state_names[i] for i in self.ref_state_idx])
internal_refs = np.array([environment.get_wrapper_attr('state_names')[i] for i in self.ref_state_idx])
ref_states_plotted = np.unique(np.append(ref_states, internal_refs))
for external_plots in self.external_ref_plots:
external_plots.set_reference(ref_states_plotted)
Expand Down
46 changes: 23 additions & 23 deletions examples/classic_controllers/controllers/cascaded_foc_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ def __init__(
torque_control="interpolate",
**controller_kwargs,
):
t32 = environment.physical_system.electrical_motor.t_32
q = environment.physical_system.electrical_motor.q
t32 = environment.get_wrapper_attr('physical_system').electrical_motor.t_32
q = environment.get_wrapper_attr('physical_system').electrical_motor.q
self.backward_transformation = lambda quantities, eps: t32(q(quantities, eps))
self.tau = environment.physical_system.tau

self.action_space = environment.action_space
self.state_space = environment.physical_system.state_space
self.state_names = environment.state_names

self.i_sd_idx = environment.state_names.index("i_sd")
self.i_sq_idx = environment.state_names.index("i_sq")
self.u_sd_idx = environment.state_names.index("u_sd")
self.u_sq_idx = environment.state_names.index("u_sq")
self.u_a_idx = environment.state_names.index("u_a")
self.u_b_idx = environment.state_names.index("u_b")
self.u_c_idx = environment.state_names.index("u_c")
self.omega_idx = environment.state_names.index("omega")
self.eps_idx = environment.state_names.index("epsilon")
self.torque_idx = environment.state_names.index("torque")
self.tau = environment.get_wrapper_attr('physical_system').tau

self.action_space = environment.get_wrapper_attr('action_space')
self.state_space = environment.get_wrapper_attr('physical_system').state_space
self.state_names = environment.get_wrapper_attr('state_names')

self.i_sd_idx = environment.get_wrapper_attr('state_names').index("i_sd")
self.i_sq_idx = environment.get_wrapper_attr('state_names').index("i_sq")
self.u_sd_idx = environment.get_wrapper_attr('state_names').index("u_sd")
self.u_sq_idx = environment.get_wrapper_attr('state_names').index("u_sq")
self.u_a_idx = environment.get_wrapper_attr('state_names').index("u_a")
self.u_b_idx = environment.get_wrapper_attr('state_names').index("u_b")
self.u_c_idx = environment.get_wrapper_attr('state_names').index("u_c")
self.omega_idx = environment.get_wrapper_attr('state_names').index("omega")
self.eps_idx = environment.get_wrapper_attr('state_names').index("epsilon")
self.torque_idx = environment.get_wrapper_attr('state_names').index("torque")
self.external_ref_plots = external_ref_plots

self.torque_control = "torque" in ref_states or "omega" in ref_states
Expand All @@ -59,10 +59,10 @@ def __init__(
self.omega_control = "omega" in ref_states and type(environment)
self.has_cont_action_space = type(self.action_space) is Box

self.limit = environment.physical_system.limits
self.nominal_values = environment.physical_system.nominal_state
self.limit = environment.get_wrapper_attr('physical_system').limits
self.nominal_values = environment.get_wrapper_attr('physical_system').nominal_state

self.mp = environment.physical_system.electrical_motor.motor_parameter
self.mp = environment.get_wrapper_attr('physical_system').electrical_motor.motor_parameter
self.psi_p = self.mp.get("psi_p", 0)
self.dead_time = 0.5
self.decoupling = controller_kwargs.get("decoupling", True)
Expand Down Expand Up @@ -136,7 +136,7 @@ def __init__(
for i in range(3)
]
self.i_abc_idx = [
environment.state_names.index(state) for state in ["i_a", "i_b", "i_c"]
environment.get_wrapper_attr('state_names').index(state) for state in ["i_a", "i_b", "i_c"]
]

self.ref = np.zeros(
Expand All @@ -145,7 +145,7 @@ def __init__(

# Set up the plots
plot_ref = np.append(
np.array([environment.state_names[i] for i in self.ref_state_idx]),
np.array([environment.get_wrapper_attr('state_names')[i] for i in self.ref_state_idx]),
ref_states,
)
for ext_ref_plot in self.external_ref_plots:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ def __init__(
external_ref_plots=(),
**controller_kwargs,
):
assert type(environment.action_space) is Box and isinstance(
environment.physical_system, DcMotorSystem
assert type(environment.get_wrapper_attr('action_space')) is Box and isinstance(
environment.get_wrapper_attr('physical_system'), DcMotorSystem
), "No suitable action space for Continuous Action Controller"
self.action_space = environment.action_space
self.state_names = environment.state_names
self.action_space = environment.get_wrapper_attr('action_space')
self.state_names = environment.get_wrapper_attr('state_names')

self.ref_idx = np.where(ref_states != "i_e")[0][0]
self.ref_state_idx = environment.state_names.index(ref_states[self.ref_idx])
self.i_idx = environment.physical_system.CURRENTS_IDX[-1]
self.u_idx = environment.physical_system.VOLTAGES_IDX[-1]
self.limit = environment.physical_system.limits[environment.state_filter]
self.nominal_values = environment.physical_system.nominal_state[
environment.state_filter
self.ref_state_idx = environment.get_wrapper_attr('state_names').index(ref_states[self.ref_idx])
self.i_idx = environment.get_wrapper_attr('physical_system').CURRENTS_IDX[-1]
self.u_idx = environment.get_wrapper_attr('physical_system').VOLTAGES_IDX[-1]
self.limit = environment.get_wrapper_attr('physical_system').limits[environment.get_wrapper_attr('state_filter')]
self.nominal_values = environment.get_wrapper_attr('physical_system').nominal_state[
environment.get_wrapper_attr('state_filter')
]
self.omega_idx = self.state_names.index("omega")

self.action = np.zeros(self.action_space.shape[0])
self.control_e = isinstance(
environment.physical_system.electrical_motor, DcExternallyExcitedMotor
environment.get_wrapper_attr('physical_system').electrical_motor, DcExternallyExcitedMotor
)

mp = environment.physical_system.electrical_motor.motor_parameter
mp = environment.get_wrapper_attr('physical_system').electrical_motor.motor_parameter
self.psi_e = mp.get("psi_e", None)
self.l_e = mp.get("l_e_prime", None)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ def __init__(
external_ref_plots=(),
**controller_kwargs,
):
assert type(environment.action_space) in [
assert type(environment.get_wrapper_attr('action_space')) in [
Discrete,
MultiDiscrete,
] and isinstance(
environment.physical_system, DcMotorSystem
environment.get_wrapper_attr('physical_system'), DcMotorSystem
), "No suitable action space for Discrete Action Controller"

self.ref_idx = np.where(ref_states != "i_e")[0][0]
self.ref_state_idx = environment.state_names.index(ref_states[self.ref_idx])
self.i_idx = environment.physical_system.CURRENTS_IDX[-1]
self.ref_state_idx = environment.get_wrapper_attr('state_names').index(ref_states[self.ref_idx])
self.i_idx = environment.get_wrapper_attr('physical_system').CURRENTS_IDX[-1]
self.control_e = isinstance(
environment.physical_system.electrical_motor, DcExternallyExcitedMotor
environment.get_wrapper_attr('physical_system').electrical_motor, DcExternallyExcitedMotor
)
self.state_names = environment.state_names
self.state_names = environment.get_wrapper_attr('state_names')

self.external_ref_plots = external_ref_plots
for ext_ref_plot in self.external_ref_plots:
Expand Down
22 changes: 11 additions & 11 deletions examples/classic_controllers/controllers/flux_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ class FluxObserver:
"""

def __init__(self, env):
mp = env.physical_system.electrical_motor.motor_parameter
mp = env.get_wrapper_attr('physical_system').electrical_motor.motor_parameter
self.l_m = mp["l_m"] # Main induction
self.l_r = mp["l_m"] + mp["l_sigr"] # Induction of the rotor
self.r_r = mp["r_r"] # Rotor resistance
self.p = mp["p"] # Pole pair number
self.tau = env.physical_system.tau # Sampling time
self.tau = env.get_wrapper_attr('physical_system').tau # Sampling time

# function to transform the currents from abc to alpha/beta coordinates
self.abc_to_alphabeta_transformation = (
env.physical_system.abc_to_alphabeta_space
env.get_wrapper_attr('physical_system').abc_to_alphabeta_space
)

# Integrated values of the flux for the two directions (Re: alpha, Im: beta)
self.integrated = np.complex(0, 0)
self.integrated = complex(0, 0)
self.i_s_idx = [
env.state_names.index("i_sa"),
env.state_names.index("i_sb"),
env.state_names.index("i_sc"),
env.get_wrapper_attr('state_names').index("i_sa"),
env.get_wrapper_attr('state_names').index("i_sb"),
env.get_wrapper_attr('state_names').index("i_sc"),
]
self.omega_idx = env.state_names.index("omega")
self.omega_idx = env.get_wrapper_attr('state_names').index("omega")

def estimate(self, state):
"""
Expand All @@ -47,9 +47,9 @@ def estimate(self, state):
[i_s_alpha, i_s_beta] = self.abc_to_alphabeta_transformation(i_s)

# Calculate delta flux
delta = np.complex(
delta = complex(
i_s_alpha, i_s_beta
) * self.r_r * self.l_m / self.l_r - self.integrated * np.complex(
) * self.r_r * self.l_m / self.l_r - self.integrated * complex(
self.r_r / self.l_r, -omega
)

Expand All @@ -59,4 +59,4 @@ def estimate(self, state):

def reset(self):
# Reset the integrated value
self.integrated = np.complex(0, 0)
self.integrated = complex(0, 0)
50 changes: 25 additions & 25 deletions examples/classic_controllers/controllers/foc_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,40 @@ def __init__(
**controller_kwargs,
):
assert isinstance(
environment.physical_system, SynchronousMotorSystem
environment.get_wrapper_attr('physical_system'), SynchronousMotorSystem
), "No suitable Environment for FOC Controller"

t32 = environment.physical_system.electrical_motor.t_32
q = environment.physical_system.electrical_motor.q
t32 = environment.get_wrapper_attr('physical_system').electrical_motor.t_32
q = environment.get_wrapper_attr('physical_system').electrical_motor.q
self.backward_transformation = lambda quantities, eps: t32(q(quantities, eps))

self.tau = environment.physical_system.tau
self.tau = environment.get_wrapper_attr('physical_system').tau

self.ref_d_idx = np.where(ref_states == "i_sd")[0][0]
self.ref_q_idx = np.where(ref_states == "i_sq")[0][0]

self.d_idx = environment.state_names.index(ref_states[self.ref_d_idx])
self.q_idx = environment.state_names.index(ref_states[self.ref_q_idx])

self.action_space = environment.action_space
self.state_space = environment.physical_system.state_space
self.state_names = environment.state_names

self.i_sd_idx = environment.state_names.index("i_sd")
self.i_sq_idx = environment.state_names.index("i_sq")
self.u_sd_idx = environment.state_names.index("u_sd")
self.u_sq_idx = environment.state_names.index("u_sq")
self.u_a_idx = environment.state_names.index("u_a")
self.u_b_idx = environment.state_names.index("u_b")
self.u_c_idx = environment.state_names.index("u_c")
self.omega_idx = environment.state_names.index("omega")
self.eps_idx = environment.state_names.index("epsilon")

self.limit = environment.physical_system.limits
self.mp = environment.physical_system.electrical_motor.motor_parameter
self.d_idx = environment.get_wrapper_attr('state_names').index(ref_states[self.ref_d_idx])
self.q_idx = environment.get_wrapper_attr('state_names').index(ref_states[self.ref_q_idx])

self.action_space = environment.get_wrapper_attr('action_space')
self.state_space = environment.get_wrapper_attr('physical_system').state_space
self.state_names = environment.get_wrapper_attr('state_names')

self.i_sd_idx = environment.get_wrapper_attr('state_names').index("i_sd")
self.i_sq_idx = environment.get_wrapper_attr('state_names').index("i_sq")
self.u_sd_idx = environment.get_wrapper_attr('state_names').index("u_sd")
self.u_sq_idx = environment.get_wrapper_attr('state_names').index("u_sq")
self.u_a_idx = environment.get_wrapper_attr('state_names').index("u_a")
self.u_b_idx = environment.get_wrapper_attr('state_names').index("u_b")
self.u_c_idx = environment.get_wrapper_attr('state_names').index("u_c")
self.omega_idx = environment.get_wrapper_attr('state_names').index("omega")
self.eps_idx = environment.get_wrapper_attr('state_names').index("epsilon")

self.limit = environment.get_wrapper_attr('physical_system').limits
self.mp = environment.get_wrapper_attr('physical_system').electrical_motor.motor_parameter
self.psi_p = self.mp.get("psi_p", 0)
self.dead_time = (
1.5 if environment.physical_system.converter._dead_time else 0.5
1.5 if environment.get_wrapper_attr('physical_system').converter._dead_time else 0.5
)

self.has_cont_action_space = type(self.action_space) is Box
Expand Down Expand Up @@ -89,7 +89,7 @@ def __init__(
for i in range(3)
]
self.i_abc_idx = [
environment.state_names.index(state) for state in ["i_a", "i_b", "i_c"]
environment.get_wrapper_attr('state_names').index(state) for state in ["i_a", "i_b", "i_c"]
]

def control(self, state, reference):
Expand Down
Loading
Loading