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 5 commits into
base: master
Choose a base branch
from
Draft
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
92 changes: 46 additions & 46 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()
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.unwrapped.physical_system.electrical_motor.t_32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, that the test run trough successful!
Personally, I would prefer to unify the code and use get_wrapper_attr('physical_system') instead of unwrapped.physical_system .
In my opinion this getter should also work for the action_space, state_names and so on.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
This proposed change has been made and will be pushed with the commit message ""change to get_wrapper_attr()""

q = environment.unwrapped.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.unwrapped.physical_system.tau

self.action_space = environment.unwrapped.action_space
self.state_space = environment.unwrapped.physical_system.state_space
self.state_names = environment.unwrapped.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.unwrapped.physical_system.limits
self.nominal_values = environment.unwrapped.physical_system.nominal_state

self.mp = environment.physical_system.electrical_motor.motor_parameter
self.mp = environment.unwrapped.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.unwrapped.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.unwrapped.state_names[i] for i in self.ref_state_idx]),
ref_states,
)
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.unwrapped.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.unwrapped.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.unwrapped.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)
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ def __init__(
**controller_kwargs,
):
self.env = environment
self.action_space = environment.action_space
self.action_space = environment.unwrapped.action_space
self.has_cont_action_space = type(self.action_space) is Box
self.state_space = environment.physical_system.state_space
self.state_names = environment.state_names
self.state_space = environment.unwrapped.physical_system.state_space
self.state_names = environment.get_wrapper_attr('state_names')

self.stages = stages
self.flux_observer = FluxObserver(self.env)
self.i_sd_idx = self.env.state_names.index("i_sd")
self.i_sq_idx = self.env.state_names.index("i_sq")
self.i_sd_idx = self.env.get_wrapper_attr('state_names').index("i_sd")
self.i_sq_idx = self.env.get_wrapper_attr('state_names').index("i_sq")
self.u_s_abc_idx = [
self.env.state_names.index(state) for state in ["u_sa", "u_sb", "u_sc"]
self.env.get_wrapper_attr('state_names').index(state) for state in ["u_sa", "u_sb", "u_sc"]
]
self.omega_idx = self.env.state_names.index("omega")
self.torque_idx = self.env.state_names.index("torque")
self.omega_idx = self.env.get_wrapper_attr('state_names').index("omega")
self.torque_idx = self.env.get_wrapper_attr('state_names').index("torque")

mp = self.env.physical_system.electrical_motor.motor_parameter
mp = self.env.unwrapped.physical_system.electrical_motor.motor_parameter
self.p = mp["p"]
self.l_m = mp["l_m"]
self.l_sigma_s = mp["l_sigs"]
Expand All @@ -53,14 +53,14 @@ def __init__(
self.r_s = mp["r_s"]
self.tau_r = self.l_r / self.r_r
self.sigma = (self.l_s * self.l_r - self.l_m**2) / (self.l_s * self.l_r)
self.limits = self.env.physical_system.limits
self.nominal_values = self.env.physical_system.nominal_state
self.limits = self.env.unwrapped.physical_system.limits
self.nominal_values = self.env.unwrapped.physical_system.nominal_state
self.tau_sigma = (
self.sigma * self.l_s / (self.r_s + self.r_r * self.l_m**2 / self.l_r**2)
)
self.tau = self.env.physical_system.tau
self.tau = self.env.unwrapped.physical_system.tau

self.dq_to_abc_transformation = environment.physical_system.dq_to_abc_space
self.dq_to_abc_transformation = environment.unwrapped.physical_system.dq_to_abc_space

self.torque_control = "torque" in ref_states or "omega" in ref_states
self.current_control = "i_sd" in ref_states
Expand Down Expand Up @@ -89,7 +89,7 @@ def __init__(
self.external_ref_plots = external_ref_plots
self.external_ref_plots = external_ref_plots
plot_ref = np.append(
np.array([environment.state_names[i] for i in self.ref_state_idx]),
np.array([environment.unwrapped.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 @@ -24,28 +24,28 @@ def __init__(
update_interval=1000,
):
self.env = environment
self.nominal_values = self.env.physical_system.nominal_state
self.state_space = self.env.physical_system.state_space
self.nominal_values = self.env.unwrapped.physical_system.nominal_state
self.state_space = self.env.unwrapped.physical_system.state_space

# Calculate parameters of the motor
mp = self.env.physical_system.electrical_motor.motor_parameter
mp = self.env.unwrapped.physical_system.electrical_motor.motor_parameter
self.l_m = mp["l_m"]
self.l_r = self.l_m + mp["l_sigr"]
self.l_s = self.l_m + mp["l_sigs"]
self.r_r = mp["r_r"]
self.r_s = mp["r_s"]
self.p = mp["p"]
self.tau = self.env.physical_system.tau
self.tau = self.env.unwrapped.physical_system.tau
tau_s = self.l_s / self.r_s

self.i_sd_idx = self.env.state_names.index("i_sd")
self.i_sq_idx = self.env.state_names.index("i_sq")
self.torque_idx = self.env.state_names.index("torque")
self.u_sa_idx = environment.state_names.index("u_sa")
self.u_sd_idx = environment.state_names.index("u_sd")
self.u_sq_idx = environment.state_names.index("u_sq")
self.omega_idx = environment.state_names.index("omega")
self.limits = self.env.physical_system.limits
self.i_sd_idx = self.env.get_wrapper_attr('state_names').index("i_sd")
self.i_sq_idx = self.env.get_wrapper_attr('state_names').index("i_sq")
self.torque_idx = self.env.get_wrapper_attr('state_names').index("torque")
self.u_sa_idx = environment.get_wrapper_attr('state_names').index("u_sa")
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.omega_idx = environment.get_wrapper_attr('state_names').index("omega")
self.limits = self.env.unwrapped.physical_system.limits

p_gain = stages[0][1]["p_gain"] * 2 * tau_s**2 # flux controller p gain
i_gain = p_gain / self.tau # flux controller i gain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ def __init__(
update_interval=1000,
torque_control="interpolate",
):
self.mp = environment.physical_system.electrical_motor.motor_parameter
self.limit = environment.physical_system.limits
self.nominal_values = environment.physical_system.nominal_state
self.mp = environment.unwrapped.physical_system.electrical_motor.motor_parameter
self.limit = environment.unwrapped.physical_system.limits
self.nominal_values = environment.unwrapped.physical_system.nominal_state
self.torque_control = torque_control

self.l_d = self.mp["l_d"]
self.l_q = self.mp["l_q"]
self.p = self.mp["p"]
self.psi_p = self.mp.get("psi_p", 0)
self.invert = -1 if (self.psi_p == 0 and self.l_q < self.l_d) else 1
self.tau = environment.physical_system.tau
self.tau = environment.unwrapped.physical_system.tau

self.omega_idx = environment.state_names.index("omega")
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.torque_idx = environment.state_names.index("torque")
self.epsilon_idx = environment.state_names.index("epsilon")
self.omega_idx = environment.get_wrapper_attr('state_names').index("omega")
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.torque_idx = environment.get_wrapper_attr('state_names').index("torque")
self.epsilon_idx = environment.get_wrapper_attr('state_names').index("epsilon")

self.a_max = 2 / np.sqrt(3) # maximum modulation level
self.k_ = 0.95
Expand All @@ -56,7 +56,7 @@ def __init__(
1 / (self.mp["l_q"] / (1.25 * self.mp["r_s"])) * (alpha - 1) / alpha**2
)

self.u_a_idx = environment.state_names.index("u_a")
self.u_a_idx = environment.get_wrapper_attr('state_names').index("u_a")
self.u_dc = np.sqrt(3) * self.limit[self.u_a_idx]
self.limited = False
self.integrated = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,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 @@ -78,5 +78,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 @@ -83,4 +83,4 @@
env.close()

# motor_dashboard.save_to_file(filename="integration_test")
motor_dashboard.show_and_hold()
motor_dashboard.show()
28 changes: 28 additions & 0 deletions tests/test_controllers_examples/test_classic_controllers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import subprocess

import pytest

file_names = ["classic_controllers_dc_motor_example.py",
"classic_controllers_ind_motor_example.py",
"classic_controllers_synch_motor_example.py",
"custom_classic_controllers_dc_motor_example.py" ,
"custom_classic_controllers_ind_motor_example.py",
"custom_classic_controllers_synch_motor_example.py",
"integration_test_classic_controllers_dc_motor.py"
]

@pytest.mark.parametrize("file_name", file_names)
def test_run_classic_controllers(file_name):
# Run the script and capture the output
directory = "examples"
subdirectory = "classic_controllers"
result = subprocess.run(["python", os.path.join(directory, subdirectory, file_name)], capture_output=True, text=True)

# Check if the script ran successfully (exit code 0)
assert result.returncode == 0, file_name + " did not exit successfully"





Loading
Loading