77import unittest
88from collections import deque
99
10- SHOW_PLOTS = False
10+ SHOW_PLOTS = True
1111
1212def plot_trajectory (Ns , x_value , u_value , zmp_refs , dt ):
1313 plt .figure (figsize = (8 , 4 ))
@@ -20,6 +20,7 @@ def plot_trajectory(Ns, x_value, u_value, zmp_refs, dt):
2020 plt .legend ()
2121 plt .tight_layout ()
2222 plt .show ()
23+
2324def zmp_pattern (ns ):
2425 zref = np .zeros ((2 , ns ))
2526 for i in range (ns ):
@@ -41,10 +42,8 @@ def zmp_pattern(ns):
4142tf = 3.0 # final time
4243
4344vars = list ()
44- for i in range (Ns ):
45- vars .append ((f"x{ i } " , nx ))
46- vars .append ((f"u{ i } " , nu ))
47- vars .append ((f"x{ Ns } " , nx ))
45+ vars .append (("x" , nx ))
46+ vars .append (("u" , nu ))
4847
4948variables = OptvarHelper (vars )
5049print (f"variables.getSize(): { variables .getSize ()} " )
@@ -58,63 +57,59 @@ def euler(x, xdot, dt):
5857
5958h = 0.83 # height of the CoM
6059dt = tf / Ns
61- integration = list ()
6260
63- for i in range (Ns ):
64- x0 = variables .getVariable (f"x{ i } " )
65- u0 = variables .getVariable (f"u{ i } " )
6661
67- r = x0 [0 :2 ]
68- rdot = x0 [2 :]
69- rddot = lipm (r , u0 , h )
62+ x = variables .getVariable (f"x" )
63+ u = variables .getVariable (f"u" )
7064
71- xdot0 = AffineHelper .pile (rdot , rddot )
65+ r = x [0 :2 ]
66+ rdot = x [2 :]
67+ rddot = lipm (r , u , h )
7268
73- integration_ = euler (x0 , xdot0 , dt )
74- integration .append (GenericTask (f"integration_{ i } " , integration_ .getM (), - integration_ .getq ()))
69+ xdot = AffineHelper .pile (rdot , rddot )
7570
71+ integration_ = euler (x , xdot , dt )
72+ integration = GenericTask (f"integration" , integration_ .getM (), - integration_ .getq ())
73+
74+
75+ print (f"integration.getA()\n : { integration .getA ()} " )
7676
77- print (f"integration[0].getA()\n : { integration [0 ].getA ()} " )
7877# to retrieve A and B for each stage:
79- print (f"integration[0].getA() @ variables.getVariable(x0).getM().T \n : { integration [0 ].getA () @ variables .getVariable ("x0" ).getM ().T } " )
80- print (f"integration[0].getA() @ variables.getVariable(u0).getM().T \n : { integration [0 ].getA () @ variables .getVariable ("u0" ).getM ().T } " )
78+ print (f"integration.getA() @ x.getM().T \n : { integration .getA () @ x .getM ().T } " )
79+ print (f"integration.getA() @ u.getM().T \n : { integration .getA () @ u .getM ().T } " )
80+
81+
8182
8283solver = hpipmoc .hpipmOC (Ns )
8384for i in range (Ns ):
84- Ai = integration [ i ] .getA () @ variables . getVariable ( f"x { i } " ) .getM ().T
85- Bi = integration [ i ] .getA () @ variables . getVariable ( f"u { i } " ) .getM ().T
85+ Ai = integration .getA () @ x .getM ().T
86+ Bi = integration .getA () @ u .getM ().T
8687 bi = np .zeros ((nx , 1 ))
8788 solver .setStageDynamics (i , Ai , Bi , bi )
8889
89- #xinit = variables.getVariable("x0") + np.array([0., 0., 0, 0.])
90- #initial_state = GenericTask("initial_state", xinit.getM(), -xinit.getq())
91-
92- #print(f"initial_state.getA()\n: {initial_state.getA()}")
93- #print(f"initial_state.getA() @ variables.getVariable(x0).getM().T\n: {initial_state.getA() @ variables.getVariable("x0").getM().T}")
9490
95- #id = [0, 1, 2, 3] # these are the indices of the first 4 state variables (x) at stage 0
96- #solver.setBoxConstraintsX(0, id, initial_state.getb(), initial_state.getb())
9791
9892zmp_tasks = list ()
9993for i in range (Ns ):
100- min_ui = GenericTask ("zmp_tracking" , variables . getVariable ( f"u { i } " ) .getM (), - variables . getVariable ( f"u { i } " ) .getq ())
94+ min_ui = GenericTask ("zmp_tracking" , u .getM (), - u .getq ())
10195 min_ui .setWeight (1e4 * np .array ([[1 , 0 ], [0 , 1 ]]))
10296 zmp_tasks .append (min_ui )
10397
10498print (f"zmp_tasks[2].getA()\n : { zmp_tasks [2 ].getA ()} " )
105- print (f"zmp_tasks[2].getA() @ variables.getVariable(u2).getM(). T\n : { zmp_tasks [2 ].getA () @ variables . getVariable ( "u2" ) .getM ().T } " )
99+ print (f"zmp_tasks[2].getA() @ u. T\n : { zmp_tasks [2 ].getA () @ u .getM ().T } " )
106100
107101print (f"zmp_tasks[2].getb()\n : { zmp_tasks [2 ].getb ()} " )
108102print (f"zmp_tasks[2].getWeight()\n : { zmp_tasks [2 ].getWeight ()} " )
109103
104+
110105zmp_refs = zmp_pattern (Ns )
111106for i in range (Ns ):
112107 zmp_tasks [i ].setb (zmp_refs [:, i ])
113108 zmp_tasks [i ].update ()
114109
115110x_tasks = list ()
116111for i in range (Ns + 1 ):
117- min_xi = GenericTask ("min_x" , variables . getVariable ( f"x { i } " ) .getM (), variables . getVariable ( f"x { i } " ) .getq ())
112+ min_xi = GenericTask ("min_x" , x .getM (), - x .getq ())
118113 Q = 1e-3 * np .array ([[0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ], [0 , 0 , 1 , 0 ], [0 , 0 , 0 , 1 ]])
119114 if i == Ns :
120115 Q = 1e6 * np .array ([[0 , 0 , 0 , 0 ], [0 , 0 , 0 , 0 ], [0 , 0 , 1 , 0 ], [0 , 0 , 0 , 1 ]])
@@ -123,10 +118,9 @@ def euler(x, xdot, dt):
123118
124119
125120for i in range (Ns ):
126- solver .setLSCost (i ,
127- x_tasks [i ].getA () @ variables .getVariable (f"x{ i } " ).getM ().T , x_tasks [i ].getWeight (), x_tasks [i ].getb (),
128- zmp_tasks [i ].getA () @ variables .getVariable (f"u{ i } " ).getM ().T , zmp_tasks [i ].getWeight (), zmp_tasks [i ].getb ())
129- solver .setLSCost (Ns , x_tasks [Ns ].getA () @ variables .getVariable (f"x{ Ns } " ).getM ().T , x_tasks [Ns ].getWeight (), x_tasks [Ns ].getb ())
121+ solver .setLSCost (i , x_tasks [i ].getA () @ x .getM ().T , x_tasks [i ].getWeight (), x_tasks [i ].getb (),
122+ zmp_tasks [i ].getA () @ u .getM ().T , zmp_tasks [i ].getWeight (), zmp_tasks [i ].getb ())
123+ solver .setLSCost (Ns , x_tasks [Ns ].getA () @ x .getM ().T , x_tasks [Ns ].getWeight (), x_tasks [Ns ].getb ())
130124
131125tic ()
132126success = solver .solve (np .array ([0. , 0. , 0 , 0. ]))
@@ -154,6 +148,7 @@ def euler(x, xdot, dt):
154148else :
155149 print ("HPIPM could not solve the problem!" )
156150
151+
157152# zeroing references
158153for i in range (Ns ):
159154 zmp_tasks [i ].setb (np .zeros ((2 , 1 )))
@@ -182,8 +177,6 @@ def euler(x, xdot, dt):
182177 ry .append (x0 [1 ,0 ])
183178 ryplot = ry .popleft ()
184179
185- #initial_state.setb(x0)
186- #initial_state.update()
187180
188181 # shift reference to left
189182 for j in range (1 , Ns ):
@@ -197,10 +190,9 @@ def euler(x, xdot, dt):
197190 # update internal qp
198191 #solver.setBoxConstraintsX(0, id, initial_state.getb(), initial_state.getb())
199192 for i in range (Ns ):
200- solver .setLSCost (i ,
201- x_tasks [i ].getA () @ variables .getVariable (f"x{ i } " ).getM ().T , x_tasks [i ].getWeight (), x_tasks [i ].getb (),
202- zmp_tasks [i ].getA () @ variables .getVariable (f"u{ i } " ).getM ().T , zmp_tasks [i ].getWeight (), zmp_tasks [i ].getb ())
203- solver .setLSCost (Ns , x_tasks [Ns ].getA () @ variables .getVariable (f"x{ Ns } " ).getM ().T , x_tasks [Ns ].getWeight (), x_tasks [Ns ].getb ())
193+ solver .setLSCost (i , x_tasks [i ].getA () @ x .getM ().T , x_tasks [i ].getWeight (), x_tasks [i ].getb (),
194+ zmp_tasks [i ].getA () @ u .getM ().T , zmp_tasks [i ].getWeight (), zmp_tasks [i ].getb ())
195+ solver .setLSCost (Ns , x_tasks [Ns ].getA () @ x .getM ().T , x_tasks [Ns ].getWeight (), x_tasks [Ns ].getb ())
204196
205197 tic ()
206198 success = solver .solve (x0 )
@@ -213,8 +205,6 @@ def euler(x, xdot, dt):
213205 u_value [:, i ] = solution [i ].u
214206 x_value [:, Ns ] = solution [Ns ].x
215207
216- utest .assertTrue (( np .linalg .norm (u_value [:,0 ] - zmp_tasks [0 ].getb (), ord = 2 ) <= 1e-4 ).all ())
217-
218208 rdot = x_value [2 :4 , 0 ].flatten ()
219209 rddot = lipm (x_value [0 :2 , 0 ], u_value [:, 0 ], h )
220210
0 commit comments