Tutoriel 4 : images de victoire et de chute sur la matrice de leds

This commit is contained in:
Philippe Roy 2023-05-08 08:53:38 +02:00
parent 1d18f53be5
commit 06bb084ea6
5 changed files with 84 additions and 56 deletions

View File

@ -51,9 +51,7 @@ bool serial_msg_complet = false; // Flag de message complet
void setup() {
// Liaison série
Serial.begin(115200); // 7 fps
/* Serial.begin(38400); */ // 6 fps
/* Serial.begin(9600); */ // trop lent 2fps
Serial.begin(115200);
// IMU
Wire.begin();
@ -66,10 +64,6 @@ void setup() {
matrix.clear();
matrix.display();
/* matrix.writeOnePicture(0xff90b2a2a6a4ae82); // Labyrinthe */
/* matrix.display(); */
// Ok
// Serial.println("Initialisation des composants I2C.");
}
/******************************************************************************
@ -79,7 +73,7 @@ void setup() {
void loop() {
/*****
* Lecture des accelerations
* Lecture des accélérations
*****/
accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
@ -89,7 +83,6 @@ void loop() {
roll = asin(-Axyz[0]);
roll_deg = roll*57.3;
roll_txt = String(roll_deg);
/* pitch = asin(Axyz[1]/cos(roll)); */
pitch = -asin(Axyz[1]/cos(roll)); // position capteur (X vers la gauche, Y vers l'arriere, Z vers le haut)
pitch_deg = pitch*57.3;
pitch_txt = String(pitch_deg);
@ -98,7 +91,6 @@ void loop() {
* IMU : Arduino -> UPBGE
*****/
//Serial.println("Roll (Rx): "+ roll_txt + " Pitch (Ry): " + pitch_txt);
Serial.print(roll_txt);
Serial.print(",");
Serial.print(pitch_txt);
@ -106,21 +98,37 @@ void loop() {
/*****
* Led Matrix : UPBGE -> Arduino
* https://xantorohara.github.io/led-matrix-editor
* labyrinte : 0xff90b2a2a6a4ae82
*****/
if (serial_msg_complet) {
/* matrix.writeString(serial_msg, 1000, ACTION_SCROLLING); */
/* matrix.display(); */
/* matrix.clear(); */
/* matrix.display(); */
/* matrix.writeOnePicture(0xff90b2a2a6a4ae82); // Labyrinthe */
int xy= serial_msg.toInt();
matrix.clear();
int xy= serial_msg.toInt(); // Message par chiffre : XY
// Postion de la bille en x et y
int x= xy/10;
int y= xy-(x*10);
matrix.clear();
matrix.writePixel(x, y, true);
if (xy<90) matrix.writePixel(x, y, true);
// Départ - Flèches
if (xy==90) {
matrix.writeOnePicture(0xe7c3a51818a5c3e7);
matrix.display();
delay(500);
Serial.println("start"); // Relance le jeu
}
// Chute
if (xy==91) {
matrix.writeOnePicture(0x81423c0000666600);
matrix.display();
delay(500);
Serial.println("start"); // Relance le jeu
}
// Victoire
if (xy==92) {
/* matrix.writeIcon(10); */
matrix.writeOnePicture(0x003c428100666600);
}
/* Orientation du plateau (N, NE, SE, S, SO, O, NO) */
/* if (serial_msg.length() ==2) { */

View File

@ -32,10 +32,8 @@ ACTIVATE = bge.logic.KX_INPUT_ACTIVE
serial_baud=115200
# serial_comm = serial.Serial('COM4',serial_baud, timeout=0.016) # Windows
serial_comm = serial.Serial('/dev/ttyACM1',serial_baud, timeout=0.016) # GNU/Linux
serial_msg_init = "99\n" # Vider la matrice de led
serial_comm.write(serial_msg_init.encode())
print (serial_comm)
serial_matrix_led=False # Afficher la position de la bille sur la matrice de leds
###############################################################################
# Gestion de la centrale inertielle (capteur IMU (inertial measurement unit))
@ -84,6 +82,7 @@ def applyRotationTo(obj, rx=None, ry=None, rz=None, Local=True):
# Lecture du capteur IMU
def capteur(cont):
obj = cont.owner # obj est l'objet associé au contrôleur donc 'Plateau'
obj_bille = scene.objects['Bille']
resolution = 0.2
# Touche ESC -> Quitter
@ -96,10 +95,21 @@ def capteur(cont):
serial_msg_in = str(serial_comm.readline())
# Affiche le message uniquement
if serial_msg_in.find("Print")>0 or serial_msg_in.find("Debug")>0 or serial_msg_in.find("Echo")>0:
print ("Communication port série : ", serial_msg_in)
serial_msg_in=""
return
# if serial_msg_in.find("Print")>0 or serial_msg_in.find("Debug")>0 or serial_msg_in.find("Echo")>0:
# print ("Communication port série : ", serial_msg_in)
# serial_msg_in=""
# return
# Mettre la bille à la position de départ avec une vitesse nulle
if serial_msg_in.find("start")>0:
if obj_bille['victoire'] or obj_bille['chute']:
obj_bille.worldLinearVelocity=(0, 0, 0)
obj_bille.worldAngularVelocity=(0, 0, 0)
obj_bille.worldPosition.x = obj_bille['init_x']
obj_bille.worldPosition.y = obj_bille['init_y']
obj_bille.worldPosition.z = obj_bille['init_z']+0.5 # On repose la bille
obj_bille['victoire']=False
obj_bille['chute'] = False
# Roll et Pitch
if serial_msg_in.find(",")>0:
@ -111,9 +121,9 @@ def capteur(cont):
applyRotationTo(scene.objects['Plateau'], x,y, 0)
# Ecriture de l'orientation du plateau sur la liaison série : programme Arduino : 3-labyrinthe-imu.ino
obj['Rx']=obj.worldOrientation.to_euler().x*57.3
obj['Ry']=obj.worldOrientation.to_euler().y*57.3
obj['Rz']=obj.worldOrientation.to_euler().z*57.3
# obj['Rx']=obj.worldOrientation.to_euler().x*57.3
# obj['Ry']=obj.worldOrientation.to_euler().y*57.3
# obj['Rz']=obj.worldOrientation.to_euler().z*57.3
# if obj['Rx']<-2 and obj['Ry'] >-2 and obj['Ry'] <2:
# serial_msg_out = "N\n"
# if obj['Rx']>2 and obj['Ry'] >-2 and obj['Ry'] <2:
@ -132,20 +142,7 @@ def capteur(cont):
# serial_msg_out = "SE\n"
# if obj['Rx']>-2 and obj['Rx']<2 and obj['Ry'] >-2 and obj['Ry'] <2:
# serial_msg_out = "X\n"
# Ecriture de la position de la bille sur la liaison série : programme Arduino : 3-labyrinthe-imu.ino
obj_bille = scene.objects['Bille']
obj_bille['x'] = obj_bille.worldPosition.x # de -3.5 à 3.5
obj_bille['y'] = obj_bille.worldPosition.y # de 3.5 à -3.5
obj_bille['Lx']=-1*round(obj_bille.worldPosition.x-3.5) # de 7 à 0
if obj_bille['Lx']<0: obj_bille['Lx']=0
if obj_bille['Lx']>7: obj_bille['Lx']=7
obj_bille['Ly']=-1*round(obj_bille.worldPosition.y-3.5) # de 0 à 7
if obj_bille['Ly']<0: obj_bille['Ly']=0
if obj_bille['Ly']>7: obj_bille['Ly']=7
serial_msg_out = str(obj_bille['Lx'])+str(obj_bille['Ly'])+"\n"
serial_comm.write(serial_msg_out.encode())
###############################################################################
# Gameplay
###############################################################################
@ -159,6 +156,10 @@ def init(cont):
obj['init_y']=obj.worldPosition.y
obj['init_z']=obj.worldPosition.z
# Afficher image de début (flèches) sur la matrice de leds
serial_msg_out = "90\n"
serial_comm.write(serial_msg_out.encode())
# Cacher le panneau de la victoire et suspendre la physique du panneau cliquable
scene.objects['Panneau victoire'].setVisible(False,True)
scene.objects['Panneau victoire - plan'].suspendPhysics (True)
@ -170,20 +171,37 @@ def cycle(cont):
obj['z']=obj.worldPosition.z # la propriété z est mis à jour avec la position globale en z de la bille
obj['vitesse z']=obj.worldLinearVelocity.z # la propriété z est mis à jour avec la position globale en z de la bille
# Chute ?
if obj['z'] < -10 and scene.objects['Panneau victoire'].visible == False:
print ("Chuuuu.....te")
# Ecriture de la position de la bille sur la liaison série : programme Arduino : 3-labyrinthe-imu.ino
if serial_matrix_led and obj['victoire']==False and obj['chute']==False:
# obj['x'] = obj.worldPosition.x # de -3.5 à 3.5
# obj['y'] = obj.worldPosition.y # de 3.5 à -3.5
obj['Lx']=-1*round(obj.worldPosition.x-3.5) # de 7 à 0
if obj['Lx']<0: obj['Lx']=0
if obj['Lx']>7: obj['Lx']=7
obj['Ly']=-1*round(obj.worldPosition.y-3.5) # de 0 à 7
if obj['Ly']<0: obj['Ly']=0
if obj['Ly']>7: obj['Ly']=7
serial_msg_out = str(obj['Lx'])+str(obj['Ly'])+"\n"
serial_comm.write(serial_msg_out.encode())
# Mettre la bille à la position de départ avec une vitesse nulle
obj.worldLinearVelocity=(0, 0, 0)
obj.worldAngularVelocity=(0, 0, 0)
obj.worldPosition.x = obj['init_x']
obj.worldPosition.y = obj['init_y']
obj.worldPosition.z = obj['init_z']+0.5 # On repose la bille
time.sleep(0.1)
# Chute ?
if obj['z'] < -10 and obj['victoire'] == False:
# Afficher image de chute sur la matrice de leds
print ("Chuuuu.....te")
serial_msg_out = "91\n"
serial_comm.write(serial_msg_out.encode())
obj['chute'] = True
# Victoire (colision de la bille avec l'arrivée)
def victoire(cont):
# Afficher image de victoire sur la matrice de leds
serial_msg_out = "92\n"
serial_comm.write(serial_msg_out.encode())
scene.objects['Bille']['victoire']=True
# Animation du Panneau victoire
scene.objects['Panneau victoire'].setVisible(True,True) # Afficher le panneau de la victoire
scene.objects['Panneau victoire - plan'].restorePhysics() # Restaurer la physique du panneau cliquable
start = 1
@ -214,7 +232,9 @@ def victoire_fermer(cont):
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive:
scene.objects['Panneau victoire'].setVisible(False,True) # Cacher le panneau de la victoire
scene.objects['Panneau victoire - plan'].suspendPhysics (True) # Suspendre la physique du panneau cliquable
scene.objects['Bille']['z']= -21 # On provoque le redémarrage si la bille est ressortie
serial_msg_out = "90\n" # Afficher image de début (flèches) sur la matrice de leds
serial_comm.write(serial_msg_out.encode())
# scene.objects['Bille']['z']= -21 # On provoque le redémarrage si la bille est ressortie
###############################################################################
# Gestion du Joystick USB