def __init__(self): super(VacuumShaker, self).__init__(self.wait_init) print "[VacuumShaker.__init__]" self.vacuum_servo = VacuumShakerPiston(**config.devices[self.VACUUM_SERVO_ID]) config.devices[self.LOAD_TANK_SWITCH]["detect_edges"] = GPIO.BOTH self.load_tank_switch = Switch(**config.devices[self.LOAD_TANK_SWITCH]) self.last_button_push = time.time() self.is_init = False
def __init__(self, collector): super(CollectorController, self).__init__(self.state_wait_init) print "[CollectorController.__init__]" self.sorter = collector.sorter self.rail = collector.rail self.vacuum_shaker = collector.vacuum_shaker self.start_collect_switch = Switch(**config.devices[self.START_COLLECT]) self.foot_up_switch = MagneticSwitch(**config.devices[self.FOOT_UP]) self.foot_down_switch = MagneticSwitch(**config.devices[self.FOOT_DOWN]) self.gate = Piston(**config.devices[self.GATE])
def genDevice(self): """ Generate logical device instance and add thrift link to host """ for i in range(self.vertexNum): thriftPort = 9090 + i self.switches.append( Switch('s' + str(i), thriftPort, SwitchRuntime(thriftPort=thriftPort))) if self.hostList[i] == 1: self.hosts.append( Host('h' + str(i), self.genMac(i), self.genIp(i), self.genIp(i, True))) else: self.hosts.append(None)
class CollectorController(Component): WAIT_TIME_GATE = 1.5 WAIT_TIME_FOOT = 2.0 WAIT_TIME_DUMP_BALLS = 1.5 MAX_BALLS_PER_ROUND = 20 MAX_DELAY_BETWEEN_BALLS = 2.0 MAX_DELAY_CYCLE = 30.0 START_COLLECT = "start_collect_switch" GATE = "gate_servo" FOOT_UP = "collector_foot_up" FOOT_DOWN = "collector_foot_down" def __init__(self, collector): super(CollectorController, self).__init__(self.state_wait_init) print "[CollectorController.__init__]" self.sorter = collector.sorter self.rail = collector.rail self.vacuum_shaker = collector.vacuum_shaker self.start_collect_switch = Switch(**config.devices[self.START_COLLECT]) self.foot_up_switch = MagneticSwitch(**config.devices[self.FOOT_UP]) self.foot_down_switch = MagneticSwitch(**config.devices[self.FOOT_DOWN]) self.gate = Piston(**config.devices[self.GATE]) def stop(self): print "[CollectorController.stop] Stop controller" self.gate.stop() def state_wait_init(self): print "[CollectorController.state_wait_init]" # Initialiser la position du collecteur, self.rail.slide_to_home() while self.rail.is_moving(): yield self.rail.slide_to_wait_for_sorting_position() while self.rail.is_moving(): yield # Wait for start switch from collector if not self.vacuum_shaker.load_tank_switch.is_pressed(): self.vacuum_shaker.load_tank_switch.wait_pressed() print "[CollectorController.state_wait_init] load_tank_switch pressed" # Wait for start switch from truck if not self.start_collect_switch.is_pressed(): self.start_collect_switch.wait_pressed() print "[CollectorController.state_wait_init] start_collect_switch pressed" self.vacuum_shaker.is_init = True Logger().start_new_cycle() # Start first cycle here self.gate.push() yield self.state_push_truck_home def state_push_truck_home(self): print "[CollectorController.state_push_truck_home]" self.rail.slide_to_home() while self.rail.is_moving(): yield yield self.state_wait_sorter def state_push_truck_away(self): print "[CollectorController.state_push_truck_away]" self.rail.slide_to_away() while self.rail.is_moving(): yield Logger().end_current_cycle() self.vacuum_shaker.wait_balls() yield self.wait(self.WAIT_TIME_DUMP_BALLS, self.state_push_truck_home) def state_push_truck_standby(self): print "[CollectorController.state_push_truck_standby]" self.rail.slide_to_wait_for_sorting_position() while self.rail.is_moving(): yield yield self.state_wait_truck_foot def ready_to_drop_balls(self): balls = self.sorter.get_ball_count() last_ball_time = self.sorter.get_last_ball_time() cycle_time = self.vacuum_shaker.last_button_push done = balls >= self.MAX_BALLS_PER_ROUND halfway_done = balls >= self.MAX_BALLS_PER_ROUND / 2 timed_out_ball = int(time.time() - last_ball_time) > self.MAX_DELAY_BETWEEN_BALLS timed_out_cycle = int(time.time() - cycle_time) > self.MAX_DELAY_CYCLE foot_down = self.foot_down_switch.is_pressed() is_home = self.rail.is_home() return is_home and foot_down and (done or (halfway_done and timed_out_ball) or (timed_out_cycle and balls > 0)) def state_wait_sorter(self): print "[CollectorController.state_wait_sorter]" self.sorter.last_ball_time = time.time() self.vacuum_shaker.last_button_push = time.time() while not self.ready_to_drop_balls(): yield if self.sorter.get_ball_count() < self.MAX_BALLS_PER_ROUND: print "[CollectorController.state_wait_sorter] Timed out. Dumping " + str( self.sorter.get_ball_count() ) + " balls!" Logger().set_current_cycle_ball_count(self.sorter.get_ball_count()) self.sorter.reset_ball_count() yield self.state_open_gate def state_open_gate(self): print "[CollectorController.state_open_gate]" self.gate.pull() yield self.wait(self.WAIT_TIME_GATE, self.state_close_gate) def state_close_gate(self): print "[CollectorController.state_close_gate]" self.gate.push() yield self.state_push_truck_standby def state_wait_truck_foot(self): print "[CollectorController.state_wait_truck_foot]" while not self.foot_up_switch.is_pressed(): yield yield self.state_push_truck_away
from device import Switch import Adafruit_BBIO.GPIO as GPIO import sys, time pin = sys.argv[1] s = Switch(pin, GPIO.BOTH) def print_rising(): print "Rising" def print_falling(): print "Falling" s.bind_rising_edge(print_rising) s.bind_falling_edge(print_falling) while True: time.sleep(1.0)
#!/usr/bin/env python from threading import Thread from device import Switch from lib import config from subprocess import Popen, STDOUT import sys import atexit pin = sys.argv[1] target = sys.argv[2:] spawn_switch = Switch(pin=pin) class StopProcess(Thread): def __init__(self, script, switch): Thread.__init__(self) self.process = script self.switch = switch def run(self): print "[process_spawner] Wait for switch being pushed to stop process" self.switch.wait_pushed(); print "[process_spawner] Stop", target self.process.terminate() p = None atexit.register(lambda:p.terminate()) while True: print "[process_spawner] Wait for switch being pushed to start process" spawn_switch.wait_pushed(); print "[process_spawner] Start", target
class VacuumShaker(Component): SERVO_DELAY = 0.25 PULL_UP_DELAY = 0.15 WAIT_TIMEOUT = 30.0 #IN production: 30s (specs) SHAKE_COUNT = 4 VACUUM_SERVO_ID = "vacuum_servo" LOAD_TANK_SWITCH = "load_tank_switch" SWITCH_TIMEOUT = 1 def __init__(self): super(VacuumShaker, self).__init__(self.wait_init) print "[VacuumShaker.__init__]" self.vacuum_servo = VacuumShakerPiston(**config.devices[self.VACUUM_SERVO_ID]) config.devices[self.LOAD_TANK_SWITCH]["detect_edges"] = GPIO.BOTH self.load_tank_switch = Switch(**config.devices[self.LOAD_TANK_SWITCH]) self.last_button_push = time.time() self.is_init = False def stop(self): print "[VacuumShaker.stop] Stoping" self.vacuum_servo.stop() def wait_init(self): self.vacuum_servo.complete_standby() while not self.is_init: yield self.last_button_push = time.time() #Ne pas descendre la premiere fois. yield partial(self.state_push, 0) def wait_balls(self): self.set_state(self.state_wait_ball); def state_wait_ball(self): print "[VacuumShaker.state_wait_ball]" self.vacuum_servo.complete_standby() start_time = time.time() while not self.load_tank_switch.was_pressed() \ and time.time() < start_time + self.WAIT_TIMEOUT: yield self.last_button_push = time.time() Logger().start_new_cycle() print "[VacuumShaker.state_push]" yield partial(self.state_push, 0) def state_pull_up(self): if self.load_tank_switch.was_pressed() and time.time() > self.last_button_push + self.SWITCH_TIMEOUT: yield self.state_wait_ball self.vacuum_servo.standby() yield self.wait(self.PULL_UP_DELAY, partial(self.state_push, 0)) def state_push(self, n): if self.load_tank_switch.was_pressed() and time.time() > self.last_button_push + self.SWITCH_TIMEOUT: yield self.state_wait_ball self.vacuum_servo.push() if n < self.SHAKE_COUNT: yield self.wait(self.SERVO_DELAY, partial(self.state_pull, n)) else: yield self.wait(self.SERVO_DELAY, self.state_pull_up) def state_pull(self, n): if self.load_tank_switch.was_pressed() and time.time() > self.last_button_push + self.SWITCH_TIMEOUT: yield self.state_wait_ball self.vacuum_servo.pull() yield self.wait(self.SERVO_DELAY, partial(self.state_push, n+1))