2022-12-05 14:07:17 +01:00
import bge # Bibliothèque Blender Game Engine (UPBGE)
2022-12-13 02:42:31 +01:00
import twin # Bibliothèque de l'environnement 3D des jumeaux numériques
2022-12-05 14:07:17 +01:00
import math
import time
###############################################################################
# porcou.py
# @title: Commandes pour le portail coulissant
# @project: Blender-EduTech
# @lang: fr
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
2023-01-14 07:49:53 +01:00
# @copyright: Copyright (C) 2020-2023 Philippe Roy
2022-12-05 14:07:17 +01:00
# @license: GNU GPL
###############################################################################
# Récupérer la scène UPBGE
scene = bge . logic . getCurrentScene ( )
2023-01-18 18:21:12 +01:00
# Configuration des variables publiques
# 'nom_variable' :
2023-01-31 11:15:21 +01:00
# - Objet 3D : [nom de l'objet 3D, propriété associée à la valeur (activate ou activated_real), type de la valeur ('d' (digital, binary), 'a', (analog) ou 'n' (numeric)), échelle (1 si ommis)]
2023-01-31 18:37:22 +01:00
# - Configuration de la broche : [nom de la propriété stockant l'object broche (pyfirmata),
# type de broche par défaut : 'd' (digital), 'a' (analog) ou 'p' (pwm)), mode de la broche par défaut : 'i' (input) ou 'o' (output)]
2023-01-25 23:37:55 +01:00
# - Configuration du graphique : ['marque', 'type de ligne', 'couleur', linewidth]] (Codification de Matplotlib)
2023-01-18 18:21:12 +01:00
#
# 'nom_variable_r' est la valeur réelle de la variable (valeur numérique) 'nom_variable' issue du jumelage numérique.
# Dans ce cas, il n'y a pas configuration de broche car elle est présente sur la variable 'nom_variable'.
2023-01-31 18:37:22 +01:00
# Ce distinguo ne concerne que les entrées, car les sorties sont pilotées par le numérique.
2023-01-18 18:21:12 +01:00
public_vars = {
2023-01-31 11:15:21 +01:00
' t ' : [ [ ' System ' , ' time ' , ' a ' ] , [ ] , [ ] ] ,
' bp_ext ' : [ [ ' Bp cote rue ' , ' activated ' , ' d ' ] , [ ' pin ' , ' d ' , ' i ' ] , [ ' . ' , ' - ' , ' green ' , 1 ] ] ,
' bp_ext_r ' : [ [ ' Bp cote rue ' , ' activated_real ' , ' d ' ] , [ ] , [ ' . ' , ' -- ' , ' green ' , 1 ] ] ,
' bp_int ' : [ [ ' Bp cote cour ' , ' activated ' , ' d ' ] , [ ' pin ' , ' d ' , ' i ' ] , [ ' . ' , ' - ' , ' darkgreen ' , 1 ] ] ,
' bp_int_r ' : [ [ ' Bp cote cour ' , ' activated_real ' , ' d ' ] , [ ] , [ ' . ' , ' -- ' , ' darkgreen ' , 1 ] ] ,
' fdc_o ' : [ [ ' Microrupteur fdc ouvert ' , ' activated ' , ' d ' ] , [ ' pin ' , ' d ' , ' i ' ] , [ ' . ' , ' - ' , ' orange ' , 1 ] ] ,
' fdc_o_r ' : [ [ ' Microrupteur fdc ouvert ' , ' activated_real ' , ' d ' ] , [ ] , [ ' . ' , ' -- ' , ' orange ' , 1 ] ] ,
' fdc_f ' : [ [ ' Microrupteur fdc ferme ' , ' activated ' , ' d ' ] , [ ' pin ' , ' d ' , ' i ' ] , [ ' . ' , ' - ' , ' darkorange ' , 1 ] ] ,
' fdc_f_r ' : [ [ ' Microrupteur fdc ferme ' , ' activated_real ' , ' d ' ] , [ ] , [ ' . ' , ' -- ' , ' darkorange ' , 1 ] ] ,
' mot_o ' : [ [ ' Moteur ' , ' open ' , ' d ' ] , [ ' pin_open ' , ' d ' , ' o ' ] , [ ' . ' , ' - ' , ' violet ' , 1 ] ] ,
' mot_f ' : [ [ ' Moteur ' , ' close ' , ' d ' ] , [ ' pin_close ' , ' d ' , ' o ' ] , [ ' . ' , ' - ' , ' darkviolet ' , 1 ] ] ,
' mot_angle ' : [ [ ' Moteur ' , ' alpha ' , ' a ' ] , [ ] , [ ' . ' , ' - ' , ' blue ' , 1 ] ] ,
' mot_vitesse ' : [ [ ' Moteur ' , ' speed ' , ' a ' ] , [ ] , [ ' . ' , ' - ' , ' darkblue ' , 1 ] ] ,
' mot_pas ' : [ [ ' Moteur ' , ' step ' , ' a ' ] , [ ] , [ ] ] ,
' portail_x ' : [ [ ' Portail ' , ' x ' , ' a ' ] , [ ] , [ ' . ' , ' - ' , ' turquoise ' , 1 ] ] ,
' portail_vitesse ' : [ [ ' Portail ' , ' speed ' , ' a ' ] , [ ] , [ ' . ' , ' - ' , ' darkturquoise ' , 1 ] ] ,
' portail_pas ' : [ [ ' Portail ' , ' step ' , ' a ' ] , [ ] , [ ] ] ,
2023-01-31 18:37:22 +01:00
' gyr ' : [ [ ' Led ' , ' activated ' , ' d ' ] , [ ' pin ' , ' d ' , ' o ' ] , [ ' . ' , ' - ' , ' gold ' , 1 ] ] ,
2023-01-31 11:15:21 +01:00
' ir_emet ' : [ [ ' Emetteur IR ' , ' activated ' , ' d ' ] , [ ' pin ' , ' d ' , ' o ' ] , [ ' . ' , ' - ' , ' red ' , 1 ] ] ,
' ir_recep ' : [ [ ' Recepteur IR ' , ' activated ' , ' d ' ] , [ ' pin ' , ' d ' , ' i ' ] , [ ' . ' , ' - ' , ' darkred ' , 1 ] ] ,
' ir_recep_r ' : [ [ ' Recepteur IR ' , ' activated_real ' , ' d ' ] , [ ] , [ ' . ' , ' -- ' , ' darkred ' , 1 ] ] }
2023-01-07 18:22:03 +01:00
2022-12-05 14:07:17 +01:00
# Couleurs
2022-12-22 08:11:43 +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
2023-01-07 10:42:19 +01:00
color_activated = ( 0.8 , 0.619 , 0.021 , 1 ) # bouton activé numériquement uniquement : jaune
color_activated_real = ( 0.799 , 0.031 , 0.038 , 1 ) # élément activé physiquement uniquement : rouge (hors clic)
color_activated_dbl = ( 0.246 , 0.687 , 0.078 , 1 ) # élément activé physiquement et numériquement : vert clair
2022-12-05 14:07:17 +01:00
# 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
2022-12-11 15:50:38 +01:00
###############################################################################
2022-12-13 02:42:31 +01:00
# Initialisation de la scène
2022-12-11 15:50:38 +01:00
###############################################################################
def init ( cont ) :
2022-12-16 00:37:11 +01:00
if cont . sensors [ ' Init ' ] . positive == False : # 1 seule fois
return False
2022-12-11 15:50:38 +01:00
twin . manip_init ( ) # Manipulation du modèle 3D
twin . cmd_init ( ) # Commandes
2023-01-07 10:42:19 +01:00
# Brochage
2023-01-18 18:21:12 +01:00
for pin in public_vars :
if public_vars [ pin ] [ 1 ] != [ ] :
scene . objects [ public_vars [ pin ] [ 0 ] [ 0 ] ] [ public_vars [ pin ] [ 1 ] [ 0 ] ] = None
2023-01-07 10:42:19 +01:00
2022-12-11 15:50:38 +01:00
# Mémorisation de la position et orientation des composants du système
scene . objects [ ' Portail ' ] [ ' init_lx ' ] = scene . objects [ ' Portail ' ] . worldPosition . x
scene . objects [ ' Portail ' ] [ ' init_ly ' ] = scene . objects [ ' Portail ' ] . worldPosition . y
scene . objects [ ' Portail ' ] [ ' init_lz ' ] = scene . objects [ ' Portail ' ] . worldPosition . z
scene . objects [ ' Engrenage ' ] [ ' init_rx ' ] = scene . objects [ ' Engrenage ' ] . worldOrientation . to_euler ( ) . x
scene . objects [ ' Engrenage ' ] [ ' init_ry ' ] = scene . objects [ ' Engrenage ' ] . worldOrientation . to_euler ( ) . y
scene . objects [ ' Engrenage ' ] [ ' init_rz ' ] = scene . objects [ ' Engrenage ' ] . worldOrientation . to_euler ( ) . z
2023-01-06 21:23:44 +01:00
2023-01-11 18:48:45 +01:00
# Groupe de focus pour les actionneurs
twin . cycle_def_focusgroup ( [ [ " Moteur " , " blue " ] ,
[ " Reducteur " , " blue " ] ,
[ " Pattes moteur " , " blue " ] ,
[ " Engrenage " , " blue-dark " ] ,
[ " Engrennage patte " , " blue-dark " ] ,
[ " Engrenage vis1 " , " grey " ] ,
[ " Engrenage vis2 " , " grey " ] ,
[ " Engrenage vis3 " , " grey " ] ] , " Moteur : mot_o(True | False), mot_f(True | False) " )
twin . cycle_def_focusgroup ( [ [ " Led " , " led_yellow " ] ] , " Gyrophare : gyr(True | False) " )
# Focus sur les boutons et capteurs
2023-01-06 21:23:44 +01:00
scene . objects [ ' Bp cote cour ' ] [ ' description ' ] = " Bouton poussoir coté cour : bp_int() "
scene . objects [ ' Bp cote rue ' ] [ ' description ' ] = " Bouton poussoir coté rue : bp_ext() "
scene . objects [ ' Microrupteur fdc ouvert ' ] [ ' description ' ] = " Capteur fin de course portail ouvert : fdc_o() "
scene . objects [ ' Microrupteur fdc ferme ' ] [ ' description ' ] = " Capteur fin de course portail fermé : fdc_f() "
scene . objects [ ' Emetteur IR ' ] [ ' description ' ] = " Capteur barrage IR (absence d \" obstacle) : ir_recep() "
scene . objects [ ' Recepteur IR ' ] [ ' description ' ] = " Capteur barrage IR (absence d \" obstacle) : ir_recep() "
2022-12-11 15:50:38 +01:00
system_init ( ) # Initialisation du système
2023-01-18 18:21:12 +01:00
def get_public_vars ( ) :
return public_vars
2023-01-09 05:54:31 +01:00
2022-12-11 15:50:38 +01:00
###############################################################################
2022-12-05 14:07:17 +01:00
# Actionneurs
###############################################################################
2022-12-13 02:42:31 +01:00
##
2022-12-22 08:11:43 +01:00
# Moteur et portail
2022-12-13 02:42:31 +01:00
##
2022-12-22 08:11:43 +01:00
def mot ( cont ) :
2022-12-11 15:50:38 +01:00
if scene . objects [ ' System ' ] [ ' run ' ] :
2023-01-19 15:35:57 +01:00
fps = 60 # frame per second
2022-12-05 14:07:17 +01:00
obj = cont . owner
2022-12-22 08:11:43 +01:00
obj_engrenage = scene . objects [ ' Engrenage ' ]
obj_portail = scene . objects [ ' Portail ' ]
2023-01-19 02:27:47 +01:00
2023-01-19 15:35:57 +01:00
# Réducteur
r = 1 / 100 # Rapport de réduction
2023-01-19 02:27:47 +01:00
# Crémaillaire
pas_dent = 7.85 # 7.85 mm soit 2.35 m (bender)
z = 14 # nb dents
2023-01-19 15:35:57 +01:00
obj [ ' step ' ] = obj [ ' speed_setting ' ] / fps # Vitesse du moteur numérique : 1,3 rad /s par défaut
obj_engrenage [ ' step ' ] = obj [ ' step ' ] * r
obj_portail [ ' step ' ] = obj_engrenage [ ' step ' ] * ( pas_dent * z ) / ( 2 * math . pi )
2023-01-07 10:42:19 +01:00
# Ouvrir
2022-12-22 08:11:43 +01:00
if obj [ ' open ' ] :
2023-01-07 10:42:19 +01:00
2023-02-03 05:58:20 +01:00
# Physique du modèle 3D
if obj [ ' prior ' ] :
obj_engrenage . applyRotation ( ( 0 , 0 , - obj_engrenage [ ' step ' ] ) , True )
obj [ ' alpha ' ] = obj [ ' alpha ' ] - obj [ ' step ' ]
if scene . objects [ ' System ' ] [ ' time ' ] != obj [ ' last_time ' ] :
obj [ ' speed ' ] = ( - obj [ ' step ' ] ) / ( scene . objects [ ' System ' ] [ ' time ' ] - obj [ ' last_time ' ] )
obj_portail . applyMovement ( ( - obj_portail [ ' step ' ] , 0 , 0 ) , True )
obj_portail [ ' x ' ] = obj_portail [ ' x ' ] - obj_portail [ ' step ' ] # Echelle pris en compte par le scale de 'System' : 0,3)
if scene . objects [ ' System ' ] [ ' time ' ] != obj [ ' last_time ' ] :
obj_portail [ ' speed ' ] = - obj_portail [ ' step ' ] / ( scene . objects [ ' System ' ] [ ' time ' ] - obj [ ' last_time ' ] )
obj [ ' last_time ' ] = scene . objects [ ' System ' ] [ ' time ' ]
2023-01-19 02:27:47 +01:00
2023-01-07 10:42:19 +01:00
# Modele 3D -> Arduino
2023-02-01 06:06:50 +01:00
if scene . objects [ ' System ' ] [ ' twins ' ] and obj [ ' prior_real ' ] :
2023-02-03 10:27:45 +01:00
if scene . objects [ ' Moteur ' ] [ ' pin_open ' ] is not None :
if scene . objects [ ' Moteur ' ] [ ' pin_close ' ] is not None :
scene . objects [ ' Moteur ' ] [ ' pin_close ' ] . write ( 0 )
scene . objects [ ' Moteur ' ] [ ' pin_open ' ] . write ( 1 )
2023-01-07 10:42:19 +01:00
# Fermer
2022-12-22 08:11:43 +01:00
# else: # Pas de priorité
if obj [ ' close ' ] :
2023-01-19 02:27:47 +01:00
2023-02-03 05:58:20 +01:00
# Physique du modèle 3D
if obj [ ' prior ' ] :
obj_engrenage . applyRotation ( ( 0 , 0 , obj_engrenage [ ' step ' ] ) , True )
obj [ ' alpha ' ] = obj [ ' alpha ' ] + obj [ ' step ' ]
if scene . objects [ ' System ' ] [ ' time ' ] != obj [ ' last_time ' ] :
obj [ ' speed ' ] = ( obj [ ' step ' ] ) / ( scene . objects [ ' System ' ] [ ' time ' ] - obj [ ' last_time ' ] )
obj_portail . applyMovement ( ( obj_portail [ ' step ' ] , 0 , 0 ) , True )
obj_portail [ ' x ' ] = obj_portail [ ' x ' ] + obj_portail [ ' step ' ] # Echelle pris en compte par le scale de 'System' : 0,3)
if scene . objects [ ' System ' ] [ ' time ' ] != obj [ ' last_time ' ] :
obj_portail [ ' speed ' ] = obj_portail [ ' step ' ] / ( scene . objects [ ' System ' ] [ ' time ' ] - obj [ ' last_time ' ] )
obj [ ' last_time ' ] = scene . objects [ ' System ' ] [ ' time ' ]
2022-12-05 14:07:17 +01:00
2023-01-07 10:42:19 +01:00
# Modele 3D -> Arduino
2023-02-01 06:06:50 +01:00
if scene . objects [ ' System ' ] [ ' twins ' ] and obj [ ' prior_real ' ] :
2023-02-03 10:27:45 +01:00
if scene . objects [ ' Moteur ' ] [ ' pin_close ' ] is not None :
if scene . objects [ ' Moteur ' ] [ ' pin_open ' ] is not None :
scene . objects [ ' Moteur ' ] [ ' pin_open ' ] . write ( 0 )
scene . objects [ ' Moteur ' ] [ ' pin_close ' ] . write ( 1 )
2023-01-07 10:42:19 +01:00
# Arrêrer
2023-02-01 06:06:50 +01:00
if obj [ ' open ' ] == False and obj [ ' close ' ] == False and obj [ ' prior ' ] :
2023-01-07 10:42:19 +01:00
2023-02-03 05:58:20 +01:00
# Physique du modèle 3D
if obj [ ' prior ' ] :
obj [ ' speed ' ] = 0
obj_portail [ ' speed ' ] = 0
obj [ ' last_time ' ] = scene . objects [ ' System ' ] [ ' time ' ]
2023-01-19 02:27:47 +01:00
2023-01-07 10:42:19 +01:00
# Modele 3D -> Arduino
2023-02-01 06:06:50 +01:00
if scene . objects [ ' System ' ] [ ' twins ' ] and obj [ ' prior_real ' ] :
2023-02-03 10:27:45 +01:00
if scene . objects [ ' Moteur ' ] [ ' pin_close ' ] is not None :
if scene . objects [ ' Moteur ' ] [ ' pin_open ' ] is not None :
scene . objects [ ' Moteur ' ] [ ' pin_open ' ] . write ( 0 )
scene . objects [ ' Moteur ' ] [ ' pin_close ' ] . write ( 0 )
2023-01-07 10:42:19 +01:00
2022-12-11 15:50:38 +01:00
###############################################################################
2022-12-05 14:07:17 +01:00
# Capteurs fin de course
###############################################################################
2022-12-13 02:42:31 +01:00
##
2022-12-05 14:07:17 +01:00
# Etat capteur fin de course portail ouvert
2022-12-13 02:42:31 +01:00
##
2022-12-22 08:11:43 +01:00
def fdc_o ( cont ) :
2022-12-11 15:50:38 +01:00
if scene . objects [ ' System ' ] [ ' run ' ] :
2022-12-05 14:07:17 +01:00
obj = cont . owner
2022-12-22 08:11:43 +01:00
2023-01-07 10:42:19 +01:00
# Arduino -> Modele 3D
2023-02-01 06:06:50 +01:00
if scene . objects [ ' System ' ] [ ' twins ' ] and obj [ ' prior_real ' ] :
2023-01-07 10:42:19 +01:00
if obj [ ' pin ' ] is not None :
if obj [ ' pin ' ] . read ( ) == True and obj [ ' activated_real ' ] == False :
obj [ ' activated_real ' ] = True
if obj [ ' pin ' ] . read ( ) == False and obj [ ' activated_real ' ] == True :
obj [ ' activated_real ' ] = False
2022-12-05 14:07:17 +01:00
# Etat capteur en fonction de la grille : worldPosition.x : 0 -> 65.5 et localPosition.x : 0 -> 218
2023-02-01 06:06:50 +01:00
if scene . objects [ ' Portail ' ] . localPosition . x < = 0 and obj [ ' activated ' ] == False and obj [ ' prior ' ] :
2022-12-22 08:11:43 +01:00
obj [ ' activated ' ] = True
2023-02-01 06:06:50 +01:00
if scene . objects [ ' Portail ' ] . localPosition . x > 0 and obj [ ' activated ' ] == True and obj [ ' prior ' ] :
2022-12-22 08:11:43 +01:00
obj [ ' activated ' ] = False
2023-01-07 10:42:19 +01:00
# Forçage par clic
2023-02-01 06:06:50 +01:00
if obj [ ' click ' ] == True and obj [ ' prior ' ] :
2022-12-22 08:11:43 +01:00
obj [ ' activated ' ] = True
2022-12-22 05:02:33 +01:00
# Couleurs
2023-01-07 10:42:19 +01:00
twin . cycle_sensitive_color ( obj )
2022-12-13 02:42:31 +01:00
##
2022-12-05 14:07:17 +01:00
# Etat capteur fin de course portail fermé
2022-12-13 02:42:31 +01:00
##
2022-12-22 08:11:43 +01:00
def fdc_f ( cont ) :
2022-12-11 15:50:38 +01:00
if scene . objects [ ' System ' ] [ ' run ' ] :
2022-12-05 14:07:17 +01:00
obj = cont . owner
2022-12-22 08:11:43 +01:00
2023-01-07 10:42:19 +01:00
# Arduino -> Modele 3D
2023-02-01 06:06:50 +01:00
if scene . objects [ ' System ' ] [ ' twins ' ] and obj [ ' prior_real ' ] :
2023-01-07 10:42:19 +01:00
if obj [ ' pin ' ] is not None :
if obj [ ' pin ' ] . read ( ) == True and obj [ ' activated_real ' ] == False :
obj [ ' activated_real ' ] = True
if obj [ ' pin ' ] . read ( ) == False and obj [ ' activated_real ' ] == True :
obj [ ' activated_real ' ] = False
2022-12-05 14:07:17 +01:00
# Etat capteur en fonction de la grille : worldPosition.x : 0 -> 65.5 et localPosition.x : 0 -> 218
2023-02-01 06:06:50 +01:00
if scene . objects [ ' Portail ' ] . localPosition . x > = 218 and obj [ ' activated ' ] == False and obj [ ' prior ' ] :
2022-12-22 08:11:43 +01:00
obj [ ' activated ' ] = True
2023-02-01 06:06:50 +01:00
if scene . objects [ ' Portail ' ] . localPosition . x < 218 and obj [ ' activated ' ] == True and obj [ ' prior ' ] :
2022-12-22 08:11:43 +01:00
obj [ ' activated ' ] = False
2022-12-05 14:07:17 +01:00
2023-01-07 10:42:19 +01:00
# Forçage par clic
2023-02-01 06:06:50 +01:00
if obj [ ' click ' ] == True and obj [ ' prior ' ] :
2022-12-22 08:11:43 +01:00
obj [ ' activated ' ] = True
2022-12-13 02:42:31 +01:00
2022-12-22 08:11:43 +01:00
# Couleurs
2023-01-07 10:42:19 +01:00
twin . cycle_sensitive_color ( obj )
2022-12-05 14:07:17 +01:00
2022-12-11 15:50:38 +01:00
###############################################################################
2022-12-13 02:42:31 +01:00
# Capteur barrage
2022-12-05 14:07:17 +01:00
###############################################################################
2022-12-13 02:42:31 +01:00
##
2022-12-05 14:07:17 +01:00
# Emetteur IR
2022-12-13 02:42:31 +01:00
##
2022-12-22 08:11:43 +01:00
def ir_emet ( cont ) :
2022-12-11 15:50:38 +01:00
if scene . objects [ ' System ' ] [ ' run ' ] :
2022-12-05 14:07:17 +01:00
obj = cont . owner
2022-12-31 16:48:54 +01:00
2023-01-11 18:48:45 +01:00
# Mouse over
if obj [ ' mo ' ] == True and obj [ ' click ' ] == False and obj . color != color_hl :
obj . color = color_hl
return
2022-12-31 16:48:54 +01:00
# Passif
if obj [ ' active ' ] == False and obj . color != color_passive :
obj . color = color_passive
2023-02-03 05:58:20 +01:00
# Physique du modèle 3D
if obj [ ' prior ' ] :
scene . objects [ ' Emetteur IR Led ' ] . setVisible ( True , False )
scene . objects [ ' Emetteur IR Led-on ' ] . setVisible ( False , False )
scene . objects [ ' Recepteur IR ' ] [ ' active ' ] = False
2022-12-31 16:48:54 +01:00
2023-01-07 10:42:19 +01:00
# Modele 3D -> Arduino
2023-02-03 05:58:20 +01:00
if scene . objects [ ' System ' ] [ ' twins ' ] and obj [ ' prior_real ' ] :
2023-01-07 10:42:19 +01:00
if scene . objects [ ' Emetteur IR ' ] [ ' pin ' ] is not None :
scene . objects [ ' Emetteur IR ' ] [ ' pin ' ] . write ( 0 )
2023-01-11 18:48:45 +01:00
return
2023-01-07 10:42:19 +01:00
2022-12-31 16:48:54 +01:00
# Active
if obj [ ' active ' ] :
# Allumage
if scene . objects [ ' Emetteur IR Led-on ' ] . visible == False :
2023-02-03 05:58:20 +01:00
# Physique du modèle 3D
if obj [ ' prior ' ] :
scene . objects [ ' Emetteur IR Led-on ' ] . setVisible ( True , False )
scene . objects [ ' Emetteur IR Led ' ] . setVisible ( False , False )
obj . color = color_active
scene . objects [ ' Recepteur IR ' ] [ ' active ' ] = True
2022-12-31 16:48:54 +01:00
2023-01-07 10:42:19 +01:00
# Modele 3D -> Arduino
2023-02-03 05:58:20 +01:00
if scene . objects [ ' System ' ] [ ' twins ' ] and obj [ ' prior_real ' ] :
2023-01-07 10:42:19 +01:00
if scene . objects [ ' Emetteur IR ' ] [ ' pin ' ] is not None :
scene . objects [ ' Emetteur IR ' ] [ ' pin ' ] . write ( 1 )
# Forçage par clic
2023-02-03 05:58:20 +01:00
if obj [ ' click ' ] == True and obj [ ' prior ' ] :
2022-12-31 16:48:54 +01:00
obj [ ' activated ' ] = True
scene . objects [ ' Recepteur IR ' ] [ ' activated ' ] = True
# Couleurs
2023-01-09 05:54:31 +01:00
# FIXME : à faire
2022-12-31 16:48:54 +01:00
if obj [ ' activated ' ] == True and obj . color != color_activated :
obj . color = color_activated
if obj [ ' activated ' ] == False :
if obj [ ' mo ' ] == True and obj . color != color_hl :
obj . color = color_hl
if obj [ ' mo ' ] == False and obj . color != color_active :
obj . color = color_active
##
# Récepteur IR
2023-01-09 05:54:31 +01:00
# FIXME : Modele 3D -> Arduino à faire
2022-12-31 16:48:54 +01:00
##
def ir_recep ( cont ) :
if scene . objects [ ' System ' ] [ ' run ' ] :
obj = cont . owner
2023-01-11 18:48:45 +01:00
# Mouse over
if obj [ ' mo ' ] == True and obj [ ' click ' ] == False and obj . color != color_hl :
obj . color = color_hl
return
2022-12-31 16:48:54 +01:00
# Passif
if obj [ ' active ' ] == False and obj . color != color_passive :
obj . color = color_passive
2023-01-11 18:48:45 +01:00
return
2022-12-31 16:48:54 +01:00
# Active
if obj [ ' active ' ] :
2023-02-03 05:58:20 +01:00
# Arduino -> Modele 3D (activé si Pin = False) FIXME : à vérifier avec le jumeau réel
if scene . objects [ ' System ' ] [ ' twins ' ] and obj [ ' prior_real ' ] :
if obj [ ' pin ' ] is not None :
if obj [ ' pin ' ] . read ( ) == False and obj [ ' activated_real ' ] == False :
obj [ ' activated_real ' ] = True
if obj [ ' pin ' ] . read ( ) == True and obj [ ' activated_real ' ] == True :
obj [ ' activated_real ' ] = False
2022-12-31 16:48:54 +01:00
2023-01-09 05:54:31 +01:00
# Forçage par clic
2023-02-03 05:58:20 +01:00
if obj [ ' click ' ] == True and obj [ ' prior ' ] :
2022-12-31 16:48:54 +01:00
obj [ ' activated ' ] = True
scene . objects [ ' Emetteur IR ' ] [ ' activated ' ] = True
# Couleurs
2023-01-09 05:54:31 +01:00
# FIXME : à faire
2022-12-31 16:48:54 +01:00
if obj [ ' activated ' ] == True and obj . color != color_activated :
obj . color = color_activated
if obj [ ' activated ' ] == False :
if obj [ ' mo ' ] == True and obj . color != color_hl :
obj . color = color_hl
if obj [ ' mo ' ] == False and obj . color != color_active :
obj . color = color_active
2022-12-11 15:50:38 +01:00
###############################################################################
# Système
2022-12-05 14:07:17 +01:00
###############################################################################
2022-12-13 02:42:31 +01:00
##
2022-12-11 15:50:38 +01:00
# Initialisation du système
2022-12-13 02:42:31 +01:00
##
2022-12-11 15:50:38 +01:00
def system_init ( ) :
2022-12-22 08:11:43 +01:00
system_reset ( )
2022-12-05 14:07:17 +01:00
2022-12-13 02:42:31 +01:00
##
2022-12-11 15:50:38 +01:00
# Réinitialisation du système
2022-12-13 02:42:31 +01:00
##
2022-12-11 15:50:38 +01:00
def system_reset ( ) :
2022-12-05 14:07:17 +01:00
2023-01-31 18:37:22 +01:00
# Entrées à l'état initial
objs = [ ' Microrupteur fdc ouvert ' , ' Microrupteur fdc ferme ' , ' Bp cote cour ' , ' Bp cote rue ' ]
for obj in objs :
scene . objects [ obj ] [ ' activated ' ] = False
scene . objects [ obj ] [ ' activated_real ' ] = False
scene . objects [ ' Recepteur IR ' ] [ ' activated ' ] = False
scene . objects [ ' Recepteur IR ' ] [ ' active ' ] = False
scene . objects [ ' Recepteur IR ' ] [ ' activated_real ' ] = True # Absence d'obstacle -> True, présence d'obstacle -> False
2022-12-11 15:50:38 +01:00
# Grille à l'état initial
2023-01-31 18:37:22 +01:00
scene . objects [ ' Portail ' ] [ ' x ' ] = 0
scene . objects [ ' Portail ' ] [ ' speed ' ] = 0
scene . objects [ ' Portail ' ] [ ' step ' ] = 0
2022-12-29 10:08:55 +01:00
scene . objects [ ' Portail ' ] . worldPosition . x = scene . objects [ ' Portail ' ] [ ' init_lx ' ] - scene . objects [ ' System ' ] [ ' init_lx ' ] + scene . objects [ ' System ' ] . worldPosition . x
scene . objects [ ' Portail ' ] . worldPosition . y = scene . objects [ ' Portail ' ] [ ' init_ly ' ] - scene . objects [ ' System ' ] [ ' init_ly ' ] + scene . objects [ ' System ' ] . worldPosition . y
scene . objects [ ' Portail ' ] . worldPosition . z = scene . objects [ ' Portail ' ] [ ' init_lz ' ] - scene . objects [ ' System ' ] [ ' init_lz ' ] + scene . objects [ ' System ' ] . worldPosition . z
2022-12-05 14:07:17 +01:00
2022-12-11 15:50:38 +01:00
# Moteur à l'état initial
2023-01-31 18:37:22 +01:00
scene . objects [ ' Moteur ' ] [ ' open ' ] = False
scene . objects [ ' Moteur ' ] [ ' close ' ] = False
scene . objects [ ' Moteur ' ] [ ' alpha ' ] = 0
scene . objects [ ' Moteur ' ] [ ' speed ' ] = 0
scene . objects [ ' Moteur ' ] [ ' speed_setting ' ] = 125.6 # Vitesse du moteur numérique : 125.6 rad /s ( 20 tr / s )
scene . objects [ ' Moteur ' ] [ ' step ' ] = 0
2022-12-05 14:07:17 +01:00
rres = 0.001 # resolution rotation
2022-12-11 15:50:38 +01:00
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 )
2022-12-05 14:07:17 +01:00
2022-12-22 08:11:43 +01:00
# Gyrophare à l'état initial
2023-01-31 18:37:22 +01:00
scene . objects [ ' Led ' ] [ ' activated ' ] = False
2022-12-11 15:50:38 +01:00
scene . objects [ ' Led ' ] . setVisible ( True , False )
2022-12-22 08:11:43 +01:00
scene . objects [ ' Led-on ' ] . setVisible ( False , False )
2022-12-05 14:07:17 +01:00
2022-12-11 15:50:38 +01:00
# Capteur barrage IR
2023-01-31 18:37:22 +01:00
scene . objects [ ' Emetteur IR ' ] [ ' activated ' ] = False
scene . objects [ ' Emetteur IR ' ] [ ' active ' ] = False
2022-12-11 15:50:38 +01:00
scene . objects [ ' Emetteur IR Led ' ] . setVisible ( True , False )
2022-12-22 08:11:43 +01:00
scene . objects [ ' Emetteur IR Led-on ' ] . setVisible ( False , False )
scene . objects [ ' Emetteur IR ' ] . color = color_passive
scene . objects [ ' Recepteur IR ' ] . color = color_passive
2022-12-05 14:07:17 +01:00
2023-01-31 18:37:22 +01:00
# Priorités activées
objs = [ ' Led ' , ' Moteur ' , ' Microrupteur fdc ouvert ' , ' Microrupteur fdc ferme ' ,
' Bp cote cour ' , ' Bp cote rue ' , ' Emetteur IR ' , ' Recepteur IR ' ]
for obj in objs :
scene . objects [ obj ] [ ' prior ' ] = True
scene . objects [ obj ] [ ' prior_real ' ] = True