LqrTest.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2021, Qiayuan Liao
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *******************************************************************************/
33 
34 //
35 // Created by chenzheng on 3/20/21.
36 //
37 
38 #include "rm_common/lqr.h"
39 #include <gtest/gtest.h>
40 
41 TEST(Lqr, lqr)
42 {
43  static const size_t STATE_DIM = 6;
44  static const size_t CONTROL_DIM = 2;
45 
46  Eigen::Matrix<double, STATE_DIM, STATE_DIM> A{}, Q{};
47  Eigen::Matrix<double, STATE_DIM, CONTROL_DIM> B{};
48  Eigen::Matrix<double, CONTROL_DIM, CONTROL_DIM> R{};
49  Eigen::Matrix<double, CONTROL_DIM, STATE_DIM> K{};
50  Eigen::Matrix<double, CONTROL_DIM, 1> Nbar{};
51 
52  A << 0, 1, 0, 0, 0, 0, 432.8077, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 73.9215, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
53  0, 0, 0;
54 
55  B << 0, 0, -49.6930, -49.6930, 0, 0, 7.3620, 7.3620, 0, 0, -26.3737, 26.3737;
56 
57  Q << 10, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1;
58 
59  R << 1, 0, 0, 1;
60 
61  K << -36.4111581829, -1.5198212494, -2.2360679774, -2.4573417169, -0.7071067811, -0.7258175096, -36.4111581829,
62  -1.5198212494, -2.2360679774, -2.4573417169, 0.7071067811, 0.7258175096;
63 
64  Lqr<double> lqr(A, B, Q, R);
65  lqr.computeK();
66  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> average = lqr.getK();
67  for (uint j = 0; j < CONTROL_DIM; ++j)
68  {
69  for (uint i = 0; i < STATE_DIM; ++i)
70  {
71  EXPECT_NEAR(K(j, i), average(j, i), 1e-9);
72  }
73  }
74 }
Lqr
Definition: lqr.h:45
lqr.h
Lqr::getK
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > getK()
Definition: lqr.h:158
TEST
TEST(Lqr, lqr)
Definition: LqrTest.cpp:41
Lqr::computeK
bool computeK()
Definition: lqr.h:132


rm_common
Author(s):
autogenerated on Sun Apr 6 2025 02:22:01