Skip to content

Commit 100e583

Browse files
committed
finished testing dimensions
1 parent 49ff8b1 commit 100e583

File tree

5 files changed

+92
-12
lines changed

5 files changed

+92
-12
lines changed

BUILD

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
22

33
cc_library(
44
name = "mpcinterface",
@@ -11,6 +11,15 @@ cc_library(
1111
],
1212
)
1313

14+
cc_test(
15+
name = "mpcinterface_test",
16+
srcs = ["test/mpcinterface_test.cpp"],
17+
deps = [
18+
":mpcinterface",
19+
"@com_google_googletest//:gtest_main",
20+
],
21+
)
22+
1423
cc_binary(
1524
name = "main",
1625
srcs = ["main.cpp"],

WORKSPACE

+6
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ http_archive(
66
strip_prefix = "eigen-3.4.0",
77
build_file = "//third_party/eigen:BUILD",
88
)
9+
10+
http_archive(
11+
name = "com_google_googletest",
12+
urls = ["https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz"],
13+
strip_prefix = "googletest-1.14.0",
14+
)

include/mpcinterface.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class MPCInterface
1515

1616
// Inputs:
1717
// State cost matrix
18-
Eigen::VectorXd Q;
18+
Eigen::MatrixXd Q;
1919

2020
// Input cost matrix
21-
Eigen::VectorXd R;
21+
Eigen::MatrixXd R;
2222

2323
// Terminal state cost matrix
24-
Eigen::VectorXd P;
24+
Eigen::MatrixXd P;
2525

2626
// State lower bound
2727
Eigen::VectorXd x_min;
@@ -42,10 +42,10 @@ class MPCInterface
4242
Eigen::MatrixXd B;
4343

4444
// State reference
45-
Eigen::VectorXd x_ref;
45+
Eigen::MatrixXd x_ref;
4646

4747
// Input reference
48-
Eigen::VectorXd u_ref;
48+
Eigen::MatrixXd u_ref;
4949

5050
// Outputs:
5151
struct CanonicalForm {

src/mpcinterface.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ MPCInterface::MPCInterface (int H, int n, int m) {
1111
this->m = m;
1212

1313
// Initialize state cost matrix
14-
this->Q = Eigen::VectorXd::Zero(n, n);
14+
this->Q = Eigen::MatrixXd::Zero(n, n);
1515

1616
// Initialize input cost matrix
17-
this->R = Eigen::VectorXd::Zero(m, m);
17+
this->R = Eigen::MatrixXd::Zero(m, m);
1818

1919
// Initialize terminal state cost matrix
20-
this->P = Eigen::VectorXd::Zero(n, n);
20+
this->P = Eigen::MatrixXd::Zero(n, n);
2121

2222
// Initialize state lower bound
2323
this->x_min = Eigen::VectorXd::Zero(n);
@@ -38,18 +38,18 @@ MPCInterface::MPCInterface (int H, int n, int m) {
3838
this->B = Eigen::MatrixXd::Zero(n, m);
3939

4040
// Initialize state reference
41-
this->x_ref = Eigen::VectorXd::Zero(n);
41+
this->x_ref = Eigen::MatrixXd::Zero(n, H);
4242

4343
// Initialize input reference
44-
this->u_ref = Eigen::VectorXd::Zero(m);
44+
this->u_ref = Eigen::MatrixXd::Zero(m, H);
4545
}
4646

4747
MPCInterface::~MPCInterface() {}
4848

4949
MPCInterface::CanonicalForm MPCInterface::getCanonicalForm() {
5050

5151
// Create P_bar
52-
Eigen::MatrixXd P_bar((H * n + H * m), (H * n + H * m));
52+
Eigen::MatrixXd P_bar(H * (n + m), H * (n + m));
5353
P_bar.setZero(); // Initialize to zeros
5454
for (int i = 0; i < H; i++) {
5555
P_bar.block(i * n, i * n, n, n) = Q;

test/mpcinterface_test.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <Eigen/Core>
2+
#include "mpcinterface.h"
3+
#include "gtest/gtest.h"
4+
5+
TEST(MPCInterfaceTest, TestInitializedMatrixDimensions) {
6+
// Initialize horizon length
7+
int H = 5;
8+
9+
// Initialize state dimension
10+
int n = 3;
11+
12+
// Initialize input dimension
13+
int m = 2;
14+
15+
// Initialize MPCInterface object
16+
MPCInterface mpc(H, n, m);
17+
18+
EXPECT_EQ(mpc.Q.rows(), n);
19+
EXPECT_EQ(mpc.Q.cols(), n);
20+
EXPECT_EQ(mpc.R.rows(), m);
21+
EXPECT_EQ(mpc.R.cols(), m);
22+
EXPECT_EQ(mpc.P.rows(), n);
23+
EXPECT_EQ(mpc.P.cols(), n);
24+
EXPECT_EQ(mpc.x_min.size(), n);
25+
EXPECT_EQ(mpc.x_max.size(), n);
26+
EXPECT_EQ(mpc.u_min.size(), m);
27+
EXPECT_EQ(mpc.u_max.size(), m);
28+
EXPECT_EQ(mpc.A.rows(), n);
29+
EXPECT_EQ(mpc.A.cols(), n);
30+
EXPECT_EQ(mpc.B.rows(), n);
31+
EXPECT_EQ(mpc.B.cols(), m);
32+
EXPECT_EQ(mpc.x_ref.cols(), H);
33+
EXPECT_EQ(mpc.x_ref.rows(), n);
34+
EXPECT_EQ(mpc.u_ref.cols(), H);
35+
EXPECT_EQ(mpc.u_ref.rows(), m);
36+
}
37+
38+
TEST(MPCInterfaceTest, TestCanonicalMatrixDimensions) {
39+
// Initialize horizon length
40+
int H = 5;
41+
42+
// Initialize state dimension
43+
int n = 3;
44+
45+
// Initialize input dimension
46+
int m = 2;
47+
48+
// Initialize MPCInterface object
49+
MPCInterface mpc(H, n, m);
50+
51+
// Generate Canonical Form
52+
MPCInterface::CanonicalForm canonical_form = mpc.getCanonicalForm();
53+
54+
EXPECT_EQ(canonical_form.P_bar.rows(), H * (n + m));
55+
EXPECT_EQ(canonical_form.P_bar.cols(), H * (n + m));
56+
EXPECT_EQ(canonical_form.q_bar.size(), H * (n + m));
57+
EXPECT_EQ(canonical_form.A_bar.rows(), 2 * H * (n + m) + H * n);
58+
EXPECT_EQ(canonical_form.A_bar.cols(), H * (n + m));
59+
EXPECT_EQ(canonical_form.l_bar.size(), 2 * H * (n + m));
60+
EXPECT_EQ(canonical_form.u_bar.size(), 2 * H * (n + m));
61+
}
62+
63+
TEST(MPCInterfaceTest, TestCanonicalForm) {
64+
65+
}

0 commit comments

Comments
 (0)