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

DqToAbcActionProcessor and CosSinProcessor fixes #245

Merged
merged 3 commits into from
Sep 2, 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
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ def _get_cos_sin(self, state):
return np.concatenate(
(
state,
[np.cos(state[self._angle_index]), np.sin(state[self._angle_index])],
[np.cos(state[self._angle_index]*np.pi), np.sin(state[self._angle_index]*np.pi)],
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from .physical_system_wrapper import PhysicalSystemWrapper


class DqToAbcActionProcessor(PhysicalSystemWrapper):
"""The DqToAbcActionProcessor converts an inner system with an AC motor and actions in abc coordinates to a
system to which actions in the dq-coordinate system can be applied.
Expand All @@ -31,12 +30,11 @@ def register_transformation(cls, motor_types):
def wrapper(callable_):
for motor_type in motor_types:
cls._registry[motor_type] = callable_

return wrapper

@classmethod
def make(cls, motor_type, *args, **kwargs):
assert motor_type in cls._registry.keys(), f"Not supported motor_type {motor_type}."
assert motor_type in cls._registry.keys(), f'Not supported motor_type {motor_type}.'
class_ = cls._registry[motor_type]
inst = class_(*args, **kwargs)
return inst
Expand All @@ -58,21 +56,20 @@ def __init__(self, angle_name, physical_system=None):

def set_physical_system(self, physical_system):
# Docstring of super class
assert isinstance(
physical_system.electrical_motor, ps.ThreePhaseMotor
), "The motor in the system has to derive from the ThreePhaseMotor to define transformations."
assert isinstance(physical_system.electrical_motor, ps.ThreePhaseMotor), \
'The motor in the system has to derive from the ThreePhaseMotor to define transformations.'
super().set_physical_system(physical_system)
self._omega_index = physical_system.state_names.index("omega")
self._omega_index = physical_system.state_names.index('omega')
self._angle_index = physical_system.state_names.index(self._angle_name)
assert self._angle_name in physical_system.state_names, (
f"Angle {self._angle_name} not in the states of the physical system. "
f"Probably a flux observer is required."
)
self._pole_pair_number = physical_system.electrical_motor.motor_parameter['p']
assert self._angle_name in physical_system.state_names, \
f'Angle {self._angle_name} not in the states of the physical system. ' \
f'Probably a flux observer is required.'

self._angle_advance = 0.5

# If dead time has been added to the system increase the angle advance by the amount of dead time steps
if hasattr(physical_system, "dead_time"):
if hasattr(physical_system, 'dead_time'):
self._angle_advance += physical_system.dead_time

return self
Expand All @@ -87,10 +84,12 @@ def reset(self, **kwargs):
return normalized_state

def _advance_angle(self, state):
return state[self._angle_index] + self._angle_advance * self._physical_system.tau * state[self._omega_index]
return state[self._angle_index] + self._angle_advance \
* self._physical_system.tau * state[self._omega_index] * self._pole_pair_number


class _ClassicDqToAbcActionProcessor(DqToAbcActionProcessor):

@property
def action_space(self):
return gymnasium.spaces.Box(-1, 1, shape=(2,), dtype=np.float64)
Expand All @@ -104,24 +103,25 @@ def simulate(self, action):
return normalized_state


DqToAbcActionProcessor.register_transformation(["PMSM"])(
lambda angle_name="epsilon", *args, **kwargs: _ClassicDqToAbcActionProcessor(angle_name, *args, **kwargs)
DqToAbcActionProcessor.register_transformation(['PMSM'])(
lambda angle_name='epsilon', *args, **kwargs: _ClassicDqToAbcActionProcessor(angle_name, *args, **kwargs)
)

DqToAbcActionProcessor.register_transformation(["SCIM"])(
lambda angle_name="psi_angle", *args, **kwargs: _ClassicDqToAbcActionProcessor(angle_name, *args, **kwargs)
DqToAbcActionProcessor.register_transformation(['SCIM'])(
lambda angle_name='psi_angle', *args, **kwargs: _ClassicDqToAbcActionProcessor(angle_name, *args, **kwargs)
)


@DqToAbcActionProcessor.register_transformation(["DFIM"])
@DqToAbcActionProcessor.register_transformation(['DFIM'])
class _DFIMDqToAbcActionProcessor(DqToAbcActionProcessor):

@property
def action_space(self):
return gymnasium.spaces.Box(-1, 1, shape=(4,))

def __init__(self, physical_system=None):
super().__init__("epsilon", physical_system=physical_system)
self._flux_angle_name = "psi_abs"
super().__init__('epsilon', physical_system=physical_system)
self._flux_angle_name = 'psi_abs'
self._flux_angle_index = None

def simulate(self, action):
Expand All @@ -144,5 +144,23 @@ def simulate(self, action):

def set_physical_system(self, physical_system):
super().set_physical_system(physical_system)
self._flux_angle_index = physical_system.state_names.index("psi_angle")
self._flux_angle_index = physical_system.state_names.index('psi_angle')
return self

@DqToAbcActionProcessor.register_transformation(['EESM'])
class _EESMDqToAbcActionProcessor(DqToAbcActionProcessor):
@property
def action_space(self):
return gymnasium.spaces.Box(-1, 1, shape=(3,))

def __init__(self, physical_system=None):
super().__init__('epsilon', physical_system=physical_system)

def simulate(self, action):
# Docstring of superclass
advanced_angle = self._advance_angle(self._state)
abc_action = self._transformation(action[:-1], advanced_angle)
abc_action = np.append(abc_action, action[-1])
normalized_state = self._physical_system.simulate(abc_action)
self._state = normalized_state * self._physical_system.limits
return normalized_state
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def test_simulate(self, processor, physical_system, action):
assert all(
state
== np.concatenate(
(ps_state, [np.cos(cos_sin_state), np.sin(cos_sin_state)])
(ps_state, [np.cos(cos_sin_state*np.pi), np.sin(cos_sin_state*np.pi)])
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def test_action_space(self, processor, physical_system):
(
np.array([0.0, 1.0]),
np.array([12.8, 0.123, 0.0]),
np.array([-0.23752263, 0.96000281, -0.72248018]),
np.array([-0.45595422, 0.99874252, -0.54278829]),
),
(
np.array([0.0, 0.5]),
np.array([-10.0, 0.123, 0.0]),
np.array([-0.49324109, 0.31757774, 0.17566335]),
np.array([ 0.36929056, -0.47656712, 0.10727657]),
),
],
)
Expand Down
Loading