def test_saveFormationToEtcd(self): logger = logging.getLogger() stream = logging.StreamHandler(sys.stdout) stream.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') stream.setFormatter(formatter) logger.addHandler(stream) manager = Manager(logger) expected_text = '[{"username": "******", "cpu-shares": 102, '\ '"ram": 100, "hostname": "app01", "ports": [{"host_port": 8080, '\ '"container_port": 8080}], "host_server": "dlceph02"}]' username = '******' formation_name = 'test_formation' f = Formation(username, formation_name) f.add_app(username, 'app01', 102, 100, [{ "host_port": 8080, "container_port": 8080 }], 'dlceph02') manager.save_formation_to_etcd(f) etcd_ret = manager.etcd.get_key('{username}/{hostname}'.format( username=username, hostname=formation_name)) logger.debug("Etcd_ret: %s" % etcd_ret) logger.debug("Expected text: %s" % expected_text) self.assertEquals(etcd_ret, expected_text)
def load_formation_from_etcd(self, username, formation_name): f = Formation(username, formation_name) app_list = json.loads( json.loads( self.etcd.get_key( '/formations/{username}/{formation_name}'.format( username=username, formation_name=formation_name)))) for app in app_list: # If our host doesn't support swapping we're going to get some garbage # message in here if "WARNING" in app['container_id']: app['container_id'] = app['container_id'].replace("WARNING: Your "\ "kernel does not support memory swap capabilities. Limitation discarded.\n","") #Message changed in docker 0.8.0 app['container_id'] = app['container_id'].replace("WARNING: WARNING:"\ "Your kernel does not support swap limit capabilities. Limitation "\ "discarded.\n","") app['container_id'].strip('\n') # Set volumes if needed volumes = None if app['volumes']: self.logger.info("Setting volumes to: " + ''.join(app['volumes'])) volumes = app['volumes'] f.add_app(app['container_id'], app['hostname'], app['cpu_shares'], app['ram'], app['port_list'], app['ssh_port'], 22, app['host_server'], volumes) # Return fully parsed and populated formation object return f
def __init__(self, team, parent=None): super(editTeamDialog, self).__init__(parent) self.team = team self.setupUi(self) self.setWindowTitle("Edit Team") managerNameLabel = QLabel() managerNameLabel.setText("<b>{0}</b>".format(team.manager)) self.teamUserInfoLayout.addWidget(managerNameLabel, 0, 1) self.teamNameEdit.setText(team.team_name) if team.email: self.emailEdit.setText(team.email) self.type = team.team_type self.formation = Formation(team.formation) self.formationRadioButtons[str(team.formation)].setChecked(True) self.total_cost = 0 self.team_players = [ s.player for s in self.team.squad.current_players() ] self.setupSelections() self.setCurrentPlayers()
def load_formation_from_etcd(self, username, formation_name): f = Formation(username,formation_name) app_list = json.loads(json.loads( self.etcd.get_key('/formations/{username}/{formation_name}'.format( username=username, formation_name=formation_name)))) for app in app_list: # If our host doesn't support swapping we're going to get some garbage # message in here if "WARNING" in app['container_id']: app['container_id'] = app['container_id'].replace("WARNING: Your "\ "kernel does not support memory swap capabilities. Limitation discarded.\n","") #Message changed in docker 0.8.0 app['container_id'] = app['container_id'].replace("WARNING: WARNING:"\ "Your kernel does not support swap limit capabilities. Limitation "\ "discarded.\n","") app['container_id'].strip('\n') # Set volumes if needed volumes = None if app['volumes']: self.logger.info("Setting volumes to: " + ''.join(app['volumes'])) volumes = app['volumes'] f.add_app(app['container_id'], app['hostname'], app['cpu_shares'], app['ram'], app['port_list'], app['ssh_port'], 22, app['host_server'], volumes) # Return fully parsed and populated formation object return f
def _parse_formation_html(self, html): soup = BeautifulSoup(html) formations = soup.find('div', {'class': 'formations'}) home, away = formations.findAll('div', recursive=False) return { 'home': Formation(self._process_formation(home, True)), 'away': Formation(self._process_formation(away)) }
def __init__(self, name, formation_type): self.name = name self.common_image_dir = constant.BATTLE_COMMON_IMAGE_DIR self.war_dir = os.path.join(constant.WARS_DIR, self.name) self.war_image_dir = os.path.join(self.war_dir, 'image') self.inventory = Inventory() self.restore = Restore() self.formation = Formation(formation_type)
class Battle: """ handle how to execute battle flow""" def __init__(self, name, formation_type): self.name = name self.common_image_dir = constant.BATTLE_COMMON_IMAGE_DIR self.war_dir = os.path.join(constant.WARS_DIR, self.name) self.war_image_dir = os.path.join(self.war_dir, 'image') self.inventory = Inventory() self.restore = Restore() self.formation = Formation(formation_type) def run(self, device): rounds = 0 presetting = ImageForPresetting( os.path.join(constant.BATTLE_COMMON_IMAGE_DIR, 'combat')) while True: presetting.click_somewhere_to_wait(device) try: self.restore.check(device) if self.formation.formation_type: self.run_formation(device) self.enter_combat(device) while self.inventory.hit_limit(device): self.enter_combat(device) self.apply_flow(device) self.battle_flow(device) if self.formation.formation_type: self.formation.already_change = False except Exception as error: print(error) continue rounds += 1 print("The round :" + str(rounds)) def run_formation(self, device): if self.formation and not self.formation.already_change: self.formation.run(device) self.formation.already_change = True def enter_combat(self, device): enter_combat = CommonFlow(self.war_dir, self.war_image_dir, 'enter.xlsx') enter_combat.run(device) def apply_flow(self, device): apply_team = CommonFlow(self.war_dir, self.war_image_dir, 'apply.xlsx') apply_team.run(device) def battle_flow(self, device): battle = CommonFlow(self.war_dir, self.common_image_dir, self.name + '.xlsx') battle.run(device)
def __init__(self, team, parent=None): super(editTeamDialog, self).__init__(parent) self.team = team self.setupUi(self) self.setWindowTitle("Edit Team") managerNameLabel = QLabel() managerNameLabel.setText("<b>{0}</b>".format(team.manager)) self.teamUserInfoLayout.addWidget(managerNameLabel, 0, 1) self.teamNameEdit.setText(team.team_name) if team.email: self.emailEdit.setText(team.email) self.type = team.team_type self.formation = Formation(team.formation) self.formationRadioButtons[str(team.formation)].setChecked(True) self.total_cost = 0 self.team_players = [s.player for s in self.team.squad.current_players()] self.setupSelections() self.setCurrentPlayers()
def formationChanged(self, formation): if formation != str(self.formation): newFormation = Formation(formation) form = changeFormationDialog(self.playerSelections(), self.formation, newFormation, self) if form.exec_(): newPlayers = [] j = 0 oldPlayers = self.playerSelections() for p, q in zip(self.formation.list(), newFormation - self.formation): players = [] for i in range(p): player = oldPlayers.pop(0) if player not in form.playersOut: players.append(player) if q > 0: for i in range(q): players.append(None) newPlayers.extend(players) self.formation = newFormation self.setupSelections() self.setCurrentPlayers(newPlayers) else: self.formationRadioButtons[str( self.formation)].setChecked(True)
def __init__(self, arg): super(FormationPosition, self).__init__() self.arg = arg self._position self._formation = Formation() self._x self._y self._caption
def test_saveFormationToEtcd(self): logger = logging.getLogger() stream = logging.StreamHandler(sys.stdout) stream.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') stream.setFormatter(formatter) logger.addHandler(stream) manager = Manager(logger) expected_text = '[{"username": "******", "cpu-shares": 102, '\ '"ram": 100, "hostname": "app01", "ports": [{"host_port": 8080, '\ '"container_port": 8080}], "host_server": "dlceph02"}]' username = '******' formation_name = 'test_formation' f = Formation(username, formation_name) f.add_app(username, 'app01', 102, 100, [{"host_port":8080, "container_port":8080}], 'dlceph02') manager.save_formation_to_etcd(f) etcd_ret = manager.etcd.get_key('{username}/{hostname}'.format(username=username, hostname=formation_name)) logger.debug("Etcd_ret: %s" % etcd_ret) logger.debug("Expected text: %s" % expected_text) self.assertEquals(etcd_ret, expected_text)
def __init__(self, network, algorithm=None, single=True, interval=0.05, duration=0.1): threading.Thread.__init__(self) self.daemon = True self.network = network self.near = [] self.critical = [] self.teammate = [] # Used for formation algorithm self.algorithm = algorithm self.in_session = False self.context = None self.interval = interval # send velocity interval self.duration = duration # send velocity duration self.formation = Formation(self.network) self.single = single # For one UAV in formation self.update_proc = Process(target=self.update_drone_list) self.priority_proc = Process(target=self.give_priorities)
class editTeamDialog(QDialog, Ui_editTeamDialog): def __init__(self, team, parent=None): super(editTeamDialog, self).__init__(parent) self.team = team self.setupUi(self) self.setWindowTitle("Edit Team") managerNameLabel = QLabel() managerNameLabel.setText("<b>{0}</b>".format(team.manager)) self.teamUserInfoLayout.addWidget(managerNameLabel, 0, 1) self.teamNameEdit.setText(team.team_name) if team.email: self.emailEdit.setText(team.email) self.type = team.team_type self.formation = Formation(team.formation) self.formationRadioButtons[str(team.formation)].setChecked(True) self.total_cost = 0 self.team_players = [ s.player for s in self.team.squad.current_players() ] self.setupSelections() self.setCurrentPlayers() def setupSelections(self): for i, selection in enumerate(self.selections): model = PlayerItemModel() selection.setModel(model) position = self.formation[i] selection.clear() selection.addItem("") for player in Player.objects.position(position): selection.addItem("{0} - {1}".format(player.code, player.name), player) self.positionLabels[i].setText(position) selection.currentIndexChanged[int].connect( partial(self.playerSelected, i)) def setCurrentPlayers(self, players=None): if players is None: players = self.team_players for selection, player in zip(self.selections, players): selection.setCurrentIndex(selection.findData(player)) def playerSearch(self, playerNum): text = self.searchBoxes[playerNum].text() index = self.selections[playerNum].findText(text) if index > -1: self.selections[playerNum].setCurrentIndex(index) self.searchBoxes[playerNum].clear() def playerSelected(self, playerNum, index): if index > 0: player = self.selections[playerNum].itemData(index) self.clubLabels[playerNum].setText(player.club) self.valueLabels[playerNum].setText(str(player.value)) else: self.clubLabels[playerNum].clear() self.valueLabels[playerNum].clear() self.total_cost = sum([ selection.itemData(selection.currentIndex()).value for selection in self.selections if selection.currentIndex() > 0 ]) if self.total_cost > config.MAX_COST: color = "red" else: color = "black" self.totalCostLabel.setText( "<font color={0}>Total Cost {1}</font>".format( color, self.total_cost)) def formationChanged(self, formation): if formation != str(self.formation): newFormation = Formation(formation) form = changeFormationDialog(self.playerSelections(), self.formation, newFormation, self) if form.exec_(): newPlayers = [] j = 0 oldPlayers = self.playerSelections() for p, q in zip(self.formation.list(), newFormation - self.formation): players = [] for i in range(p): player = oldPlayers.pop(0) if player not in form.playersOut: players.append(player) if q > 0: for i in range(q): players.append(None) newPlayers.extend(players) self.formation = newFormation self.setupSelections() self.setCurrentPlayers(newPlayers) else: self.formationRadioButtons[str( self.formation)].setChecked(True) def playerSelections(self): return [ selection.itemData(selection.currentIndex()) for selection in self.selections if selection.currentIndex() > 0 ] def accept(self): players = self.playerSelections() self.team.team_name = self.teamNameEdit.text() self.team.email = unicode(self.emailEdit.text()).lower() if self.validTeam(players): playersOut = [ player for player in self.team_players if player not in players ] subs_made = len(playersOut) if subs_made > 0: if self.team.subs_used + subs_made > config.MAX_SUBS: QMessageBox.critical( self, "Substitution Error", "This manager has insufficient substitutions remaining" ) self.setCurrentPlayers() else: playersIn = [ player for player in players if player not in self.team_players ] form = confirmSubsDialog(playersOut, playersIn, self) if form.exec_(): self.team.subs_used += subs_made self.team.total_cost = self.total_cost self.team.formation = self.formation self.team.squad.substitute(playersIn, playersOut, form.datetime) self.confirmSubs(playersIn, playersOut, form.datetime) QDialog.accept(self) else: self.setCurrentPlayers() else: QDialog.accept(self) self.team.save() def confirmSubs(self, playersIn, playersOut, datetime): week = gameinfo.weekNum(datetime) + 1 date = gameinfo.weekToDate(week).date() html = render_to_string( 'subs_email.html', { 'team': self.team, 'players_in': playersIn, 'players_out': playersOut, 'week': week, 'date': date }) settings = QSettings() if settings.contains('SMTP/Server'): smtp_server = settings.value('SMTP/Server') else: smtp_server = None if not smtp_server: QMessageBox.critical(self, "Email Error", "Please add an SMTP server in settings.") else: email = HTMLEmail([self.team.email], "*****@*****.**", "Fantasy Football League", bcc='*****@*****.**') email.sendMail("Sub Confirmation", html, smtp_server) def validTeam(self, players): valid = True if players is not None and not len(set(players)) == 11: QMessageBox.critical( self, "Player Input Error", "There are not 11 unique players in the team.") valid = False if self.total_cost > config.MAX_COST: QMessageBox.critical( self, "Team Cost Error", "The team must not cost more than {0} in total.".format( config.MAX_COST)) valid = False return valid
def create_containers(self, user, number, formation_name, cpu_shares, ram, port_list, hostname_scheme, volume_list, force_host_server=None): f = Formation(user, formation_name) # Convert ram to bytes from MB ram = ram * 1024 * 1024 # Get the cluster machines on each creation cluster_list = self.get_docker_cluster() circular_cluster_list = CircularList(self.order_cluster_by_load(cluster_list)) # Loop for the requested amount of containers to be created for i in range(1, number+1): # [{"host_port":ssh_host_port, "container_port":ssh_container_port}] ssh_host_port = 9022 + i ssh_container_port = 22 host_server = circular_cluster_list[i].hostname # We are being asked to overwrite this if force_host_server: host_server = force_host_server validated_ports = [] while self.check_port_used(host_server, ssh_host_port): ssh_host_port = ssh_host_port +1 for port in port_list: self.logger.info("Checking if port {port} on {host} is in use".format( port=port, host=host_server)) if ':' in port: ports = port.split(':') # Only check if the host port is free. The container port should be free while self.check_port_used(host_server, ports[0]): ports[0] = int(ports[0]) + 1 # Add this to the validated port list validated_ports.append('{host_port}:{container_port}'.format( host_port = str(ports[0]), container_port = str(ports[1]))) else: while self.check_port_used(host_server, port): port = int(port) + 1 validated_ports.append(str(port)) self.logger.info('Adding app to formation {formation_name}: {hostname}{number} cpu_shares={cpu} ' 'ram={ram} ports={ports} host_server={host_server}'.format(formation_name=formation_name, hostname=hostname_scheme, number=str(i).zfill(3), cpu=cpu_shares, ram=ram, ports=validated_ports, host_server=host_server)) f.add_app(None, '{hostname}{number}'.format(hostname=hostname_scheme, number=str(i).zfill(3)), cpu_shares, ram, validated_ports, ssh_host_port, ssh_container_port, circular_cluster_list[i].hostname, volume_list) # Lets get this party started for app in f.application_list: self.start_application(app) self.logger.info("Sleeping 2 seconds while the container starts") time.sleep(2) self.bootstrap_application(app) self.logger.info("Saving the formation to ETCD") self.save_formation_to_etcd(f)
100, seed=25, obstacle_density=0.35) game_map = Map(map_data) start = Point(2, 2) end = Point(25, 65) rel_pos = [ Point(-1, 1), Point(1, 1), Point(0, 0), Point(-1, -1), Point(1, -1), ] units_pos = [start + pos for pos in rel_pos] units = [] for i, unit_pos in enumerate(units_pos): unit = FORMATION.copy(start + rel_pos[i]) unit.add_to_formation( Formation(i, rel_pos, start, units_pos, game_map.is_valid_point), i == 2) # 2nd index unit is leader unit.set_dest(end) unit.game_map = game_map units.append(unit) game_map = Map(map_data) game_map.active = units anim = animate_game_state(game_map, frames=200) anim.save('run_game.gif', writer='imagemagick', fps=10)
class editTeamDialog(QDialog, Ui_editTeamDialog): def __init__(self, team, parent=None): super(editTeamDialog, self).__init__(parent) self.team = team self.setupUi(self) self.setWindowTitle("Edit Team") managerNameLabel = QLabel() managerNameLabel.setText("<b>{0}</b>".format(team.manager)) self.teamUserInfoLayout.addWidget(managerNameLabel, 0, 1) self.teamNameEdit.setText(team.team_name) if team.email: self.emailEdit.setText(team.email) self.type = team.team_type self.formation = Formation(team.formation) self.formationRadioButtons[str(team.formation)].setChecked(True) self.total_cost = 0 self.team_players = [s.player for s in self.team.squad.current_players()] self.setupSelections() self.setCurrentPlayers() def setupSelections(self): for i, selection in enumerate(self.selections): model = PlayerItemModel() selection.setModel(model) position = self.formation[i] selection.clear() selection.addItem("") for player in Player.objects.position(position): selection.addItem("{0} - {1}".format(player.code, player.name), player) self.positionLabels[i].setText(position) selection.currentIndexChanged[int].connect(partial(self.playerSelected, i)) def setCurrentPlayers(self, players=None): if players is None: players = self.team_players for selection, player in zip(self.selections, players): selection.setCurrentIndex(selection.findData(player)) def playerSearch(self, playerNum): text = self.searchBoxes[playerNum].text() index = self.selections[playerNum].findText(text) if index > -1: self.selections[playerNum].setCurrentIndex(index) self.searchBoxes[playerNum].clear() def playerSelected(self, playerNum, index): if index > 0: player = self.selections[playerNum].itemData(index) self.clubLabels[playerNum].setText(player.club) self.valueLabels[playerNum].setText(str(player.value)) else: self.clubLabels[playerNum].clear() self.valueLabels[playerNum].clear() self.total_cost = sum([selection.itemData(selection.currentIndex()).value for selection in self.selections if selection.currentIndex() > 0]) if self.total_cost > config.MAX_COST: color = "red" else: color = "black" self.totalCostLabel.setText("<font color={0}>Total Cost {1}</font>".format(color, self.total_cost)) def formationChanged(self, formation): if formation != str(self.formation): newFormation = Formation(formation) form = changeFormationDialog(self.playerSelections(), self.formation, newFormation, self) if form.exec_(): newPlayers = [] j=0 oldPlayers = self.playerSelections() for p,q in zip(self.formation.list(),newFormation-self.formation): players=[] for i in range(p): player = oldPlayers.pop(0) if player not in form.playersOut: players.append(player) if q>0: for i in range(q): players.append(None) newPlayers.extend(players) self.formation = newFormation self.setupSelections() self.setCurrentPlayers(newPlayers) else: self.formationRadioButtons[str(self.formation)].setChecked(True) def playerSelections(self): return [selection.itemData(selection.currentIndex()) for selection in self.selections if selection.currentIndex() > 0] def accept(self): players = self.playerSelections() self.team.team_name = self.teamNameEdit.text() self.team.email = unicode(self.emailEdit.text()).lower() if self.validTeam(players): playersOut = [player for player in self.team_players if player not in players] subs_made = len(playersOut) if subs_made > 0: if self.team.subs_used + subs_made > config.MAX_SUBS: QMessageBox.critical(self, "Substitution Error", "This manager has insufficient substitutions remaining") self.setCurrentPlayers() else: playersIn = [player for player in players if player not in self.team_players] form = confirmSubsDialog(playersOut, playersIn, self) if form.exec_(): self.team.subs_used += subs_made self.team.total_cost = self.total_cost self.team.formation = self.formation self.team.squad.substitute(playersIn, playersOut, form.datetime) self.confirmSubs(playersIn, playersOut, form.datetime) QDialog.accept(self) else: self.setCurrentPlayers() else: QDialog.accept(self) self.team.save() def confirmSubs(self, playersIn, playersOut, datetime): week = gameinfo.weekNum(datetime) + 1 date = gameinfo.weekToDate(week).date() html = render_to_string('subs_email.html', {'team': self.team, 'players_in': playersIn, 'players_out': playersOut, 'week': week, 'date': date} ) settings = QSettings() if settings.contains('SMTP/Server'): smtp_server = settings.value('SMTP/Server') else: smtp_server = None if not smtp_server: QMessageBox.critical(self, "Email Error", "Please add an SMTP server in settings.") else: email = HTMLEmail([self.team.email], "*****@*****.**", "Fantasy Football League", bcc='*****@*****.**') email.sendMail("Sub Confirmation", html, smtp_server) def validTeam(self, players): valid = True if players is not None and not len(set(players)) == 11: QMessageBox.critical(self, "Player Input Error", "There are not 11 unique players in the team.") valid = False if self.total_cost > config.MAX_COST: QMessageBox.critical( self, "Team Cost Error", "The team must not cost more than {0} in total.".format(config.MAX_COST)) valid = False return valid
from formation import Formation MAX_COST = 50.0 MAX_SUBS = 8 FORMATION = Formation("4-4-2")
elif slot.side() == "right": self.score_[rule["field_line"]]["left"] += points else: self.score_[rule["field_line"]]["right"] += points self.done_ = True def get_formation_score(self): if not self.done_: self.__compute_score() # print "::scoreteam score sectors: " # print self.score_ for i in self.score_.keys(): for j in self.score_[i].keys(): self.total_score_ += self.score_[i][j] # print "::scoreteam total score: " + str(self.total_score_) return self.total_score_ if __name__ == "__main__": player_list = Player.create_default_team() print player_list print "\n\n\n" form = Formation.create_random_formation([2, 2, 2], player_list) print form print "\n\n\n" sct = ScoreTeam(form) print sct.get_formation_score() print sct.score_
def create_containers(self, user, number, formation_name, cpu_shares, ram, port_list, hostname_scheme, volume_list, docker_image, force_host_server=None): f = Formation(user, formation_name) # Convert ram to bytes from MB ram = ram * 1024 * 1024 # Get the cluster machines on each creation cluster_list = self.get_docker_cluster() circular_cluster_list = CircularList( self.order_cluster_by_load(cluster_list)) # Loop for the requested amount of containers to be created for i in range(1, number + 1): # [{"host_port":ssh_host_port, "container_port":ssh_container_port}] ssh_host_port = 9022 + i ssh_container_port = 22 host_server = circular_cluster_list[i].hostname hostname = '{hostname}{number}'.format(hostname=hostname_scheme, number=str(i).zfill(3)) # First check if we can add this host to salt. If not exit with -1 if self.check_salt_key_used(hostname): self.logger.error( 'Salt key is already taken for {hostname}'.format( hostname=hostname)) sys.exit(-1) # We are being asked to overwrite this if force_host_server: host_server = force_host_server validated_ports = [] while self.check_port_used(host_server, ssh_host_port): ssh_host_port = ssh_host_port + 1 for port in port_list: self.logger.info( "Checking if port {port} on {host} is in use".format( port=port, host=host_server)) if ':' in port: ports = port.split(':') # Only check if the host port is free. The container port should be free while self.check_port_used(host_server, ports[0]): ports[0] = int(ports[0]) + 1 # Add this to the validated port list validated_ports.append( '{host_port}:{container_port}'.format( host_port=str(ports[0]), container_port=str(ports[1]))) else: while self.check_port_used(host_server, port): port = int(port) + 1 validated_ports.append(str(port)) self.logger.info( 'Adding app to formation {formation_name}: {hostname} cpu_shares={cpu} ' 'ram={ram} ports={ports} host_server={host_server} docker_image={docker_image}' .format(formation_name=formation_name, hostname=hostname, cpu=cpu_shares, ram=ram, ports=validated_ports, host_server=host_server, docker_image=docker_image)) f.add_app(None, '{hostname}'.format(hostname=hostname), cpu_shares, ram, validated_ports, ssh_host_port, ssh_container_port, host_server, docker_image, volume_list) # Lets get this party started for app in f.application_list: self.start_application(app) #self.logger.info("Sleeping 2 seconds while the container starts") #time.sleep(2) #self.bootstrap_application(app) self.logger.info("Saving the formation to ETCD") self.save_formation_to_etcd(f)
class CollisionThread(threading.Thread): def __init__(self, network, algorithm=None, single=True, interval=0.05, duration=0.1): threading.Thread.__init__(self) self.daemon = True self.network = network self.near = [] self.critical = [] self.teammate = [] # Used for formation algorithm self.algorithm = algorithm self.in_session = False self.context = None self.interval = interval # send velocity interval self.duration = duration # send velocity duration self.formation = Formation(self.network) self.single = single # For one UAV in formation self.update_proc = Process(target=self.update_drone_list) self.priority_proc = Process(target=self.give_priorities) def run(self): # Deploy your collision avoidance algorithm here if self.algorithm == 'priorities': logger.info("Algorithm priorities activated") elif self.algorithm == 'formation': logger.info("Algorithm formation activated") else: pass while True: if self.algorithm == None: self.no_protocol() elif self.algorithm == 'priorities': self.priorities_protocol() elif self.algorithm == 'formation': self.formation_protocol() elif self.algorithm == 'landing': self.landing_protocol() elif self.algorithm == 'ownlanding': self.ownlanding_protocol() else: pass time.sleep(self.network.POLL_RATE) """A Collision Avoidance API""" def formation_protocol(self): self.update_drone_list() self.print_drones_in_vicinity() if self.network.vehicle_params.mode == "POSHOLD": return if self.network.vehicle_params.mode == "RTL" or self.network.vehicle_params.mode == "LAND": return if not self.formation.target_reached: self.reachTarget() # Check if reach Targets if not self.formation.home_returned and self.formation.home_returning: self.reachHome() # Check if return home location if self.formation.target_reached and self.formation.home_returned: self.algorithm = 'ownlanding' logger.info("Algorithm ownLanding activated") else: self.APF_formation(gotohome=False) def landing_protocol(self): """ Without formation force, and change to LAND after reaching the Team home position :return: """ self.update_drone_list() self.print_drones_in_vicinity() if self.network.vehicle_params.mode == "POSHOLD": return if self.network.vehicle_params.mode == "RTL" or self.network.vehicle_params.mode == "LAND": return else: self.changeMode("LAND") def ownlanding_protocol(self): """ Without formation force, and change to RTL after reaching the own home position :return: """ self.update_drone_list() self.print_drones_in_vicinity() if self.network.vehicle_params.mode == "RTL" or self.network.vehicle_params.mode == "LAND": return if not self.formation.ownhome_returned: self.reach_ownHome() else: if self.network.vehicle_params.mode is not "POSHOLD": self.changeMode("POSHOLD") if self.readytoLand(): self.changeMode("RTL") if not self.network.vehicle_params.readytoLand: self.APF_formation(gotohome=True) def no_protocol(self): # What to do if no protocol is specified # Currently it just outputs the drones in vicinity # self.update_proc.start() # self.update_proc.join() self.update_drone_list() self.print_drones_in_vicinity() def priorities_protocol(self): # A naive protocol based in priorities: # Stops every drone whose priority is other than '1' # Drone with priority '1' continues mission/movement # Currently works for AUTO mode # Give priorities # self.priority_proc.start() # self.priority_proc.join() self.give_priorities() # self.update_proc.start() # self.update_proc.join() self.update_drone_list() self.print_drones_in_vicinity() # Nothing to do if no drones are around and drone is not in avoidance state if len(self.near) == 0 and not self.in_session: return # Get priority number priority_num = self.get_priority_num() # Perform actions self.in_session = True if priority_num == 1: self.give_control() else: self.take_control() def give_priorities(self): """ -If drone is grounded then its priority is low, exempting mode 'ARMED' -System status needs to be checked first, if it's critical it has the greatest capability of all -----If status is OK then highest priority is given to highest mission importance first ---------Among same mission importances, highest priority is given to the ones with less capabilities ------------If they have the same status, mission importance, capabilities fine-tune with battery level """ # Temporary priority lists top = [] high = [] medium = [] low = [] # Depending the autopilot. The following modes are found in ardupilot grounded_state = [ 'UNINIT', 'BOOT', 'POWEROFF', 'STANDBY', 'CALIBRATING', 'LOCKED' ] auto_modes = [ 'RTL', 'LOITER', 'AUTO', 'GUIDED', 'CIRCLE', 'LAND', 'BRAKE', 'LIFTOFF' ] manual_modes = [ 'ALT_HOLD', 'STABILIZE', 'MANUAL', 'ACRO', 'SPORT', 'DRIFT', 'POSHOLD', 'SIMPLE', 'SUPER_SIMPLE' ] # Assign each drone to its priority list priority_num = 1 for drone in self.network.drones: has_capabilities = drone.set_global_alt or drone.set_attitude has_mayday = (drone.system_status == 'CRITICAL') or (drone.system_status == 'EMERGENCY') # Manual flying drones without capabilities # Drones in Emergency/Critical state if (drone.mode in manual_modes and not has_capabilities and drone.system_status not in grounded_state) or has_mayday: top.append(drone) # Top importance drones in flying state or ready to fly elif (drone.mission_importance == 2 and drone.system_status not in grounded_state or drone.mission_importance == 2 and drone.system_status not in grounded_state): high.append(drone) # Drones not in level-2 importance # Drones in flying state or armed with remote capabilities # Drones flying or armed in one of the automatic flying modes elif ((drone.mode in auto_modes and drone.system_status not in grounded_state) or (drone.mode in manual_modes and has_capabilities and drone.system_status not in grounded_state)): medium.append(drone) # Grounded drones with low importance elif drone.system_status in grounded_state: low.append(drone) # Assign priority number, redundant because list is sorted # based on priorities (top to lowest) but you never know... drone.priority = priority_num priority_num = priority_num + 1 # Sort each sublist based on mission importance (top and high priority lists don't need that) medium.sort(key=attrgetter('mission_importance'), reverse=True) low.sort(key=attrgetter('mission_importance'), reverse=True) # Fine-tune sorting with battery level top.sort(key=attrgetter('battery_level')) high.sort(key=attrgetter('battery_level')) medium.sort(key=attrgetter('battery_level')) low.sort(key=attrgetter('battery_level')) # Combine everything back to drones list drones = [top, high, medium, low] self.network.drones = list(itertools.chain.from_iterable(drones)) if self.debug: # Print priorities print "Printing Priorities:" for drone in self.network.drones: print "ID:", drone.ID, "SYSID_THISMAV:", drone.SYSID_THISMAV, " Priority: ", drone.priority def take_control(self): """Changes speed to zero by changing mode and overriding the RC3 channel""" if self.network.vehicle.mode.name == 'POSHOLD': # Already in POSHOLD mode pass else: # Save context self.save_context() # Change mode and assert self.network.vehicle.mode = VehicleMode("POSHOLD") # while self.network.vehicle.mode.name != "POSHOLD": # time.sleep(0.2) # self.network.vehicle.mode = VehicleMode("GUIDED") # while self.network.vehicle.mode.name != "POSHOLD": # time.sleep(0.5) # Give RC command so that we can bypass RC failsafe, 1500 means stay steady self.network.vehicle.channels.overrides['3'] = 1500 # throttle logger.info("Control taken!") def give_control(self): """Gives control by restoring to pre-avoidance state""" if self.context != None: self.restore_context() # Cancel RC override self.network.vehicle.channels.overrides['3'] = None # End session self.in_session = False logger.info("Control given!") def save_context(self): """Currently keeping information about mode and mission""" # Save context: mode, mission mode_name = self.network.vehicle.mode.name cur_mission = self.current_mission() next_waypoint = self.network.vehicle.commands.next self.context = Context(mode_name, cur_mission, next_waypoint) def restore_context(self): """Returns to the state before collision avoidance handling""" # Set to GUIDED mode to add any new commands if self.network.vehicle.mode.name != 'GUIDED': self.network.vehicle.mode = VehicleMode("GUIDED") # while self.network.vehicle.mode.name != "GUIDED": #Wait until mode is changed # time.sleep(0.5) # Save x, y, z values of mission waypoints in lists since commands.clear() # seems to clear every instance of CommandSequence object x = [] y = [] z = [] for wp in self.context.mission: x.append(wp.x) y.append(wp.y) z.append(wp.z) cmds = self.network.vehicle.commands cmds.clear() # Add pre-avoidance context: # Waypoints for i in range(0, len(x)): cmds.add( Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 0, 0, 0, 0, 0, x[i], y[i], z[i])) cmds.upload() # Next waypoint self.network.vehicle.commands.next = self.context.next_wp # Flight mode self.network.vehicle.mode = VehicleMode(self.context.mode) def update_drone_list(self): # Empty previous list components self.near[:] = [] self.critical[:] = [] self.teammate[:] = [] # 1.Remove entries that have not been updated for the last MAX_STAY seconds self.network.drones = [item for item in self.network.drones if \ (item.last_recv == None) \ or (time.time() - item.last_recv <= self.network.MAX_STAY)] # 2.Update own vehicle parameter entry in the right position for i in range(0, len(self.network.drones)): if self.network.drones[i].ID == self.network.vehicle_params.ID: self.network.drones[i] = self.network.vehicle_params break # 3.Update near-zone and critical-zone lists drone_list = self.network.drones # Only our drone is in the list if len(drone_list) == 1: pass else: # This value is slightly not concurrent own = self.network.vehicle_params # From the formation drones. self.teammate = [ item for item in drone_list if (item.ID != own.ID) ] # From the detected drones, add any within a SAFETY-to-CRITICAL-metre range self.near = [item for item in drone_list if \ (item.ID != own.ID) & (geo.get_distance_metres(own.global_lat, own.global_lon, item.global_lat, item.global_lon) <= self.network.NEAR_ZONE) \ & (abs(own.global_alt - item.global_alt) <= self.network.NEAR_ZONE)] # From the near drones, add any within a CRITICAL-metre range self.critical = [item for item in drone_list if \ (item.ID != own.ID) \ & (geo.get_distance_metres(own.global_lat, own.global_lon, item.global_lat, item.global_lon) <= self.network.CRITICAL_ZONE) \ & (abs(own.global_alt - item.global_alt) <= self.network.CRITICAL_ZONE)] def print_drones_in_vicinity(self): # Print drone IDs that are in close and critical range # Inform if no such drones are found logger.debug("Distance to target: %s", self.formation.get_distance2target()) if len(self.near) == 0 and len(self.critical) == 0 and len( self.teammate) == 0: # print "No dangerous drones found" pass else: own_lat = self.network.vehicle_params.global_lat own_lon = self.network.vehicle_params.global_lon ownPos_NED = geo.get_location_NED( self.formation.TeamHomeLocation, self.network.vehicle_params.global_lat, self.network.vehicle_params.global_lon, self.network.vehicle_params.global_alt) if self.algorithm == 'priorities': for drone in self.near: logger.info( "Drone approaching! ID: %s ; SYSID_THISMAV: %s !", drone.ID, drone.SYSID_THISMAV) logger.info( "Distance: %s", geo.get_distance_metres(own_lat, own_lon, drone.global_lat, drone.global_lon)) logger.info("Velocity: %s", drone.velocity) for drone in self.critical: logger.info( "Drone too close!!!! ID: %s ; SYSID_THISMAV: %s !", drone.ID, drone.SYSID_THISMAV) logger.info( "Distance: %s", geo.get_distance_metres(own_lat, own_lon, drone.global_lat, drone.global_lon)) logger.info("Velocity: %s", drone.velocity) elif self.algorithm == 'formation': for drone in self.teammate: objPos_NED = geo.get_location_NED( self.formation.TeamHomeLocation, drone.global_lat, drone.global_lon, drone.global_alt) logger.debug( "=========================================================" ) logger.debug("== Teammate drone; SYSID_THISMAV: %s !", drone.SYSID_THISMAV) logger.debug( "== DistanceWGS: %s", geo.get_distance_metres(own_lat, own_lon, drone.global_lat, drone.global_lon)) logger.debug("== DistanceNED: %s", geo.get_distance_NED(objPos_NED, ownPos_NED)) logger.debug("== Velocity: %s", drone.velocity) logger.debug( "=========================================================" ) def current_mission(self): # Retrieves current mission of vehicle cmds = self.network.vehicle.commands cmds.download cmds.wait_ready() return cmds def get_priority_num(self): priority_num = None for drone in self.network.drones: if drone.ID == self.network.vehicle_params.ID: priority_num = drone.priority break logger.info("Flying drone's priority number is: %s", priority_num) return priority_num def send_ned_velocity(self, velocity_x, velocity_y, velocity_z): """ Move vehicle in direction based on specified velocity vectors and for the specified duration. This uses the SET_POSITION_TARGET_LOCAL_NED command with a type mask enabling only velocity components (http://dev.ardupilot.com/wiki/copter-commands-in-guided-mode/#set_position_target_local_ned). Note that from AC3.3 the message should be re-sent every second (after about 3 seconds with no message the velocity will drop back to zero). In AC3.2.1 and earlier the specified velocity persists until it is canceled. The code below should work on either version (sending the message multiple times does not cause problems). See the above link for information on the type_mask (0=enable, 1=ignore). At time of writing, acceleration and yaw bits are ignored. """ logger.debug("NED Frame Velocity send: %s", [velocity_x, velocity_y, velocity_z]) msg = self.network.vehicle.message_factory.set_position_target_local_ned_encode( 0, # time_boot_ms (not used) 0, 0, # target system, target component mavutil.mavlink.MAV_FRAME_LOCAL_NED, # frame 0b0000111111000111, # type_mask (only speeds enabled) 0, 0, 0, # x, y, z positions (not used) velocity_x, velocity_y, velocity_z, # x, y, z velocity in m/s 0, 0, 0, # x, y, z acceleration (not supported yet, ignored in GCS_Mavlink) 0, 0) # yaw, yaw_rate (not supported yet, ignored in GCS_Mavlink) duration_range = int(self.duration / self.interval) # send command to vehicle on 1 Hz cycle for x in range(0, duration_range): self.network.vehicle.send_mavlink(msg) time.sleep(self.interval) def follow_leader_velocity(self): """ Temporary follow the SYSID_THISMAV 1 in speed. :return: """ if self.network.vehicle_params.SYSID_THISMAV != 1: for drone in self.teammate: if drone.SYSID_THISMAV == 1: velocity_x = drone.velocity[0] velocity_y = drone.velocity[1] velocity_z = drone.velocity[2] self.send_ned_velocity(velocity_x, velocity_y, velocity_z) def APF_formation(self, gotohome=False): velocity_x, velocity_y, velocity_z = self.formation.SendVelocity( self.teammate, self.single, gotohome) self.send_ned_velocity(velocity_x, velocity_y, velocity_z) # self.send_global_velocity(velocity_x, velocity_y, velocity_z) def check_takeoff_land(self): """ Check whether the teammate has already taken off or it is landing :return: """ # Return and do nothing if it is landing if (self.network.vehicle_params.mode == "RTL" or self.network.vehicle_params.mode == "LAND") \ and self.network.vehicle_params.global_alt != 0: return # Pass if it has already taken off if self.network.vehicle_params.armed and self.network.vehicle_params.global_alt != 0: for drone in self.teammate: if (drone.mode == "RTL" or drone.mode == "LAND") and drone.global_alt != 0: # On the way to home location or landing logger.info("Drone: %s landing, Landing off", drone.SYSID_THISMAV) self.network.vehicle.mode = VehicleMode(drone.mode) break # or it is on the land and the other's not landing else: for drone in self.teammate: if drone.armed and drone.global_alt != 0 and drone.mode == 'GUIDED': logger.info("Drone: %s taken off. Taking off !! ", drone.SYSID_THISMAV) arm_and_takeoff(self.network.vehicle) break def changeMode(self, mode): """ Change mode to Poshold or Guided :return: """ self.network.vehicle.mode = VehicleMode(mode) if mode == "POSHOLD": # Give RC command so that we can bypass RC failsafe, 1500 means stay steady self.network.vehicle.channels.overrides['3'] = 1500 # throttle if mode == "GUIDED": # Cancel RC override self.network.vehicle.channels.overrides['3'] = None logger.info("Mode changed to: %s", mode) return def reachTarget(self): return self.formation.reachTarget(self.teammate, self.single) def reachHome(self): return self.formation.reachHome(self.teammate, self.single) def reach_ownHome(self): return self.formation.reach_ownHome() def readytoLand(self): if not self.network.vehicle_params.readytoLand: self.formation.ownReadytoLand() if not self.single: result = self.network.vehicle_params.readytoLand for drone in self.teammate: result = result and drone.readytoLand return result else: return self.network.vehicle_params.readytoLand
def startPlot(self): self.c = pyqtSignal() planeNum = self.checkedNum #飞机数量 # 随机产生飞机初始坐标 x = Plane.random.uniform(1000 * self.num, 5000 * self.num) y = Plane.random.uniform(1000 * self.num, 5000 * self.num) z = Plane.random.uniform(5000, 15000) if self.sig1 == 0: #输入自定义坐标,则使用输入的坐标 if self.lineEdit.text() != '': x = int(self.lineEdit.text()) if self.lineEdit_2.text() != '': y = int(self.lineEdit_2.text()) if self.lineEdit_3.text() != '': z = int(self.lineEdit_3.text()) #飞机初始位置数据 p0 = [ x, y, z, Plane.random.uniform(0, math.pi), 0, Plane.random.uniform(150, 200), 0, 0 ] x2 = Plane.random.uniform(1000 * self.num, 5000 * self.num) y2 = Plane.random.uniform(1000 * self.num, 5000 * self.num) if self.sig2 == 0: #航母初始位置自定义 if self.lineEdit_4.text() != '': x2 = int(self.lineEdit_4.text()) if self.lineEdit_5.text() != '': y2 = int(self.lineEdit_5.text()) #航母初始位置数据 p1 = [x2, y2, 0, Plane.random.uniform(0, math.pi), 10, 0, 0, 0] planedata = [] shipdata = [] transdata = [] #航母数据生成 if len(self.shipActionArray) > 0: ship = Ship(self.shipActionArray, self.destroyer, self.frigate, p1) ship.creatrad() shipdata = copy.deepcopy(ship.Q_all) #飞机数据生成 if len(self.actionArray) > 0 and planeNum > 0: data = Plane.datac(self.actionArray, self.planetype.currentText(), p0, self.num) data.trackall() form = Formation() gap = self.planeGap #飞机间间隔 #判断编队方式 if self.formation.currentText() == "跟随编队": planedata = form.linearFormation(data.Q, planeNum, gap) if self.formation.currentText() == "菱形编队": planedata = form.diamondFormation(data.Q, planeNum, gap) if len(planedata) != 0: transdata = copy.deepcopy(self.F.dtransf(planedata)) #飞机数据转换 self.planedata.append(transdata) #把当前生成的数据加入到数组中,保存 self.shipdata.append(shipdata) #更新图像 self.gridlayout2.removeWidget(self.F) sip.delete(self.F) self.F = matplot.MyFigure([], []) self.F.plot(self.planedata, self.shipdata) self.gridlayout2.addWidget(self.F, 0, 1) #飞机的雷达数据 if len(transdata) != 0: radatai = [] for kk in range(len(transdata)): #遍历每一架飞机 distance = [x[3] for x in transdata[kk]] ##当前飞机对我方的距离 rad = Radar.datac(self.planetype.currentText(), len(transdata[kk])) rad.eledata(distance) radatai.append(rad.Q) self.raddata.append(radatai) #将雷达数据加入到数组保存 self.num = self.num + 1 #计数加一