mirror of
https://forge.apps.education.fr/blender-edutech/blender-edutech-tutoriels.git
synced 2024-01-27 09:42:33 +01:00
Tutoriel 4 : images de victoire et de chute sur la matrice de leds
This commit is contained in:
parent
1d18f53be5
commit
06bb084ea6
Binary file not shown.
Binary file not shown.
@ -51,9 +51,7 @@ bool serial_msg_complet = false; // Flag de message complet
|
|||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
// Liaison série
|
// Liaison série
|
||||||
Serial.begin(115200); // 7 fps
|
Serial.begin(115200);
|
||||||
/* Serial.begin(38400); */ // 6 fps
|
|
||||||
/* Serial.begin(9600); */ // trop lent 2fps
|
|
||||||
|
|
||||||
// IMU
|
// IMU
|
||||||
Wire.begin();
|
Wire.begin();
|
||||||
@ -66,10 +64,6 @@ void setup() {
|
|||||||
matrix.clear();
|
matrix.clear();
|
||||||
matrix.display();
|
matrix.display();
|
||||||
/* matrix.writeOnePicture(0xff90b2a2a6a4ae82); // Labyrinthe */
|
/* matrix.writeOnePicture(0xff90b2a2a6a4ae82); // Labyrinthe */
|
||||||
/* matrix.display(); */
|
|
||||||
|
|
||||||
// Ok
|
|
||||||
// Serial.println("Initialisation des composants I2C.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
@ -79,7 +73,7 @@ void setup() {
|
|||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
/*****
|
/*****
|
||||||
* Lecture des accelerations
|
* Lecture des accélérations
|
||||||
*****/
|
*****/
|
||||||
|
|
||||||
accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
|
accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
|
||||||
@ -89,7 +83,6 @@ void loop() {
|
|||||||
roll = asin(-Axyz[0]);
|
roll = asin(-Axyz[0]);
|
||||||
roll_deg = roll*57.3;
|
roll_deg = roll*57.3;
|
||||||
roll_txt = String(roll_deg);
|
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 = -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_deg = pitch*57.3;
|
||||||
pitch_txt = String(pitch_deg);
|
pitch_txt = String(pitch_deg);
|
||||||
@ -98,7 +91,6 @@ void loop() {
|
|||||||
* IMU : Arduino -> UPBGE
|
* IMU : Arduino -> UPBGE
|
||||||
*****/
|
*****/
|
||||||
|
|
||||||
//Serial.println("Roll (Rx): "+ roll_txt + " Pitch (Ry): " + pitch_txt);
|
|
||||||
Serial.print(roll_txt);
|
Serial.print(roll_txt);
|
||||||
Serial.print(",");
|
Serial.print(",");
|
||||||
Serial.print(pitch_txt);
|
Serial.print(pitch_txt);
|
||||||
@ -106,21 +98,37 @@ void loop() {
|
|||||||
|
|
||||||
/*****
|
/*****
|
||||||
* Led Matrix : UPBGE -> Arduino
|
* Led Matrix : UPBGE -> Arduino
|
||||||
* https://xantorohara.github.io/led-matrix-editor
|
|
||||||
* labyrinte : 0xff90b2a2a6a4ae82
|
|
||||||
*****/
|
*****/
|
||||||
|
|
||||||
if (serial_msg_complet) {
|
if (serial_msg_complet) {
|
||||||
/* matrix.writeString(serial_msg, 1000, ACTION_SCROLLING); */
|
matrix.clear();
|
||||||
/* matrix.display(); */
|
int xy= serial_msg.toInt(); // Message par chiffre : XY
|
||||||
/* matrix.clear(); */
|
|
||||||
/* matrix.display(); */
|
// Postion de la bille en x et y
|
||||||
/* matrix.writeOnePicture(0xff90b2a2a6a4ae82); // Labyrinthe */
|
|
||||||
int xy= serial_msg.toInt();
|
|
||||||
int x= xy/10;
|
int x= xy/10;
|
||||||
int y= xy-(x*10);
|
int y= xy-(x*10);
|
||||||
matrix.clear();
|
if (xy<90) matrix.writePixel(x, y, true);
|
||||||
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) */
|
/* Orientation du plateau (N, NE, SE, S, SO, O, NO) */
|
||||||
/* if (serial_msg.length() ==2) { */
|
/* if (serial_msg.length() ==2) { */
|
||||||
|
Binary file not shown.
@ -32,10 +32,8 @@ ACTIVATE = bge.logic.KX_INPUT_ACTIVE
|
|||||||
serial_baud=115200
|
serial_baud=115200
|
||||||
# serial_comm = serial.Serial('COM4',serial_baud, timeout=0.016) # Windows
|
# 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_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)
|
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))
|
# 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
|
# Lecture du capteur IMU
|
||||||
def capteur(cont):
|
def capteur(cont):
|
||||||
obj = cont.owner # obj est l'objet associé au contrôleur donc 'Plateau'
|
obj = cont.owner # obj est l'objet associé au contrôleur donc 'Plateau'
|
||||||
|
obj_bille = scene.objects['Bille']
|
||||||
resolution = 0.2
|
resolution = 0.2
|
||||||
|
|
||||||
# Touche ESC -> Quitter
|
# Touche ESC -> Quitter
|
||||||
@ -96,10 +95,21 @@ def capteur(cont):
|
|||||||
serial_msg_in = str(serial_comm.readline())
|
serial_msg_in = str(serial_comm.readline())
|
||||||
|
|
||||||
# Affiche le message uniquement
|
# 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:
|
# 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)
|
# print ("Communication port série : ", serial_msg_in)
|
||||||
serial_msg_in=""
|
# serial_msg_in=""
|
||||||
return
|
# 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
|
# Roll et Pitch
|
||||||
if serial_msg_in.find(",")>0:
|
if serial_msg_in.find(",")>0:
|
||||||
@ -111,9 +121,9 @@ def capteur(cont):
|
|||||||
applyRotationTo(scene.objects['Plateau'], x,y, 0)
|
applyRotationTo(scene.objects['Plateau'], x,y, 0)
|
||||||
|
|
||||||
# Ecriture de l'orientation du plateau sur la liaison série : programme Arduino : 3-labyrinthe-imu.ino
|
# 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['Rx']=obj.worldOrientation.to_euler().x*57.3
|
||||||
obj['Ry']=obj.worldOrientation.to_euler().y*57.3
|
# obj['Ry']=obj.worldOrientation.to_euler().y*57.3
|
||||||
obj['Rz']=obj.worldOrientation.to_euler().z*57.3
|
# obj['Rz']=obj.worldOrientation.to_euler().z*57.3
|
||||||
# if obj['Rx']<-2 and obj['Ry'] >-2 and obj['Ry'] <2:
|
# if obj['Rx']<-2 and obj['Ry'] >-2 and obj['Ry'] <2:
|
||||||
# serial_msg_out = "N\n"
|
# serial_msg_out = "N\n"
|
||||||
# if obj['Rx']>2 and obj['Ry'] >-2 and obj['Ry'] <2:
|
# if obj['Rx']>2 and obj['Ry'] >-2 and obj['Ry'] <2:
|
||||||
@ -132,20 +142,7 @@ def capteur(cont):
|
|||||||
# serial_msg_out = "SE\n"
|
# serial_msg_out = "SE\n"
|
||||||
# if obj['Rx']>-2 and obj['Rx']<2 and obj['Ry'] >-2 and obj['Ry'] <2:
|
# if obj['Rx']>-2 and obj['Rx']<2 and obj['Ry'] >-2 and obj['Ry'] <2:
|
||||||
# serial_msg_out = "X\n"
|
# 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
|
# Gameplay
|
||||||
###############################################################################
|
###############################################################################
|
||||||
@ -159,6 +156,10 @@ def init(cont):
|
|||||||
obj['init_y']=obj.worldPosition.y
|
obj['init_y']=obj.worldPosition.y
|
||||||
obj['init_z']=obj.worldPosition.z
|
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
|
# Cacher le panneau de la victoire et suspendre la physique du panneau cliquable
|
||||||
scene.objects['Panneau victoire'].setVisible(False,True)
|
scene.objects['Panneau victoire'].setVisible(False,True)
|
||||||
scene.objects['Panneau victoire - plan'].suspendPhysics (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['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
|
obj['vitesse z']=obj.worldLinearVelocity.z # la propriété z est mis à jour avec la position globale en z de la bille
|
||||||
|
|
||||||
# Chute ?
|
# Ecriture de la position de la bille sur la liaison série : programme Arduino : 3-labyrinthe-imu.ino
|
||||||
if obj['z'] < -10 and scene.objects['Panneau victoire'].visible == False:
|
if serial_matrix_led and obj['victoire']==False and obj['chute']==False:
|
||||||
print ("Chuuuu.....te")
|
# 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
|
# Chute ?
|
||||||
obj.worldLinearVelocity=(0, 0, 0)
|
if obj['z'] < -10 and obj['victoire'] == False:
|
||||||
obj.worldAngularVelocity=(0, 0, 0)
|
|
||||||
obj.worldPosition.x = obj['init_x']
|
# Afficher image de chute sur la matrice de leds
|
||||||
obj.worldPosition.y = obj['init_y']
|
print ("Chuuuu.....te")
|
||||||
obj.worldPosition.z = obj['init_z']+0.5 # On repose la bille
|
serial_msg_out = "91\n"
|
||||||
time.sleep(0.1)
|
serial_comm.write(serial_msg_out.encode())
|
||||||
|
obj['chute'] = True
|
||||||
|
|
||||||
# Victoire (colision de la bille avec l'arrivée)
|
# Victoire (colision de la bille avec l'arrivée)
|
||||||
def victoire(cont):
|
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'].setVisible(True,True) # Afficher le panneau de la victoire
|
||||||
scene.objects['Panneau victoire - plan'].restorePhysics() # Restaurer la physique du panneau cliquable
|
scene.objects['Panneau victoire - plan'].restorePhysics() # Restaurer la physique du panneau cliquable
|
||||||
start = 1
|
start = 1
|
||||||
@ -214,7 +232,9 @@ def victoire_fermer(cont):
|
|||||||
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive:
|
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'].setVisible(False,True) # Cacher le panneau de la victoire
|
||||||
scene.objects['Panneau victoire - plan'].suspendPhysics (True) # Suspendre la physique du panneau cliquable
|
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
|
# Gestion du Joystick USB
|
||||||
|
Loading…
x
Reference in New Issue
Block a user