Skip to content

Commit

Permalink
Some last changes examples and added sb3 DDPG PMSM example
Browse files Browse the repository at this point in the history
  • Loading branch information
bhk11 committed Nov 7, 2024
1 parent 5747506 commit cfccc20
Show file tree
Hide file tree
Showing 11 changed files with 715 additions and 1,660 deletions.
14 changes: 12 additions & 2 deletions examples/environment_features/external_speed_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import gym_electric_motor as gem
from gym_electric_motor import reference_generators as rg
from gym_electric_motor.visualization import MotorDashboard
from gym_electric_motor.envs.motors import ActionType, ControlType, Motor, MotorType
from gym_electric_motor.physical_systems.solvers import ScipySolveIvpSolver

import time
from scipy import signal
Expand Down Expand Up @@ -67,11 +69,19 @@
# inital value is given by bias of the profile
sampling_time = 1e-4

#define the motor env object
motor = Motor(
motor_type=MotorType.SeriesDc,
control_type=ControlType.CurrentControl,
action_type=ActionType.Continuous,
)


if __name__ == "__main__":
# Create the environment
env = gem.make(
"Cont-CC-SeriesDc-v0",
ode_solver="scipy.solve_ivp",
motor.env_id(),
ode_solver=ScipySolveIvpSolver(),
tau=sampling_time,
reference_generator=const_switch_gen,
visualization=MotorDashboard(state_plots=["omega", "i"], reward_plot=True),
Expand Down
9 changes: 6 additions & 3 deletions examples/environment_features/scim_ideal_grid_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import numpy as np
import gym_electric_motor as gem
from gym_electric_motor.envs.motors import ActionType, ControlType, Motor, MotorType
from gym_electric_motor.physical_systems.solvers import ScipyOdeSolver
import matplotlib.pyplot as plt


Expand All @@ -31,17 +33,18 @@ def grid_voltage(t):

return grid_voltage

motor = Motor(motor_type=MotorType.SquirrelCageInductionMotor, control_type=ControlType.CurrentControl, action_type=ActionType.Continuous)

# Create the environment
env = gem.make(
# Choose the squirrel cage induction motor (SCIM) with continuous-control-set
"Cont-CC-SCIM-v0",
motor.env_id(),
#
load=gem.physical_systems.PolynomialStaticLoad(
dict(a=0.0, b=0.0, c=0.0, j_load=1e-6)
),
# Define the numerical solver for the simulation
ode_solver="scipy.ode",
ode_solver=ScipyOdeSolver(),
# Define which state variables are to be monitored concerning limit violations
# "()" means, that limit violation will not necessitate an env.reset()
constraints=(),
Expand All @@ -53,7 +56,7 @@ def grid_voltage(t):
limits = env.physical_system.limits

# reset the environment such that the simulation can be started
(state, reference) = env.reset()
(state, reference),_ = env.reset()

# We define these arrays in order to save our simulation results in them
# Initial state and initial time are directly inserted
Expand Down
10 changes: 7 additions & 3 deletions examples/environment_features/userdefined_initialization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import gym_electric_motor as gem
from gym_electric_motor import reference_generators as rg
from gym_electric_motor.visualization import MotorDashboard
from gym_electric_motor.envs.motors import ActionType, ControlType, Motor, MotorType
from gym_electric_motor.physical_systems.solvers import ScipySolveIvpSolver
import time

"""
Expand Down Expand Up @@ -39,15 +41,17 @@
# initializer for a specific speed
load_init = {"states": {"omega": 20}}

motor = Motor(motor_type=MotorType.SeriesDc, control_type=ControlType.CurrentControl, action_type=ActionType.Continuous)

if __name__ == "__main__":
env = gem.make(
"Cont-CC-SeriesDc-v0",
motor.env_id(),
visualization=MotorDashboard(state_plots=["omega", "i"]),
motor=dict(
motor_parameter=dict(j_rotor=0.001), motor_initializer=gaussian_init
),
load=dict(j_load=0.001, load_initializer=uniform_init),
ode_solver="scipy.solve_ivp",
ode_solver=ScipySolveIvpSolver(),
reference_generator=rg.SwitchedReferenceGenerator(
sub_generators=[
rg.SinusoidalReferenceGenerator(reference_state="omega"),
Expand All @@ -63,7 +67,7 @@
cum_rew = 0

for j in range(10):
state, reference = env.reset()
(state, reference), _ = env.reset()

# Print the initial states:
denorm_state = state * env.limits
Expand Down
209 changes: 14 additions & 195 deletions examples/model_predictive_controllers/pmsm_mpc_dq_current_control.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/observers/state_observer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from classic_controllers import Controller # noqa: E402
from externally_referenced_state_plot import ExternallyReferencedStatePlot # noqa: E402


if __name__ == "__main__":
"""
motor type: 'PermExDc' Permanently Excited DC Motor
Expand All @@ -27,7 +28,7 @@
action_type: 'Cont' Continuous Action Space
'Finite' Discrete Action Space
"""

motor = Motor(
MotorType.PermanentlyExcitedDcMotor,
ControlType.SpeedControl,
Expand Down Expand Up @@ -66,7 +67,6 @@
"""
controller = Controller.make(env, external_ref_plots=external_ref_plots)
# controller = GemController.make(env, env_id=motor.env_id())

(state, reference), _ = env.reset(seed=1337)
print("state_names: ", motor.states())
Expand Down
Loading

0 comments on commit cfccc20

Please sign in to comment.