def detflower(nbrList): new_nbr = 1 for nbr in nbrList: (state, asdf, color) = hba.get_msg_from_nbr(nbr,new_nbr) if state == STATE_FLOWER: return (nbr, COLORS[color]) return (None, None)
def detflower(nbrList): new_nbr = 1 for nbr in nbrList: (state, asdf, color) = hba.get_msg_from_nbr(nbr, new_nbr) if state == STATE_FLOWER: return (nbr, COLORS[color]) return (None, None)
def get_queen(): nbr_list = hba.get_robot_neighbors() new_nbrs = 0 for nbr in nbr_list: is_queen = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_STATE] == STATE_QUEEN if is_queen: return nbr return None
def get_nbrs_in_dark(): new_nbrs = 0 nbr_list = hba.get_robot_neighbors() nbrs_in_dark = [] for nbr in nbr_list: state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_IDX_STATE] if state == STATE_DARK: nbrs_in_dark.append(nbr) return nbrs_in_dark
def get_nbrs_in_light(): new_nbrs = 0 nbr_list = hba.get_robot_neighbors() nbrs_in_light = [] for nbr in nbr_list: state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_IDX_STATE] if state == STATE_LIGHT: nbrs_in_light.append(nbr) return nbrs_in_light
def detflower(nbrList): for nbr in nbrList: (unimportant, stuff, colormsg) = hba.get_msg_from_nbr(nbr,0) if colormsg == 0: color = 'red' elif colormsg == 1: color = 'green' elif colormsg == 2: color = 'blue' if color != None: return (color, nbr) return (None, None)
def spring(): at_tree_odo = None tree_pose = None followers = 0 have_seen_leader = False beh.init(0.22, 40, 0.5, 0.1) motion.init_rv(1000, MOTION_RV, MOTION_CAPTURE_DISTANCE, MOTION_RELEASE_DISTANCE, MOTION_CAPTURE_ANGLE, MOTION_RELEASE_ANGLE) state = STATE_IDLE while True: # run the system updates new_nbrs = beh.update() nbr_list = neighbors.get_neighbors() if new_nbrs: print state beh_out = beh.BEH_INACTIVE # set colors, because why not do it at the top color_counts = [0, 0, 0] for i in range(3): color_counts[i] = min([5, (state - 5 * i)]) if state == STATE_IDLE: leds.set_pattern('rb', 'group', LED_BRIGHTNESS) elif state == STATE_SUCCESS: leds.set_pattern('g', 'circle', LED_BRIGHTNESS) else: leds.set_pattern(color_counts, 'count', LED_BRIGHTNESS) if rone.button_get_value('g'): tree_pose = None followers = 0 have_seen_leader = False state = STATE_IDLE # this is the main finite-state machine if not state in [ STATE_IDLE, STATE_LEADER, STATE_SUCCESS, STATE_FOLLOW ]: for nbr in nbr_list: nbr_state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_IDX_STATE] if nbr_state in [STATE_LEADER, STATE_SUCCESS]: start_time = sys.time() state = STATE_FOLLOW if state == STATE_IDLE: if rone.button_get_value('r'): pose.set_pose(0, 0, 0) state = STATE_WANDER elif rone.button_get_value('b'): state = STATE_QUEEN elif state == STATE_QUEEN: pass elif state == STATE_WANDER: ## leds.set_pattern('r', 'circle', LED_BRIGHTNESS) nav = hba.find_nav_tower_nbr(NAV_ID) beh_out = beh.avoid_nbr(nav, MOTION_TV) queen = None for nbr in nbr_list: nbr_state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_IDX_STATE] if nbr_state == STATE_QUEEN: queen = nbr if bump_front(): if queen != None: state = STATE_SUCCESS else: tree_pose = pose.get_pose() motion.set_goal((0.0, 0.0), MOTION_TV) at_tree_odo = pose.get_odometer() state = STATE_RETURN elif nav == None: motion.set_goal((0.0, 0.0), MOTION_TV) state = STATE_RETURN elif state == STATE_RETURN: ## nav_tower = hba.find_nav_tower_nbr(NAV_ID) queen = None recruiter = None for nbr in nbr_list: nbr_state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_IDX_STATE] if nbr_state == STATE_QUEEN: queen = nbr elif nbr_state == STATE_RECRUIT: recruiter = nbr if queen != None: if (recruiter == None) and (tree_pose != None) and \ close_to_nbr(queen): start_time = sys.time() dist_traveled = pose.get_odometer() - at_tree_odo at_queen_odo = pose.get_odometer() state = STATE_RECRUIT elif not closer_to_nbr(queen): beh_out = beh.follow_nbr(queen, MOTION_TV) else: start_time = sys.time() state = STATE_FOLLOW else: (tv, rv) = motion.update() beh_out = beh.tvrv(tv, rv) elif state == STATE_RECRUIT: new_followers = 0 for nbr in nbr_list: nbr_state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_IDX_STATE] if nbr_state == STATE_RECRUIT: if neighbors.get_nbr_id(nbr) > rone.get_id(): state = STATE_FOLLOW elif nbr_state in [STATE_FOLLOW, STATE_QUEEN]: new_followers += 1 if new_followers > followers: print 'reset timer' start_time = sys.time() followers = max([followers, new_followers]) if followers == 4 or sys.time() > start_time + WAIT_TIME: tree_pos = (tree_pose[0], tree_pose[1]) motion.set_goal(tree_pos, MOTION_TV) state = STATE_LEADER elif state == STATE_FOLLOW: recruiter = None leader = None success = False new_followers = 1 for nbr in nbr_list: nbr_state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_IDX_STATE] if nbr_state == STATE_RECRUIT: recruiter = nbr elif nbr_state == STATE_LEADER: leader = nbr elif nbr_state == STATE_SUCCESS: leader = nbr success = True elif nbr_state in [STATE_FOLLOW, STATE_WANDER]: new_followers += 1 if success: have_seen_leader = True if new_followers > followers: start_time = sys.time() followers = max([followers, new_followers]) if recruiter == None: if leader == None: if have_seen_leader: beh_out = beh.tvrv(MOTION_TV, 0) elif followers == 5 or sys.time() > start_time + WAIT_TIME: followers = 0 state = STATE_WANDER else: if success and bump() and close_to_nbr(leader): state = STATE_SUCCESS beh_out = beh.follow_nbr(leader, MOTION_TV) elif state == STATE_LEADER: ## (tv, rv) = motion.update() ## beh_out = beh.tvrv(tv, rv) ## if motion.is_done(): ## state = STATE_SUCCESS beh_out = beh.tvrv(-MOTION_TV, 0) if bump() or (pose.get_odometer() - at_queen_odo) > dist_traveled: state = STATE_SUCCESS elif state == STATE_SUCCESS: pass # end of the FSM if state not in [ STATE_IDLE, STATE_RECRUIT, STATE_LEADER, STATE_SUCCESS, STATE_QUEEN ]: bump_beh_out = beh.bump_beh(MOTION_TV) beh_out = beh.subsume([beh_out, bump_beh_out]) # set the beh velocities beh.motion_set(beh_out) #set the HBA message msg = [0, 0, 0] msg[MSG_IDX_STATE] = state hba.set_msg(msg[0], msg[1], msg[2])
def find_recruiter(nbrList): for nbr in nbrList: state = hba.get_msg_from_nbr(nbr,new_nbrs)[MSG_STATE] if state == STATE_RECRUIT: return nbr return None
def message_demo(): beh.init(0.22, 40, 0.5, 0.1) state = STATE_LEADER num_r = 1 num_g = 1 num_b = 1 button_red_old = 0 button_green_old = 0 button_blue_old = 0 while True: # run the system updates new_nbrs = beh.update() nbr_list = neighbors.get_neighbors() # init the velocities beh_out = beh.BEH_INACTIVE # read the buttons and update the local counts (num_r, button_red_old) = check_button('r', num_r, button_red_old) (num_g, button_green_old) = check_button('g', num_g, button_green_old) (num_b, button_blue_old) = check_button('b', num_b, button_blue_old) # build this robot's neighbor message distance_local = num_r mode_local = num_g quality_local = num_b hba.set_msg(distance_local, mode_local, quality_local) # find the neighbor with the lowest ID nbr_leader = nbrList_getRobotWithLowID(nbr_list) # are you the leader? if nbr_leader == None: # no neighbors. you are the leader state = STATE_LEADER else: low_ID = neighbors.get_nbr_id(nbr_leader) # if new_nbrs: # print 'nbr lowID=',nbr_leader if low_ID < rone.get_id(): state = STATE_MINION else: state = STATE_LEADER # this is the main finite-state machine if state == STATE_LEADER: msg = (distance_local, mode_local, quality_local) if new_nbrs: print "leader:", msg brightness = LED_BRIGHTNESSER elif state == STATE_MINION: if nbr_leader != None: msg = hba.get_msg_from_nbr(nbr_leader, new_nbrs) if new_nbrs: print "minion. leader = ", str(low_ID), " msg=", msg brightness = LED_BRIGHTNESS # end of the FSM # unpack the message and display it on the LEDs (distance, mode, quality) = msg leds.set_pattern((distance, mode, quality), 'count', brightness) # set the velocities beh.motion_set(beh_out)
def find_recruiter(nbrList): for nbr in nbrList: state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_STATE] if state == STATE_RECRUIT: return nbr return None
def spring(): at_tree_odo = None tree_pose = None followers = 0 have_seen_leader = False beh.init(0.22, 40, 0.5, 0.1) motion.init_rv(1000, MOTION_RV, MOTION_CAPTURE_DISTANCE, MOTION_RELEASE_DISTANCE , MOTION_CAPTURE_ANGLE, MOTION_RELEASE_ANGLE) state = STATE_IDLE while True: # run the system updates new_nbrs = beh.update() nbr_list = neighbors.get_neighbors() if new_nbrs: print state beh_out = beh.BEH_INACTIVE # set colors, because why not do it at the top color_counts = [0, 0, 0] for i in range(3): color_counts[i] = min([5, (state - 5 * i)]) if state == STATE_IDLE: leds.set_pattern('rb', 'group', LED_BRIGHTNESS) elif state == STATE_SUCCESS: leds.set_pattern('g', 'circle', LED_BRIGHTNESS) else: leds.set_pattern(color_counts, 'count', LED_BRIGHTNESS) if rone.button_get_value('g'): tree_pose = None followers = 0 have_seen_leader = False state = STATE_IDLE # this is the main finite-state machine if not state in [STATE_IDLE, STATE_LEADER, STATE_SUCCESS, STATE_FOLLOW]: for nbr in nbr_list: nbr_state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_IDX_STATE] if nbr_state in [STATE_LEADER, STATE_SUCCESS]: start_time = sys.time() state = STATE_FOLLOW if state == STATE_IDLE: if rone.button_get_value('r'): pose.set_pose(0, 0, 0) state = STATE_WANDER elif rone.button_get_value('b'): state = STATE_QUEEN elif state == STATE_QUEEN: pass elif state == STATE_WANDER: ## leds.set_pattern('r', 'circle', LED_BRIGHTNESS) nav = hba.find_nav_tower_nbr(NAV_ID) beh_out = beh.avoid_nbr(nav, MOTION_TV) queen = None for nbr in nbr_list: nbr_state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_IDX_STATE] if nbr_state == STATE_QUEEN: queen = nbr if bump_front(): if queen != None: state = STATE_SUCCESS else: tree_pose = pose.get_pose() motion.set_goal((0.0, 0.0), MOTION_TV) at_tree_odo = pose.get_odometer() state = STATE_RETURN elif nav == None: motion.set_goal((0.0, 0.0), MOTION_TV) state = STATE_RETURN elif state == STATE_RETURN: ## nav_tower = hba.find_nav_tower_nbr(NAV_ID) queen = None recruiter = None for nbr in nbr_list: nbr_state = hba.get_msg_from_nbr(nbr, new_nbrs)[MSG_IDX_STATE] if nbr_state == STATE_QUEEN: queen = nbr elif nbr_state == STATE_RECRUIT: recruiter = nbr if queen != None: if (recruiter == None) and (tree_pose != None) and \ close_to_nbr(queen): start_time = sys.time() dist_traveled = pose.get_odometer() - at_tree_odo at_queen_odo = pose.get_odometer() state = STATE_RECRUIT elif not closer_to_nbr(queen): beh_out = beh.follow_nbr(queen, MOTION_TV) else: start_time = sys.time() state = STATE_FOLLOW else: (tv, rv) = motion.update() beh_out = beh.tvrv(tv, rv) elif state == STATE_RECRUIT: new_followers = 0 for nbr in nbr_list: nbr_state = hba.get_msg_from_nbr(nbr,new_nbrs)[MSG_IDX_STATE] if nbr_state == STATE_RECRUIT: if neighbors.get_nbr_id(nbr) > rone.get_id(): state = STATE_FOLLOW elif nbr_state in [STATE_FOLLOW, STATE_QUEEN]: new_followers += 1 if new_followers > followers: print 'reset timer' start_time = sys.time() followers = max([followers, new_followers]) if followers == 4 or sys.time() > start_time + WAIT_TIME: tree_pos = (tree_pose[0], tree_pose[1]) motion.set_goal(tree_pos, MOTION_TV) state = STATE_LEADER elif state == STATE_FOLLOW: recruiter = None leader = None success = False new_followers = 1 for nbr in nbr_list: nbr_state = hba.get_msg_from_nbr(nbr,new_nbrs)[MSG_IDX_STATE] if nbr_state == STATE_RECRUIT: recruiter = nbr elif nbr_state == STATE_LEADER: leader = nbr elif nbr_state == STATE_SUCCESS: leader = nbr success = True elif nbr_state in [STATE_FOLLOW, STATE_WANDER]: new_followers += 1 if success: have_seen_leader = True if new_followers > followers: start_time = sys.time() followers = max([followers, new_followers]) if recruiter == None: if leader == None: if have_seen_leader: beh_out = beh.tvrv(MOTION_TV, 0) elif followers == 5 or sys.time() > start_time + WAIT_TIME: followers = 0 state = STATE_WANDER else: if success and bump() and close_to_nbr(leader): state = STATE_SUCCESS beh_out = beh.follow_nbr(leader, MOTION_TV) elif state == STATE_LEADER: ## (tv, rv) = motion.update() ## beh_out = beh.tvrv(tv, rv) ## if motion.is_done(): ## state = STATE_SUCCESS beh_out = beh.tvrv(-MOTION_TV, 0) if bump() or (pose.get_odometer() - at_queen_odo) > dist_traveled: state = STATE_SUCCESS elif state == STATE_SUCCESS: pass # end of the FSM if state not in [STATE_IDLE, STATE_RECRUIT, STATE_LEADER, STATE_SUCCESS, STATE_QUEEN]: bump_beh_out = beh.bump_beh(MOTION_TV) beh_out = beh.subsume([beh_out, bump_beh_out]) # set the beh velocities beh.motion_set(beh_out) #set the HBA message msg = [0, 0, 0] msg[MSG_IDX_STATE] = state hba.set_msg(msg[0], msg[1], msg[2])
def message_demo(): beh.init(0.22, 40, 0.5, 0.1) state = STATE_LEADER num_r = 1 num_g = 1 num_b = 1 button_red_old = 0 button_green_old = 0 button_blue_old = 0 while True: # run the system updates new_nbrs = beh.update() nbr_list = neighbors.get_neighbors() # init the velocities beh_out = beh.BEH_INACTIVE # read the buttons and update the local counts (num_r, button_red_old) = check_button("r", num_r, button_red_old) (num_g, button_green_old) = check_button("g", num_g, button_green_old) (num_b, button_blue_old) = check_button("b", num_b, button_blue_old) # build this robot's neighbor message distance_local = num_r mode_local = num_g quality_local = num_b hba.set_msg(distance_local, mode_local, quality_local) # find the neighbor with the lowest ID nbr_leader = nbrList_getRobotWithLowID(nbr_list) # are you the leader? if nbr_leader == None: # no neighbors. you are the leader state = STATE_LEADER else: low_ID = neighbors.get_nbr_id(nbr_leader) # if new_nbrs: # print 'nbr lowID=',nbr_leader if low_ID < rone.get_id(): state = STATE_MINION else: state = STATE_LEADER # this is the main finite-state machine if state == STATE_LEADER: msg = (distance_local, mode_local, quality_local) if new_nbrs: print "leader:", msg brightness = LED_BRIGHTNESSER elif state == STATE_MINION: if nbr_leader != None: msg = hba.get_msg_from_nbr(nbr_leader, new_nbrs) if new_nbrs: print "minion. leader = ", str(low_ID), " msg=", msg brightness = LED_BRIGHTNESS # end of the FSM # unpack the message and display it on the LEDs (distance, mode, quality) = msg leds.set_pattern((distance, mode, quality), "count", brightness) # set the velocities beh.motion_set(beh_out)