mirror of
https://forge.apps.education.fr/blender-edutech/blender-edutech-tutoriels.git
synced 2024-01-27 09:42:33 +01:00
Tutoriel 3 : capteur imu
This commit is contained in:
parent
87eb5d1c78
commit
87f20e7252
Binary file not shown.
@ -32,12 +32,11 @@ ACTIVATE = bge.logic.KX_INPUT_ACTIVE
|
||||
# Communication avec la carte Arduino
|
||||
###############################################################################
|
||||
|
||||
# serial_baud=500000
|
||||
serial_baud=115200 # 7 fps
|
||||
# serial_baud=38400 # 6 fps
|
||||
# serial_baud=9600 # 2 fps
|
||||
# serial_comm = serial.Serial('COM4',serial_baud) # Windows
|
||||
serial_comm = serial.Serial('/dev/ttyACM0',serial_baud) # GNU/Linux
|
||||
# serial_comm = serial.Serial('COM4',serial_baud, timeout=0.016) # Windows
|
||||
serial_comm = serial.Serial('/dev/ttyACM0',serial_baud, timeout=0.016) # GNU/Linux
|
||||
print (serial_comm)
|
||||
|
||||
###############################################################################
|
||||
@ -98,41 +97,27 @@ def capteur(cont):
|
||||
serial_comm.close()
|
||||
bge.logic.endGame()
|
||||
|
||||
# # Gestion du FPS - Tous les tics
|
||||
# milliseconds = int(time.time() * 1000) # Tous les tics
|
||||
# if milliseconds != fps_time:
|
||||
# fps = int(1000/(milliseconds-fps_time))
|
||||
# else:
|
||||
# fps = "----"
|
||||
# # print ("Durée entre deux tics (16 ms), fps (60) :", milliseconds-fps_time, fps)
|
||||
# fps_time = milliseconds
|
||||
|
||||
# Désactivation du capteur pendant la chute
|
||||
if scene.objects['Bille']['chute']:
|
||||
return
|
||||
# else:
|
||||
# return
|
||||
|
||||
# Lecture de la liaison série : programme Arduino : 3-labyrinthe-imu.ino
|
||||
serial_msg = str(serial_comm.readline()) # Communication série : Arduino -> UPBGE
|
||||
txt = serial_msg.split(',',2)
|
||||
# print (txt)
|
||||
x_txt = txt[0][2:]
|
||||
y_txt = txt[1][:-5]
|
||||
# print (x_txt, y_txt)
|
||||
# obj['IMU x']=-float(x_txt)
|
||||
# obj['IMU y']=-float(y_txt)
|
||||
# obj['Rx']=obj.worldOrientation.to_euler().x*57.3 # 360 / (2 * pi)
|
||||
# obj['Ry']=obj.worldOrientation.to_euler().y*57.3 # 360 / (2 * pi)
|
||||
# obj['Rz']=obj.worldOrientation.to_euler().z*57.3 # 360 / (2 * pi)
|
||||
serial_msg = str(serial_comm.readline())
|
||||
# print (serial_msg)
|
||||
if serial_msg.find(",")>0:
|
||||
# if ("\r\n") in serial_msg:
|
||||
# print (serial_msg)
|
||||
txt = serial_msg.split(',',2)
|
||||
# print (txt)
|
||||
x_txt = txt[0][2:]
|
||||
y_txt = txt[1][:-5]
|
||||
x=-(float(x_txt)/57.3) * resolution # 1/ 360 / (2 * pi)
|
||||
y=-(float(y_txt)/57.3) * resolution # 1/ 360 / (2 * pi)
|
||||
|
||||
x=-(float(x_txt)/57.3) * resolution # 1/ 360 / (2 * pi)
|
||||
y=-(float(y_txt)/57.3) * resolution # 1/ 360 / (2 * pi)
|
||||
|
||||
# Roll et Pitch
|
||||
# applyRotationTo(scene.objects['Plateau'], x,0, 0)
|
||||
applyRotationTo(scene.objects['Plateau'], x,y, 0)
|
||||
# obj.applyRotation((0,0,-obj.worldOrientation.to_euler().z), False)
|
||||
# Roll et Pitch
|
||||
# applyRotationTo(scene.objects['Plateau'], x,0, 0)
|
||||
applyRotationTo(scene.objects['Plateau'], x,y, 0)
|
||||
# obj.applyRotation((0,0,-obj.worldOrientation.to_euler().z), False)
|
||||
|
||||
###############################################################################
|
||||
# Gameplay
|
||||
@ -141,7 +126,7 @@ def capteur(cont):
|
||||
# Initialisation de la scène
|
||||
def init(cont):
|
||||
obj = cont.owner # obj est l'objet associé au contrôleur donc 'Bille'
|
||||
eevee_qualite(0)
|
||||
# eevee_qualite(0)
|
||||
|
||||
# Mémorisation de la position de départ de la bille
|
||||
obj['init_x']=obj.worldPosition.x
|
||||
@ -171,8 +156,8 @@ def chute(cont):
|
||||
print ("Chuuuu.....te")
|
||||
|
||||
# Replacement du plateau (tous les angles à 0 en plusieurs fois)
|
||||
while obj_plateau.worldOrientation.to_euler().x != 0 and obj_plateau.worldOrientation.to_euler().y !=0 and obj_plateau.worldOrientation.to_euler().z !=0 :
|
||||
obj_plateau.applyRotation((-obj_plateau.worldOrientation.to_euler().x, -obj_plateau.worldOrientation.to_euler().y, -obj_plateau.worldOrientation.to_euler().z), False)
|
||||
# while obj_plateau.worldOrientation.to_euler().x != 0 and obj_plateau.worldOrientation.to_euler().y !=0 and obj_plateau.worldOrientation.to_euler().z !=0 :
|
||||
# obj_plateau.applyRotation((-obj_plateau.worldOrientation.to_euler().x, -obj_plateau.worldOrientation.to_euler().y, -obj_plateau.worldOrientation.to_euler().z), False)
|
||||
|
||||
# Mettre la bille à la position de départ avec une vitesse nulle
|
||||
obj.worldLinearVelocity=(0, 0, 0)
|
||||
@ -182,6 +167,7 @@ def chute(cont):
|
||||
obj.worldPosition.z = obj['init_z']+0.5 # On repose la bille
|
||||
time.sleep(0.1)
|
||||
obj['chute']=False
|
||||
scene.objects['Plateau']['chute']=False
|
||||
|
||||
# Victoire (colision de la bille avec l'arrivée)
|
||||
def victoire(cont):
|
||||
|
@ -41,28 +41,15 @@ float pitch_deg;
|
||||
String roll_txt;
|
||||
String pitch_txt;
|
||||
|
||||
/******************************************************************************
|
||||
* Pupitre
|
||||
******************************************************************************/
|
||||
|
||||
// Adressage de la led Arduino
|
||||
const int led = 13; // Led de mouvement (onboard)
|
||||
const int led_com = 10; // Led de communication modele 3d-> arduino
|
||||
|
||||
/******************************************************************************
|
||||
* Initialisation
|
||||
******************************************************************************/
|
||||
|
||||
void setup() {
|
||||
|
||||
pinMode(led, OUTPUT); // Led de mouvement
|
||||
pinMode(led_com, OUTPUT); // Led de communication modele 3d-> arduino
|
||||
digitalWrite(led, LOW);
|
||||
digitalWrite(led_com, LOW);
|
||||
|
||||
// Moniteur serie
|
||||
// Liaison série
|
||||
Serial.begin(115200); // 7 fps
|
||||
/* Serial.begin(38400); */ // 6 fps
|
||||
/* Serial.begin(38400); */ // 6 fps
|
||||
/* Serial.begin(9600); */ // trop lent 2fps
|
||||
|
||||
// I2C
|
||||
@ -94,7 +81,7 @@ void loop() {
|
||||
pitch_txt = String(pitch_deg);
|
||||
|
||||
/*****
|
||||
* Communication : Arduino -> modèle 3d
|
||||
* Communication : Arduino -> UPBGE
|
||||
*****/
|
||||
|
||||
// Serial.println("Roll (Rx): "+ roll_txt + " Pitch (Ry): " + pitch_txt);
|
||||
|
@ -62,8 +62,8 @@ void setup() {
|
||||
|
||||
// Moniteur serie
|
||||
Serial.begin(115200); // 7 fps
|
||||
// Serial.begin(9600); // trop lent 2fps
|
||||
//Serial.begin(500000);
|
||||
// Serial.begin(38400); // 7 fps
|
||||
//Serial.begin(9600); // trop lent 2fps
|
||||
|
||||
// I2C
|
||||
Wire.begin();
|
||||
@ -97,11 +97,7 @@ void loop() {
|
||||
* Communication : Arduino -> modèle 3d
|
||||
*****/
|
||||
|
||||
// Serial.println("Roll (Rx): "+ roll_txt + " Pitch (Ry): " + pitch_txt);
|
||||
Serial.print(roll_txt);
|
||||
Serial.print(",");
|
||||
Serial.print(pitch_txt);
|
||||
Serial.println();
|
||||
Serial.println("Roll (Rx): "+ roll_txt + " Pitch (Ry): " + pitch_txt);
|
||||
|
||||
/* delay(300); */
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user