|
5 | 5 | """ |
6 | 6 |
|
7 | 7 | """ |
8 | | -solver = CplexSolver(CPX_PARAM_SCRIND=0) |
9 | | -
|
10 | | -const A = [1.0 0.0; |
11 | | - 0.0 1.0] |
12 | | -const B = [1.0 1.0; |
13 | | - 0.0 1.0] |
14 | | -const q = [1.0; 1.0] |
15 | | -const r = [1.0; 1.0] |
16 | | -
|
17 | | -const u_max = 0.1 |
18 | | -const T = 50 |
19 | | -const M = 10000.0 |
20 | 8 |
|
21 | 9 | function control(is, gs, ob) |
22 | 10 |
|
|
65 | 53 | return s_vec, u_vec |
66 | 54 | end |
67 | 55 |
|
68 | | -function plot_obstacle(ob) |
69 | | - for i in 1:length(ob[:,1]) |
70 | | - x = [ob[i,1],ob[i,2],ob[i,2],ob[i,1],ob[i,1]] |
71 | | - y = [ob[i,3],ob[i,3],ob[i,4],ob[i,4],ob[i,3]] |
72 | | - plt.plot(x,y,"-g") |
73 | | - end |
74 | | -end |
75 | 56 |
|
76 | 57 | function main() |
77 | | - println(PROGRAM_FILE," start!!") |
78 | | -
|
79 | | - s = [10.0, 5.0] # init state |
80 | | - gs = [5.0, 7.0] # goal state |
81 | | -
|
82 | | - ob = [7.0 8.0 3.0 8.0; |
83 | | - 5.5 6.0 6.0 10.0;] # [xmin xmax ymin ymax] |
84 | | -
|
85 | | - h_sx = [] |
86 | | - h_sy = [] |
87 | 58 |
|
88 | 59 | for i=1:10000 |
89 | | - s_p, u_p = control(s, gs, ob) |
90 | 60 |
|
91 | 61 | if sqrt((gs[1]-s[1])^2+(gs[2]-s[2])^2) <= 0.1 |
92 | 62 | println("Goal!!!") |
|
98 | 68 | push!(h_sx, s[1]) |
99 | 69 | push!(h_sy, s[2]) |
100 | 70 |
|
101 | | - plt.cla() |
102 | | - plt.plot(gs[1],gs[2],"*r") |
103 | | - plt.plot(s_p[1,:],s_p[2,:],"xb") |
104 | | - plot_obstacle(ob) |
105 | | - plt.plot(s_p[1,:],s_p[2,:],"xb") |
106 | | - plt.plot(h_sx,h_sy,"-b") |
107 | | - plt.plot(s[1],s[2],"or") |
108 | | - plt.axis("equal") |
109 | | - plt.grid(true) |
110 | | - plt.pause(0.0001) |
111 | | -
|
112 | 71 | end |
113 | 72 |
|
114 | 73 | plt.cla() |
|
123 | 82 | end |
124 | 83 | """ |
125 | 84 |
|
| 85 | +import cvxpy |
| 86 | +import numpy as np |
| 87 | +import matplotlib.pyplot as plt |
| 88 | + |
| 89 | +# parameter |
| 90 | +A = np.matrix([[1.0, 0.0], |
| 91 | + [0.0, 1.0]]) |
| 92 | +B = np.matrix([[1.0, 1.0], |
| 93 | + [0.0, 1.0]]) |
| 94 | +q = np.matrix([[1.0], |
| 95 | + [1.0]]) |
| 96 | +r = np.matrix([[1.0], |
| 97 | + [1.0]]) |
| 98 | + |
| 99 | +u_max = 0.1 |
| 100 | +T = 50 |
| 101 | +M = 10000.0 |
| 102 | + |
| 103 | + |
| 104 | +def plot_obstacle(ob): |
| 105 | + for i in range(len(ob)): |
| 106 | + x = [ob[i, 0], ob[i, 1], ob[i, 1], ob[i, 0], ob[i, 0]] |
| 107 | + y = [ob[i, 2], ob[i, 2], ob[i, 3], ob[i, 3], ob[i, 2]] |
| 108 | + plt.plot(x, y, "-g") |
| 109 | + |
| 110 | + |
| 111 | +def control(s1, gs, ob): |
| 112 | + |
| 113 | + # w = cvxpy.Variable(2, T) |
| 114 | + # v = cvxpy.Variable(2, T) |
| 115 | + s = cvxpy.Variable(2, T) |
| 116 | + u = cvxpy.Variable(2, T) |
| 117 | + # ob = 2 |
| 118 | + # o = cvxpy.Bool(4 * ob, T) |
| 119 | + |
| 120 | + constraints = [-u_max <= u, u <= u_max] |
| 121 | + |
| 122 | + constraints.append(s[:, 1] == s1) |
| 123 | + |
| 124 | + # obj = [s] |
| 125 | + # for i in range(T) |
| 126 | + |
| 127 | + # @constraint(model, s[:, i] - gs . <= w[:, i]) |
| 128 | + # @constraint(model, -s[:, i] + gs . <= w[:, i]) |
| 129 | + # @constraint(model, u[:, i] . <= v[:, i]) |
| 130 | + # @constraint(model, -u[:, i] . <= v[:, i]) |
| 131 | + # push!(obj, q'*w[1:end,i]+r' * v[1:2, i]) |
| 132 | + |
| 133 | + # # obstable avoidanse |
| 134 | + # for io in 1: |
| 135 | + # nob |
| 136 | + # start_ind = 1 + (io - 1) * 4 |
| 137 | + |
| 138 | + # @constraint(model, sum(o[start_ind:start_ind + 3, i]) <= 3) |
| 139 | + # @constraint(model, s[1, i] <= ob[io, 1] + M * o[start_ind, i]) |
| 140 | + # @constraint(model, -s[1, i] <= -ob[io, 2] + M * o[start_ind + 1, i]) |
| 141 | + # @constraint(model, s[2, i] <= ob[io, 3] + M * o[start_ind + 2, i]) |
| 142 | + # @constraint(model, -s[2, i] <= -ob[io, 4] + M * o[start_ind + 3, i]) |
| 143 | + # end |
| 144 | + # end |
| 145 | + |
| 146 | + for i in range(T - 1): |
| 147 | + constraints.append(s[:, 1] == s1) |
| 148 | + |
| 149 | + # @constraint(model, s[:, i + 1] . == A * s[:, i] + B * u[:, i]) |
| 150 | + |
| 151 | + objective = cvxpy.Minimize(cvxpy.sum_squares(s)) |
| 152 | + |
| 153 | + prob = cvxpy.Problem(objective, constraints) |
| 154 | + |
| 155 | + prob.solve() |
| 156 | + |
| 157 | + s_p = [] |
| 158 | + u_p = np.matrix([[0.1], [0.1]]) |
| 159 | + |
| 160 | + return s_p, u_p |
| 161 | + |
126 | 162 |
|
127 | 163 | def main(): |
128 | 164 | print(__file__ + " start!!") |
129 | 165 |
|
| 166 | + s = np.matrix([10.0, 5.0]).T # init state |
| 167 | + gs = np.matrix([5.0, 7.0]).T # goal state |
| 168 | + |
| 169 | + ob = np.matrix([[7.0, 8.0, 3.0, 8.0], |
| 170 | + [5.5, 6.0, 6.0, 10.0]]) # [xmin xmax ymin ymax] |
| 171 | + |
| 172 | + h_sx = [] |
| 173 | + h_sy = [] |
| 174 | + |
| 175 | + for i in range(10000): |
| 176 | + print(i) |
| 177 | + s_p, u_p = control(s, gs, ob) |
| 178 | + |
| 179 | + s = A * s + B * u_p[:, 0] # simulation |
| 180 | + |
| 181 | + h_sx.append(s[0, 0]) |
| 182 | + h_sy.append(s[1, 0]) |
| 183 | + |
| 184 | + plt.cla() |
| 185 | + plt.plot(gs[0], gs[1], "*r") |
| 186 | + plot_obstacle(ob) |
| 187 | + # plt.plot(s_p[1, :], s_p[2, :], "xb") |
| 188 | + # plt.plot(s_p[1, :], s_p[2, :], "xb") |
| 189 | + plt.plot(h_sx, h_sy, "-b") |
| 190 | + plt.plot(s[0], s[1], "or") |
| 191 | + plt.axis("equal") |
| 192 | + plt.grid(True) |
| 193 | + plt.pause(0.0001) |
| 194 | + |
130 | 195 |
|
131 | 196 | if __name__ == '__main__': |
132 | 197 | main() |
0 commit comments