Created
February 29, 2024 13:29
-
-
Save s-celles/5b464bcddea63d3b3d79507d2779a79f to your computer and use it in GitHub Desktop.
Slew rate limiter - Python implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SlewRateLimiter: | |
def __init__(self, slew_rate, neg_slew_rate=None, t_init=0, y_init=0.0): | |
self.slew_rate = slew_rate | |
if neg_slew_rate is None: | |
self.neg_slew_rate = -slew_rate | |
else: | |
self.neg_slew_rate = neg_slew_rate | |
self.t = t_init | |
self.y = y_init | |
def process(self, t, y): | |
delta_t = t - self.t | |
if delta_t != 0: | |
rate = (y - self.y) / delta_t | |
clamped_rate = clamp(rate, self.neg_slew_rate, self.slew_rate) | |
new_y = self.y + clamped_rate * delta_t | |
else: | |
new_y = y | |
self.t = t | |
self.y = new_y | |
return new_y | |
import numpy as np | |
import matplotlib.pyplot as plt | |
T = np.arange(start=-1.0, stop=10.0, step=0.01) | |
t0, t1 = 1.0, 6.0 | |
U = U0 * (np.heaviside(T - t0, 0.5) - np.heaviside(T - t1, 0.5)) | |
slew_rate = SlewRateLimiter(1.0, neg_slew_rate=-0.75) | |
Y = [] | |
for i, t in enumerate(T): | |
Y.append(slew_rate.process(t, U[i])) | |
plt.plot(T, U, label="u") | |
plt.plot(T, Y, label="y", linestyle='--') | |
plt.xlabel("Temps (s)") | |
plt.ylabel("Signal") | |
plt.legend() |
Author
s-celles
commented
Feb 29, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment