|
1 | 1 | Ts = 4.0 |
2 | 2 | sys = [ tf(1.90,[18.0,1]) tf(1.90,[18.0,1]) tf(1.90,[18.0,1]); |
3 | 3 | tf(-0.74,[8.0,1]) tf(0.74,[8.0,1]) tf(-0.74,[8.0,1]) ] |
4 | | -#= |
| 4 | + |
5 | 5 | @testset "SteadyKalmanFilter construction" begin |
6 | 6 | linmodel1 = LinModel(sys,Ts,i_u=[1,2]) |
7 | 7 | skalmanfilter1 = SteadyKalmanFilter(linmodel1) |
@@ -618,16 +618,21 @@ end |
618 | 618 | @test_throws ArgumentError MovingHorizonEstimator(linmodel1) |
619 | 619 | @test_throws ArgumentError MovingHorizonEstimator(linmodel1, He=0) |
620 | 620 | end |
621 | | -=# |
622 | | -@testset "MovingHorizonEstimator estimator methods" begin |
623 | | - linmodel1 = LinModel(sys,Ts,i_u=[1,2], i_d=[3]) |
| 621 | + |
| 622 | +@testset "MovingHorizonEstimator estimation and getinfo" begin |
| 623 | + linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2], i_d=[3]), uop=[10,50], yop=[50,30], dop=[5]) |
624 | 624 | f(x,u,d) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d |
625 | 625 | h(x,d) = linmodel1.C*x + linmodel1.Dd*d |
626 | 626 | nonlinmodel = setop!(NonLinModel(f, h, Ts, 2, 4, 2, 1), uop=[10,50], yop=[50,30], dop=[5]) |
627 | 627 | mhe1 = MovingHorizonEstimator(nonlinmodel, He=2) |
628 | | - @test updatestate!(mhe1, [10, 50], [50, 30], [5]) ≈ zeros(6) atol=1e-9 |
| 628 | + x̂ = updatestate!(mhe1, [10, 50], [50, 30], [5]) |
| 629 | + @test x̂ ≈ zeros(6) atol=1e-9 |
629 | 630 | @test mhe1.x̂ ≈ zeros(6) atol=1e-9 |
630 | 631 | @test evaloutput(mhe1, [5]) ≈ mhe1([5]) ≈ [50, 30] |
| 632 | + info = getinfo(mhe1) |
| 633 | + @test info[:x̂] ≈ x̂ atol=1e-9 |
| 634 | + @test info[:Ŷ][end-1:end] ≈ [50, 30] atol=1e-9 |
| 635 | + |
631 | 636 | @test initstate!(mhe1, [10, 50], [50, 30+1], [5]) ≈ zeros(6) atol=1e-9 |
632 | 637 | setstate!(mhe1, [1,2,3,4,5,6]) |
633 | 638 | @test mhe1.x̂ ≈ [1,2,3,4,5,6] |
|
639 | 644 | updatestate!(mhe1, [10, 50], [51, 32], [5]) |
640 | 645 | end |
641 | 646 | @test mhe1([5]) ≈ [51, 32] atol=1e-3 |
| 647 | + |
642 | 648 | mhe2 = MovingHorizonEstimator(linmodel1, He=2, nint_u=[1, 1], nint_ym=[0, 0]) |
| 649 | + x̂ = updatestate!(mhe2, [10, 50], [50, 30], [5]) |
| 650 | + @test x̂ ≈ zeros(6) atol=1e-9 |
| 651 | + @test mhe2.x̂ ≈ zeros(6) atol=1e-9 |
| 652 | + @test evaloutput(mhe2, [5]) ≈ mhe2([5]) ≈ [50, 30] |
| 653 | + info = getinfo(mhe2) |
| 654 | + @test info[:x̂] ≈ x̂ atol=1e-9 |
| 655 | + @test info[:Ŷ][end-1:end] ≈ [50, 30] atol=1e-9 |
643 | 656 | for i in 1:100 |
644 | 657 | updatestate!(mhe2, [11, 52], [50, 30], [5]) |
645 | 658 | end |
@@ -698,41 +711,87 @@ end |
698 | 711 | end |
699 | 712 |
|
700 | 713 | @testset "MovingHorizonEstimator constraint violation" begin |
701 | | - linmodel1 = LinModel(sys,Ts,i_u=[1,2]) |
702 | | - f(x,u,_) = linmodel1.A*x + linmodel1.Bu*u |
703 | | - h(x,_) = linmodel1.C*x |
704 | | - nonlinmodel = setop!(NonLinModel(f, h, Ts, 2, 2, 2), uop=[10,50], yop=[50,30]) |
705 | | - mhe = MovingHorizonEstimator(nonlinmodel, He=1, nint_ym=0) |
| 714 | + linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30]) |
| 715 | + mhe = MovingHorizonEstimator(linmodel1, He=1, nint_ym=0) |
706 | 716 |
|
707 | 717 | setconstraint!(mhe, x̂min=[-100,-100], x̂max=[100,100]) |
708 | 718 | setconstraint!(mhe, ŵmin=[-100,-100], ŵmax=[100,100]) |
709 | 719 | setconstraint!(mhe, v̂min=[-100,-100], v̂max=[100,100]) |
710 | 720 |
|
711 | 721 | setconstraint!(mhe, x̂min=[1,1], x̂max=[100,100]) |
712 | 722 | x̂ = updatestate!(mhe, [10, 50], [50, 30]) |
713 | | - @test x̂ ≈ [1, 1] atol=1e-3 |
| 723 | + @test x̂ ≈ [1, 1] atol=1e-2 |
714 | 724 |
|
715 | 725 | setconstraint!(mhe, x̂min=[-100,-100], x̂max=[-1,-1]) |
716 | 726 | x̂ = updatestate!(mhe, [10, 50], [50, 30]) |
717 | | - @test x̂ ≈ [-1, -1] atol=1e-3 |
| 727 | + @test x̂ ≈ [-1, -1] atol=1e-2 |
718 | 728 |
|
719 | 729 | setconstraint!(mhe, x̂min=[-100,-100], x̂max=[100,100]) |
720 | 730 | setconstraint!(mhe, ŵmin=[-100,-100], ŵmax=[100,100]) |
721 | 731 | setconstraint!(mhe, v̂min=[-100,-100], v̂max=[100,100]) |
722 | 732 |
|
723 | 733 | setconstraint!(mhe, ŵmin=[1,1], ŵmax=[100,100]) |
724 | 734 | x̂ = updatestate!(mhe, [10, 50], [50, 30]) |
725 | | - @test mhe.Ŵ ≈ [1,1] atol=1e-3 |
| 735 | + @test mhe.Ŵ ≈ [1,1] atol=1e-2 |
726 | 736 |
|
727 | 737 | setconstraint!(mhe, ŵmin=[-100,-100], ŵmax=[-1,-1]) |
728 | 738 | x̂ = updatestate!(mhe, [10, 50], [50, 30]) |
729 | | - @test mhe.Ŵ ≈ [-1,-1] atol=1e-3 |
| 739 | + @test mhe.Ŵ ≈ [-1,-1] atol=1e-2 |
730 | 740 |
|
731 | 741 | setconstraint!(mhe, x̂min=[-100,-100], x̂max=[100,100]) |
732 | 742 | setconstraint!(mhe, ŵmin=[-100,-100], ŵmax=[100,100]) |
733 | 743 | setconstraint!(mhe, v̂min=[-100,-100], v̂max=[100,100]) |
734 | 744 |
|
735 | | - #setconstraint!(mhe, v̂min=[1,1], v̂max=[100,100]) |
736 | | - #x̂ = updatestate!(mhe, [10, 50], [50, 30]) |
737 | | - #@test [50,30] - ModelPredictiveControl.ĥ(mhe,mhe.mode,X̂ ≈ [1,1] atol=1e-3 |
| 745 | + setconstraint!(mhe, v̂min=[1,1], v̂max=[100,100]) |
| 746 | + x̂ = updatestate!(mhe, [10, 50], [50, 30]) |
| 747 | + info = getinfo(mhe) |
| 748 | + @test info[:V̂] ≈ [1,1] atol=1e-2 |
| 749 | + |
| 750 | + setconstraint!(mhe, v̂min=[-100,-100], v̂max=[-1,-1]) |
| 751 | + x̂ = updatestate!(mhe, [10, 50], [50, 30]) |
| 752 | + info = getinfo(mhe) |
| 753 | + @test info[:V̂] ≈ [-1,-1] atol=1e-2 |
| 754 | + |
| 755 | + f(x,u,_) = linmodel1.A*x + linmodel1.Bu*u |
| 756 | + h(x,_) = linmodel1.C*x |
| 757 | + nonlinmodel = setop!(NonLinModel(f, h, Ts, 2, 2, 2), uop=[10,50], yop=[50,30]) |
| 758 | + mhe2 = MovingHorizonEstimator(nonlinmodel, He=1, nint_ym=0) |
| 759 | + |
| 760 | + setconstraint!(mhe2, x̂min=[-100,-100], x̂max=[100,100]) |
| 761 | + setconstraint!(mhe2, ŵmin=[-100,-100], ŵmax=[100,100]) |
| 762 | + setconstraint!(mhe2, v̂min=[-100,-100], v̂max=[100,100]) |
| 763 | + |
| 764 | + setconstraint!(mhe2, x̂min=[1,1], x̂max=[100,100]) |
| 765 | + x̂ = updatestate!(mhe2, [10, 50], [50, 30]) |
| 766 | + @test x̂ ≈ [1, 1] atol=1e-2 |
| 767 | + |
| 768 | + setconstraint!(mhe2, x̂min=[-100,-100], x̂max=[-1,-1]) |
| 769 | + x̂ = updatestate!(mhe2, [10, 50], [50, 30]) |
| 770 | + @test x̂ ≈ [-1, -1] atol=1e-2 |
| 771 | + |
| 772 | + setconstraint!(mhe2, x̂min=[-100,-100], x̂max=[100,100]) |
| 773 | + setconstraint!(mhe2, ŵmin=[-100,-100], ŵmax=[100,100]) |
| 774 | + setconstraint!(mhe2, v̂min=[-100,-100], v̂max=[100,100]) |
| 775 | + |
| 776 | + setconstraint!(mhe2, ŵmin=[1,1], ŵmax=[100,100]) |
| 777 | + x̂ = updatestate!(mhe2, [10, 50], [50, 30]) |
| 778 | + @test mhe2.Ŵ ≈ [1,1] atol=1e-2 |
| 779 | + |
| 780 | + setconstraint!(mhe2, ŵmin=[-100,-100], ŵmax=[-1,-1]) |
| 781 | + x̂ = updatestate!(mhe2, [10, 50], [50, 30]) |
| 782 | + @test mhe2.Ŵ ≈ [-1,-1] atol=1e-2 |
| 783 | + |
| 784 | + setconstraint!(mhe2, x̂min=[-100,-100], x̂max=[100,100]) |
| 785 | + setconstraint!(mhe2, ŵmin=[-100,-100], ŵmax=[100,100]) |
| 786 | + setconstraint!(mhe2, v̂min=[-100,-100], v̂max=[100,100]) |
| 787 | + |
| 788 | + setconstraint!(mhe2, v̂min=[1,1], v̂max=[100,100]) |
| 789 | + x̂ = updatestate!(mhe2, [10, 50], [50, 30]) |
| 790 | + info = getinfo(mhe2) |
| 791 | + @test info[:V̂] ≈ [1,1] atol=1e-2 |
| 792 | + |
| 793 | + setconstraint!(mhe2, v̂min=[-100,-100], v̂max=[-1,-1]) |
| 794 | + x̂ = updatestate!(mhe2, [10, 50], [50, 30]) |
| 795 | + info = getinfo(mhe2) |
| 796 | + @test info[:V̂] ≈ [-1,-1] atol=1e-2 |
738 | 797 | end |
0 commit comments