def FTL_sorted(): tv = 0 rv = 0 nbr_list = neighborsX.get_neighbors() if len(nbr_list) > 0: # You have neighbors. Follow max-min robot-id. If you're the lowest, become leader. follow_id = -1 follow_nbr = None for nbr in nbr_list: nbr_id = neighborsX.get_nbr_id(nbr) if nbr_id < rone.get_id() and nbr_id > follow_id: follow_id = nbr_id follow_nbr = nbr if follow_id == -1: # leader leds.set_pattern('rg', 'blink_fast', LED_BRIGHTNESS) tv = FTL_TV rv = 0 #bump_avoid() else: # follower leds.set_pattern('b', 'blink_fast', LED_BRIGHTNESS) (tv, rv) = follow_motion_controller(follow_nbr) else: # no neighbors. do nothing leds.set_pattern('r', 'circle', LED_BRIGHTNESS) velocity.set_tvrv(tv, rv)
def average_orientation(): # calculate average heading of all neighbors # compute vector sum of orientation angles and then average nbr_list = neighborsX.get_neighbors() if len(nbr_list) > 0: # You have neighbors vector_sum = [0, 0] for nbr in nbr_list: nbr_heading = neighbor_heading(nbr) vector_sum[0] += math.cos(nbr_heading) vector_sum[1] += math.sin(nbr_heading) average_heading = math.atan2(vector_sum[1], vector_sum[0]) return (FTL_TV, bound(MOTION_RV_GAIN * average_heading, MOTION_RV_MAX)) else: leds.set_pattern('r', 'circle', LED_BRIGHTNESS) return (0, 0)
def match_orientation(): # pair-wise orientation match from high-id to low-id tv = 0 rv = 0 nbr_list = neighborsX.get_neighbors() if len(nbr_list) > 0: # You have neighbors. Follow any neighbor. get the first neighbor leds.set_pattern('rb', 'blink_fast', LED_BRIGHTNESS) nbr = nbr_list[0] rv = bound(MOTION_RV_GAIN * neighbor_heading(nbr), MOTION_RV_MAX) else: #no neighbors. do nothing leds.set_pattern('b', 'circle', LED_BRIGHTNESS) # student code end velocity.set_tvrv(tv, rv)
def FTL_follower(): # act on the information from the nbr state. Note that this might be # information stored from the last message we received, because message # information remains active for a while tv = 0 rv = 0 nbr_list = neighborsX.get_neighbors() if len(nbr_list) > 0: # You have neighbors. Follow any neighbor. get the first neighbor leds.set_pattern('b', 'blink_fast', LED_BRIGHTNESS) nbr = nbr_list[0] (tv, rv) = follow_motion_controller(nbr) else: #no neighbors. do nothing leds.set_pattern('b', 'circle', LED_BRIGHTNESS) velocity.set_tvrv(tv, rv)
def flock(): # have the robots follow the leader as a flock global FTL_leader_tv, FTL_leader_rv tv = 0 rv = 0 nbr_list = neighborsX.get_neighbors() if len(nbr_list) > 0: leds.set_pattern('bg', 'blink_fast', LED_BRIGHTNESS) (tv, rv) = average_orientation() radio_msg = radio_get_message() if radio_msg != None: # we have a message! put lights in active mode (FTL_leader_tv, FTL_leader_rv) = leader_motion_controller(radio_msg) rv += FTL_leader_rv else: # no neighbors. do nothing leds.set_pattern('r', 'circle', LED_BRIGHTNESS) velocity.set_tvrv(tv, rv)