Skip to content

Commit be660ce

Browse files
committed
default outfcn() bug fix + unit tests for flatsys
1 parent d01a52c commit be660ce

5 files changed

Lines changed: 36 additions & 5 deletions

File tree

control/flatsys/bezier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class BezierFamily(BasisFamily):
5555
"""
5656
def __init__(self, N, T=1):
5757
"""Create a polynomial basis of order N."""
58-
self.N = N # save number of basis functions
58+
super(BezierFamily, self).__init__(N)
5959
self.T = T # save end of time interval
6060

6161
# Compute the kth derivative of the ith basis function at time t

control/flatsys/flatsys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def _flat_updfcn(self, t, x, u, params={}):
216216
def _flat_outfcn(self, t, x, u, params={}):
217217
# Return the flat output
218218
zflag = self.forward(x, u, params)
219-
return np.array(zflag[:][0])
219+
return np.array([zflag[i][0] for i in range(len(zflag))])
220220

221221

222222
# Utility function to compute flag matrix given a basis

control/flatsys/poly.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class PolyFamily(BasisFamily):
5252
"""
5353
def __init__(self, N):
5454
"""Create a polynomial basis of order N."""
55-
self.N = N # save number of basis functions
55+
super(PolyFamily, self).__init__(N)
5656

5757
# Compute the kth derivative of the ith basis function at time t
5858
def eval_deriv(self, i, k, t):

control/tests/flatsys_test.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,46 @@ def test_kinematic_car(self, vehicle_flat, poly):
113113
np.testing.assert_array_almost_equal(uf, u[:, 1])
114114

115115
# Simulate the system and make sure we stay close to desired traj
116-
T = np.linspace(0, Tf, 500)
116+
T = np.linspace(0, Tf, 100)
117117
xd, ud = traj.eval(T)
118+
resp = ct.input_output_response(vehicle_flat, T, ud, x0)
119+
np.testing.assert_array_almost_equal(resp.states, xd, decimal=2)
118120

119121
# For SciPy 1.0+, integrate equations and compare to desired
120122
if StrictVersion(sp.__version__) >= "1.0":
121123
t, y, x = ct.input_output_response(
122124
vehicle_flat, T, ud, x0, return_x=True)
123125
np.testing.assert_allclose(x, xd, atol=0.01, rtol=0.01)
124126

127+
def test_flat_default_output(self, vehicle_flat):
128+
# Construct a flat system with the default outputs
129+
flatsys = fs.FlatSystem(
130+
vehicle_flat.forward, vehicle_flat.reverse, vehicle_flat.updfcn,
131+
inputs=vehicle_flat.ninputs, outputs=vehicle_flat.ninputs,
132+
states=vehicle_flat.nstates)
133+
134+
# Define the endpoints of the trajectory
135+
x0 = [0., -2., 0.]; u0 = [10., 0.]
136+
xf = [100., 2., 0.]; uf = [10., 0.]
137+
Tf = 10
138+
139+
# Find trajectory between initial and final conditions
140+
poly = fs.PolyFamily(6)
141+
traj1 = fs.point_to_point(vehicle_flat, Tf, x0, u0, xf, uf, basis=poly)
142+
traj2 = fs.point_to_point(flatsys, Tf, x0, u0, xf, uf, basis=poly)
143+
144+
# Verify that the trajectory computation is correct
145+
T = np.linspace(0, Tf, 10)
146+
x1, u1 = traj1.eval(T)
147+
x2, u2 = traj2.eval(T)
148+
np.testing.assert_array_almost_equal(x1, x2)
149+
np.testing.assert_array_almost_equal(u1, u2)
150+
151+
# Run a simulation and verify that the outputs are correct
152+
resp1 = ct.input_output_response(vehicle_flat, T, u1, x0)
153+
resp2 = ct.input_output_response(flatsys, T, u1, x0)
154+
np.testing.assert_array_almost_equal(resp1.outputs[0:2], resp2.outputs)
155+
125156
def test_flat_cost_constr(self):
126157
# Double integrator system
127158
sys = ct.ss([[0, 1], [0, 0]], [[0], [1]], [[1, 0]], 0)

examples/kincar-flatsys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def plot_results(t, x, ud):
109109
# Create differentially flat input/output system
110110
vehicle_flat = fs.FlatSystem(
111111
vehicle_flat_forward, vehicle_flat_reverse, vehicle_update,
112-
inputs=('v', 'delta'), outputs=('x', 'y', 'theta'),
112+
inputs=('v', 'delta'), outputs=('x', 'y'),
113113
states=('x', 'y', 'theta'))
114114

115115
# Define the endpoints of the trajectory

0 commit comments

Comments
 (0)