@@ -110,83 +110,88 @@ def walk(self, t_sup=0.8, z_c=0.8, a=10, b=1, plot=False):
110110
111111 # plot
112112 if plot :
113- # for plot trajectory, plot in for loop
114- for c in range (len (self .com_trajectory )):
115- if c > len (com_trajectory_for_plot ):
116- # set up plotter
117- plt .cla ()
118- # for stopping simulation with the esc key.
119- plt .gcf ().canvas .mpl_connect (
120- 'key_release_event' ,
121- lambda event :
122- [exit (0 ) if event .key == 'escape' else None ])
123- ax .set_zlim (0 , z_c * 2 )
124- ax .set_xlim (0 , 1 )
125- ax .set_ylim (- 0.5 , 0.5 )
126-
127- # update com_trajectory_for_plot
128- com_trajectory_for_plot .append (self .com_trajectory [c ])
129-
130- # plot com
131- ax .plot ([p [0 ] for p in com_trajectory_for_plot ],
132- [p [1 ] for p in com_trajectory_for_plot ], [
133- 0 for _ in com_trajectory_for_plot ],
134- color = "red" )
135-
136- # plot inverted pendulum
137- ax .plot ([px_star , com_trajectory_for_plot [- 1 ][0 ]],
138- [py_star , com_trajectory_for_plot [- 1 ][1 ]],
139- [0 , z_c ], color = "green" , linewidth = 3 )
140- ax .scatter ([com_trajectory_for_plot [- 1 ][0 ]],
141- [com_trajectory_for_plot [- 1 ][1 ]],
142- [z_c ], color = "green" , s = 300 )
143-
144- # foot rectangle for self.ref_p
145- foot_width = 0.06
146- foot_height = 0.04
147- for j in range (len (self .ref_p )):
148- angle = self .ref_p [j ][2 ] + \
149- math .atan2 (foot_height ,
150- foot_width ) - math .pi
151- r = math .sqrt (
152- math .pow (foot_width / 3. , 2 ) + math .pow (
153- foot_height / 2. , 2 ))
154- rec = pat .Rectangle (xy = (
155- self .ref_p [j ][0 ] + r * math .cos (angle ),
156- self .ref_p [j ][1 ] + r * math .sin (angle )),
157- width = foot_width ,
158- height = foot_height ,
159- angle = self .ref_p [j ][
160- 2 ] * 180 / math .pi ,
161- color = "blue" , fill = False ,
162- ls = ":" )
163- ax .add_patch (rec )
164- art3d .pathpatch_2d_to_3d (rec , z = 0 , zdir = "z" )
165-
166- # foot rectangle for self.act_p
167- for j in range (len (self .act_p )):
168- angle = self .act_p [j ][2 ] + \
169- math .atan2 (foot_height ,
170- foot_width ) - math .pi
171- r = math .sqrt (
172- math .pow (foot_width / 3. , 2 ) + math .pow (
173- foot_height / 2. , 2 ))
174- rec = pat .Rectangle (xy = (
175- self .act_p [j ][0 ] + r * math .cos (angle ),
176- self .act_p [j ][1 ] + r * math .sin (angle )),
177- width = foot_width ,
178- height = foot_height ,
179- angle = self .act_p [j ][
180- 2 ] * 180 / math .pi ,
181- color = "blue" , fill = False )
182- ax .add_patch (rec )
183- art3d .pathpatch_2d_to_3d (rec , z = 0 , zdir = "z" )
184-
185- plt .draw ()
186- plt .pause (0.001 )
113+ self .plot_animation (ax , com_trajectory_for_plot , px_star ,
114+ py_star , z_c )
187115 if plot :
188116 plt .show ()
189117
118+ def plot_animation (self , ax , com_trajectory_for_plot , px_star , py_star ,
119+ z_c ): # pragma: no cover
120+ # for plot trajectory, plot in for loop
121+ for c in range (len (self .com_trajectory )):
122+ if c > len (com_trajectory_for_plot ):
123+ # set up plotter
124+ plt .cla ()
125+ # for stopping simulation with the esc key.
126+ plt .gcf ().canvas .mpl_connect (
127+ 'key_release_event' ,
128+ lambda event :
129+ [exit (0 ) if event .key == 'escape' else None ])
130+ ax .set_zlim (0 , z_c * 2 )
131+ ax .set_xlim (0 , 1 )
132+ ax .set_ylim (- 0.5 , 0.5 )
133+
134+ # update com_trajectory_for_plot
135+ com_trajectory_for_plot .append (self .com_trajectory [c ])
136+
137+ # plot com
138+ ax .plot ([p [0 ] for p in com_trajectory_for_plot ],
139+ [p [1 ] for p in com_trajectory_for_plot ], [
140+ 0 for _ in com_trajectory_for_plot ],
141+ color = "red" )
142+
143+ # plot inverted pendulum
144+ ax .plot ([px_star , com_trajectory_for_plot [- 1 ][0 ]],
145+ [py_star , com_trajectory_for_plot [- 1 ][1 ]],
146+ [0 , z_c ], color = "green" , linewidth = 3 )
147+ ax .scatter ([com_trajectory_for_plot [- 1 ][0 ]],
148+ [com_trajectory_for_plot [- 1 ][1 ]],
149+ [z_c ], color = "green" , s = 300 )
150+
151+ # foot rectangle for self.ref_p
152+ foot_width = 0.06
153+ foot_height = 0.04
154+ for j in range (len (self .ref_p )):
155+ angle = self .ref_p [j ][2 ] + \
156+ math .atan2 (foot_height ,
157+ foot_width ) - math .pi
158+ r = math .sqrt (
159+ math .pow (foot_width / 3. , 2 ) + math .pow (
160+ foot_height / 2. , 2 ))
161+ rec = pat .Rectangle (xy = (
162+ self .ref_p [j ][0 ] + r * math .cos (angle ),
163+ self .ref_p [j ][1 ] + r * math .sin (angle )),
164+ width = foot_width ,
165+ height = foot_height ,
166+ angle = self .ref_p [j ][
167+ 2 ] * 180 / math .pi ,
168+ color = "blue" , fill = False ,
169+ ls = ":" )
170+ ax .add_patch (rec )
171+ art3d .pathpatch_2d_to_3d (rec , z = 0 , zdir = "z" )
172+
173+ # foot rectangle for self.act_p
174+ for j in range (len (self .act_p )):
175+ angle = self .act_p [j ][2 ] + \
176+ math .atan2 (foot_height ,
177+ foot_width ) - math .pi
178+ r = math .sqrt (
179+ math .pow (foot_width / 3. , 2 ) + math .pow (
180+ foot_height / 2. , 2 ))
181+ rec = pat .Rectangle (xy = (
182+ self .act_p [j ][0 ] + r * math .cos (angle ),
183+ self .act_p [j ][1 ] + r * math .sin (angle )),
184+ width = foot_width ,
185+ height = foot_height ,
186+ angle = self .act_p [j ][
187+ 2 ] * 180 / math .pi ,
188+ color = "blue" , fill = False )
189+ ax .add_patch (rec )
190+ art3d .pathpatch_2d_to_3d (rec , z = 0 , zdir = "z" )
191+
192+ plt .draw ()
193+ plt .pause (0.001 )
194+
190195
191196if __name__ == "__main__" :
192197 bipedal_planner = BipedalPlanner ()
0 commit comments