|
3 | 3 | author: Atsushi Sakai |
4 | 4 | """ |
5 | 5 |
|
6 | | -import matplotlib.pyplot as plt |
7 | | -import numpy as np |
8 | 6 | import math |
9 | 7 | import time |
| 8 | + |
10 | 9 | import cvxpy |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import numpy as np |
11 | 12 |
|
12 | 13 | # Model parameters |
13 | 14 |
|
@@ -39,10 +40,11 @@ def main(): |
39 | 40 | for i in range(50): |
40 | 41 |
|
41 | 42 | # calc control input |
42 | | - optimized_x, optimized_delta_x, optimized_theta, optimized_delta_theta, optimized_input = mpc_control(x) |
| 43 | + opt_x, opt_delta_x, opt_theta, opt_delta_theta, opt_input = \ |
| 44 | + mpc_control(x) |
43 | 45 |
|
44 | 46 | # get input |
45 | | - u = optimized_input[0] |
| 47 | + u = opt_input[0] |
46 | 48 |
|
47 | 49 | # simulate inverted pendulum cart |
48 | 50 | x = simulation(x, u) |
@@ -86,17 +88,19 @@ def mpc_control(x0): |
86 | 88 | print("calc time:{0} [sec]".format(elapsed_time)) |
87 | 89 |
|
88 | 90 | if prob.status == cvxpy.OPTIMAL: |
89 | | - ox = get_nparray_from_matrix(x.value[0, :]) |
90 | | - dx = get_nparray_from_matrix(x.value[1, :]) |
91 | | - theta = get_nparray_from_matrix(x.value[2, :]) |
92 | | - dtheta = get_nparray_from_matrix(x.value[3, :]) |
| 91 | + ox = get_numpy_array_from_matrix(x.value[0, :]) |
| 92 | + dx = get_numpy_array_from_matrix(x.value[1, :]) |
| 93 | + theta = get_numpy_array_from_matrix(x.value[2, :]) |
| 94 | + d_theta = get_numpy_array_from_matrix(x.value[3, :]) |
93 | 95 |
|
94 | | - ou = get_nparray_from_matrix(u.value[0, :]) |
| 96 | + ou = get_numpy_array_from_matrix(u.value[0, :]) |
| 97 | + else: |
| 98 | + ox, dx, theta, d_theta, ou = None, None, None, None, None |
95 | 99 |
|
96 | | - return ox, dx, theta, dtheta, ou |
| 100 | + return ox, dx, theta, d_theta, ou |
97 | 101 |
|
98 | 102 |
|
99 | | -def get_nparray_from_matrix(x): |
| 103 | +def get_numpy_array_from_matrix(x): |
100 | 104 | """ |
101 | 105 | get build-in list from matrix |
102 | 106 | """ |
@@ -133,7 +137,7 @@ def plot_cart(xt, theta): |
133 | 137 | radius = 0.1 |
134 | 138 |
|
135 | 139 | cx = np.array([-cart_w / 2.0, cart_w / 2.0, cart_w / |
136 | | - 2.0, -cart_w / 2.0, -cart_w / 2.0]) |
| 140 | + 2.0, -cart_w / 2.0, -cart_w / 2.0]) |
137 | 141 | cy = np.array([0.0, 0.0, cart_h, cart_h, 0.0]) |
138 | 142 | cy += radius * 2.0 |
139 | 143 |
|
|
0 commit comments