def orientation_with_ball(self, xo, yo): o_bot = self.orientation o = c.polar(complex(xo, yo) - self.positionc)[1] delta = (o - o_bot) delta += np.pi delta = delta % (2 * np.pi) delta -= np.pi vitesse_angulaire = min( sat_orientation_balle, abs(delta) * K_orientation_balle) * np.sign(delta) if (abs(delta) > .05): p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=-vitesse_angulaire / 9.25, spinner=True, velangular=vitesse_angulaire) else: p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=0, velangular=0., spinner=True) grSim.send(p)
def commande_position( self, x, y, o, spin=False): #commande vers une position avec orientation position_arrivee = Balle(x, y) Exb, Eyb = pt.Gradient(position_arrivee.potentiel) xd = int((self.x + longueur) / (2 * longueur) * nbPoints) yd = int((self.y + largeur) / (2 * largeur) * nbPoints) # print(Ex[yd,xd],xd,self.x,self.y) a, b = np.polyfit([x, self.x], [y, self.y], 1) self.champ_autre(self.x, x, a, b) Ex, Ey = Exb + k * self.Ex_autre, Eyb + k * self.Ey_autre Ex, Ey = pt.norme(Ex, Ey) vecteur = position_arrivee.position - self.positionc distance, phi = c.polar(vecteur) delta = o - self.orientation vitesse_tangente = K * (np.sin(self.orientation) * Ey[yd, xd] + np.cos(self.orientation) * Ex[yd, xd]) vitesse_normale = K * (-np.sin(self.orientation) * Ex[yd, xd] + np.cos(self.orientation) * Ey[yd, xd]) if (abs(distance) > 35) | (abs(delta) > 0.05): if distance < seuil_distance: vitesse_tangente = vitesse_tangente * distance / seuil_distance vitesse_normale = vitesse_normale * distance / seuil_distance p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=vitesse_tangente, velnormal=vitesse_normale, velangular=delta, spinner=spin) grSim.send(p) return 'EN COURS' # time.sleep(0.05) else: p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=0, velangular=0., spinner=True) grSim.send(p) return 'DONE'
def commande_balle(self): #permet de recuperer la balle ballon = self.match.balle Exb, Eyb = pt.Gradient(ballon.potentiel) a, b = np.polyfit([ballon.x, self.x], [ballon.y, self.y], 1) self.champ_autre(self.x, ballon.x, a, b) Ex, Ey = Exb + k * self.Ex_autre, Eyb + k * self.Ey_autre Ex, Ey = pt.norme(Ex, Ey) vecteur = ballon.position - self.positionc distance, phi = c.polar(vecteur) delta = phi - self.orientation xd = int((self.x + longueur) / (2 * longueur) * nbPoints) yd = int((self.y + largeur) / (2 * largeur) * nbPoints) spin = False vitesse_tangente = K * (np.sin(self.orientation) * Ey[yd, xd] + np.cos(self.orientation) * Ex[yd, xd]) vitesse_normale = K * (-np.sin(self.orientation) * Ex[yd, xd] + np.cos(self.orientation) * Ey[yd, xd]) if not self.hasTheBall(): if distance < seuil_distance: vitesse_tangente = vitesse_tangente * distance / seuil_distance vitesse_normale = vitesse_normale * distance / seuil_distance spin = True p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=vitesse_tangente, velnormal=vitesse_normale, velangular=delta / 0.2, spinner=spin) grSim.send(p) return 'EN COURS' else: p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0, velnormal=0, velangular=0, spinner=True) grSim.send(p) return 'DONE'
def Tir(self): if self.team == self.match.blue.nom: xbut, ybut = self.match.blue.but_adversaire else: xbut, ybut = self.match.yellow.but_adversaire posbut = complex(xbut, ybut) posbot = self.positionc vecteur = posbut - posbot distance, phi = c.polar(vecteur) # print(phi) self.orientation_with_ball(xbut, ybut) # self.commande_position(self.x,self.y,xbut,ybut) delta = phi - self.orientation if (abs(delta) < .05): p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=0, velangular=0., kickspeedx=10.) grSim.send(p) print('Boom') return 'DONE' else: return 'EN COURS'
def Passe(self): passeur = self.positionc mate = self.teammate() receveur = mate.positionc direction = receveur - passeur distance, phi = c.polar(direction) self.orientation_with_ball(phi) angle = (phi + np.pi) % (2 * np.pi) if angle > np.pi: angle -= 2 * np.pi mate.commande_position(mate.x, mate.y, angle) delta = phi - self.orientation delta1 = angle - mate.orientation if (abs(delta) < .05) & (abs(delta1) < 0.05): p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=0, velangular=0., kickspeedx=distance / 400) grSim.send(p) print('Passe')
def PasseEnProfondeur(self): passeur = self.positionc mate = self.teammate() receveur = mate.goto direction = receveur - passeur distance, phi = c.polar(direction) #orientation du passeur vers l'endroit de la passe # self.orientation_with_ball(mate.x,mate.y) self.commande_position(self.x, self.y, mate.x, mate.y) angle = (phi + np.pi) % (2 * np.pi) if angle > np.pi: angle -= 2 * np.pi delta = phi - self.orientation #Alignement réussi if abs(delta) < .05: p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=0, velangular=0., kickspeedx=self.puissanceKicker(distance)) grSim.send(p) print('Passe') self.defPoste('DEMARQUE') return 'DONE' else: return 'EN COURS'
def commande(x,y,o,Id): pos=complex(x,y) balls, blueBots, yellowBots = getVision() for bot in blueBots: if bot[5]==Id: botInfo=bot xbot,ybot,obot=botInfo[6],botInfo[7],botInfo[8] posbot=complex(xbot,ybot) vecteur=pos-posbot distance,phi=c.polar(vecteur) delta=o-obot print((abs(distance)>5)&(abs(delta)>0.01)) while not(abs(distance)<35): xd=int((xbot+longueur)/(2*longueur)*nbPoints) yd=int((ybot+largeur)/(2*largeur)*nbPoints) # print(Ex[yd,xd],xd,xbot) p = psl.packetCommandBot(isYellow=False, id=Id, veltangent=K*(sin(obot)*Ey[yd,xd]+cos(obot)*Ex[yd,xd]), velnormal=K*(-sin(obot)*Ex[yd,xd]+cos(obot)*Ey[yd,xd]), velangular=delta) grSim.send(p) time.sleep(0.1) pos=complex(x,y) balls, blueBots, yellowBots = getVision() for bot in blueBots: if bot[5]==Id: botInfo=bot xbot,ybot,obot=botInfo[6],botInfo[7],botInfo[8] posbot=complex(xbot,ybot) vecteur=pos-posbot distance,phi=c.polar(vecteur) delta=o-obot p = psl.packetCommandBot(isYellow=False, id=Id, veltangent=0., velnormal=0, velangular=0., spinner=True) grSim.send(p)
def orientation_with_ball(self, o): o_bot = self.orientation delta = o - o_bot if (abs(delta) > .05): p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=-delta / 8.75, spinner=True, velangular=delta) else: p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=0, velangular=0., spinner=True) grSim.send(p)
def commande_robot(self,Vtang,Vnorm,Vang,spinner=False,tir=0): if self.grSim!=None: p = psl.packetCommandBot(self.team=='Y', id=self.id, veltangent=Vtang, velnormal=Vnorm, velangular=Vang, spinner=spinner, kickspeedx=tir) self.grSim.send(p) if self.com: start=time.time() self.commande_com(self.id,Vtang,Vnorm,Vang,spinner,tir) print(time.time()-start)
def Passe(self): passeur = self.positionc mate = self.teammate() receveur = mate.positionc direction = receveur - passeur distance, phi = c.polar(direction) #orientation du passeur vers le receveur self.orientation_with_ball(mate.x, mate.y) # self.commande_position(self.x,self.y,mate.x,mate.y) angle = (phi + np.pi) % (2 * np.pi) if angle > np.pi: angle -= 2 * np.pi #orientation du receveur vers le passeur mate.commande_position(mate.x, mate.y, self.x, self.y) delta = phi - self.orientation delta1 = angle - mate.orientation #Alignement réussi IDEE arrêt lors du changement de signe if (abs(delta) < .05) & (abs(delta1) < 0.05): p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=0, velangular=0., kickspeedx=distance / 400) grSim.send(p) print('Passe') self.defPoste('DEMARQUE') self.teammate().defPoste('RECEVEUR') return 'DONE' else: return 'EN COURS'
def Tir(self): xbut = 1350 ybut = 100 posbut = complex(xbut, ybut) posbot = self.positionc vecteur = posbut - posbot distance, phi = c.polar(vecteur) # print(phi) self.orientation_with_ball(phi) delta = phi - self.orientation if (abs(delta) < .05): p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=0, velangular=0., kickspeedx=10.) grSim.send(p) print('Boom')
def action(self): for joueur in self.joueurs: if joueur.poste[-1] == 'SHOOTER': joueur.Tir() elif joueur.poste[-1] == 'CHASER': joueur.status = joueur.commande_balle() elif joueur.poste[-1] == 'AT2': p = psl.packetCommandBot(joueur.team == 'Y', id=joueur.id, veltangent=0., velnormal=0, velangular=0.) grSim.send(p) elif joueur.poste[-1] == 'BALLER': # field=self.joueurs[0].match.create_game() # final_pos,score=ia.play_game('ia2',game=field) # joueur.goto=final_pos # joueur.status=joueur.commande_position(joueur.goto[0], joueur.goto[1], 0,spin=True) joueur.defPoste('DRIBBLE') joueur.defPoste('DRIBBLE') # print(final_pos) # time.pause(5) elif joueur.poste[-1] == 'DRIBBLE': joueur.status = joueur.commande_position(joueur.goto[0], joueur.goto[1], 0, spin=True) joueur.defPoste('DRIBBLE') elif (joueur.poste[-1] == 'PASSEUR') & (joueur.teammate().poste[-1] == 'RECEVEUR'): joueur.Passe() joueur.defPoste('PASSEUR') elif (joueur.poste[-1] == 'RECEVEUR') & (joueur.teammate().poste[-1] == 'AT2'): joueur.reception() joueur.defPoste('RECEVEUR') if joueur.distance_balle < 100: joueur.poste[-1] == 'CHASER' elif joueur.poste[-1] == 'GOAL': joueur.goal() elif joueur.poste[-1] == 'DEF': if self.side == 'L': x = -600 o = 0 else: x = 600 o = np.pi joueur.commande_position(x, 0, o) elif joueur.poste[-1] == 'TACKLE': joueur.commande_balle()
spin = not spin cross = button[BUTTON_CROSS] if (R1 != button[BUTTON_R1]) & (R1 == False): print('r1') if idr == 0: idr = 1 else: idr = 0 R1 = button[BUTTON_R1] p = psl.packetCommandBot(False, idr, veltangent=0.5 * vt, velnormal=0.5 * vn, velangular=3 * va, spinner=spin, kickspeedx=tir) grSim.send(p) time.sleep(0.05) # ani=animation.FuncAnimation(fig,animate,interval=30) # plt.show() # match_test.joueurs[2].commande_balle() # match_test.joueurs[2].Passe() # time.sleep(0.3) # match_test.joueurs[3].Tir() # match_test.joueurs[2].Tir() # match_test.balle.Position()
def commande_position(self, x, y, xo, yo, balle=False, spin=False): position_arrivee = Balle(x, y) vecteur = position_arrivee.position - self.positionc distance, phi = c.polar(vecteur) o = c.polar(complex(xo, yo) - self.positionc)[1] delta = (o - self.orientation) delta += np.pi delta = delta % (2 * np.pi) delta -= np.pi vitesse_angulaire = min(sat_vitesse_angulaire, abs(delta) * K_angulaire) * np.sign(delta) distance_autres_joueurs = [] distance_autres_joueurs_balle = [] for robot in self.match.joueurs: if robot != self: distance_autres_joueurs.append( self.distanceToXY(robot.positionc)) distance_autres_joueurs_balle.append( robot.distanceToXY(robot.match.balle.position)) #si l'objectif est proche mais un autre robot aussi alors on fait abstraction des champs repulsifs if (min(distance_autres_joueurs) > distance) & (min(distance_autres_joueurs_balle) < 300) & (distance < 300) & balle: # print("oui") p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=K_proche * np.cos(delta), velnormal=K_proche * np.sin(delta), velangular=vitesse_angulaire, spinner=True) grSim.send(p) return 'EN COURS' elif (abs(distance) > 35) | (abs(delta) > 0.05): #champ créé par l'objectif Exb, Eyb = pt.Gradient(position_arrivee.potentiel) #Calcul des positions x,y discrètes sur la grille xd = int((self.x + longueur) / (2 * longueur) * nbPoints) yd = int((self.y + largeur) / (2 * largeur) * nbPoints) # print(Ex[yd,xd],xd,self.x,self.y) a, b = np.polyfit([x, self.x], [y, self.y], 1) self.champ_autre(self.x, x, y, a, b) Ex, Ey = Exb + k * self.Ex_autre, Eyb + k * self.Ey_autre #champ surfaces interdites Ex += 2 * surface_x Ey += 2 * surface_y Ex, Ey = pt.norme(Ex, Ey) # #Affichage du champ # fig = plt.figure() # ax1 = fig.add_subplot(1, 1, 1) # ax1.quiver(x_grid,y_grid,Ex,Ey,width=0.0008) #Saturation vitesse_tangente = min( saturation_vitesse_tangente, K_max * (np.sin(self.orientation) * Ey[yd, xd] + np.cos(self.orientation) * Ex[yd, xd])) vitesse_normale = min( saturation_vitesse_normale, K_max * (-np.sin(self.orientation) * Ex[yd, xd] + np.cos(self.orientation) * Ey[yd, xd])) if distance < seuil_distance: vitesse_tangente = vitesse_tangente * distance / seuil_distance vitesse_normale = vitesse_normale * distance / seuil_distance spin = True p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=vitesse_tangente, velnormal=vitesse_normale, velangular=vitesse_angulaire, spinner=spin) grSim.send(p) return 'EN COURS' #Objectif atteint else: p = psl.packetCommandBot(self.team == 'Y', id=self.id, veltangent=0., velnormal=0, velangular=0., spinner=True) grSim.send(p) return 'DONE'
# p = psl.packetCommandBot(False, # id=0, # veltangent=0., # velnormal=0, # velangular=0.) # grSim.send(p) # else : # match_test.joueurs[2].commande_balle() # break except KeyboardInterrupt: #mise à zéro de tous les robots lors de l'intérruption du programme print('INTERRUPTION') p = psl.packetCommandBot(True, id=0, veltangent=0., velnormal=0, velangular=0.) grSim.send(p) p = psl.packetCommandBot(True, id=1, veltangent=0., velnormal=0, velangular=0.) grSim.send(p) p = psl.packetCommandBot(False, id=0, veltangent=0., velnormal=0, velangular=0.) grSim.send(p)
def commande(self, veltangent, velnormal, velangular, spinner, kickspeedx): p = psl.packetCommandBot(self.equipe.couleur == "jaune", self.ID, veltangent, velnormal, velangular, spinner, kickspeedx) grSim.send(p)
def commande_position(self,x,y,xo,yo,balle=False,spin=False): position_arrivee=Balle(x,y) vecteur=position_arrivee.position-self.positionc distance,phi=c.polar(vecteur) o=c.polar(complex(xo,yo)-self.positionc)[1] delta=(o-self.orientation) delta+=np.pi delta=delta%(2*np.pi) delta-=np.pi vitesse_angulaire=min(sat_vitesse_angulaire,abs(delta)*K_angulaire)*np.sign(delta) distance_autres_joueurs=[] for robot in self.match.joueurs: if robot!=self: distance_autres_joueurs.append(self.distanceToXY(robot.positionc)) #si l'objectif est proche mais un autre robot aussi alors on fait abstraction des champs repulsifs if (min(distance_autres_joueurs)<R_non_evitement) & (distance<250)&balle: p = psl.packetCommandBot(self.team=='Y', id=self.id, veltangent=K_proche*np.cos(delta), velnormal=K_proche*np.sin(delta), velangular=vitesse_angulaire, spinner=True) grSim.send(p) return 'EN COURS' elif (abs(distance)>35)|(abs(delta)>0.05): Exb,Eyb=pt.Gradient(position_arrivee.potentiel) xd=int((self.x+longueur)/(2*longueur)*nbPoints) yd=int((self.y+largeur)/(2*largeur)*nbPoints) # print(Ex[yd,xd],xd,self.x,self.y) a,b=np.polyfit([x,self.x],[y,self.y],1) self.champ_autre(self.x,x,a,b) Ex,Ey=Exb+k*self.Ex_autre,Eyb+k*self.Ey_autre Ex,Ey=pt.norme(Ex,Ey) vitesse_tangente=min(saturation_vitesse_tangente,K_max*(np.sin(self.orientation)*Ey[yd,xd]+np.cos(self.orientation)*Ex[yd,xd])) vitesse_normale=min(saturation_vitesse_normale,K_max*(-np.sin(self.orientation)*Ex[yd,xd]+np.cos(self.orientation)*Ey[yd,xd])) if distance<seuil_distance: vitesse_tangente=vitesse_tangente*distance/seuil_distance vitesse_normale=vitesse_normale*distance/seuil_distance spin=True # if distance<seuil_distance: # vitesse_tangente=vitesse_tangente*distance/seuil_distance # vitesse_normale=vitesse_normale*distance/seuil_distance p = psl.packetCommandBot(self.team=='Y', id=self.id, veltangent=vitesse_tangente, velnormal=vitesse_normale, velangular=vitesse_angulaire, spinner=spin) grSim.send(p) return 'EN COURS' # time.sleep(0.05) else : p = psl.packetCommandBot(self.team=='Y', id=self.id, veltangent=0., velnormal=0, velangular=0., spinner=True) grSim.send(p) return 'DONE'