def __init__(self): self.behaviors = [] self.active_behaviors = [] self.sensobs = [] self.motob = Motob() self.arbitrator = Arbitrator(self) self.setup()
def __init__(self): self.behaviors = [] self.active_behaviors = [] self.sensobs = [] self.motob = Motob() self.arbitrator = Arbitrator(self) self.picture_taken = False
def __init__(self): self.behaviours = [] #list of all behavior objects (BHRs) self.active_behaviours = [] #a list of all currently-active BHRs. self.sensobs = [] #a list of all sensory objects self.motobs = [Motob()] #a list of all motor objects self.arb = Arbitrator( ) #this resolves motor requests produced by the behaviors.
def __init__(self): # Initialize arbitrator self.arbitrator = Arbitrator() # Initialize motobs (single object for both motors on the Zumo) self.motobs.append(Motob(Motors())) # Initialize sensors self.sensors = { 'ultrasonic': Ultrasonic(0.05), 'IR': IRProximitySensor(), 'reflectance': ReflectanceSensors(False, 0, 900), 'camera': Camera(), } self.active_sensors = [self.sensors['ultrasonic'], self.sensors['IR'], self.sensors['reflectance']] # Initialize sensobs self.sensobs = { 'distance': DistanceSensob([self.sensors['ultrasonic']]), 'line_pos': LinePosSensob([self.sensors['reflectance']]), 'proximity': ProximitySensob([self.sensors['IR']]), 'red_search': RedSearchSensob([self.sensors['camera']]), } self.active_sensobs = [self.sensobs['distance'], self.sensobs['line_pos'], self.sensobs['proximity']] time.sleep(1)
def __init__(self): self.behaviors = [] #Liste over alle behaviors, aktive og inaktive self.active_behaviors = [] # Kun aktive behaviors self.sensobs = [] # Liste over sensorer self.motobs = Motob(self) # Liste med motorobjekter self.arbitrator = Arbitrator() # Arbitrator-objektet, velger winning behavior self.num_timesteps = 0 # Hvor mange timesteps som er kjort self.can_take_photo = False
def __init__(self): self.running = True self.behaviours = [] self.active_behaviours = [] self.inactive_behaviours = [] self.sensobs = [] self.motob = Motob() self.arbitrator = Arbitrator(self) self.setup()
def __init__(self): self.behaviors = [] # Stores all behaviors self.active_behaviors = [] # Stores the active behaviors self.inactive_behaviors = [] # Stores the inactive behaviors self.sensobs = [] # Stores the sensobs self.motobs = [] # Stores the motob self.arbitrator = Arbitrator( self, True) # Createas and stores a deterministic arbitrator self.current_timestep = 0 # Timestep at start is 0
def __init__(self): cam = CamUltra() ref = ReflectanceSob() self.sensobs = [cam, ref] self.active_behaviours = [AttackRed(cam), BeScared(cam), StayInMap(ref), Wander()] self.motobs = [Motob(self)] self.arbitrator = Arbitrator(self) #self.active_behaviours = [] self.inactive_behaviours = []
def __init__(self): self.behaviors = [] self.active_behaviors = [] self.sensobs = [] self.motobs = [] self.arbitrator = Arbitrator(self) self.halt = False self.closeObject = False self.redObject = False
def __init__(self): self.behaviors = [ ] # behavior-listen, med både inaktive og aktive behaviors self.active_behaviors = [] # liste med aktive behaviors self.sensobs = [] # liste med sensor-objekter self.motobs = Motob(self) # list med motor-objekter self.arbitrator = Arbitrator( ) # arbitrator-objektet, velger winning-behavior self.num_timesteps = 0 # antall timesteps som er kjørt self.can_take_photo = False
def __init__(self): self.__behaviors = [] # List of all behaviors self.active_behaviors = [] # List of all _active_ behaviors self.__sensobs = [] # List of all sensory objects self.motob = Motob() # List of all motor objects self.__arbitrator = Arbitrator( self) # Arbitrator chooses the next behavior self.timesteps = 0 self.notifications = []
def __init__(self): self.behaviors = [ ] # a list of all the behavior objects used by the bbcon self.active_behaviors = [ ] # a list of all behaviors that are currently active. self.sensobs = [] # a list of all sensory objects used by the bbcon self.motobs = Motob() # a list of all motor objects used by the bbcon self.arbOb = Arbitrator( ) # the arbitrator object that will resolve actuator requests produced by the behaviors. self.num_timesteps = 0 # number of timesteps done
def __init__(self): self.behaviors = [ ] #En liste av alle behavior objektene som brukes av BBCON self.active_behaviors = [] #En liste med de aktive behavior objektene self.sensobs = [ ] #En liste av alle sensorobjektene som brukes av BBCON self.motobs = Motob( ) #En liste ac alle motor objektene som brukes av BBCON self.arbitrator = Arbitrator( self, False) #Arbitratoren som skal løse requests fra behaviors self.take_pic = False
def __init__(self, config_values, motob): """ Init """ self.config_values = config_values self.behaviors = [] self.active_behaviors = [] self.inactive_behaviors = [] self.sensobs = [] self.motob = motob self.arbitrator = Arbitrator(self) self.rages = 0
def start(): bbcon = BBCON() arb = Arbitrator(bbcon) motor = Motors() reflect_sens = ReflectanceSensors(False) cam = Camera() ir = IR() ultra = Ultrasonic() bbcon.add_motobs(motor) bbcon.set_arb(arb) bbcon.add_behaviour(AvoidObj(bbcon, ultra, ir)) bbcon.add_behaviour(Behaviour_line_follower(bbcon, reflect_sens)) bbcon.add_behaviour(fub(bb=bbcon)) #behaviour avoid blue, has to be added last, do not change this, will screw up bbcon code bbcon.add_behaviour(Behaviour_avoid_blue(bb=bbcon, cam=cam, ultra=ultra)) bbcon.add_sensob(reflect_sens) bbcon.add_sensob(ir) bbcon.add_sensob(ultra) #cam has to be added last, will screw up bbcon code if not bbcon.add_sensob(cam) butt = ZumoButton() butt.wait_for_press() print("Start zumo") while True: bbcon.run_one_timestep()
def __init__(self): self.behaviors = [] self.sensobs = [] self.motob = Motob() self.arbitrator = Arbitrator()
def __init__(self, behaviors=None, active_behaviors=None, sensobs=None, motobs=Motob(), arbitrator=Arbitrator()): # Make arguments non-mutable (because PyCharm says so) if sensobs is None: sensobs = [] if active_behaviors is None: active_behaviors = [] if behaviors is None: behaviors = [] # Checks if bbcon pointer is initialized if arbitrator.bbcon is None: arbitrator.bbcon = self # Save arguments self.behaviors = behaviors self.active_behaviors = active_behaviors self.sensobs = sensobs self.motobs = motobs self.arbitrator = arbitrator # Save halt flag self.halt_request = False
def __init__(self): self.behaviors = [] self.active_behaviors = [] self.sensobs = [] self.motobs = [] self.arbitrator = Arbitrator(); self.active = True
def main(): motob = Motob() bbcon = Bbcon(motob) arbitrator = Arbitrator(bbcon) bbcon.set_arbitrator(arbitrator) # sensorer og sensob ult_sensor = Ultrasonic() ref_sensor = ReflectanceSensors(auto_calibrate=False) reflectance_sensob = ReflectanceSensob(ref_sensor) ultrasonic_sensob = UltrasonicSensob(ult_sensor) camera_sensob = CameraSensob(None, color=0) #behaviors dont_crash = DontCrash(bbcon, ultrasonic_sensob) follow_line = FollowLine(bbcon, reflectance_sensob) find_object = FindColoredObject(bbcon, camera_sensob) bbcon.add_behavior(dont_crash) bbcon.add_behavior(follow_line) bbcon.add_behavior(find_object) try: ZumoButton().wait_for_press() while not bbcon.object_found: # Kjører helt til vi finner objektet bbcon.run_one_timestep() except KeyboardInterrupt: motob.motor.stop() finally: GPIO.cleanup()
def __init__(self): self.arbitrator = Arbitrator(self) #Oppretter fire sensob objekter. Kamera, ir, reflectance og ultra. cam = Sensob() cam.add_sensor(Camera()) ir = Sensob() ir.add_sensor(IRProximitySensor()) reflect = Sensob() reflect.add_sensor(ReflectanceSensors()) ultra = Sensob() ultra.add_sensor(Ultrasonic()) self.sensob = [cam,ir,reflect,ultra] self.motobs = [Motob()] self.behaviors = [] self.wall_detected = False self.wall_checked = False
class BBCON: def __init__(self): self.ARB = ARB(self) self.recommendations = [] self.motob = Motob() self.behaviors = set() # all behaviors self.active = set() # active behaviors self.inactive = set() # inactive behaviors self.sensobs = set() self.motobs = [self.motob] # motor object(s) def add_rec(self, r): self.recommendations.append(r) def add_behavior(self, b): self.behaviors.add(b) self.inactive.add(b) # all behaviors are inactive be default def activate(self, b): if b in self.inactive and b in self.behaviors: print('activating', b) self.active.add(b) self.inactive.remove(b) self.sensobs.add(b.get_sensob()) def deactivate(self, b): if b in self.active and b in self.behaviors: print('deactivating', b) self.inactive.add(b) self.active.remove(b) self.sensobs.remove(b.get_sensob()) def run(self): del self.recommendations[:] # Update all sensobs print('updating sensobs') for sensob in self.sensobs: # print ('updating',sensob.__class__.__name__) sensob.update() # Update all behaviors print('updating behaviors') for behavior in self.active: behavior.update() recommendation = self.ARB.choose_action() print('recommendation = ', recommendation) # Update motobs for motob in self.motobs: motob.update(recommendation[0]) # pass # Reset sensobs for sensob in self.sensobs: sensob.reset()
def lineCollision(): reflect = ReflectanceSensors() ZumoButton().wait_for_press() motor = Motors() stuck = CheckStuck() ultra = Ultrasonic() proxim = IRProximitySensor() sensobCol = Sensob() sensobCol.set_sensors([ultra, proxim]) motob = Motob(motor=motor) sensobLine = Sensob() sensobLine.set_sensors([reflect]) arb = Arbitrator(motob=motob) bbcon = BBCON(arbitrator=arb, motob=motob) bbcon.set_checkStucker(stuck) line = LineFollow(1, [sensobLine]) col = CollisionAvoidance(1, [sensobCol]) bbcon.add_behavior(line) bbcon.add_behavior(col) bbcon.add_sensob(sensobCol) bbcon.add_sensob(sensobLine) count = 0 while count < 20: bbcon.run_one_timestep() count += 1
def lineTest(): reflect = ReflectanceSensors() ZumoButton().wait_for_press() motor = Motors() stuck = CheckStuck() camera = Camera() motob = Motob(motor) arbitrator = Arbitrator(motob=motob) sensob = Sensob() sensob.set_sensors([reflect]) bbcon = BBCON(arbitrator=arbitrator, motob=motob) bbcon.add_sensob(sensob) bbcon.set_checkStucker(stuck) b = LineFollow(1, [sensob]) bbcon.add_behavior(b) timesteps = 0 while timesteps < 30: bbcon.run_one_timestep() timesteps += 1
def trackTest(): ZumoButton().wait_for_press() motor = Motors() ultra = Ultrasonic() camera = Camera() stuck = CheckStuck() motob = Motob(motor) arbitrator = Arbitrator(motob=motob) sensob = Sensob() sensob.set_sensors([ultra, camera]) bbcon = BBCON(arbitrator=arbitrator, motob=motob) b = TrackObject(priority=1, sensobs=[sensob]) bbcon.set_checkStucker(stuck) bbcon.add_behavior(b) bbcon.activate_behavior(0) bbcon.add_sensob(sensob) timesteps = 0 while timesteps < 25: bbcon.run_one_timestep() timesteps += 1
class Bbcon: def __init__(self): self.behaviors = [ ] # a list of all the behavior objects used by the bbcon self.active_behaviors = [ ] # a list of all behaviors that are currently active. self.sensobs = [] # a list of all sensory objects used by the bbcon self.motobs = Motob() # a list of all motor objects used by the bbcon self.arbOb = Arbitrator( ) # the arbitrator object that will resolve actuator requests produced by the behaviors. self.num_timesteps = 0 # number of timesteps done # append a newly-created behavior onto the behaviors list. def add_behavior(self, behavior): if behavior not in self.behaviors: self.behaviors.append(behavior) # append a newly-created sensob onto the sensobs list. def add_sensor(self, sensor): if sensor not in self.sensobs: self.sensobs.append(sensor) # add an existing behavior onto the active-behaviors list. def activate_bahavior(self, behavior): if behavior in self.behaviors: self.active_behaviors.append(behavior) # remove an existing behavior from the active behaviors list. def deactive_behavior(self, behavior): if behavior in self.active_behaviors: self.active_behaviors.remove(behavior) # Constitutes the core BBCON activity def run_one_timestep(self): """ Main function. :return: """ # Updates behaviours which in return updates sensobs. for behaviour in self.behaviors: behaviour.update() # Returns recommondations of motor_recoms = self.arbOb.choose_action(self.active_behaviors) print("motor req: ", motor_recoms) # Update motobs self.motobs.update(motor_recoms) # Waits for motors to run sleep(0.5) # Reset sensor values for sensor in self.sensobs: sensor.reset() self.num_timesteps += 1
class BBCON: def __init__(self): self.behaviors = [] self.sensobs = [] self.motob = Motob() self.motob.update("brake") self.arbitrator = Arbitrator() def add_behavior(self, behavior): self.behaviors.append(behavior) def add_sensob(self, sensob): self.sensobs.append(sensob) # def activate_behavior(self, behavior): # self.active_behaviors.add(behavior) # def deactive_behavior(self, behavior): # self.active_behaviors.remove(behavior) def move(self, action): self.motob.update(action) def run_one_timestep(self, delay=0.667): start_time = time() # Reflectance sensor index 0 # Proximity sensor index 1 # Camera sensor index 2 #self.data = [s.update() for s in self.sensobs] self.data = [] for s in self.sensobs: tmp = time() self.data.append(s.update()) print("Time for {0}: {1}".format(s.sensor, round((time() - tmp)*1000, ndigits = 2))) mrs = [] for b in self.behaviors: tmp = time() mrs.append(b.update()) print("Time for {0}: {1}".format(b, round((time() - tmp)*1000, ndigits = 2))) #tmp = time() #mrs = [b.update() for b in self.behaviors] print(sorted(mrs, key=lambda x: x[1])) action = self.arbitrator.choose_action(mrs) print(action) self.move(action) execution_time = time() - start_time #delay = max(delay - execution_time, 0) delay = (delay - execution_time) if execution_time < delay else 0 print("Delay time: ", delay, "\n") sleep(delay)
class BBCON: """Behavoir-Based Controller-klasse""" def __init__(self, config_values, motob): """ Init """ self.config_values = config_values self.behaviors = [] self.active_behaviors = [] self.inactive_behaviors = [] self.sensobs = [] self.motob = motob self.arbitrator = Arbitrator(self) self.rages = 0 def add_behavior(self, behavior): """ Legger til en nylig lagd handling til behaviors-listen """ self.behaviors.append(behavior) def add_sensob(self, sensob): """ Legger til en nylig lagd sensob til sensob-listen""" self.sensobs.append(sensob) sensob.bbcon = self def active_behavior(self, behavior): """Legger til en eksisterende handling til listen med aktive handlinger""" self.active_behaviors.append(behavior) def deactive_behavior(self, behavior): """Fjerner en tidligere aktiv handling fra listen med aktive handliner""" self.active_behaviors.remove(behavior) def run_one_timestep(self): """Loopen""" # 2. Ber alle behavior oppdatere seg selv, og legger til i riktige lister # dersom de nå har endret status fra aktiv til ikke aktiv, eller motsatt. for behavior in self.behaviors: behavior.update() if behavior.active_flag and behavior not in self.active_behaviors: self.active_behavior(behavior) elif not behavior.active_flag and behavior in self.active_behaviors: self.deactive_behavior(behavior) # 3. Result består av den vinnende handlingen, som arbitrator-en bestemmer i sin choose_action-metode, # sin motorandbefaling og halt request flag, av typen, [motorandbefaling, halt request flag =True/False] result = self.arbitrator.choose_action() # 4. Oppdatere alle motobs if result[ 1]: # Dersom vinnende handling har halt_request = True, så skal BBCON avslutte og returnere True self.motob.update("stop") return True # slik at roboten kjører så lenge print("Anbefaler: " + result[0]) self.motob.update( result[0]) # Oppdaterer alle motob-ene med anbefalingene time.sleep(0.1)
def __init__(self): """ init """ self.sensobs = [] self.add_sensob(Sensob(Ultrasonic())) self.add_sensob(Sensob(ReflectanceSensors())) self.add_sensob(Sensob(Camera())) self.motob = Motob() self.behaviors = [] self.add_behavior(Behavior(self, [10000, 10000, [10000, 10000, 10000]], "drive", 1)) self.add_behavior(Behavior(self, [30, 10000, [10000, 10000, 10000]], "stop", 3)) self.add_behavior(Behavior(self, [10000, 0.3, [10000, 10000, 10000]], "turnaround", 2)) self.add_behavior(Behavior(self, [10000, 10000, [210, 10000, 10000]], "turn_left", 5)) #self.add_behavior(Behavior(self, [10000, 10000, [10000, 200, 10000]], "turn_right", 4)) self.active_behaviors = [] self.arbitrator = Arbitrator()
def rett_fram(): ir = ReflectanceSensors(True) s = Sensob(ir) ab = Avoid_borders(s, s) wr = Walk_randomly(s, s) a = Arbitrator() m = Motob() print("Motob set") ZumoButton().wait_for_press() print("Button pressed") while True: ab.update() wr.update() print("Vekt: ", ab.weight) print("Rec: ", ab.motor_recommendations) winner_rec = a.choose_action(ab, wr) print("recom: ", winner_rec) m.update(winner_rec)
class BBCON: def __init__(self): self.behaviors = [] self.sensobs = [] self.motob = Motob() self.arbitrator = Arbitrator() def add_behavior(self, behavior): self.behaviors.append(behavior) def add_sensob(self, sensob): self.sensobs.append(sensob) #def activate_behavior(self, behavior): # self.active_behaviors.add(behavior) #def deactive_behavior(self, behavior): # self.active_behaviors.remove(behavior) def move(self, action): self.motob.update(action) def run_one_timestep(self, delay = 0.667): start_time = time() # Reflectance sensor index 0 # Proximity sensor index 1 # Camera sensor index 2 print("Updating sensors...", end = "") #self.data = [s.update() for s in self.sensobs] self.data = [] for s in self.sensobs: tmp = time() self.data.append(s.update()) print("Time for:", s.sensor, ": ", round((time()-tmp)*1000, ndigits = 4), "ms") print("Updating behaviors...") tmp = time() mrs = [b.update() for b in self.behaviors] print((time()-tmp)*1000, "ms") action = self.arbitrator.choose_action(mrs) print("Moving.") self.move(action) execution_time = time() - start_time #delay = max(delay - execution_time, 0) delay = (delay - execution_time) if execution_time < delay else 0 print("Delay time: ", delay) sleep(delay)
class Bbcon: def __init__(self): self.arbitrator = Arbitrator(self) #Oppretter fire sensob objekter. Kamera, ir, reflectance og ultra. cam = Sensob() cam.add_sensor(Camera()) ir = Sensob() ir.add_sensor(IRProximitySensor()) reflect = Sensob() reflect.add_sensor(ReflectanceSensors()) ultra = Sensob() ultra.add_sensor(Ultrasonic()) self.sensob = [cam,ir,reflect,ultra] self.motobs = [Motob()] self.behaviors = [] self.wall_detected = False self.wall_checked = False def activate_behavior(self, behavior): behavior.active_flag=True def dactivate_behavior(self, behavior): behavior.active_flag=False def add_behavior(self, behavior): self.behaviors.append(behavior) def add_sensob(self, sensob): self.sensob.append(sensob) def run(self): ZumoButton().wait_for_press() while True: print("number of behaviors:", len(self.behaviors)) for behavior in self.behaviors: if not behavior.active_flag: behavior.consider_activation() if behavior.active_flag: print(behavior) behavior.sensor.update() behavior.sense_and_act() # print(behavior.motor_recommendation) recommendations = self.arbitrator.choose_action() print("rec ", recommendations) if recommendations[1]== True: print("Prosess avsluttet.") break for motob in self.motobs: motob.update(recommendations[0]) time.sleep(0.2) for sensob in self.sensob: sensob.reset()
def __init__(self, time_step=0.2): """:param time_step: float time in seconds between to wait after each time step """ self.sensors = [] # All sensors self.behavs = [] # All behaviours self.active_behavs = [] # Active behaviours self.sensobs = [] # Sensory objects self.motobs = [] # Motor objects self.arbit = Arbitrator(self) # Arbitrator self.time_step = time_step
def __init__(self): self.ARB = ARB(self) self.recommendations = [] self.motob = Motob() self.behaviors = set() # all behaviors self.active = set() # active behaviors self.inactive = set() # inactive behaviors self.sensobs = set() self.motobs = [self.motob] # motor object(s)
def __init__(self): """ Initiates the Behavior-Based Robotic Controller :param arbitrator: arbitrator, will provide behaviors? """ self.behaviors = [] self.active_behaviors = [] self.inactive_behaviors = [] self.sensobs = [] self.motobs = [] self.motobs.append(BeltsController()) self.belts = self.motobs[0] self.arbitrator = Arbitrator(self) self.current_timestep = 0 self.controlled_robot = "Zumo Robot" self.halt_request = False
class BBCON: """Controller managing behaviors, sensobs and motobs, updating and resetting them""" def __init__(self): self.behaviors = [] self.active_behaviors = [] self.sensobs = [] self.motobs = [] self.arbitrator = Arbitrator(self) self.halt = False self.closeObject = False self.redObject = False def add_behavior(self, behavior): self.behaviors.append(behavior) def add_sensob(self, sensob): self.sensobs.append(sensob) def activate_behavior(self, behavior): if not behavior in self.active_behaviors: print("ACTIVATING:", behavior) self.active_behaviors.append(behavior) return True return False def deactivate_behavior(self, active_behavior): if active_behavior in self.active_behaviors: print("DEACTIVATING:", active_behavior) self.active_behaviors.remove(active_behavior) return True return False def run_one_timestep(self): #for sensob in self.sensobs: # sensob.update() for behavior in self.active_behaviors: for sensob in behavior.sensob: sensob.update() for behavior in self.behaviors: behavior.update() halt, action = self.arbitrator.choose_action() if halt: self.halt = True for motob in self.motobs: motob.update(action) time.sleep(.2) for sensob in self.sensobs: sensob.reset()
class Bbcon: def __init__(self): cam = CamUltra() ref = ReflectanceSob() self.sensobs = [cam, ref] self.active_behaviours = [AttackRed(cam), BeScared(cam), StayInMap(ref), Wander()] self.motobs = [Motob(self)] self.arbitrator = Arbitrator(self) #self.active_behaviours = [] self.inactive_behaviours = [] def activate_behaviour(self, behaviour): if behaviour not in self.active_behaviours: self.active_behaviours.append(behaviour) if behaviour in self.inactive_behaviours: self.inactive_behaviours.remove(behaviour) def deactivate_behaviour(self, behaviour): if behaviour in self.active_behaviours: self.active_behaviours.remove(behaviour) if behaviour not in self.inactive_behaviours: self.inactive_behaviours.append(behaviour) def r(self): while True: print('Updating sensors') for sensob in self.sensobs: sensob.update() print('Updating behaviours') for behaviour in self.active_behaviours: behaviour.sense_and_act() recommendations = self.arbitrator.choose_action() print('Updating motobs') for motob in self.motobs: motob.update(recommendations) if recommendations[-1] == False: print('=== HALTING ===') break print('=============') print('Step complete') print('=============') # for sensob in self.sonsobs: # sensob.reset()
class Bbcon(object): def __init__(self, time_step=0.2): """:param time_step: float time in seconds between to wait after each time step """ self.sensors = [] # All sensors self.behavs = [] # All behaviours self.active_behavs = [] # Active behaviours self.sensobs = [] # Sensory objects self.motobs = [] # Motor objects self.arbit = Arbitrator(self) # Arbitrator self.time_step = time_step def add_sensor(self, sensor): self.sensors.append(sensor) def remove_sensor(self, sensor): try: self.sensors.remove(sensor) except ValueError: pass def add_behaviour(self, behaviour): self.behavs.append(behaviour) def add_sensory_object(self, sensory_object): self.sensobs.append(sensory_object) def activate_behaviour(self, behaviour): self.active_behavs.append(behaviour) def deactivate_behaviour(self, behaviour): """Remove behaviour. :return: boolean True if removed, False otherwise """ try: self.active_behavs.remove(behaviour) except ValueError: return False else: return True def set_arbit(self, arbitrator): self.arbit = arbitrator def run_one_timestep(self): """ Returns if execution should continue. False => Halt, stop program True => Keep running :return: bool """ print("="*50) print() forks = [] # Update sensors for sensor in self.sensors: if isinstance(sensor, Camera): thread = threading.Thread(target=sensor.update) forks.append(thread) thread.start() else: sensor.update() for fork in forks: fork.join() forks = [] # Update sensobs for sensob in self.sensobs: thread = threading.Thread(target=sensob.update) forks.append(thread) thread.start() for fork in forks: fork.join() # Update behaviours for behav in self.active_behavs: behav.update() # Invoke arbitrator motor_rec = self.arbit.choose_action() # Returns a tuple(list(motor_recommendations), halt) print("Arbitrator chose: "+str(motor_rec)) if motor_rec[1]: # Check halt recommendation return False # Halt and exit program # Update motobs print(self.motobs) i = 0 for motob in self.motobs: # Updates each motob with it's respective motor recommendation print("Bbcon: Updating motob " + str(i)) motob.update(motor_rec[0][i]) i += 1 # Wait time.sleep(0.5) #waits half a second # Reset sensors for sensor in self.sensors: sensor.reset() return True
class Bbcon(): def __init__(self): self.behaviors = [] self.active_behaviors = [] self.sensobs = [] self.motobs = [] self.arbitrator = Arbitrator(); self.active = True def add_behavior(self, behavior): self.behaviors.append(behavior) def add_sensob(self, sensob): self.sensobs.append(sensob) def add_motob(self, motob): self.motobs.append(motob) def activate_behavior(self, behavior): if behavior in self.behaviors: self.active_behaviors.append(behavior) def deactivate_behavior(self, behavior): if behavior in self.active_behaviors: self.active_behaviors.remove(behavior) def activate_all_behaviors(self): for b in self.behaviors: self.activate_behavior(b) def get_active_behaviors(self): return self.active_behaviors def halt_request(self): self.active = False for m in self.motobs: m.stop() Led().pulse(10, 0.1) def run_one_timestep(self): # Update all sensobs self.update_sensobs() # Update all behaviours self.update_behaviors() # Invoke the arbitrator recommendations = self.arbitrator.choose_action(self.active_behaviors) # Update all motobs based on the above print(recommendations) self.update_motors(recommendations) # Exits if button is pressed again if ZumoButton().check_if_pressed() == 0: self.halt_request() # Resets sensors self.reset_sensobs() def update_sensobs(self): for sensob in self.sensobs: sensob.update() def reset_sensobs(self): for sensob in self.sensobs: sensob.reset() def update_behaviors(self): for behavior in self.behaviors: behavior.update() def update_motors(self, recommendation): for motob in self.motobs: motob.update(recommendation)