class Rail(Component): LEFT = 1 RIGHT = 0 AWAY_POSITION = 0 HOME_POSITION = 16700 /2 WAIT_FOR_SORTING_POSITION = HOME_POSITION - (600/2) def __init__(self): super(Rail, self).__init__(self.state_sorting_position) self.is_moving_val = False self.current_position = self.WAIT_FOR_SORTING_POSITION; self.stepper = Stepper(**devices["stepper_rail"]) self.switch_home = Switch(devices["rail"]["switch_home"]) self.switch_away = Switch(devices["rail"]["switch_away"]) def stop(self): print "[Rail.stop] Stop stepper rail" self.stepper.stop(); def go_to_position(self, destination, stop_condition = None): self.is_moving_val = True steps = destination - self.current_position print "steps %d" % steps if steps == 0: return 0 direction = max(0,min(steps, 1)) #0 when going to away, 1 when going to home print "move of %d to %d" % (abs(steps), direction) self.stepper.move(direction,abs(steps), stop_condition) return abs(steps) def slide_to_home(self): self.is_moving_val = True self.set_state( self.state_slide_to_home ); def slide_to_wait_for_sorting_position(self): self.is_moving_val = True self.set_state( self.state_slide_to_wait_for_sorting_position ); def slide_to_away(self): self.is_moving_val = True self.set_state( self.state_slide_to_away ); def state_sorting_position(self): self.is_moving_val = False yield self.state_sorting_position def state_home(self): self.is_moving_val = False yield self.state_home def state_away(self): self.is_moving_val = False yield self.state_away def state_slide_to_home(self): if not self.is_home(): distance = self.go_to_position(self.HOME_POSITION, self.is_home) self.current_position = self.HOME_POSITION while self.stepper.is_moving(): yield else: self.current_position = self.HOME_POSITION yield self.state_check_homing def state_slide_to_wait_for_sorting_position(self): distance = self.go_to_position(self.WAIT_FOR_SORTING_POSITION) self.current_position = self.WAIT_FOR_SORTING_POSITION # no validation possible... while self.stepper.is_moving(): yield yield self.state_sorting_position def state_slide_to_away(self): if not self.is_away(): distance = self.go_to_position(self.AWAY_POSITION, self.is_away) self.current_position = self.AWAY_POSITION while self.stepper.is_moving(): yield else: self.current_position = self.AWAY_POSITION yield self.state_check_away def state_check_away(self): while not self.is_away(): self.stepper.move(self.RIGHT, self.HOME_POSITION, self.is_away) while self.stepper.is_moving(): yield yield self.state_away def state_check_homing(self): while not self.is_home(): self.stepper.move(self.LEFT, self.HOME_POSITION, self.is_home) while self.stepper.is_moving(): yield yield self.state_home def is_home(self): return self.switch_home.is_pressed() def is_away(self): return self.switch_away.is_pressed() def is_moving(self): return self.is_moving_val
class CamionFoot: CAMION_FOOT_CONFIG_ID = "camion_foot" FOOT_UP_SWITCH_ID = "camion_foot_up_switch" FOOT_DOWN_SWITCH_ID = "camion_foot_down_switch" FOOT_STEPPER_ID = "camion_stepper" DROP_FOOT_DIRECTION = 1 LIFT_FOOT_DIRECTION = 0 __TIMEOUT_PROTECTION_INTERFERENCE = 2.0 def __init__(self): print "[Camion.__init__]" self.config = config.devices[self.CAMION_FOOT_CONFIG_ID] self.stepper = Stepper(**config.devices[self.FOOT_STEPPER_ID]) self.down_switch = Switch(**config.devices[self.FOOT_DOWN_SWITCH_ID]) self.up_switch = Switch(**config.devices[self.FOOT_UP_SWITCH_ID]) self.__last_time_foot_was_brought_up = None def stop(self): print "[CamionFoot.stop] Stop foot" self.stepper.stop() def go_to_initial_position(self): print "[CamionFoot.go_to_initial_position] Drop foot then go up a li' bit more" self.drop() print "[CamionFoot.go_to_initial_position] A lil' bit more.." self.stepper.move(self.DROP_FOOT_DIRECTION, self.config["stepper_start_position_ticks"]) while self.stepper.is_moving(): time.sleep(0.01) def put_in_start_position(self): print "[CamionFoot.put_in_start_position]" #Drop camion. Foot on floor so bring it up more than you need to so the switch on the collector is activated. self.stepper.move(self.LIFT_FOOT_DIRECTION, 2*self.config["stepper_start_position_ticks"]) while self.stepper.is_moving(): time.sleep(0.01) time.sleep(1) self.drop() # Make sure the foot touch the ground def drop(self): print "[CamionFoot.drop]" #Bring it up till it's done! __protection_interference = self.__last_time_foot_was_brought_up if __protection_interference and __protection_interference + self.__TIMEOUT_PROTECTION_INTERFERENCE > time.time(): return #Ignorer ce drop while not self.down_switch.is_pressed(): self.stepper.move(self.DROP_FOOT_DIRECTION, self.config["stepper_complete_ticks"], self.down_switch.is_pressed) while self.stepper.is_moving(): time.sleep(0.01) #It's ok, only component on the truck time.sleep(0.01) def bring_up(self): print "[CamionFoot.bring_up]" #Bring it up till it's done! while not self.up_switch.is_pressed(): self.stepper.move(self.LIFT_FOOT_DIRECTION, self.config["stepper_complete_ticks"], self.up_switch.is_pressed) while self.stepper.is_moving(): time.sleep(0.01) #It's ok, only component on the truck time.sleep(0.01) self.__last_time_foot_was_brought_up = time.time()