2022-12-11 08:40:31 +01:00
|
|
|
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
2023-01-28 17:24:21 +01:00
|
|
|
import twin_threading # Multithreading (multitâches)
|
2023-01-09 05:54:31 +01:00
|
|
|
from twin_serial import jumeau, jumeau_stop, serial_close # Liaison série
|
2023-01-21 06:56:54 +01:00
|
|
|
from twin_daq import get, daq, csv_generate, plot, plot_generate # Acquisition des données
|
2022-12-11 08:40:31 +01:00
|
|
|
import time
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# porcou_lib.py
|
2022-12-18 22:29:06 +01:00
|
|
|
# @title: Bibliothèque utilisateur du portail coulissant
|
2022-12-11 08:40:31 +01:00
|
|
|
# @project: Blender-EduTech
|
|
|
|
# @lang: fr
|
|
|
|
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
2023-01-06 21:23:44 +01:00
|
|
|
# @copyright: Copyright (C) 2020-2023 Philippe Roy
|
2022-12-11 08:40:31 +01:00
|
|
|
# @license: GNU GPL
|
|
|
|
###############################################################################
|
|
|
|
|
2023-01-07 18:22:03 +01:00
|
|
|
# Récupérer la scène UPBGE
|
2022-12-11 08:40:31 +01:00
|
|
|
scene = bge.logic.getCurrentScene()
|
2022-12-11 15:50:38 +01:00
|
|
|
|
|
|
|
# UPBGE constants
|
|
|
|
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
|
|
|
|
|
2023-01-28 17:24:21 +01:00
|
|
|
# Threads de commandes
|
|
|
|
threads_cmd=[]
|
|
|
|
|
|
|
|
# Threads de visualisation de données
|
|
|
|
threads_plot=[]
|
|
|
|
|
2022-12-11 15:50:38 +01:00
|
|
|
###############################################################################
|
2022-12-23 14:30:13 +01:00
|
|
|
# Gyrophare
|
2022-12-11 08:40:31 +01:00
|
|
|
###############################################################################
|
|
|
|
|
2022-12-23 14:30:13 +01:00
|
|
|
# Ordre pour allumer le gyrophare
|
2022-12-22 08:11:43 +01:00
|
|
|
def gyr (order):
|
|
|
|
scene.objects['Led']['activated']=order
|
2022-12-11 08:40:31 +01:00
|
|
|
|
2022-12-23 14:30:13 +01:00
|
|
|
###############################################################################
|
|
|
|
# Actionneurs
|
|
|
|
###############################################################################
|
|
|
|
|
2022-12-22 08:11:43 +01:00
|
|
|
# Ordre pour le moteur phase ouvrir
|
|
|
|
def mot_o (order):
|
|
|
|
scene.objects['Moteur']['open']=order
|
2022-12-11 08:40:31 +01:00
|
|
|
|
2022-12-22 08:11:43 +01:00
|
|
|
# Ordre pour le moteur phase fermer
|
|
|
|
def mot_f (order):
|
|
|
|
scene.objects['Moteur']['close']=order
|
2022-12-11 08:40:31 +01:00
|
|
|
|
2023-01-19 02:27:47 +01:00
|
|
|
# Réglage de la vitesse du moteur numérique
|
|
|
|
def mot_digitset (speed=None):
|
|
|
|
if speed==None:
|
2023-01-19 15:35:57 +01:00
|
|
|
scene.objects['Moteur']['speed_setting']=125.6 # Vitesse du moteur numérique : 125.6 rad /s ( 20 tr / s )
|
2023-01-19 02:27:47 +01:00
|
|
|
else:
|
|
|
|
scene.objects['Moteur']['speed_setting']=speed
|
|
|
|
|
2022-12-22 08:11:43 +01:00
|
|
|
# Ordre pour le capteur barrage IR
|
|
|
|
def ir_emet(order):
|
|
|
|
scene.objects['Emetteur IR']['active']=order
|
2022-12-11 08:40:31 +01:00
|
|
|
|
2022-12-11 15:50:38 +01:00
|
|
|
###############################################################################
|
2022-12-11 08:40:31 +01:00
|
|
|
# Capteurs
|
|
|
|
###############################################################################
|
|
|
|
|
2022-12-22 08:11:43 +01:00
|
|
|
# Compte-rendu du capteur fin de course portail ouvert
|
2022-12-13 02:42:31 +01:00
|
|
|
def fdc_o ():
|
2023-01-31 18:37:22 +01:00
|
|
|
if scene.objects['Microrupteur fdc ouvert']['prior'] and scene.objects['Microrupteur fdc ouvert']['activated']:
|
2023-01-07 10:42:19 +01:00
|
|
|
return True
|
2023-01-31 18:37:22 +01:00
|
|
|
if scene.objects['Microrupteur fdc ouvert']['prior_real'] and scene.objects['Microrupteur fdc ouvert']['activated_real']:
|
|
|
|
return True
|
|
|
|
return False
|
2022-12-11 08:40:31 +01:00
|
|
|
|
2023-01-07 10:42:19 +01:00
|
|
|
# Compte-rendu du capteur fin de course portail fermé
|
2022-12-13 02:42:31 +01:00
|
|
|
def fdc_f ():
|
2023-01-31 18:37:22 +01:00
|
|
|
if scene.objects['Microrupteur fdc ferme']['prior'] and scene.objects['Microrupteur fdc ferme']['activated']:
|
2023-01-07 10:42:19 +01:00
|
|
|
return True
|
2023-01-31 18:37:22 +01:00
|
|
|
if scene.objects['Microrupteur fdc ferme']['prior_real'] and scene.objects['Microrupteur fdc ferme']['activated_real']:
|
|
|
|
return True
|
|
|
|
return False
|
2023-01-07 10:42:19 +01:00
|
|
|
|
2022-12-22 08:11:43 +01:00
|
|
|
# Compte-rendu du capteur barrage IR
|
2023-01-09 05:54:31 +01:00
|
|
|
# Absence d'obstacle -> True, présence d'obstacle -> False
|
2022-12-11 08:40:31 +01:00
|
|
|
def ir_recep ():
|
2023-01-31 18:37:22 +01:00
|
|
|
if scene.objects['Recepteur IR']['prior'] and scene.objects['Recepteur IR']['activated']==False:
|
|
|
|
return False
|
|
|
|
if scene.objects['Recepteur IR']['prior_real'] and scene.objects['Recepteur IR']['activated_real']==False:
|
|
|
|
return False
|
|
|
|
return True
|
2022-12-11 08:40:31 +01:00
|
|
|
|
2022-12-11 15:50:38 +01:00
|
|
|
###############################################################################
|
2022-12-11 08:40:31 +01:00
|
|
|
# Boutons poussoirs
|
|
|
|
###############################################################################
|
|
|
|
|
2022-12-22 08:11:43 +01:00
|
|
|
# Compte-rendu du bouton pousssoir coté rue
|
2022-12-11 08:40:31 +01:00
|
|
|
def bp_ext ():
|
2023-01-31 18:37:22 +01:00
|
|
|
if scene.objects['Bp cote rue']['prior'] and scene.objects['Bp cote rue']['activated']:
|
2023-01-07 10:42:19 +01:00
|
|
|
return True
|
2023-01-31 18:37:22 +01:00
|
|
|
if scene.objects['Bp cote rue']['prior_real'] and scene.objects['Bp cote rue']['activated_real']:
|
|
|
|
return True
|
|
|
|
return False
|
2022-12-11 08:40:31 +01:00
|
|
|
|
2022-12-22 08:11:43 +01:00
|
|
|
# Compte-rendu du bouton pousssoir coté cour
|
2022-12-11 08:40:31 +01:00
|
|
|
def bp_int ():
|
2023-01-31 18:37:22 +01:00
|
|
|
if scene.objects['Bp cote cour']['prior'] and scene.objects['Bp cote cour']['activated']:
|
|
|
|
return True
|
|
|
|
if scene.objects['Bp cote cour']['prior_real'] and scene.objects['Bp cote cour']['activated_real']:
|
2023-01-07 10:42:19 +01:00
|
|
|
return True
|
2023-01-31 18:37:22 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Jumelage
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
# Priorité du jumelage
|
|
|
|
# prior et prior_real : les deux jumeaux pilotent avec une priorité à l'activation (valeur par défaut),
|
|
|
|
# prior : uniquement le jumeau numérique est pilotant
|
|
|
|
# prior_real : uniquement le jumeau réel est pilotant
|
|
|
|
|
|
|
|
def jumeau_mode (priority_real=True, priority_digital=True):
|
|
|
|
|
|
|
|
# Jumeau numérique
|
|
|
|
if priority_digital:
|
|
|
|
scene.objects['Microrupteur fdc ouvert']['prior']=True
|
|
|
|
scene.objects['Microrupteur fdc ferme']['prior']=True
|
|
|
|
scene.objects['Bp cote cour']['prior'] =True
|
|
|
|
scene.objects['Bp cote rue']['prior'] =True
|
|
|
|
scene.objects['Recepteur IR']['prior'] =True
|
2023-01-07 10:42:19 +01:00
|
|
|
else:
|
2023-01-31 18:37:22 +01:00
|
|
|
scene.objects['Microrupteur fdc ouvert']['prior']=False
|
|
|
|
scene.objects['Microrupteur fdc ferme']['prior']=False
|
|
|
|
scene.objects['Bp cote cour']['prior'] =False
|
|
|
|
scene.objects['Bp cote rue']['prior'] =False
|
|
|
|
scene.objects['Recepteur IR']['prior'] =False
|
|
|
|
|
|
|
|
# Jumeau réel
|
|
|
|
if priority_real:
|
|
|
|
scene.objects['Microrupteur fdc ouvert']['prior_real']=True
|
|
|
|
scene.objects['Microrupteur fdc ferme']['prior_real']=True
|
|
|
|
scene.objects['Bp cote cour']['prior_real'] =True
|
|
|
|
scene.objects['Bp cote rue']['prior_real'] =True
|
|
|
|
scene.objects['Recepteur IR']['prior_real'] =True
|
|
|
|
else:
|
|
|
|
scene.objects['Microrupteur fdc ouvert']['prior_real']=False
|
|
|
|
scene.objects['Microrupteur fdc ferme']['prior_real']=False
|
|
|
|
scene.objects['Bp cote cour']['prior_real'] =False
|
|
|
|
scene.objects['Bp cote rue']['prior_real'] =False
|
|
|
|
scene.objects['Recepteur IR']['prior_real'] =False
|
|
|
|
|
|
|
|
def broche_mode (priority_real=True, priority_digital=True):
|
|
|
|
pass
|
2022-12-11 08:40:31 +01:00
|
|
|
|
2023-01-06 21:23:44 +01:00
|
|
|
###############################################################################
|
|
|
|
# Cycle
|
2022-12-11 08:40:31 +01:00
|
|
|
###############################################################################
|
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
##
|
2023-01-06 21:23:44 +01:00
|
|
|
# Temporisation
|
2023-01-21 06:56:54 +01:00
|
|
|
##
|
|
|
|
|
2022-12-11 08:40:31 +01:00
|
|
|
def tempo (duree):
|
|
|
|
time.sleep(duree)
|
2023-01-06 21:23:44 +01:00
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
##
|
2023-01-18 06:06:15 +01:00
|
|
|
# t (temps)
|
2023-01-21 06:56:54 +01:00
|
|
|
##
|
2023-01-18 06:06:15 +01:00
|
|
|
|
|
|
|
def get_t ():
|
2023-01-18 18:21:12 +01:00
|
|
|
# return truncate(scene.objects['System']['time'], 3)
|
|
|
|
return round(scene.objects['System']['time'], 3)
|
2023-01-18 06:06:15 +01:00
|
|
|
|
|
|
|
def set_t (date):
|
|
|
|
scene.objects['System']['time']=date
|
|
|
|
|
|
|
|
def reset_t ():
|
|
|
|
scene.objects['System']['time']=0
|
|
|
|
|
2023-01-28 17:24:21 +01:00
|
|
|
##
|
|
|
|
# Départ
|
|
|
|
##
|
|
|
|
|
|
|
|
def start(fct):
|
|
|
|
twin_threading.start(threads_cmd, "commands", fct)
|
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
##
|
2023-01-07 00:01:01 +01:00
|
|
|
# Arrêt
|
2023-01-21 06:56:54 +01:00
|
|
|
##
|
|
|
|
|
2023-01-07 00:01:01 +01:00
|
|
|
def stop():
|
2023-01-28 17:24:21 +01:00
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
# Jumeau
|
2023-01-07 00:01:01 +01:00
|
|
|
if scene.objects['System']['twins']:
|
2023-01-09 05:54:31 +01:00
|
|
|
serial_close(scene.objects['System']['board'])
|
2023-01-07 10:42:19 +01:00
|
|
|
time.sleep(1)
|
2023-01-21 06:56:54 +01:00
|
|
|
|
|
|
|
# Suivi de données
|
|
|
|
if scene.objects['System']['daq']:
|
|
|
|
csv_generate()
|
|
|
|
if scene.objects['System']['plot']:
|
2023-01-28 17:24:21 +01:00
|
|
|
plot_generate(threads_plot)
|
2023-01-21 06:56:54 +01:00
|
|
|
|
|
|
|
# Thread
|
2023-01-28 17:24:21 +01:00
|
|
|
twin_threading.stop(threads_cmd, "commands")
|
2023-01-07 00:01:01 +01:00
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
##
|
2023-01-07 00:01:01 +01:00
|
|
|
# Fin naturelle
|
2023-01-21 06:56:54 +01:00
|
|
|
##
|
|
|
|
|
2023-01-06 21:23:44 +01:00
|
|
|
def end():
|
2023-01-28 17:24:21 +01:00
|
|
|
fin()
|
2023-01-06 21:23:44 +01:00
|
|
|
|
|
|
|
def fin():
|
2023-01-28 17:24:21 +01:00
|
|
|
if scene.objects['System']['debug_thread']:
|
|
|
|
print ("Thread commands is arrived.")
|
|
|
|
time.sleep(0.125)
|
|
|
|
scene.objects['System']['thread_cmd']=False
|
|
|
|
time.sleep(0.125)
|