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

Update gem controllers #259

Merged
merged 3 commits into from
Nov 15, 2024
Merged
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
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
Loading