2022-12-11 08:40:31 +01:00
|
|
|
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
2023-01-14 14:03:15 +01:00
|
|
|
import subprocess # Multiprocessus
|
|
|
|
from twin_threading import thread_cmd_start, thread_cmd_stop, thread_cmd_end # 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-14 14:03:15 +01:00
|
|
|
import os
|
|
|
|
import sys
|
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
|
|
|
|
|
|
|
|
###############################################################################
|
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
|
|
|
|
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-07 10:42:19 +01:00
|
|
|
if scene.objects['Microrupteur fdc ouvert']['activated'] or scene.objects['Microrupteur fdc ouvert']['activated_real']:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
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-07 10:42:19 +01:00
|
|
|
if scene.objects['Microrupteur fdc ferme']['activated'] or scene.objects['Microrupteur fdc ferme']['activated_real']:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
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-07 10:42:19 +01:00
|
|
|
if scene.objects['Recepteur IR']['activated'] or scene.objects['Recepteur IR']['activated_real']==False:
|
2023-01-01 14:48:27 +01:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
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-07 10:42:19 +01:00
|
|
|
if scene.objects['Bp cote rue']['activated'] or scene.objects['Bp cote rue']['activated_real']:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
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-07 10:42:19 +01:00
|
|
|
if scene.objects['Bp cote cour']['activated'] or scene.objects['Bp cote cour']['activated_real']:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
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-06 21:23:44 +01:00
|
|
|
# Temporisation
|
2022-12-11 08:40:31 +01:00
|
|
|
def tempo (duree):
|
|
|
|
time.sleep(duree)
|
2023-01-06 21:23:44 +01:00
|
|
|
|
2023-01-07 00:01:01 +01:00
|
|
|
# Arrêt
|
|
|
|
def stop():
|
|
|
|
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-15 11:18:40 +01:00
|
|
|
scene.objects['System']['plot_draw']=False
|
2023-01-07 00:01:01 +01:00
|
|
|
thread_cmd_stop()
|
|
|
|
|
|
|
|
# Fin naturelle
|
2023-01-06 21:23:44 +01:00
|
|
|
def end():
|
|
|
|
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-15 11:18:40 +01:00
|
|
|
scene.objects['System']['plot_draw']=False
|
2023-01-06 21:23:44 +01:00
|
|
|
thread_cmd_end()
|
|
|
|
|
|
|
|
def fin():
|
|
|
|
end()
|
|
|
|
|
|
|
|
def quit():
|
|
|
|
end()
|
2023-01-14 14:03:15 +01:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Visualisation des données
|
|
|
|
###############################################################################
|
|
|
|
|
2023-01-15 10:05:41 +01:00
|
|
|
# Lancement du grapheur
|
2023-01-15 11:18:40 +01:00
|
|
|
def plot(data):
|
2023-01-15 10:05:41 +01:00
|
|
|
# subprocess.run([sys.executable, os.path.join(os.getcwd(), "twin_plot.py")], , stdin=subprocess.PIPE) # Process bloquant
|
2023-01-15 11:18:40 +01:00
|
|
|
|
|
|
|
# Terminer le processus précédent
|
|
|
|
if scene.objects['System']['plot_proc'] is not None:
|
|
|
|
if scene.objects['System']['plot_proc'].poll()==None:
|
|
|
|
scene.objects['System']['plot_proc'].terminate()
|
|
|
|
scene.objects['System']['plot_draw'] = True
|
|
|
|
scene.objects['System']['plot_time'] = 0
|
|
|
|
scene.objects['System']['plot_data'] = data # FIXME : conversion mémonique -> 3D
|
2023-01-15 10:05:41 +01:00
|
|
|
scene.objects['System']['plot_proc'] = subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot.py")], stdin=subprocess.PIPE, encoding = 'utf8')
|
|
|
|
scene.objects['System']['plot']=True
|
|
|
|
|
2023-01-15 11:18:40 +01:00
|
|
|
# Communication avec Qt5
|
2023-01-15 10:05:41 +01:00
|
|
|
def plot_maj(cont):
|
|
|
|
if cont.sensors['Plot'].positive :
|
2023-01-15 11:18:40 +01:00
|
|
|
|
|
|
|
# Affichage du graphique
|
|
|
|
if scene.objects['System']['plot_draw']:
|
|
|
|
|
|
|
|
# Préparation du message
|
|
|
|
# FIXME : ajouter les valeurs réelles et valeurs numériques ('activated_real')
|
|
|
|
msg=str(scene.objects['System']['plot_time'])
|
|
|
|
for obj in scene.objects['System']['plot_data']:
|
|
|
|
if scene.objects[obj]['activated']:
|
|
|
|
msg = msg+",1"
|
|
|
|
else:
|
|
|
|
msg = msg+",0"
|
|
|
|
msg = msg+"\n"
|
|
|
|
|
|
|
|
# Envoi (Pipe)
|
|
|
|
if scene.objects['System']['plot_proc'].poll()==None:
|
|
|
|
# scene.objects['System']['plot_proc'].communicate(input=time_send.encode())[0] # Communication bloquante
|
|
|
|
scene.objects['System']['plot_proc'].stdin.write(msg)
|
|
|
|
else:
|
|
|
|
print ("Stop")
|
|
|
|
scene.objects['System']['plot']=False
|
|
|
|
scene.objects['System']['plot_draw'] =False
|
|
|
|
scene.objects['System']['plot_proc'].terminate()
|
|
|
|
|
|
|
|
# Arret de l'affichage du graphique
|
|
|
|
else :
|
|
|
|
if scene.objects['System']['plot_proc'].poll()==None:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print ("Stop")
|
|
|
|
scene.objects['System']['plot']=False
|
|
|
|
scene.objects['System']['plot_draw'] =False
|
|
|
|
scene.objects['System']['plot_proc'].terminate()
|