jumeaux-numeriques/monte_charge/montchg.py

332 lines
14 KiB
Python
Raw Normal View History

import bge # Bibliothèque Blender Game Engine (UPBGE)
import twin # Bibliothèque de l'environnement 3D des jumeaux numériques
import math
import time
###############################################################################
# montchg.py
# @title: Commandes pour le monte-charge
# @project: Blender-EduTech
# @lang: fr
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
# @copyright: Copyright (C) 2022 Philippe Roy
# @license: GNU GPL
###############################################################################
# Récupérer la scène UPBGE
scene = bge.logic.getCurrentScene()
# Couleurs
2022-12-21 19:01:21 +01:00
color_passive = (0.800, 0.005, 0.315,1) # bouton non activable : magenta
color_active = (0.799, 0.130, 0.063,1) # bouton activable : orange
color_hl = (0.8, 0.8, 0.8, 1) # bouton focus : blanc
color_activated = (0.8, 0.619, 0.021, 1) # bouton activé : jaune
# Constantes UPBGE
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
###############################################################################
# Initialisation de la scène
###############################################################################
def init(cont):
if cont.sensors['Init'].positive == False: # 1 seule fois
return False
twin.manip_init() # Manipulation du modèle 3D
twin.cmd_init() # Commandes
2022-12-21 19:01:21 +01:00
# Mémorisation de la position et orientation des composants du système au départ
2022-12-21 22:23:47 +01:00
scene.objects['Cabine']['init_lx']=scene.objects['Cabine'].worldPosition.x
scene.objects['Cabine']['init_ly']=scene.objects['Cabine'].worldPosition.y
scene.objects['Cabine']['init_lz']=scene.objects['Cabine'].worldPosition.z
scene.objects['Contrepoids']['init_lx']=scene.objects['Contrepoids'].worldPosition.x
scene.objects['Contrepoids']['init_ly']=scene.objects['Contrepoids'].worldPosition.y
scene.objects['Contrepoids']['init_lz']=scene.objects['Contrepoids'].worldPosition.z
scene.objects['Moteur vis sans fin']['init_rx']=scene.objects['Moteur vis sans fin'].worldOrientation.to_euler().x
scene.objects['Moteur vis sans fin']['init_ry']=scene.objects['Moteur vis sans fin'].worldOrientation.to_euler().y
scene.objects['Moteur vis sans fin']['init_rz']=scene.objects['Moteur vis sans fin'].worldOrientation.to_euler().z
scene.objects['Moteur pignon']['init_rx']=scene.objects['Moteur pignon'].worldOrientation.to_euler().x
scene.objects['Moteur pignon']['init_ry']=scene.objects['Moteur pignon'].worldOrientation.to_euler().y
scene.objects['Moteur pignon']['init_rz']=scene.objects['Moteur pignon'].worldOrientation.to_euler().z
system_init() # Initialisation du système
###############################################################################
# Actionneurs
###############################################################################
##
# Action du simulateur pour le moteur
##
2022-12-21 19:01:21 +01:00
# def sml_moteur (cont):
# if scene.objects['System']['run']:
# obj = cont.owner
# pas_rot = math.pi/7 # z = 14
# pas = 2.35619/0.3 # pas echelle 1:1 = 2.35619 -> pas à l'échelle de la maquette (0,3) : 2.35619/0.3 = 7,85396
# vitesse = 0.05
# engrenage_obj = scene.objects['Engrenage']
# portail_obj = scene.objects['Portail']
# if obj['actif_ouvrir']:
# # print (scene.objects['Portail'].worldPosition.x)
# engrenage_obj.applyRotation((0, 0, -pas_rot*vitesse), True)
# portail_obj.applyMovement((-pas*vitesse, 0, 0), True)
# # else:
# if obj['actif_fermer']:
# # print (scene.objects['Portail'].worldPosition.x)
# engrenage_obj.applyRotation((0, 0, pas_rot*vitesse), True)
# portail_obj.applyMovement((pas*vitesse, 0, 0), True)
###############################################################################
# Capteurs fin de course
###############################################################################
##
# Etat capteur fin de course portail ouvert
##
2022-12-21 19:01:21 +01:00
# def sml_fdc_ouvert (cont):
# if scene.objects['System']['run'] :
# obj = cont.owner
# obj_etat=obj['actif']
# obj_microrupteur=scene.objects['Microrupteur fdc ouvert']
# # Etat capteur en fonction de la grille : worldPosition.x : 0 -> 65.5 et localPosition.x : 0 -> 218
# if scene.objects['Portail'].localPosition.x <= 0 and obj['actif'] == False :
# obj['actif'] = True
# if scene.objects['Portail'].localPosition.x > 0 and obj_microrupteur['actif'] == False and obj['actif'] == True :
# obj['actif'] = False
# #Forçage
# if obj_microrupteur['actif'] == True:
# obj['actif'] = True
# #Couleurs
# if obj['actif'] == True and obj_microrupteur.color !=couleur_jaune:
# obj_microrupteur.color =couleur_jaune
# if obj['actif'] == False :
# if obj_microrupteur['MO'] == True and obj_microrupteur.color !=couleur_blanc:
# obj_microrupteur.color =couleur_blanc
# if obj_microrupteur['MO'] == False and obj_microrupteur.color !=couleur_orange:
# obj_microrupteur.color =couleur_orange
##
# Etat capteur fin de course portail fermé
##
2022-12-21 19:01:21 +01:00
# def sml_fdc_ferme (cont):
# if scene.objects['System']['run'] :
# obj = cont.owner
# obj_etat=obj['actif']
# obj_microrupteur=scene.objects['Microrupteur fdc ferme']
# # Etat capteur en fonction de la grille : worldPosition.x : 0 -> 65.5 et localPosition.x : 0 -> 218
# if scene.objects['Portail'].localPosition.x >= 218 and obj['actif'] == False :
# obj['actif'] = True
# if scene.objects['Portail'].localPosition.x < 218 and obj_microrupteur['actif'] == False and obj['actif'] == True :
# obj['actif'] = False
# #Forçage
# if obj_microrupteur['actif'] == True:
# obj['actif'] = True
# #Couleurs
# if obj['actif'] == True and obj_microrupteur.color !=couleur_jaune:
# obj_microrupteur.color =couleur_jaune
# if obj['actif'] == False :
# if obj_microrupteur['MO'] == True and obj_microrupteur.color !=couleur_blanc:
# obj_microrupteur.color =couleur_blanc
# if obj_microrupteur['MO'] == False and obj_microrupteur.color !=couleur_orange:
# obj_microrupteur.color =couleur_orange
###############################################################################
2022-12-21 19:01:21 +01:00
# Système
###############################################################################
##
2022-12-21 19:01:21 +01:00
# Initialisation du système
# # Le moteur est géré en continue.
##
2022-12-21 19:01:21 +01:00
def system_init ():
system_reset()
##
2022-12-21 19:01:21 +01:00
# Réinitialisation du système
##
2022-12-21 19:01:21 +01:00
def system_reset ():
2022-12-21 19:01:21 +01:00
# Voyants aux états initiaux
scene.objects['Led niveau 0'].setVisible(True,False)
scene.objects['Led niveau 0-on'].setVisible(False,False)
scene.objects['Led niveau 1'].setVisible(True,False)
scene.objects['Led niveau 1-on'].setVisible(False,False)
# Cabine
# # applyRotationTo(scene.objects['System'], 0, 0, 0)
2022-12-21 22:23:47 +01:00
scene.objects['Cabine'].worldPosition.x = scene.objects['Cabine']['init_lx']
scene.objects['Cabine'].worldPosition.y = scene.objects['Cabine']['init_ly']
scene.objects['Cabine'].worldPosition.z = scene.objects['Cabine']['init_lz']
scene.objects['Contrepoids'].worldPosition.x = scene.objects['Contrepoids']['init_lx']
scene.objects['Contrepoids'].worldPosition.y = scene.objects['Contrepoids']['init_ly']
scene.objects['Contrepoids'].worldPosition.z = scene.objects['Contrepoids']['init_lz']
2022-12-21 19:01:21 +01:00
# # Moteur à l'état initial
# rres=0.001 # resolution rotation
# obj1=scene.objects['Engrenage']
# while (obj1.localOrientation.to_euler().y) > 1.1*rres :
# obj1.applyRotation((0, 0, -rres), True)
# while (obj1.localOrientation.to_euler().y) < -1.1*rres :
# obj1.applyRotation((0, 0, rres), True)
# I/O à l'état initial
scene.objects['Led niveau 0']['activated']=False
scene.objects['Led niveau 1']['activated']=False
scene.objects['Bp niveau 0']['activated']=False
scene.objects['Bp niveau 1']['activated']=False
scene.objects['Microrupteur niveau 0']['activated']=False
scene.objects['Microrupteur niveau 1']['activated']=False
##
2022-12-21 19:01:21 +01:00
# Boucle principale
##
2022-12-21 19:01:21 +01:00
def system_run ():
# # Lecture de la liaison série : programme Arduino : berceau_arduino.ino
# serial_msg = str(serial_comm.readline()) # Communication série : arduino -> modele 3d
# # Affiche le message uniquement
# if serial_msg.find("Print")>0 or serial_msg.find("Debug")>0 or serial_msg.find("Echo")>0:
# print ("Communication port série : ", serial_msg)
# serial_msg=""
# return
2022-12-21 22:23:47 +01:00
# Voyant niveau 0
# Modele 3d -> Arduino : FIXME
# Arduino -> Modele 3d : FIXME
if scene.objects['Led niveau 0']['activated']==True and scene.objects['Led niveau 0-on'].visible == False:
scene.objects['Led niveau 0-on'].setVisible(True,False)
scene.objects['Led niveau 0'].setVisible(False,False)
if scene.objects['Led niveau 0']['activated']==False and scene.objects['Led niveau 0-on'].visible == True:
scene.objects['Led niveau 0'].setVisible(True,False)
scene.objects['Led niveau 0-on'].setVisible(False,False)
# Voyant niveau 1
# Modele 3d -> Arduino : FIXME
# Arduino -> Modele 3d : FIXME
if scene.objects['Led niveau 1']['activated']==True and scene.objects['Led niveau 1-on'].visible == False:
scene.objects['Led niveau 1-on'].setVisible(True,False)
scene.objects['Led niveau 1'].setVisible(False,False)
if scene.objects['Led niveau 1']['activated']==False and scene.objects['Led niveau 1-on'].visible == True:
scene.objects['Led niveau 1'].setVisible(True,False)
scene.objects['Led niveau 1-on'].setVisible(False,False)
# Moteur et cabine
# Modele 3d -> Arduino : FIXME
# Arduino -> Modele 3d : FIXME
obj = cont.owner
pas_rot = math.pi/7 # z = 14
pas = 2.35619/0.3 # pas echelle 1:1 = 2.35619 -> pas à l'échelle de la maquette (0,3) : 2.35619/0.3 = 7,85396
vitesse = 0.05
obj_moteur = scene.objects['Moteur']
obj_vissansfin = scene.objects['Moteur vis sans fin']
obj_pignon = scene.objects['Moteur pignon']
obj_cabine = scene.objects['Cabine']
obj_contrepoids = scene.objects['Contrepoids']
obj_cable = scene.objects['Cable']
if obj_moteur['up']:
# print (scene.objects['Portail'].worldPosition.x)
obj_cabine.applyMovement((0, 0, pas*vitesse), True)
# engrenage_obj.applyRotation((0, 0, -pas_rot*vitesse), True)
# else:
if obj_moteur['down']:
# print (scene.objects['Portail'].worldPosition.x)
obj_cabine.applyMovement((0, 0, -pas*vitesse), True)
# engrenage_obj.applyRotation((0, 0, pas_rot*vitesse), True)
# print ("serial_msg : ", serial_msg)
# roll_txt = txt_extrac(serial_msg,"Roll (Rx): ", " Pitch")
# # pitch_txt= txt_extrac(serial_msg,"Pitch (Ry): ", " \\r\\n")
# pitch_txt = txt_extrac(serial_msg,"Pitch (Ry): ", " bt_a_m:")
# if roll_txt !="" and pitch_txt !="" :
# obj1=scene.objects['Plateau']
# roll=float(roll_txt)/57.3
# pitch=float(pitch_txt)/57.3
# # print ("Roll : ", roll, " Pitch : ", pitch)
# applyRotationTo(scene.objects['Plateau'], roll,0, 0)
# applyRotationTo(scene.objects['Plateau'], roll,pitch, 0)
# scene.objects['Plateau']['roll']=roll
# scene.objects['Plateau']['pitch']=pitch
# Capteurs
2022-12-21 19:01:21 +01:00
# Modele 3d -> Arduino : FIXME
# Arduino -> Modele 3d : FIXME
# Arduino -> numérique
# bt_a_m_txt = txt_extrac_bool(serial_msg,"bt_a_m: ")
# bt_a_d_txt = txt_extrac_bool(serial_msg,"bt_a_d: ")
# bt_b_m_txt = txt_extrac_bool(serial_msg,"bt_b_m: ")
# bt_b_d_txt = txt_extrac_bool(serial_msg,"bt_b_d: ")
# bt_c_m_txt = txt_extrac_bool(serial_msg,"bt_c_m: ")
# bt_c_d_txt = txt_extrac_bool(serial_msg,"bt_c_d: ")
# bp_phy('Bp Am', bt_a_m_txt)
# bp_phy('Bp Ad', bt_a_d_txt)
# bp_phy('Bp Bm', bt_b_m_txt)
# bp_phy('Bp Bd', bt_b_d_txt)
# bp_phy('Bp Cm', bt_c_m_txt)
# bp_phy('Bp Cd', bt_c_d_txt)
2022-12-21 22:23:47 +01:00
# # Affichage de l'activation physique des boutons
# def bp_phy(obj_name, bp_phy_sig):
# obj=scene.objects[obj_name]
# if bp_phy_sig =="0":
# obj['actif_phy'] = True
# obj.color = couleur_jaune
# else:
# if obj['actif_phy']:
# obj['actif_phy'] = False
# obj.color = couleur_orange
###############################################################################
2022-12-21 19:01:21 +01:00
# Interface
###############################################################################
##
2022-12-21 19:01:21 +01:00
# Highlight sur les éléments cliquables du systèmes
##
2022-12-21 19:01:21 +01:00
def hl(cont):
obj = cont.owner
name=obj.name
2022-12-21 19:01:21 +01:00
# Activation
if cont.sensors['MO'].status == JUST_ACTIVATED and scene.objects['System']['run']:
scene.objects[name].color = color_hl
2022-12-21 19:01:21 +01:00
# Désactivation
if cont.sensors['MO'].status == JUST_RELEASED:
scene.objects[name].color = color_active
##
2022-12-21 19:01:21 +01:00
# Click sur les éléments cliquables du systèmes (activation numérique)
##
2022-12-21 19:01:21 +01:00
def click(cont):
obj = cont.owner
name=obj.name
2022-12-21 19:01:21 +01:00
# Activation
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['System']['manip_mode']==0:
scene.objects[name].color = color_activated
obj['activated'] = True
# Modele 3d -> Arduino : FIXME
2022-12-21 19:01:21 +01:00
# Désactivation
if cont.sensors['Click'].status == JUST_RELEASED:
obj['activated'] = False
# Modele 3d -> Arduino : FIXME
if cont.sensors['MO'].positive:
scene.objects[name].color = color_hl
else:
scene.objects[name].color = color_active