Skip to content

Commit

Permalink
Merge pull request #259 from upb-lea/update_gem_controllers
Browse files Browse the repository at this point in the history
Update gem controllers
  • Loading branch information
XyDrKRulof authored Nov 15, 2024
2 parents 7b778cc + acdc6b1 commit 5590044
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Dropped support for Python 3.8
- Linted and formatted all files
- Changed max. steps in some test files to improve test speed by 30%
- Changed the syntax from gem_controller.py to be compatible with the gymnasium interface
## Fixed
- #244 Sphinx docu build
- #233 EESM ODE update
Expand Down
37 changes: 37 additions & 0 deletions examples/gem_controllers/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import gym_electric_motor as gem
import gem_controllers as gc
from gym_electric_motor.physical_system_wrappers import FluxObserver



if __name__ == '__main__':

# choose the action space
action_space = 'Cont' # 'Cont' or 'Finite'

# choose the control task
control_task = 'CC' # 'SC' (speed control), 'TC' (torque control) or 'CC' (current control)

# chosse the motor type
motor_type = 'PMSM' # 'PermExDc', 'ExtExDc', 'SeriesDc', 'ShuntDc', 'PMSM', 'EESM', 'SynRM' or 'SCIM'

env_id = action_space + '-' + control_task + '-' + motor_type + '-v0'

# using a flux observer for the SCIM
physical_system_wrappers = (FluxObserver(),) if motor_type == 'SCIM' else ()

# Initilize the environment
env = gem.make(env_id, physical_system_wrappers=physical_system_wrappers)

# Initialize the controller
c = gc.GemController.make(
env,
env_id,
a=8,
block_diagram=True,
current_safety_margin=0.25,
save_block_diagram_as=(),
)

# Control the environment
c.control_environment(env, n_steps=30000, render_env=True, max_episode_length=10000)
8 changes: 4 additions & 4 deletions src/gem_controllers/gem_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def control_environment(self, env, n_steps, max_episode_length=np.inf, render_en
render_env(bool): Flag, if the states of the environment should be plotted.
"""

state, reference = env.reset()
(state, reference), _ = env.reset()
if self.block_diagram:
self.block_diagram.open()
self.reset()
Expand All @@ -162,10 +162,10 @@ def control_environment(self, env, n_steps, max_episode_length=np.inf, render_en
if render_env:
env.render() # Plot the states
action = self.control(state, reference) # Calculate the action
(state, reference), _, done, _ = env.step(action) # Simulate one step of the environment
if done or current_episode_length >= max_episode_length:
(state, reference), _, trun, done, _ = env.step(action) # Simulate one step of the environment
if done or trun or current_episode_length >= max_episode_length:
# Reset the environment and controller
state, reference = env.reset()
(state, reference), _ = env.reset()
self.reset()
current_episode_length = 0
current_episode_length = current_episode_length + 1
Expand Down

0 comments on commit 5590044

Please sign in to comment.