@@ -1334,8 +1334,8 @@ end
1334
1334
@testitem " MovingHorizonEstimator v.s. Kalman filters" setup= [SetupMPCtests] begin
1335
1335
using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
1336
1336
linmodel1 = setop! (LinModel (sys,Ts,i_d= [3 ]), uop= [10 ,50 ], yop= [50 ,30 ], dop= [20 ])
1337
- mhe = MovingHorizonEstimator (linmodel1, He= 3 , nint_ym= 0 , direct= false )
1338
1337
kf = KalmanFilter (linmodel1, nint_ym= 0 , direct= false )
1338
+ mhe = MovingHorizonEstimator (linmodel1, He= 3 , nint_ym= 0 , direct= false )
1339
1339
X̂_mhe = zeros (4 , 6 )
1340
1340
X̂_kf = zeros (4 , 6 )
1341
1341
for i in 1 : 6
@@ -1347,28 +1347,33 @@ end
1347
1347
updatestate! (mhe, [11 , 50 ], y, [25 ])
1348
1348
updatestate! (kf, [11 , 50 ], y, [25 ])
1349
1349
end
1350
- @test X̂_mhe ≈ X̂_kf atol= 1e-3 rtol= 1e-3
1351
- mhe = MovingHorizonEstimator (linmodel1, He= 3 , nint_ym= 0 , direct= true )
1350
+ @test X̂_mhe ≈ X̂_kf atol= 1e-6 rtol= 1e-6
1352
1351
kf = KalmanFilter (linmodel1, nint_ym= 0 , direct= true )
1352
+ # recuperate P̂(-1|-1) exact value using the Kalman filter:
1353
+ preparestate! (kf, [50 , 30 ], [20 ])
1354
+ σP̂ = sqrt .(diag (kf. P̂))
1355
+ mhe = MovingHorizonEstimator (linmodel1, He= 3 , nint_ym= 0 , direct= true , σP_0= σP̂)
1356
+ updatestate! (kf, [10 , 50 ], [50 , 30 ], [20 ])
1353
1357
X̂_mhe = zeros (4 , 6 )
1354
1358
X̂_kf = zeros (4 , 6 )
1355
1359
for i in 1 : 6
1356
- y = [50 ,31 ] + randn (2 )
1360
+ y = [50 ,31 ] # + randn(2)
1357
1361
x̂_mhe = preparestate! (mhe, y, [25 ])
1358
1362
x̂_kf = preparestate! (kf, y, [25 ])
1359
1363
X̂_mhe[:,i] = x̂_mhe
1360
1364
X̂_kf[:,i] = x̂_kf
1361
1365
updatestate! (mhe, [11 , 50 ], y, [25 ])
1362
1366
updatestate! (kf, [11 , 50 ], y, [25 ])
1363
1367
end
1364
- @test X̂_mhe ≈ X̂_kf atol= 1e-3 rtol= 1e-3
1368
+ @test X̂_mhe ≈ X̂_kf atol= 1e-6 rtol= 1e-6
1369
+
1365
1370
f = (x,u,d,model) -> model. A* x + model. Bu* u + model. Bd* d
1366
1371
h = (x,d,model) -> model. C* x + model. Dd* d
1367
1372
nonlinmodel = NonLinModel (f, h, Ts, 2 , 4 , 2 , 1 , p= linmodel1, solver= nothing )
1368
1373
nonlinmodel = setop! (nonlinmodel, uop= [10 ,50 ], yop= [50 ,30 ], dop= [20 ])
1369
- mhe = MovingHorizonEstimator (nonlinmodel, He= 5 , nint_ym= 0 , direct= false )
1370
1374
ukf = UnscentedKalmanFilter (nonlinmodel, nint_ym= 0 , direct= false )
1371
1375
ekf = ExtendedKalmanFilter (nonlinmodel, nint_ym= 0 , direct= false )
1376
+ mhe = MovingHorizonEstimator (nonlinmodel, He= 5 , nint_ym= 0 , direct= false )
1372
1377
X̂_mhe = zeros (4 , 6 )
1373
1378
X̂_ukf = zeros (4 , 6 )
1374
1379
X̂_ekf = zeros (4 , 6 )
@@ -1384,11 +1389,18 @@ end
1384
1389
updatestate! (ukf, [11 , 50 ], y, [25 ])
1385
1390
updatestate! (ekf, [11 , 50 ], y, [25 ])
1386
1391
end
1387
- @test X̂_mhe ≈ X̂_ukf atol= 1e-3 rtol= 1e-3
1388
- @test X̂_mhe ≈ X̂_ekf atol= 1e-3 rtol= 1e-3
1389
- mhe = MovingHorizonEstimator (nonlinmodel, He = 5 , nint_ym = 0 , direct = true )
1392
+ @test X̂_mhe ≈ X̂_ukf atol= 1e-6 rtol= 1e-6
1393
+ @test X̂_mhe ≈ X̂_ekf atol= 1e-6 rtol= 1e-6
1394
+
1390
1395
ukf = UnscentedKalmanFilter (nonlinmodel, nint_ym= 0 , direct= true )
1391
1396
ekf = ExtendedKalmanFilter (nonlinmodel, nint_ym= 0 , direct= true )
1397
+ # recuperate P̂(-1|-1) exact value using the Unscented Kalman filter:
1398
+ preparestate! (ukf, [50 , 30 ], [20 ])
1399
+ preparestate! (ekf, [50 , 30 ], [20 ])
1400
+ σP̂ = sqrt .(diag (ukf. P̂))
1401
+ mhe = MovingHorizonEstimator (nonlinmodel, He= 5 , nint_ym= 0 , direct= true , σP_0= σP̂)
1402
+ updatestate! (ukf, [10 , 50 ], [50 , 30 ], [20 ])
1403
+ updatestate! (ekf, [10 , 50 ], [50 , 30 ], [20 ])
1392
1404
X̂_mhe = zeros (4 , 6 )
1393
1405
X̂_ukf = zeros (4 , 6 )
1394
1406
X̂_ekf = zeros (4 , 6 )
@@ -1404,8 +1416,8 @@ end
1404
1416
updatestate! (ukf, [11 , 50 ], y, [25 ])
1405
1417
updatestate! (ekf, [11 , 50 ], y, [25 ])
1406
1418
end
1407
- @test X̂_mhe ≈ X̂_ukf atol= 1e-3 rtol= 1e-3
1408
- @test X̂_mhe ≈ X̂_ekf atol= 1e-3 rtol= 1e-3
1419
+ @test X̂_mhe ≈ X̂_ukf atol= 1e-6 rtol= 1e-6
1420
+ @test X̂_mhe ≈ X̂_ekf atol= 1e-6 rtol= 1e-6
1409
1421
end
1410
1422
1411
1423
@testitem " MovingHorizonEstimator LinModel v.s. NonLinModel" setup= [SetupMPCtests] begin
0 commit comments