|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | + |
| 6 | +class Actuator(ABC): |
| 7 | + """Abstract class used to define actuators. |
| 8 | +
|
| 9 | + Actuators are used to model the dynamics of control systems such as |
| 10 | + throttle, thrust vector, and roll control. They can be used to simulate the response of |
| 11 | + the control system to changes in throttle, thrust vector, or roll torque commands.""" |
| 12 | + |
| 13 | + def __init__( |
| 14 | + self, |
| 15 | + name, |
| 16 | + demand_rate=None, |
| 17 | + actuator_range=(-np.inf, np.inf), |
| 18 | + actuator_rate_limit=None, |
| 19 | + clamp=True, |
| 20 | + actuator_initial_output=0.0, |
| 21 | + actuator_time_constant=None, |
| 22 | + ): |
| 23 | + """Initializes the Actuator class. |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + name : str |
| 28 | + Name of the actuator. |
| 29 | + demand_rate : float, optional |
| 30 | + Demand rate (Hz) of the actuator. Default is None for continuous-time actuator. |
| 31 | + actuator_range : tuple, optional |
| 32 | + Range of the actuator output. Default is (-np.Inf, np.Inf). |
| 33 | + actuator_rate_limit : float, optional |
| 34 | + Rate limit of the actuator per second. Default is None. |
| 35 | + clamp : bool, optional |
| 36 | + Whether to clamp the actuator output. Default is True. |
| 37 | + actuator_initial_output : float, optional |
| 38 | + Initial output of the actuator. Default is 0.0. |
| 39 | + actuator_time_constant : float, optional |
| 40 | + Time constant of the actuator, implemented as a discrete IIR filter. Default is None. |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + None |
| 45 | + """ |
| 46 | + |
| 47 | + self.name = name |
| 48 | + |
| 49 | + assert demand_rate > 0 or demand_rate is None, ( |
| 50 | + "demand_rate must be positive or None." |
| 51 | + ) |
| 52 | + self.demand_rate = demand_rate |
| 53 | + |
| 54 | + assert actuator_range[0] <= actuator_range[1], ( |
| 55 | + "actuator_range[0] must be <= actuator_range[1]." |
| 56 | + ) |
| 57 | + self.actuator_range = actuator_range |
| 58 | + |
| 59 | + assert actuator_rate_limit is None or actuator_rate_limit >= 0, ( |
| 60 | + "actuator_rate_limit must be non-negative or None." |
| 61 | + ) |
| 62 | + self.actuator_rate_limit = actuator_rate_limit |
| 63 | + |
| 64 | + self.clamp = clamp |
| 65 | + |
| 66 | + assert actuator_time_constant is None or actuator_time_constant >= 0, ( |
| 67 | + "actuator_time_constant must be non-negative or None." |
| 68 | + ) |
| 69 | + self.actuator_time_constant = actuator_time_constant |
| 70 | + self._update_iir_coefficients() |
| 71 | + |
| 72 | + self.actuator_initial_output = actuator_initial_output |
| 73 | + self._actuator_output = actuator_initial_output |
| 74 | + |
| 75 | + def _update_iir_coefficients(self): |
| 76 | + """Updates the IIR filter coefficient based on time constant and |
| 77 | + demand rate. Uses first-order discrete-time system: |
| 78 | + y[n] = alpha * u[n] + (1 - alpha) * y[n-1] |
| 79 | + where alpha = Ts / (tau + Ts) |
| 80 | + """ |
| 81 | + |
| 82 | + if self.actuator_time_constant is not None and self.actuator_time_constant > 0: |
| 83 | + if self.demand_rate is not None: |
| 84 | + demand_period = 1.0 / self.demand_rate |
| 85 | + self._alpha = demand_period / ( |
| 86 | + self.actuator_time_constant + demand_period |
| 87 | + ) |
| 88 | + else: |
| 89 | + print( |
| 90 | + f"Warning: Actuator time constant currently only implemented on discrete controllers. '{self.name}' dynamics not applied." |
| 91 | + ) |
| 92 | + self._alpha = 1.0 # No filtering, direct pass-through |
| 93 | + else: |
| 94 | + self._alpha = 1.0 # No filtering, direct pass-through |
| 95 | + |
| 96 | + @property |
| 97 | + def actuator_output(self): |
| 98 | + return self._actuator_output |
| 99 | + |
| 100 | + @actuator_output.setter |
| 101 | + def actuator_output(self, value): |
| 102 | + """Sets the actuator output with optional clamping or warning. |
| 103 | +
|
| 104 | + Parameters |
| 105 | + ---------- |
| 106 | + value : float |
| 107 | + Desired actuator output. |
| 108 | +
|
| 109 | + Returns |
| 110 | + ------- |
| 111 | + None |
| 112 | + """ |
| 113 | + # Apply first-order IIR actuator dynamics |
| 114 | + value = self._alpha * value + (1 - self._alpha) * self._actuator_output |
| 115 | + |
| 116 | + # Apply rate limit if specified |
| 117 | + if self.actuator_rate_limit is not None: |
| 118 | + if self.demand_rate is not None: |
| 119 | + max_change = self.actuator_rate_limit / self.demand_rate |
| 120 | + change = value - self._actuator_output |
| 121 | + if abs(change) > max_change: |
| 122 | + value = self._actuator_output + np.sign(change) * max_change |
| 123 | + print( |
| 124 | + f"Warning: Actuator '{self.name}' output change {change:.3f} exceeds rate limit of {max_change:.3f} per time step." |
| 125 | + ) |
| 126 | + else: |
| 127 | + print( |
| 128 | + f"Warning: Actuator rate limit currently only implemented for discrete controllers. '{self.name}' rate limit not applied." |
| 129 | + ) |
| 130 | + |
| 131 | + # Apply range limits if specified |
| 132 | + if self.clamp: |
| 133 | + value = np.clip(value, self.actuator_range[0], self.actuator_range[1]) |
| 134 | + else: |
| 135 | + if value < self.actuator_range[0] or value > self.actuator_range[1]: |
| 136 | + print( |
| 137 | + f"Warning: Actuator '{self.name}' output {value:.3f} exceeds range limits {self.actuator_range}." |
| 138 | + ) |
| 139 | + |
| 140 | + self._actuator_output = value |
| 141 | + |
| 142 | + def _reset(self): |
| 143 | + """Resets the actuator to its initial state. This method |
| 144 | + is called at the beginning of each simulation to ensure the actuator |
| 145 | + is in the correct state.""" |
| 146 | + self._actuator_output = self.actuator_initial_output |
| 147 | + |
| 148 | + @abstractmethod |
| 149 | + def info(self): |
| 150 | + """Prints summarized information of the actuator. |
| 151 | +
|
| 152 | + Returns |
| 153 | + ------- |
| 154 | + None |
| 155 | + """ |
| 156 | + |
| 157 | + @abstractmethod |
| 158 | + def all_info(self): |
| 159 | + """Prints all information of the actuator. |
| 160 | +
|
| 161 | + Returns |
| 162 | + ------- |
| 163 | + None |
| 164 | + """ |
0 commit comments