Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Lint fixes on benchmarks and examples/*.py #1135

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

Merged
merged 3 commits into from
Mar 8, 2025
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: 0 additions & 1 deletion benchmarks/flatsys_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import numpy as np
import math
import control as ct
import control.flatsys as flat
import control.optimal as opt

Expand Down
1 change: 0 additions & 1 deletion benchmarks/optestim_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# used for optimization-based estimation.

import numpy as np
import math
import control as ct
import control.optimal as opt

Expand Down
8 changes: 0 additions & 8 deletions benchmarks/optimal_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# performance of the functions used for optimization-base control.

import numpy as np
import math
import control as ct
import control.flatsys as fs
import control.optimal as opt
Expand All @@ -21,7 +20,6 @@
'RK23': ('RK23', {}),
'RK23_sloppy': ('RK23', {'atol': 1e-4, 'rtol': 1e-2}),
'RK45': ('RK45', {}),
'RK45': ('RK45', {}),
'RK45_sloppy': ('RK45', {'atol': 1e-4, 'rtol': 1e-2}),
'LSODA': ('LSODA', {}),
}
Expand Down Expand Up @@ -129,9 +127,6 @@ def time_optimal_lq_methods(integrator_name, minimizer_name, method):
Tf = 10
timepts = np.linspace(0, Tf, 20)

# Create the basis function to use
basis = get_basis('poly', 12, Tf)

res = opt.solve_ocp(
sys, timepts, x0, traj_cost, constraints, terminal_cost=term_cost,
solve_ivp_method=integrator[0], solve_ivp_kwargs=integrator[1],
Expand Down Expand Up @@ -223,8 +218,6 @@ def time_discrete_aircraft_mpc(minimizer_name):
# compute the steady state values for a particular value of the input
ud = np.array([0.8, -0.3])
xd = np.linalg.inv(np.eye(5) - A) @ B @ ud
yd = C @ xd

# provide constraints on the system signals
constraints = [opt.input_range_constraint(sys, [-5, -6], [5, 6])]

Expand All @@ -234,7 +227,6 @@ def time_discrete_aircraft_mpc(minimizer_name):
cost = opt.quadratic_cost(model, Q, R, x0=xd, u0=ud)

# Set the time horizon and time points
Tf = 3
timepts = np.arange(0, 6) * 0.2

# Get the minimizer parameters to use
Expand Down
4 changes: 2 additions & 2 deletions examples/bdalg-matlab.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# bdalg-matlab.py - demonstrate some MATLAB commands for block diagram altebra
# bdalg-matlab.py - demonstrate some MATLAB commands for block diagram algebra
# RMM, 29 May 09

from control.matlab import * # MATLAB-like functions
from control.matlab import ss, ss2tf, tf, tf2ss # MATLAB-like functions

# System matrices
A1 = [[0, 1.], [-4, -1]]
Expand Down
4 changes: 2 additions & 2 deletions examples/check-controllability-and-observability.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
RMM, 6 Sep 2010
"""

import numpy as np # Load the scipy functions
from control.matlab import * # Load the controls systems library
import numpy as np # Load the numpy functions
from control.matlab import ss, ctrb, obsv # Load the controls systems library

# Parameters defining the system

Expand Down
7 changes: 3 additions & 4 deletions examples/cruise-control.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ def pi_update(t, x, u, params={}):
# Assign variables for inputs and states (for readability)
v = u[0] # current velocity
vref = u[1] # reference velocity
z = x[0] # integrated error

# Compute the nominal controller output (needed for anti-windup)
u_a = pi_output(t, x, u, params)
Expand Down Expand Up @@ -394,7 +393,7 @@ def sf_output(t, z, u, params={}):
ud = params.get('ud', 0)

# Get the system state and reference input
x, y, r = u[0], u[1], u[2]
x, r = u[0], u[2]

return ud - K * (x - xd) - ki * z + kf * (r - yd)

Expand Down Expand Up @@ -440,13 +439,13 @@ def sf_output(t, z, u, params={}):
4./180. * pi for t in T]
t, y = ct.input_output_response(
cruise_sf, T, [vref, gear, theta_hill], [X0[0], 0],
params={'K': K, 'kf': kf, 'ki': 0.0, 'kf': kf, 'xd': xd, 'ud': ud, 'yd': yd})
params={'K': K, 'kf': kf, 'ki': 0.0, 'xd': xd, 'ud': ud, 'yd': yd})
subplots = cruise_plot(cruise_sf, t, y, label='Proportional', linetype='b--')

# Response of the system with state feedback + integral action
t, y = ct.input_output_response(
cruise_sf, T, [vref, gear, theta_hill], [X0[0], 0],
params={'K': K, 'kf': kf, 'ki': 0.1, 'kf': kf, 'xd': xd, 'ud': ud, 'yd': yd})
params={'K': K, 'kf': kf, 'ki': 0.1, 'xd': xd, 'ud': ud, 'yd': yd})
cruise_plot(cruise_sf, t, y, label='PI control', t_hill=8, linetype='b-',
subplots=subplots, legend=True)

Expand Down
1 change: 0 additions & 1 deletion examples/kincar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import numpy as np
import matplotlib.pyplot as plt
import control as ct
import control.flatsys as fs

#
Expand Down
1 change: 0 additions & 1 deletion examples/mrac_siso_mit.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def adaptive_controller_state(t, xc, uc, params):
# Parameters
gam = params["gam"]
Am = params["Am"]
Bm = params["Bm"]
signB = params["signB"]

# Controller inputs
Expand Down
3 changes: 1 addition & 2 deletions examples/phase_plane_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
# using the phaseplot module. Most of these figures line up with examples
# in FBS2e, with different display options shown as different subplots.

import time
import warnings
from math import pi, sqrt
from math import pi

import matplotlib.pyplot as plt
import numpy as np
Expand Down
45 changes: 22 additions & 23 deletions examples/pvtol-nested-ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import os
import matplotlib.pyplot as plt # MATLAB plotting functions
from control.matlab import * # MATLAB-like functions
import numpy as np
import math
import control as ct
Expand All @@ -23,12 +22,12 @@
c = 0.05 # damping factor (estimated)

# Transfer functions for dynamics
Pi = tf([r], [J, 0, 0]) # inner loop (roll)
Po = tf([1], [m, c, 0]) # outer loop (position)
Pi = ct.tf([r], [J, 0, 0]) # inner loop (roll)
Po = ct.tf([1], [m, c, 0]) # outer loop (position)

# Use state space versions
Pi = tf2ss(Pi)
Po = tf2ss(Po)
Pi = ct.tf2ss(Pi)
Po = ct.tf2ss(Po)

#
# Inner loop control design
Expand All @@ -40,68 +39,68 @@

# Design a simple lead controller for the system
k, a, b = 200, 2, 50
Ci = k*tf([1, a], [1, b]) # lead compensator
Ci = k*ct.tf([1, a], [1, b]) # lead compensator

# Convert to statespace
Ci = tf2ss(Ci)
Ci = ct.tf2ss(Ci)

# Compute the loop transfer function for the inner loop
Li = Pi*Ci


# Bode plot for the open loop process
plt.figure(1)
bode(Pi)
ct.bode(Pi)

# Bode plot for the loop transfer function, with margins
plt.figure(2)
bode(Li)
ct.bode(Li)

# Compute out the gain and phase margins
#! Not implemented
# (gm, pm, wcg, wcp) = margin(Li);

# Compute the sensitivity and complementary sensitivity functions
Si = feedback(1, Li)
Si = ct.feedback(1, Li)
Ti = Li*Si

# Check to make sure that the specification is met
plt.figure(3)
gangof4(Pi, Ci)
ct.gangof4(Pi, Ci)

# Compute out the actual transfer function from u1 to v1 (see L8.2 notes)
# Hi = Ci*(1-m*g*Pi)/(1+Ci*Pi);
Hi = parallel(feedback(Ci, Pi), -m*g*feedback(Ci*Pi, 1))
Hi = ct.parallel(ct.feedback(Ci, Pi), -m*g*ct.feedback(Ci*Pi, 1))

plt.figure(4)
plt.clf()
bode(Hi)
ct.bode(Hi)

# Now design the lateral control system
a, b, K = 0.02, 5, 2
Co = -K*tf([1, 0.3], [1, 10]) # another lead compensator
Co = -K*ct.tf([1, 0.3], [1, 10]) # another lead compensator

# Convert to statespace
Co = tf2ss(Co)
Co = ct.tf2ss(Co)

# Compute the loop transfer function for the outer loop
Lo = -m*g*Po*Co

plt.figure(5)
bode(Lo, display_margins=True) # margin(Lo)
ct.bode(Lo, display_margins=True) # margin(Lo)

# Finally compute the real outer-loop loop gain + responses
L = Co*Hi*Po
S = feedback(1, L)
T = feedback(L, 1)
S = ct.feedback(1, L)
T = ct.feedback(L, 1)

# Compute stability margins
#! Not yet implemented
# (gm, pm, wgc, wpc) = margin(L);

plt.figure(6)
plt.clf()
out = ct.bode(L, logspace(-4, 3), initial_phase=-math.pi/2)
out = ct.bode(L, np.logspace(-4, 3), initial_phase=-math.pi/2)
axs = ct.get_plot_axes(out)

# Add crossover line to magnitude plot
Expand All @@ -111,7 +110,7 @@
# Nyquist plot for complete design
#
plt.figure(7)
nyquist(L)
ct.nyquist(L)

# set up the color
color = 'b'
Expand All @@ -126,10 +125,10 @@
# 'EdgeColor', color, 'FaceColor', color);

plt.figure(9)
Yvec, Tvec = step(T, linspace(1, 20))
Yvec, Tvec = ct.step_response(T, np.linspace(1, 20))
plt.plot(Tvec.T, Yvec.T)

Yvec, Tvec = step(Co*S, linspace(1, 20))
Yvec, Tvec = ct.step_response(Co*S, np.linspace(1, 20))
plt.plot(Tvec.T, Yvec.T)

#TODO: PZmap for statespace systems has not yet been implemented.
Expand All @@ -142,7 +141,7 @@
# Gang of Four
plt.figure(11)
plt.clf()
gangof4(Hi*Po, Co, linspace(-2, 3))
ct.gangof4(Hi*Po, Co, np.linspace(-2, 3))

if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
plt.show()
9 changes: 0 additions & 9 deletions examples/pvtol.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ def _pvtol_flat_forward(states, inputs, params={}):
F1, F2 = inputs

# Use equations of motion for higher derivates
x1ddot = (F1 * cos(theta) - F2 * sin(theta)) / m
x2ddot = (F1 * sin(theta) + F2 * cos(theta) - m * g) / m
thddot = (r * F1) / J

# Flat output is a point above the vertical axis
Expand Down Expand Up @@ -110,7 +108,6 @@ def _pvtol_flat_reverse(zflag, params={}):
J = params.get('J', 0.0475) # inertia around pitch axis
r = params.get('r', 0.25) # distance to center of force
g = params.get('g', 9.8) # gravitational constant
c = params.get('c', 0.05) # damping factor (estimated)

# Given the flat variables, solve for the state
theta = np.arctan2(-zflag[0][2], zflag[1][2] + g)
Expand Down Expand Up @@ -185,18 +182,13 @@ def _windy_update(t, x, u, params):
def _noisy_update(t, x, u, params):
# Get the inputs
F1, F2, Dx, Dy = u[:4]
if u.shape[0] > 4:
Nx, Ny, Nth = u[4:]
else:
Nx, Ny, Nth = 0, 0, 0

# Get the system response from the original dynamics
xdot, ydot, thetadot, xddot, yddot, thddot = \
_pvtol_update(t, x, u[0:2], params)

# Get the parameter values we need
m = params.get('m', 4.) # mass of aircraft
J = params.get('J', 0.0475) # inertia around pitch axis

# Now add the disturbances
xddot += Dx / m
Expand All @@ -219,7 +211,6 @@ def _noisy_output(t, x, u, params):
def pvtol_noisy_A(x, u, params={}):
# Get the parameter values we need
m = params.get('m', 4.) # mass of aircraft
J = params.get('J', 0.0475) # inertia around pitch axis
c = params.get('c', 0.05) # damping factor (estimated)

# Get the angle and compute sine and cosine
Expand Down
7 changes: 4 additions & 3 deletions examples/secord-matlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import os
import matplotlib.pyplot as plt # MATLAB plotting functions
from control.matlab import * # MATLAB-like functions
import numpy as np
from control.matlab import ss, step, bode, nyquist, rlocus # MATLAB-like functions

# Parameters defining the system
m = 250.0 # system mass
Expand All @@ -24,15 +25,15 @@

# Bode plot for the system
plt.figure(2)
mag, phase, om = bode(sys, logspace(-2, 2), plot=True)
mag, phase, om = bode(sys, np.logspace(-2, 2), plot=True)
plt.show(block=False)

# Nyquist plot for the system
plt.figure(3)
nyquist(sys)
plt.show(block=False)

# Root lcous plot for the system
# Root locus plot for the system
rlocus(sys)

if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
Expand Down
10 changes: 5 additions & 5 deletions examples/sisotool_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@

#%%
import matplotlib.pyplot as plt
from control.matlab import *
import control as ct

# first example, aircraft attitude equation
s = tf([1,0],[1])
s = ct.tf([1,0],[1])
Kq = -24
T2 = 1.4
damping = 2/(13**.5)
omega = 13**.5
H = (Kq*(1+T2*s))/(s*(s**2+2*damping*omega*s+omega**2))
plt.close('all')
sisotool(-H)
ct.sisotool(-H)

#%%

# a simple RL, with multiple poles in the origin
plt.close('all')
H = (s+0.3)/(s**4 + 4*s**3 + 6.25*s**2)
sisotool(H)
ct.sisotool(H)

#%%

Expand All @@ -43,4 +43,4 @@
plt.close('all')
H = (b0 + b1*s + b2*s**2) / (a0 + a1*s + a2*s**2 + a3*s**3)

sisotool(H)
ct.sisotool(H)
Loading