@@ -114,6 +114,7 @@ def rotate_cube(self, direction, nb):
114114 ramp_up_sp = 0 )
115115 self .turntable .wait_for_running ()
116116 self .turntable .wait_for_position (final_pos )
117+ self .turntable .wait_for_stop ()
117118
118119 if nb >= 1 :
119120 for i in range (nb ):
@@ -152,13 +153,15 @@ def rotate_cube_blocked(self, direction, nb):
152153 ramp_up_sp = 0 )
153154 self .turntable .wait_for_running ()
154155 self .turntable .wait_for_position (temp_pos )
156+ self .turntable .wait_for_stop ()
155157
156158 self .turntable .run_to_abs_pos (position_sp = final_pos ,
157159 speed_sp = int (Rubiks .rotate_speed / 4 ),
158160 stop_action = 'hold' ,
159161 ramp_up_sp = 0 )
160162 self .turntable .wait_for_running ()
161163 self .turntable .wait_for_position (final_pos , stall_ok = True )
164+ self .turntable .wait_for_stop ()
162165
163166 def rotate_cube_blocked_1 (self ):
164167 self .rotate_cube_blocked (1 , 1 )
@@ -177,27 +180,38 @@ def flipper_hold_cube(self, speed=300):
177180 if (current_position <= Rubiks .hold_cube_pos - 10 or
178181 current_position >= Rubiks .hold_cube_pos + 10 ):
179182 self .flipper .run_to_abs_pos (position_sp = Rubiks .hold_cube_pos ,
180- # ramp_down_sp=400,
183+ ramp_down_sp = 400 ,
181184 speed_sp = speed )
182185 self .flipper .wait_for_running ()
183- self .flipper .wait_for_position (Rubiks .hold_cube_pos , stall_ok = True )
186+ self .flipper .wait_for_position (Rubiks .hold_cube_pos )
187+ self .flipper .wait_for_stop ()
188+ sleep (0.05 )
184189
185190 def flipper_away (self , speed = 300 ):
186191 """
187192 Move the flipper arm out of the way
188193 """
189194 log .info ("flipper_away()" )
190195 self .flipper .run_to_abs_pos (position_sp = 0 ,
191- # ramp_down_sp=400,
196+ ramp_down_sp = 400 ,
192197 speed_sp = speed )
193198 self .flipper .wait_for_running ()
194199
195200 try :
196201 self .flipper .wait_for_position (0 )
202+ self .flipper .wait_for_stop ()
197203 except MotorStall :
198204 self .flipper .stop ()
199205
200206 def flip (self ):
207+ """
208+ Motors will sometimes stall if you call run_to_abs_pos() multiple
209+ times back to back on the same motor. To avoid this we call a 50ms
210+ sleep in flipper_hold_cube() and after each run_to_abs_pos() below.
211+
212+ We have to sleep after the 2nd run_to_abs_pos() because sometimes
213+ flip() is called back to back.
214+ """
201215 log .info ("flip()" )
202216
203217 if self .shutdown :
@@ -208,20 +222,24 @@ def flip(self):
208222
209223 # Grab the cube and pull back
210224 self .flipper .run_to_abs_pos (position_sp = 190 ,
211- # ramp_up_sp=200,
212- # ramp_down_sp=0,
225+ ramp_up_sp = 200 ,
226+ ramp_down_sp = 0 ,
213227 speed_sp = self .flip_speed )
214228 self .flipper .wait_for_running ()
215- self .flipper .wait_for_position (180 , stall_ok = True )
229+ self .flipper .wait_for_position (190 )
230+ self .flipper .wait_for_stop ()
231+ sleep (0.05 )
216232
217233 # At this point the cube is at an angle, push it forward to
218234 # drop it back down in the turntable
219235 self .flipper .run_to_abs_pos (position_sp = Rubiks .hold_cube_pos ,
220- # ramp_up_sp=200,
221- # ramp_down_sp=400,
236+ ramp_up_sp = 200 ,
237+ ramp_down_sp = 400 ,
222238 speed_sp = self .flip_speed_push )
223239 self .flipper .wait_for_running ()
224- self .flipper .wait_for_position (Rubiks .hold_cube_pos , stall_ok = True )
240+ self .flipper .wait_for_position (Rubiks .hold_cube_pos )
241+ self .flipper .wait_for_stop ()
242+ sleep (0.05 )
225243
226244 transformation = [2 , 4 , 1 , 3 , 0 , 5 ]
227245 self .apply_transformation (transformation )
@@ -233,6 +251,7 @@ def colorarm_middle(self):
233251 stop_action = 'hold' )
234252 self .colorarm .wait_for_running ()
235253 self .colorarm .wait_for_position (- 750 )
254+ self .colorarm .wait_for_stop ()
236255
237256 def colorarm_corner (self , square_index ):
238257 log .info ("colorarm_corner(%d)" % square_index )
@@ -255,7 +274,7 @@ def colorarm_corner(self, square_index):
255274
256275 def colorarm_edge (self , square_index ):
257276 log .info ("colorarm_edge(%d)" % square_index )
258- position_target = - 650
277+ position_target = - 640
259278
260279 if square_index == 2 :
261280 pass
@@ -279,6 +298,7 @@ def colorarm_remove(self):
279298 self .colorarm .wait_for_running ()
280299 try :
281300 self .colorarm .wait_for_position (0 )
301+ self .colorarm .wait_for_stop ()
282302 except MotorStall :
283303 self .colorarm .stop ()
284304
@@ -288,6 +308,7 @@ def colorarm_remove_halfway(self):
288308 speed_sp = 600 )
289309 self .colorarm .wait_for_running ()
290310 self .colorarm .wait_for_position (- 400 )
311+ self .colorarm .wait_for_stop ()
291312
292313 def scan_middle (self , face_number ):
293314 log .info ("scan_middle() %d/6" % face_number )
@@ -351,7 +372,7 @@ def scan_face(self, face_number):
351372 # The gear ratio is 3:1 so 1080 is one full rotation
352373 self .turntable .reset ()
353374 self .turntable .run_to_abs_pos (position_sp = 1080 ,
354- speed_sp = 200 ,
375+ speed_sp = Rubiks . rotate_speed ,
355376 stop_action = 'hold' )
356377
357378 while True :
@@ -421,7 +442,7 @@ def scan(self):
421442 if self .shutdown :
422443 return
423444
424- log .info ("RGB data :\n %s\n " % pformat (self .colors ))
445+ log .info ("RGB json :\n %s\n " % json . dumps (self .colors ))
425446 self .rgb_solver = RubiksColorSolver ()
426447 self .rgb_solver .enter_scan_data (self .colors )
427448 self .cube_kociemba = self .rgb_solver .crunch_colors ()
@@ -495,7 +516,6 @@ def resolve(self):
495516 if rub .shutdown :
496517 return
497518
498- # dwalton - run k here
499519 output = Popen (['kociemba' , '' .join (map (str , self .cube_kociemba ))], stdout = PIPE ).communicate ()[0 ]
500520 actions = output .strip ().split ()
501521 self .run_kociemba_actions (actions )
@@ -554,7 +574,6 @@ def wait_for_cube_insert(self):
554574 # Push the cube to the right so that it is in the expected
555575 # position when we begin scanning
556576 rub .flipper_hold_cube (100 )
557- sleep (0.1 )
558577 rub .flipper_away (100 )
559578
560579 rub .scan ()
0 commit comments