import control
import numpy as np
import matplotlib.pyplot as plt
import kontrol
# Load the transfer function
plant = kontrol.load_transfer_function("../system_modeling/transfer_function_x1_without_guess.pkl")
# Get a controller. regulator_type="D" for derivative control, which is a natural choice for damping.
# This function generates a critical damping controller.
controller = kontrol.regulator.oscillator.pid(plant, regulator_type="D")
# Get a low-pass filter.
# This lower the cut-off frequency of the low-pass filter until
# a specified phase margin is attained (defaults to 45 degrees.)
# Use the phase_margin option to override this.
low_pass = kontrol.regulator.post_filter.post_low_pass(plant, regulator=controller, order=4)
# Final filter.
controller *= low_pass
# Inspect the open-loop transfer function
oltf = controller*plant
f = np.logspace(-3, 2, 1024)
plt.subplot(211)
plt.loglog(f, abs((oltf)(1j*2*np.pi*f)), label="Open-loop transfer function")
plt.legend(loc=0)
plt.grid(which="both")
plt.ylabel("Magnitude")
plt.subplot(212)
plt.semilogx(f, 180/np.pi*np.angle(oltf(1j*2*np.pi*f)))
# plt.legend(loc=0)
plt.grid(which="both")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Phase (degree)")