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 __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 __init__(self): print "[Camion.__init__]" self.foot = CamionFoot() config.devices[self.COLLECTOR_SWITCH_ID]["detect_edges"] = GPIO.BOTH self.collector_switch = Switch(**config.devices[self.COLLECTOR_SWITCH_ID]) #used by the truck to know when he have to drop the foot config.devices[self.PLACE_IN_POSITION_SWITCH_ID]["detect_edges"] = GPIO.RISING self.in_position_switch = Switch(**config.devices[self.PLACE_IN_POSITION_SWITCH_ID]) self.rf_receiver = RFReceiver(**config.devices[self.RF_SWITCH]) self.safe_lift_foot = None
class Camion: PLACE_IN_POSITION_SWITCH_ID = "camion_in_position_switch" COLLECTOR_SWITCH_ID = "camion_collector_switch" RF_SWITCH = "camion_rf_switch" WATCH_FOOT_TIMEOUT = 60 def __init__(self): print "[Camion.__init__]" self.foot = CamionFoot() config.devices[self.COLLECTOR_SWITCH_ID]["detect_edges"] = GPIO.BOTH self.collector_switch = Switch(**config.devices[self.COLLECTOR_SWITCH_ID]) #used by the truck to know when he have to drop the foot config.devices[self.PLACE_IN_POSITION_SWITCH_ID]["detect_edges"] = GPIO.RISING self.in_position_switch = Switch(**config.devices[self.PLACE_IN_POSITION_SWITCH_ID]) self.rf_receiver = RFReceiver(**config.devices[self.RF_SWITCH]) self.safe_lift_foot = None def activate_bindings(self): self.collector_switch.bind_rising_edge(self.drop_foot_safe) self.collector_switch.bind_falling_edge(self.bring_foot_up_safe) def stop(self): print "[Camion.stop] Stop camion" self.is_running = False self.foot.stop() PWM.cleanup() def force_stop(self): self.stop() sys.exit(0); def listen_stop_signal(self): time.sleep(60*14) self.rf_receiver.reset() self.rf_receiver.wait_for_signal() self.is_running = False self.bring_foot_up_safe() #Remonter le pied a la fin def run(self): self.is_running = True print "[Camion.run] Put camion down and wait for go_to_start_position signal" self.put_in_initial_position() print "[Camion.run] Start camion - waiting for signal" self.rf_receiver.wait_for_signal() print "[Camion.run] Signal received" # Removed because of other teams tests. stop_listener = threading.Thread(target=self.listen_stop_signal) stop_listener.start() self.activate_bindings(); #Activate the home switch bindings print "[Camion.run] Camion started" self.put_in_start_position(); while self.is_running: time.sleep(0.01) print "[Camion.run] Camion stopped" def put_in_initial_position(self): """Wait to put in position signal here""" print "[Camion.put_in_waiting_for_signal_position] Waiting for start switch" self.in_position_switch.wait_pressed() self.foot.go_to_initial_position() def set_received_rf(self): self.rf_received = True def put_in_start_position(self): self.foot.put_in_start_position() def drop_foot(self): self.foot.drop() def bring_foot_up(self): self.foot.bring_up() def drop_foot_safe(self): self.foot.drop() self.safe_lift_foot = threading.Thread(target=self.__watch_foot_timeout) self.safe_lift_foot.is_running = True self.safe_lift_foot.start() def bring_foot_up_safe(self): if self.safe_lift_foot: self.safe_lift_foot.is_running = False self.foot.bring_up() def __watch_foot_timeout(self): start_time = time.time() while self.safe_lift_foot.is_running and start_time + self.WATCH_FOOT_TIMEOUT > time.time(): time.sleep(0.25) if self.safe_lift_foot.is_running: self.bring_foot_up_safe()
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()