mirror of
https://forge.apps.education.fr/blender-edutech/jumeaux-numeriques.git
synced 2024-01-27 06:56:18 +01:00
Gestion des threads factorisée + moteur physique du volet roulant
This commit is contained in:
parent
abf6691b2e
commit
48336b248c
@ -9,20 +9,20 @@ from montchg_lib import * # Bibliothèque portail coulissant
|
||||
# Instructions élémentaires pour le monte-charge
|
||||
#
|
||||
# Actions (ordre = True ou False) :
|
||||
# - Monter le monte-charge (moteur sens trigo) : mot_m (True | False)
|
||||
# - Descendre le monte-charge (moteur sens horaire) : mot_d (True | False)
|
||||
# - Monter le monte-charge (moteur sens trigo) : mot_m(True | False)
|
||||
# - Descendre le monte-charge (moteur sens horaire) : mot_d(True | False)
|
||||
#
|
||||
# Capteurs (valeur retournée = True ou False) :
|
||||
# - Capteur présence cabine niveau 0 : pc_0()
|
||||
# - Capteur présence cabine niveau 1 : pc_1()
|
||||
#
|
||||
# Les consignes du pupitre (valeur retournée = True ou False) :
|
||||
# Consignes du pupitre (valeur retournée = True ou False) :
|
||||
# - Bouton poussoir appel niveau 0 : ba_0()
|
||||
# - Bouton poussoir appel niveau 1 : ba_1()
|
||||
#
|
||||
# Les retours d'information du pupitre (allumer = True ou False) :
|
||||
# - Voyant témoin d'étage niveau 0 : voy_0 (True | False)
|
||||
# - Voyant témoin d'étage niveau 1 : voy_1 (True | False)
|
||||
# Retours d'information du pupitre (allumer = True ou False) :
|
||||
# - Voyant témoin d'étage niveau 0 : voy_0(True | False)
|
||||
# - Voyant témoin d'étage niveau 1 : voy_1(True | False)
|
||||
#
|
||||
# Gestion du temps :
|
||||
# - Temporisation en seconde : tempo(duree)
|
||||
|
@ -1,8 +1,5 @@
|
||||
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
||||
import threading # Multithreading
|
||||
import trace
|
||||
import sys
|
||||
import time
|
||||
from twin_threading import * # Multithreading
|
||||
|
||||
import serial # Liaison série
|
||||
import pyfirmata # Protocole Firmata
|
||||
@ -20,10 +17,6 @@ from serial.tools.list_ports import comports # Détection du port automatique
|
||||
|
||||
scene = bge.logic.getCurrentScene()
|
||||
|
||||
# Threads
|
||||
threads_cmd=[]
|
||||
debug_thread = scene.objects['System']['debug_thread']
|
||||
|
||||
# Carte du jumeau numérique
|
||||
board = None
|
||||
board_it = None # Iterator (input)
|
||||
@ -37,114 +30,15 @@ JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
|
||||
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
|
||||
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
|
||||
|
||||
###############################################################################
|
||||
# Méthode kill pour les tâches (threads)
|
||||
###############################################################################
|
||||
|
||||
class thread_with_trace(threading.Thread):
|
||||
def __init__(self, *args, **keywords):
|
||||
threading.Thread.__init__(self, *args, **keywords)
|
||||
self.killed = False
|
||||
|
||||
def start(self):
|
||||
self.__run_backup = self.run
|
||||
self.run = self.__run
|
||||
threading.Thread.start(self)
|
||||
|
||||
def __run(self):
|
||||
sys.settrace(self.globaltrace)
|
||||
self.__run_backup()
|
||||
self.run = self.__run_backup
|
||||
|
||||
def globaltrace(self, frame, event, arg):
|
||||
if event == 'call':
|
||||
return self.localtrace
|
||||
else:
|
||||
return None
|
||||
|
||||
def localtrace(self, frame, event, arg):
|
||||
if self.killed:
|
||||
if event == 'line':
|
||||
raise SystemExit()
|
||||
return self.localtrace
|
||||
|
||||
def kill(self):
|
||||
self.killed = True
|
||||
|
||||
###############################################################################
|
||||
# Start et stop des tâches (threads)
|
||||
###############################################################################
|
||||
|
||||
def thread_start(threads, type_txt, fct):
|
||||
threads.append(thread_with_trace(target = fct))
|
||||
threads[len(threads)-1].start()
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#", len(threads)-1, "open.")
|
||||
|
||||
def thread_stop(threads, type_txt):
|
||||
i=0
|
||||
zombie_flag=False
|
||||
for t in threads:
|
||||
if not t.is_alive():
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"closed.")
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"still open ...")
|
||||
t.kill()
|
||||
t.join()
|
||||
if not t.is_alive():
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"killed.")
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"zombie...")
|
||||
zombie_flag=True
|
||||
i +=1
|
||||
if zombie_flag==False:
|
||||
if (debug_thread):
|
||||
print ("All threads",type_txt, "are closed.")
|
||||
scene.objects['System']['thread_cmd']=False
|
||||
return True
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("There are zombies threads",type_txt, ".")
|
||||
return False
|
||||
|
||||
def thread_cmd_start(fct):
|
||||
thread_start(threads_cmd, "commands", fct)
|
||||
|
||||
def thread_cmd_stop():
|
||||
thread_stop(threads_cmd, "commands")
|
||||
|
||||
def end():
|
||||
|
||||
# Jumeau numérique
|
||||
if scene.objects['System']['twins']:
|
||||
jumeau_close()
|
||||
|
||||
# Thread
|
||||
if (debug_thread):
|
||||
print ("Thread commands is arrived.")
|
||||
time.sleep(0.125)
|
||||
scene.objects['System']['thread_cmd']=False
|
||||
time.sleep(0.125)
|
||||
|
||||
def fin():
|
||||
end()
|
||||
|
||||
def quit():
|
||||
end()
|
||||
|
||||
###############################################################################
|
||||
# Voyants
|
||||
###############################################################################
|
||||
|
||||
# Ordre pour le voyant 0
|
||||
# Ordre pour allumer le voyant niveau 0
|
||||
def voy_0 (order):
|
||||
scene.objects['Led niveau 0']['activated']=order
|
||||
|
||||
# Ordre pour le voyant 1
|
||||
# Ordre pour allumer le voyant niveau 1
|
||||
def voy_1 (order):
|
||||
scene.objects['Led niveau 1']['activated']=order
|
||||
|
||||
|
Binary file not shown.
1
monte_charge/twin_threading.py
Symbolic link
1
monte_charge/twin_threading.py
Symbolic link
@ -0,0 +1 @@
|
||||
/home/phroy/Bureau/seriousgames/blender-edutech/git/digital_twin/twin_threading.py
|
1
poppy_ergo_jr/twin_threading.py
Symbolic link
1
poppy_ergo_jr/twin_threading.py
Symbolic link
@ -0,0 +1 @@
|
||||
/home/phroy/Bureau/seriousgames/blender-edutech/git/digital_twin/twin_threading.py
|
@ -19,16 +19,16 @@ from porcou_lib import * # Bibliothèque portail coulissant
|
||||
# - Capteur fin de course portail fermé : fdc_f()
|
||||
# - Capteur barrage IR (absence d'obstacle) : ir_recep()
|
||||
#
|
||||
# Les consignes du pupitre (valeur retournée = True ou False) :
|
||||
# - Bouton poussoir appel niveau 0 : ba_0()
|
||||
# - Bouton poussoir appel niveau 1 : ba_1()
|
||||
# Consignes du pupitre (valeur retournée = True ou False) :
|
||||
# - Bouton poussoir coté rue : bp_ext()
|
||||
# - Bouton poussoir coté cour : bp_int()
|
||||
#
|
||||
# Gestion du temps :
|
||||
# - Temporisation en seconde : tempo(duree)
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
# Brochage du jumeau réel
|
||||
# Brochage du portail coulissant
|
||||
brochage={
|
||||
'bp_ext' : [2,'d','i'],
|
||||
'ebp_int' : [3,'d','i'],
|
||||
|
@ -1,7 +1,5 @@
|
||||
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
||||
import threading # Multithreading
|
||||
import trace
|
||||
import sys
|
||||
from twin_threading import * # Multithreading
|
||||
import time
|
||||
|
||||
import serial # Liaison série
|
||||
@ -20,10 +18,6 @@ from serial.tools.list_ports import comports # Détection du port automatique
|
||||
|
||||
scene = bge.logic.getCurrentScene()
|
||||
|
||||
# Threads
|
||||
threads_cmd=[]
|
||||
debug_thread = scene.objects['System']['debug_thread']
|
||||
|
||||
# Carte du jumeau numérique
|
||||
board = None
|
||||
board_it = None # Iterator (input)
|
||||
@ -46,109 +40,10 @@ ACTIVATE = bge.logic.KX_INPUT_ACTIVE
|
||||
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
|
||||
|
||||
###############################################################################
|
||||
# Méthode kill pour les tâches (threads)
|
||||
# Gyrophare
|
||||
###############################################################################
|
||||
|
||||
class thread_with_trace(threading.Thread):
|
||||
def __init__(self, *args, **keywords):
|
||||
threading.Thread.__init__(self, *args, **keywords)
|
||||
self.killed = False
|
||||
|
||||
def start(self):
|
||||
self.__run_backup = self.run
|
||||
self.run = self.__run
|
||||
threading.Thread.start(self)
|
||||
|
||||
def __run(self):
|
||||
sys.settrace(self.globaltrace)
|
||||
self.__run_backup()
|
||||
self.run = self.__run_backup
|
||||
|
||||
def globaltrace(self, frame, event, arg):
|
||||
if event == 'call':
|
||||
return self.localtrace
|
||||
else:
|
||||
return None
|
||||
|
||||
def localtrace(self, frame, event, arg):
|
||||
if self.killed:
|
||||
if event == 'line':
|
||||
raise SystemExit()
|
||||
return self.localtrace
|
||||
|
||||
def kill(self):
|
||||
self.killed = True
|
||||
|
||||
###############################################################################
|
||||
# Start et stop des tâches (threads)
|
||||
###############################################################################
|
||||
|
||||
def thread_start(threads, type_txt, fct):
|
||||
threads.append(thread_with_trace(target = fct))
|
||||
threads[len(threads)-1].start()
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#", len(threads)-1, "open.")
|
||||
|
||||
def thread_stop(threads, type_txt):
|
||||
i=0
|
||||
zombie_flag=False
|
||||
for t in threads:
|
||||
if not t.is_alive():
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"closed.")
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"still open ...")
|
||||
t.kill()
|
||||
t.join()
|
||||
if not t.is_alive():
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"killed.")
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"zombie...")
|
||||
zombie_flag=True
|
||||
i +=1
|
||||
if zombie_flag==False:
|
||||
if (debug_thread):
|
||||
print ("All threads",type_txt, "are closed.")
|
||||
scene.objects['System']['thread_cmd']=False
|
||||
return True
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("There are zombies threads",type_txt, ".")
|
||||
return False
|
||||
|
||||
def thread_cmd_start(fct):
|
||||
thread_start(threads_cmd, "commands", fct)
|
||||
|
||||
def thread_cmd_stop():
|
||||
thread_stop(threads_cmd, "commands")
|
||||
|
||||
def end():
|
||||
|
||||
# Jumeau numérique
|
||||
if scene.objects['System']['twins']:
|
||||
jumeau_close()
|
||||
|
||||
# Thread
|
||||
if (debug_thread):
|
||||
print ("Thread commands is arrived.")
|
||||
time.sleep(0.125)
|
||||
scene.objects['System']['thread_cmd']=False
|
||||
time.sleep(0.125)
|
||||
|
||||
def fin():
|
||||
end()
|
||||
|
||||
def quit():
|
||||
end()
|
||||
|
||||
###############################################################################
|
||||
# Actionneurs
|
||||
###############################################################################
|
||||
|
||||
# Ordre du gyrophare
|
||||
# Ordre pour allumer le gyrophare
|
||||
def gyr (order):
|
||||
# global gyr_pin
|
||||
scene.objects['Led']['activated']=order
|
||||
@ -158,6 +53,10 @@ def gyr (order):
|
||||
# else:
|
||||
# gyr_pin.write(0)
|
||||
|
||||
###############################################################################
|
||||
# Actionneurs
|
||||
###############################################################################
|
||||
|
||||
# Ordre pour le moteur phase ouvrir
|
||||
def mot_o (order):
|
||||
scene.objects['Moteur']['open']=order
|
||||
|
1
portail_coulissant/twin_threading.py
Symbolic link
1
portail_coulissant/twin_threading.py
Symbolic link
@ -0,0 +1 @@
|
||||
/home/phroy/Bureau/seriousgames/blender-edutech/git/digital_twin/twin_threading.py
|
@ -1,6 +1,6 @@
|
||||
<data>
|
||||
<screen>
|
||||
<width>1458</width>
|
||||
<height>820</height>
|
||||
<width>1480</width>
|
||||
<height>832</height>
|
||||
</screen>
|
||||
</data>
|
207
twin_serial.py
Normal file
207
twin_serial.py
Normal file
@ -0,0 +1,207 @@
|
||||
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
||||
import numpy as np
|
||||
|
||||
###############################################################################
|
||||
# twin_serial.py
|
||||
# @title: Gestion de la laison série
|
||||
# @project: Blender-EduTech
|
||||
# @lang: fr
|
||||
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
||||
# @copyright: Copyright (C) 2022 Philippe Roy
|
||||
# @license: GNU GPL
|
||||
###############################################################################
|
||||
|
||||
# UPBGE scene
|
||||
scene = bge.logic.getCurrentScene()
|
||||
|
||||
##
|
||||
# Recherche automatique du port
|
||||
##
|
||||
|
||||
def serial_autoget_port():
|
||||
# USB Vendor ID, USB Product ID
|
||||
board_dict={'microbit' :[3368, 516],
|
||||
'uno' :[9025, 67],
|
||||
'mega' :[9025, 66]}
|
||||
for com in comports(): # Arduino Uno
|
||||
if com.vid == board_dict["uno"][0] and com.pid == board_dict["uno"][1]:
|
||||
return [com.device,"Arduino Uno"]
|
||||
for com in comports(): # Arduino Mega
|
||||
if com.vid == board_dict["mega"][0] and com.pid == board_dict["mega"][1]:
|
||||
return [com.device,"Arduino Mega"]
|
||||
return [None,""]
|
||||
|
||||
##
|
||||
# Création de l'objet carte (protocole Firmata)
|
||||
##
|
||||
|
||||
def board_init(port):
|
||||
try:
|
||||
return pyfirmata.Arduino(port)
|
||||
except:
|
||||
return None
|
||||
|
||||
##
|
||||
# Création de l'objet serial (communication série)
|
||||
##
|
||||
|
||||
def serial_init(port,speed):
|
||||
try:
|
||||
return serial.Serial(port,speed)
|
||||
except:
|
||||
return None
|
||||
|
||||
##
|
||||
# Affiche la liste des cartes (communication série)
|
||||
##
|
||||
|
||||
def serial_devices():
|
||||
for com in comports():
|
||||
print ("Name : "+str(com.name)+"\n"
|
||||
+" Device : "+str(com.device)+"\n"
|
||||
+" Hardware ID : "+str(com.hwid)+"\n"
|
||||
+" USB Vendor ID : "+str(com.vid)+"\n"
|
||||
+" USB Product ID : "+str(com.pid)+"\n"
|
||||
+" USB device location : "+str(com.location)+"\n"
|
||||
+" USB manufacturer : "+str(com.manufacturer)+"\n"
|
||||
+" USB product : "+str(com.product)+"\n"
|
||||
+" Interface-specific : "+str(com.interface))
|
||||
|
||||
##
|
||||
# Activation de la communication avec la carte de communication (Arduino, Micro:bit)
|
||||
# Vitesse : 115200 -> 7 fps, 38400 -> 6 fps, 9600 -> 2 fps
|
||||
# pyserial : baudrate=115200
|
||||
# pyfirmata : baudrate=57600
|
||||
##
|
||||
|
||||
def jumeau(pins):
|
||||
global board
|
||||
# global gyr_pin
|
||||
|
||||
# UI : étape 1
|
||||
scene.objects['Twins-text']['Text'] = "Connection en cours ..."
|
||||
|
||||
# Mise en place de la carte
|
||||
speed = 57600
|
||||
[device,board_name] =serial_autoget_port() # Recherche automatique du port
|
||||
if device is None:
|
||||
scene.objects['System']['twins'] = False
|
||||
scene.objects['Twins-text']['Text'] = "Aucune connection disponible : jumeau réel débranché."
|
||||
return False
|
||||
board = board_init(device)
|
||||
if board is None:
|
||||
scene.objects['System']['twins'] = False
|
||||
scene.objects['Twins-text']['Text'] = "Aucune connection disponible : port "+device+" pas prêt."
|
||||
return False
|
||||
scene.objects['System']['twins'] = True
|
||||
# scene.objects['System']['twins_close'] = False
|
||||
scene.objects['System']['twins_port'] = device
|
||||
scene.objects['System']['twins_speed'] = speed
|
||||
# scene.objects['System']['twins_readline'] = ""
|
||||
board_it = pyfirmata.util.Iterator(board) # Itérateur pour les entrées
|
||||
board_it.start()
|
||||
|
||||
# UI : étape 2
|
||||
if board =="":
|
||||
scene.objects['Twins-text']['Text'] = "Connection ouverte : "+device+" - "+str(speed)+" baud."
|
||||
else:
|
||||
scene.objects['Twins-text']['Text'] = "Connection ouverte : "+board_name+" sur "+device+" à "+str(speed)+" baud."
|
||||
tempo (0.1)
|
||||
|
||||
# Déclaration des entrées - sorties
|
||||
# for pin in pins:
|
||||
# print (pin)
|
||||
# if
|
||||
# bp_ext_pin = board_io('d:'+str(es_dict['bp_ext'])+':i') # Bouton poussoir coté rue
|
||||
# bp_int_pin = board_io('d:'+str(es_dict['bp_int'])+':i') # Bouton poussoir coté cour
|
||||
# fdc_o_pin = board_io('d:'+str(es_dict['fdc_o'])+':i') # Capteur fin de course portail ouvert
|
||||
# fdc_f_pin = board_io('d:'+str(es_dict['fdc_f'])+':i') # Capteur fin de course portail fermé
|
||||
# ir_recept_pin = board_io('d:'+str(es_dict['ir_recept'])+':i') # Recepteur pour le capteur barrage IR
|
||||
|
||||
# gyr_pin = board_io('d:'+str(es_dict['gyr'])+':o') # Gyrophare
|
||||
# mot_o_pin = board_io('d:'+str(es_dict['mot_o'])+':o') # Ouvrir le portail (moteur sens trigo)
|
||||
# mot_f_pin = board_io('d:'+str(es_dict['mot_f'])+':o') # Fermer le portail (moteur sens horaire
|
||||
# ir_emett_pin = board_io('d:'+str(es_dict['ir_emett'])+':o') # Emetteur pour le capteur barrage IR
|
||||
return True
|
||||
|
||||
# def board_io(da,pin,io):
|
||||
# if pin_def is not None:
|
||||
# return board.get_pin(da+':'+pin_def)
|
||||
# else:
|
||||
# print ("Définition entrée-sortie non trouvée : "+pin_def)
|
||||
|
||||
##
|
||||
# Fermeture de la communication série
|
||||
##
|
||||
|
||||
def jumeau_close():
|
||||
global board
|
||||
# twins_serial.close() # Fermer proprement le port série
|
||||
board.exit() # Fermer proprement la communication avec la carte
|
||||
scene.objects['System']['twins'] = False
|
||||
scene.objects['Twins-text']['Text'] = "Connection fermée."
|
||||
|
||||
# Configuration manuelle du port
|
||||
# FIXME
|
||||
def jumeau_config(port, speed):
|
||||
pass
|
||||
# global board
|
||||
# global twins_serial
|
||||
# if scene.objects['System']['twins']:
|
||||
# serial_msg1 = "CF\n"
|
||||
# twins_serial.write(serial_msg1.encode())
|
||||
# tempo (1)
|
||||
# serial_msg2 = str(speed)+"\n"
|
||||
# twins_serial.write(serial_msg2.encode())
|
||||
# tempo (1)
|
||||
# serial_msg3 = str(temps_avancer)+"\n"
|
||||
# twins_serial.write(serial_msg3.encode())
|
||||
# tempo (1)
|
||||
# serial_msg4 = str(temps_tourner)+"\n"
|
||||
# twins_serial.write(serial_msg4.encode())
|
||||
# tempo (1)
|
||||
# serial_msg5 = "FC\n"
|
||||
# twins_serial.write(serial_msg5.encode())
|
||||
|
||||
##
|
||||
# Envoi d'un message vers la communication série
|
||||
##
|
||||
|
||||
# def serie_msg(text):
|
||||
# global twins_serial
|
||||
# text2= text+"\n"
|
||||
# scene.objects['Twins-text']['Text'] = "Communication : envoi message : "+text
|
||||
# twins_serial.write(text2.encode())
|
||||
|
||||
##
|
||||
# Mise en écoute de jumeau numérique (figeage de la scène)
|
||||
##
|
||||
|
||||
# def twins_listen(cont):
|
||||
# global twins_serial
|
||||
# if scene.objects['System']['twins']:
|
||||
# if scene.objects['System']['twins_readline'] != "":
|
||||
# scene.objects['Twins-text']['Text'] = "Écoute de la connection figeage de la scène.... Message reçu : "+scene.objects['System']['twins_readline']
|
||||
# else:
|
||||
# scene.objects['Twins-text']['Text'] = "Écoute de la connection figeage de la scène..."
|
||||
# if cont.sensors['Property'].positive:
|
||||
# if scene.objects['System']['twins_listen'] :
|
||||
# serial_msg = twins_serial.readline()
|
||||
# if serial_msg is not None:
|
||||
# scene.objects['System']['twins_readline'] = str(serial_msg)
|
||||
# # scene.objects['Twins-text']['Text'] = "Message reçu : "+str(serial_msg)
|
||||
# scene.objects['System']['twins_listen'] = False
|
||||
|
||||
##
|
||||
# Réception d'un message de la communication série
|
||||
##
|
||||
|
||||
# def serie_rcpt():
|
||||
# # scene.objects['Twins-text']['Text'] = "Écoute de la \nconnection\n figeage de \n la scène"
|
||||
# scene.objects['System']['twins_readline'] = ""
|
||||
# scene.objects['System']['twins_listen'] = True
|
||||
# while scene.objects['System']['twins_readline'] == "":
|
||||
# if scene.objects['System']['twins_readline'] != "":
|
||||
# break
|
||||
# # scene.objects['Twins-text']['Text'] = "Connection\nouverte :\n"+scene.objects['System']['twins_port']+"\n"+str(scene.objects['System']['twins_speed'])+" baud"
|
||||
# return scene.objects['System']['twins_readline']
|
120
twin_threading.py
Normal file
120
twin_threading.py
Normal file
@ -0,0 +1,120 @@
|
||||
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
||||
import threading # Multithreading
|
||||
import trace
|
||||
import sys
|
||||
import time
|
||||
|
||||
###############################################################################
|
||||
# twin_threading.py
|
||||
# @title: Gestion des tâches (threads)
|
||||
# @project: Blender-EduTech
|
||||
# @lang: fr
|
||||
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
||||
# @copyright: Copyright (C) 2020-2022 Philippe Roy
|
||||
# @license: GNU GPL
|
||||
###############################################################################
|
||||
|
||||
scene = bge.logic.getCurrentScene()
|
||||
|
||||
# Threads
|
||||
threads_cmd=[]
|
||||
debug_thread = scene.objects['System']['debug_thread']
|
||||
|
||||
###############################################################################
|
||||
# Méthode kill pour les tâches (threads)
|
||||
###############################################################################
|
||||
|
||||
class thread_with_trace(threading.Thread):
|
||||
def __init__(self, *args, **keywords):
|
||||
threading.Thread.__init__(self, *args, **keywords)
|
||||
self.killed = False
|
||||
|
||||
def start(self):
|
||||
self.__run_backup = self.run
|
||||
self.run = self.__run
|
||||
threading.Thread.start(self)
|
||||
|
||||
def __run(self):
|
||||
sys.settrace(self.globaltrace)
|
||||
self.__run_backup()
|
||||
self.run = self.__run_backup
|
||||
|
||||
def globaltrace(self, frame, event, arg):
|
||||
if event == 'call':
|
||||
return self.localtrace
|
||||
else:
|
||||
return None
|
||||
|
||||
def localtrace(self, frame, event, arg):
|
||||
if self.killed:
|
||||
if event == 'line':
|
||||
raise SystemExit()
|
||||
return self.localtrace
|
||||
|
||||
def kill(self):
|
||||
self.killed = True
|
||||
|
||||
###############################################################################
|
||||
# Start et stop des tâches (threads)
|
||||
###############################################################################
|
||||
|
||||
def thread_start(threads, type_txt, fct):
|
||||
threads.append(thread_with_trace(target = fct))
|
||||
threads[len(threads)-1].start()
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#", len(threads)-1, "open.")
|
||||
|
||||
def thread_stop(threads, type_txt):
|
||||
i=0
|
||||
zombie_flag=False
|
||||
for t in threads:
|
||||
if not t.is_alive():
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"closed.")
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"still open ...")
|
||||
t.kill()
|
||||
t.join()
|
||||
if not t.is_alive():
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"killed.")
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"zombie...")
|
||||
zombie_flag=True
|
||||
i +=1
|
||||
if zombie_flag==False:
|
||||
if (debug_thread):
|
||||
print ("All threads",type_txt, "are closed.")
|
||||
scene.objects['System']['thread_cmd']=False
|
||||
return True
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("There are zombies threads",type_txt, ".")
|
||||
return False
|
||||
|
||||
def thread_cmd_start(fct):
|
||||
thread_start(threads_cmd, "commands", fct)
|
||||
|
||||
def thread_cmd_stop():
|
||||
thread_stop(threads_cmd, "commands")
|
||||
|
||||
def end():
|
||||
|
||||
# Jumeau numérique
|
||||
# if scene.objects['System']['twins']:
|
||||
# jumeau_close()
|
||||
|
||||
# Thread
|
||||
if (debug_thread):
|
||||
print ("Thread commands is arrived.")
|
||||
time.sleep(0.125)
|
||||
scene.objects['System']['thread_cmd']=False
|
||||
time.sleep(0.125)
|
||||
|
||||
def fin():
|
||||
end()
|
||||
|
||||
def quit():
|
||||
end()
|
1
volet_roulant/twin_threading.py
Symbolic link
1
volet_roulant/twin_threading.py
Symbolic link
@ -0,0 +1 @@
|
||||
/home/phroy/Bureau/seriousgames/blender-edutech/git/digital_twin/twin_threading.py
|
Binary file not shown.
@ -40,176 +40,169 @@ def init(cont):
|
||||
twin.cmd_init() # Commandes
|
||||
|
||||
# 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
|
||||
# FIXME : animation par keyframe
|
||||
# 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
|
||||
|
||||
system_init() # Initialisation du système
|
||||
|
||||
###############################################################################
|
||||
# Voyants
|
||||
###############################################################################
|
||||
|
||||
##
|
||||
# Etat voyant mode automatique
|
||||
# Modele 3d -> Arduino : FIXME
|
||||
# Arduino -> Modele 3d : FIXME
|
||||
##
|
||||
|
||||
def voy_auto (cont):
|
||||
if scene.objects['System']['run']:
|
||||
obj = cont.owner
|
||||
if obj['activated'] and scene.objects['Led auto-on'].visible == False:
|
||||
scene.objects['Led auto-on'].setVisible(True,False)
|
||||
scene.objects['Led auto'].setVisible(False,False)
|
||||
if obj['activated']==False and scene.objects['Led auto-on'].visible == True:
|
||||
scene.objects['Led auto'].setVisible(True,False)
|
||||
scene.objects['Led auto-on'].setVisible(False,False)
|
||||
|
||||
###############################################################################
|
||||
# Actionneurs
|
||||
###############################################################################
|
||||
|
||||
##
|
||||
# Action du simulateur pour le clignotant
|
||||
# Moteur et volet
|
||||
# Modele 3d -> Arduino : FIXME
|
||||
# Arduino -> Modele 3d : FIXME
|
||||
##
|
||||
|
||||
def sml_clignotant (cont):
|
||||
def mot (cont):
|
||||
if scene.objects['System']['run']:
|
||||
obj = cont.owner
|
||||
if obj['actif'] and scene.objects['Led allumee'].visible == False:
|
||||
scene.objects['Led allumee'].setVisible(True,False)
|
||||
scene.objects['Led'].setVisible(False,False)
|
||||
# print ("Clignotant allumée")
|
||||
if obj['actif']==False and scene.objects['Led allumee'].visible == True:
|
||||
scene.objects['Led'].setVisible(True,False)
|
||||
scene.objects['Led allumee'].setVisible(False,False)
|
||||
# print ("Clignotant éteint")
|
||||
vitesse = 0.015
|
||||
|
||||
##
|
||||
# Action du simulateur pour le moteur
|
||||
##
|
||||
|
||||
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)
|
||||
# FIXME : animation par keyframe
|
||||
|
||||
# pas_cabine = 10 # Diam axe = 3 mm -> 1 tour = 3*math.pi
|
||||
# pas_pignon = pas_cabine/(3*math.pi) # Z pignon = 48 dents
|
||||
# pas_vissansfin = pas_pignon*48
|
||||
# obj_vissansfin = scene.objects['Moteur vis sans fin']
|
||||
# obj_pignon = scene.objects['Moteur pignon']
|
||||
# obj_cabine = scene.objects['Cabine']
|
||||
# obj_cabine['z']= scene.objects['Cabine'].localPosition.z # Affichage de l'altitude de la cabine
|
||||
# obj_contrepoids = scene.objects['Contrepoids']
|
||||
# obj_cable = scene.objects['Cable'] # FIXME : plus tard
|
||||
# if obj['up']:
|
||||
# obj_vissansfin.applyRotation((0, 0, pas_vissansfin*vitesse), True)
|
||||
# obj_pignon.applyRotation((pas_pignon*vitesse, 0, 0), True)
|
||||
# obj_cabine.applyMovement((0, 0, pas_cabine*vitesse), True)
|
||||
# obj_contrepoids.applyMovement((0, 0, -pas_cabine*vitesse), True)
|
||||
# # else: # Pas de priorité
|
||||
# if obj['down']:
|
||||
# obj_vissansfin.applyRotation((0, 0, -pas_vissansfin*vitesse), True)
|
||||
# obj_pignon.applyRotation((-pas_pignon*vitesse, 0, 0), True)
|
||||
# obj_cabine.applyMovement((0, 0, -pas_cabine*vitesse), True)
|
||||
# obj_contrepoids.applyMovement((0, 0, pas_cabine*vitesse), True)
|
||||
|
||||
###############################################################################
|
||||
# Capteurs fin de course
|
||||
###############################################################################
|
||||
|
||||
##
|
||||
# Etat capteur fin de course portail ouvert
|
||||
# Etat capteur fin de course volet en haut
|
||||
# Modele 3d -> Arduino : FIXME
|
||||
# Arduino -> Modele 3d : FIXME
|
||||
##
|
||||
|
||||
def sml_fdc_ouvert (cont):
|
||||
def fdc_h (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
|
||||
# FIXME : animation par keyframe
|
||||
# if scene.objects['Portail'].localPosition.x <= 0 and obj['activated'] == False :
|
||||
# obj['activated'] = True
|
||||
# if scene.objects['Portail'].localPosition.x > 0 and obj['activated'] == True :
|
||||
# obj['activated'] = False
|
||||
|
||||
# Forçage (click)
|
||||
if obj['click'] == True:
|
||||
obj['activated'] = True
|
||||
|
||||
# Couleurs
|
||||
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
|
||||
|
||||
##
|
||||
# Etat capteur fin de course portail fermé
|
||||
# Etat capteur fin de course volet en bas
|
||||
# Modele 3d -> Arduino : FIXME
|
||||
# Arduino -> Modele 3d : FIXME
|
||||
##
|
||||
|
||||
def sml_fdc_ferme (cont):
|
||||
def fdc_b (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
|
||||
# FIXME : animation par keyframe
|
||||
# if scene.objects['Portail'].localPosition.x >= 218 and obj['activated'] == False :
|
||||
# obj['activated'] = True
|
||||
# if scene.objects['Portail'].localPosition.x < 218 and obj['activated'] == True :
|
||||
# obj['activated'] = False
|
||||
|
||||
##
|
||||
# Forçage capteur fin de course avec la souris
|
||||
##
|
||||
# Forçage (click)
|
||||
if obj['click'] == True:
|
||||
obj['activated'] = True
|
||||
|
||||
def sml_microrupteur (cont):
|
||||
if scene.objects['System']['run'] :
|
||||
system_click(cont, cont.owner)
|
||||
# Couleurs
|
||||
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
|
||||
|
||||
###############################################################################
|
||||
# Capteur barrage
|
||||
# Boutons
|
||||
###############################################################################
|
||||
|
||||
##
|
||||
# Emetteur IR
|
||||
##
|
||||
# Modele 3d -> Arduino : FIXME
|
||||
# Arduino -> Modele 3d : FIXME
|
||||
|
||||
def sml_emet_ir (cont):
|
||||
if scene.objects['System']['run'] :
|
||||
obj = cont.owner
|
||||
obj_emetteur_ir=scene.objects['Emetteur IR']
|
||||
obj_recepteur_ir=scene.objects['Recepteur IR']
|
||||
if obj['actif'] and scene.objects['Emetteur IR Led allumee'].visible == False:
|
||||
scene.objects['Emetteur IR Led allumee'].setVisible(True,False)
|
||||
scene.objects['Emetteur IR Led'].setVisible(False,False)
|
||||
obj_emetteur_ir.color = couleur_orange
|
||||
obj_recepteur_ir.color = couleur_orange
|
||||
if obj['actif']==False and scene.objects['Emetteur IR Led allumee'].visible == True:
|
||||
scene.objects['Emetteur IR Led'].setVisible(True,False)
|
||||
scene.objects['Emetteur IR Led allumee'].setVisible(False,False)
|
||||
obj_emetteur_ir.color = couleur_magenta
|
||||
obj_recepteur_ir.color = couleur_magenta
|
||||
# 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)
|
||||
|
||||
##
|
||||
# Récepteur IR
|
||||
##
|
||||
|
||||
def sml_recep_ir (cont):
|
||||
if scene.objects['System']['run'] and scene.objects['Module emetteur IR']['actif'] :
|
||||
obj = cont.owner
|
||||
system_click(cont, scene.objects['Module recepteur IR'])
|
||||
if scene.objects['Module recepteur IR']['actif']==True :
|
||||
scene.objects['Emetteur IR'].color = couleur_jaune
|
||||
scene.objects['Recepteur IR'].color = couleur_jaune
|
||||
|
||||
###############################################################################
|
||||
# Boutons poussoirs
|
||||
###############################################################################
|
||||
|
||||
##
|
||||
# Bouton pousssoir coté rue
|
||||
##
|
||||
|
||||
def sml_bp_rue (cont):
|
||||
if scene.objects['System']['run'] :
|
||||
system_click(cont, scene.objects['Module bouton cote rue'])
|
||||
|
||||
##
|
||||
# Bouton pousssoir coté cour
|
||||
##
|
||||
|
||||
def sml_bp_cour (cont):
|
||||
if scene.objects['System']['run'] :
|
||||
system_click(cont, scene.objects['Module bouton cote cour'])
|
||||
# # 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
|
||||
|
||||
###############################################################################
|
||||
# Système
|
||||
@ -222,17 +215,15 @@ def sml_bp_cour (cont):
|
||||
|
||||
def system_init ():
|
||||
|
||||
# Clignotant
|
||||
scene.objects['Module led']['actif']=False
|
||||
scene.objects['Led allumee'].setVisible(False,False)
|
||||
scene.objects['Led'].setVisible(True,False)
|
||||
# Voyants statiques
|
||||
scene.objects['Led capteur courant'].setVisible(True,False)
|
||||
scene.objects['Led capteur courant-on'].setVisible(False,False)
|
||||
scene.objects['Led moteur'].setVisible(True,False)
|
||||
scene.objects['Led moteur-on'].setVisible(False,False)
|
||||
scene.objects['Led alimentation'].setVisible(True,False)
|
||||
scene.objects['Led alimentation-on'].setVisible(False,False)
|
||||
|
||||
# Emetteur IR
|
||||
scene.objects['Module emetteur IR']['actif']=False
|
||||
scene.objects['Emetteur IR Led allumee'].setVisible(False,False)
|
||||
scene.objects['Emetteur IR Led'].setVisible(True,False)
|
||||
scene.objects['Emetteur IR'].color = couleur_magenta
|
||||
scene.objects['Recepteur IR'].color = couleur_magenta
|
||||
system_reset()
|
||||
|
||||
##
|
||||
# Réinitialisation du système
|
||||
@ -240,62 +231,21 @@ def system_init ():
|
||||
|
||||
def system_reset ():
|
||||
|
||||
# Grille à l'état initial
|
||||
# applyRotationTo(scene.objects['System'], 0, 0, 0)
|
||||
scene.objects['Portail'].worldPosition.x = scene.objects['Portail']['init_lx']
|
||||
scene.objects['Portail'].worldPosition.y = scene.objects['Portail']['init_ly']
|
||||
scene.objects['Portail'].worldPosition.z = scene.objects['Portail']['init_lz']
|
||||
# Voyants aux états initiaux
|
||||
scene.objects['Led auto'].setVisible(True,False)
|
||||
scene.objects['Led auto-on'].setVisible(False,False)
|
||||
|
||||
# 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)
|
||||
|
||||
# Led à l'état initial
|
||||
scene.objects['Led'].setVisible(True,False)
|
||||
scene.objects['Led allumee'].setVisible(False,False)
|
||||
|
||||
# Capteur barrage IR
|
||||
scene.objects['Emetteur IR Led'].setVisible(True,False)
|
||||
scene.objects['Emetteur IR Led allumee'].setVisible(False,False)
|
||||
scene.objects['Emetteur IR'].color = couleur_magenta
|
||||
scene.objects['Recepteur IR'].color = couleur_magenta
|
||||
# Volet
|
||||
# FIXME : animation par keyframe
|
||||
|
||||
# I/O à l'état initial
|
||||
scene.objects['Module led']['actif']=False
|
||||
scene.objects['Ensemble moteur']['actif_ouvrir']=False
|
||||
scene.objects['Ensemble moteur']['actif_fermer']=False
|
||||
scene.objects['Capteur fdc ouvert']['actif'] =True
|
||||
scene.objects['Capteur fdc ferme']['actif'] =False
|
||||
scene.objects['Module emetteur IR']['actif'] =False
|
||||
scene.objects['Module recepteur IR']['actif'] =False
|
||||
scene.objects['Module bouton cote rue']['actif'] =False
|
||||
scene.objects['Module bouton cote cour']['actif'] =False
|
||||
|
||||
##
|
||||
# Click sur les éléments cliquables du systèmes
|
||||
##
|
||||
|
||||
def system_click(cont, obj_activation):
|
||||
obj = cont.owner
|
||||
if cont.sensors['MO'].status == JUST_ACTIVATED :
|
||||
obj.color = couleur_blanc
|
||||
obj['MO']=True
|
||||
if cont.sensors['Click'].status == JUST_ACTIVATED and scene.objects['System']['manip_mode']==0:
|
||||
if obj['MO']:
|
||||
obj_activation['actif'] = True
|
||||
obj.color = couleur_jaune
|
||||
if cont.sensors['MO'].status == JUST_RELEASED :
|
||||
obj['MO']=False
|
||||
if cont.sensors['Click'].status == ACTIVATE :
|
||||
obj.color = couleur_jaune
|
||||
else:
|
||||
obj.color = couleur_orange
|
||||
if cont.sensors['Click'].status == JUST_RELEASED:
|
||||
obj_activation['actif'] = False
|
||||
obj.color = couleur_blanc
|
||||
if cont.sensors['MO'].status != ACTIVATE :
|
||||
obj.color = couleur_orange
|
||||
scene.objects['Led auto']['activated']=False
|
||||
scene.objects['Bp monter']['activated']=False
|
||||
scene.objects['Bp arret']['activated']=False
|
||||
scene.objects['Bp descendre']['activated']=False
|
||||
scene.objects['Bp auto']['activated']=False
|
||||
scene.objects['Microrupteur haut']['activated']=False
|
||||
scene.objects['Microrupteur bas']['activated']=False
|
||||
scene.objects['Moteur']['up']=False
|
||||
scene.objects['Moteur']['down']=False
|
||||
scene.objects['Recepteur LDR']['activated']=False
|
||||
|
@ -9,25 +9,28 @@ from volrou_lib import * # Bibliothèque volet roulant
|
||||
# Instructions élémentaires pour le volet roulant
|
||||
#
|
||||
# Actions (ordre = True ou False) :
|
||||
# - Allumer voyant niveau 0 : voy_0 (True|False)
|
||||
# - Allumer voyant niveau 1 : voy_1 (True|False)
|
||||
# - Monter le monte-charge (moteur sens trigo) : mot_m (True|False)
|
||||
# - Descendre le monte-charge (moteur sens horaire) : mot_d (True|False)
|
||||
# - Monter le volet (moteur sens trigo) : mot_m(True | False)
|
||||
# - Descendre le volet (moteur sens horaire) : mot_d(True | False)
|
||||
#
|
||||
# Capteurs (valeur retournée = True ou False) :
|
||||
# - Capteur présence cabine niveau 0 : pc_0()
|
||||
# - Capteur présence cabine niveau 1 : pc_1()
|
||||
# - Capteur fin de course volet en haut : fdc_h()
|
||||
# - Capteur fin de course volet en bas : fdc_b()
|
||||
#
|
||||
# Pupitre (valeur retournée = True ou False) :
|
||||
# - Bouton poussoir appel niveau 0 : ba_0()
|
||||
# - Bouton poussoir appel niveau 1 : ba_1()
|
||||
# Consignes du pupitre (valeur retournée = True ou False) :
|
||||
# - Bouton poussoir monter volet : bp_m()
|
||||
# - Bouton poussoir arrêt volet : bp_a()
|
||||
# - Bouton poussoir descendre volet : bp_d()
|
||||
# - Bouton poussoir mode automatique : bp_auto()
|
||||
#
|
||||
# Retour d'information du pupitre (allumer = True ou False) :
|
||||
# - Voyant témoin mode automatique : voy_auto(True | False)
|
||||
#
|
||||
# Gestion du temps :
|
||||
# - Temporisation en seconde : tempo(duree)
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
# Brochage du monte-charge
|
||||
# Brochage du volet roulant
|
||||
brochage={}
|
||||
|
||||
###############################################################################
|
||||
@ -41,7 +44,7 @@ brochage={}
|
||||
def commandes():
|
||||
|
||||
# Ecrire votre code ici ...
|
||||
voy_0(True) # Activer le voyant du niveau 0
|
||||
voy_auto(True) # Activer le voyant du niveau 0
|
||||
while True:
|
||||
pass
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
||||
import threading # Multithreading
|
||||
import trace
|
||||
import sys
|
||||
from twin_threading import * # Multithreading
|
||||
import time
|
||||
|
||||
import serial # Liaison série
|
||||
@ -20,13 +18,11 @@ from serial.tools.list_ports import comports # Détection du port automatique
|
||||
|
||||
scene = bge.logic.getCurrentScene()
|
||||
|
||||
# Threads
|
||||
threads_cmd=[]
|
||||
debug_thread = scene.objects['System']['debug_thread']
|
||||
|
||||
# Jumeau numérique
|
||||
# Carte du jumeau numérique
|
||||
board = None
|
||||
board_it = None # Iterator (input)
|
||||
|
||||
# Brochage du jumeau numérique
|
||||
bp_int_pin = None
|
||||
bp_ext_pin = None
|
||||
fdc_o_pin = None
|
||||
@ -44,161 +40,56 @@ ACTIVATE = bge.logic.KX_INPUT_ACTIVE
|
||||
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
|
||||
|
||||
###############################################################################
|
||||
# Méthode kill pour les tâches (threads)
|
||||
# Voyant
|
||||
###############################################################################
|
||||
|
||||
class thread_with_trace(threading.Thread):
|
||||
def __init__(self, *args, **keywords):
|
||||
threading.Thread.__init__(self, *args, **keywords)
|
||||
self.killed = False
|
||||
|
||||
def start(self):
|
||||
self.__run_backup = self.run
|
||||
self.run = self.__run
|
||||
threading.Thread.start(self)
|
||||
|
||||
def __run(self):
|
||||
sys.settrace(self.globaltrace)
|
||||
self.__run_backup()
|
||||
self.run = self.__run_backup
|
||||
|
||||
def globaltrace(self, frame, event, arg):
|
||||
if event == 'call':
|
||||
return self.localtrace
|
||||
else:
|
||||
return None
|
||||
|
||||
def localtrace(self, frame, event, arg):
|
||||
if self.killed:
|
||||
if event == 'line':
|
||||
raise SystemExit()
|
||||
return self.localtrace
|
||||
|
||||
def kill(self):
|
||||
self.killed = True
|
||||
|
||||
###############################################################################
|
||||
# Start et stop des tâches (threads)
|
||||
###############################################################################
|
||||
|
||||
def thread_start(threads, type_txt, fct):
|
||||
threads.append(thread_with_trace(target = fct))
|
||||
threads[len(threads)-1].start()
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#", len(threads)-1, "open.")
|
||||
|
||||
def thread_stop(threads, type_txt):
|
||||
i=0
|
||||
zombie_flag=False
|
||||
for t in threads:
|
||||
if not t.is_alive():
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"closed.")
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"still open ...")
|
||||
t.kill()
|
||||
t.join()
|
||||
if not t.is_alive():
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"killed.")
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("Thread",type_txt, "#",i,"zombie...")
|
||||
zombie_flag=True
|
||||
i +=1
|
||||
if zombie_flag==False:
|
||||
if (debug_thread):
|
||||
print ("All threads",type_txt, "are closed.")
|
||||
scene.objects['System']['thread_cmd']=False
|
||||
return True
|
||||
else:
|
||||
if (debug_thread):
|
||||
print ("There are zombies threads",type_txt, ".")
|
||||
return False
|
||||
|
||||
def thread_cmd_start(fct):
|
||||
thread_start(threads_cmd, "commands", fct)
|
||||
|
||||
def thread_cmd_stop():
|
||||
thread_stop(threads_cmd, "commands")
|
||||
|
||||
def end():
|
||||
|
||||
# Jumeau numérique
|
||||
if scene.objects['System']['twins']:
|
||||
# serial_msg = "FI\n"
|
||||
# twins_serial.write(serial_msg.encode()) # Communication série : modele 3d -> carte communication ( arduino | micro:bit )
|
||||
jumeau_close()
|
||||
|
||||
# Thread
|
||||
if (debug_thread):
|
||||
print ("Thread commands is arrived.")
|
||||
time.sleep(0.125)
|
||||
scene.objects['System']['thread_cmd']=False
|
||||
time.sleep(0.125)
|
||||
|
||||
def fin():
|
||||
end()
|
||||
|
||||
def quit():
|
||||
end()
|
||||
# Ordre pour allumer le voyant témoin mode automatique
|
||||
def voy_auto (order):
|
||||
scene.objects['Led auto']['activated']=order
|
||||
|
||||
###############################################################################
|
||||
# Actionneurs
|
||||
###############################################################################
|
||||
|
||||
# Ordres utilisateur du clignotant
|
||||
def gyr (ordre):
|
||||
global gyr_pin
|
||||
scene.objects['Module led']['actif']=ordre
|
||||
if scene.objects['System']['twins'] :
|
||||
if ordre :
|
||||
gyr_pin.write(1)
|
||||
else:
|
||||
gyr_pin.write(0)
|
||||
|
||||
# Ordres utilisateur du moteur
|
||||
def mot_o (ordre):
|
||||
scene.objects['Ensemble moteur']['actif_ouvrir']=ordre
|
||||
|
||||
def mot_f (ordre):
|
||||
scene.objects['Ensemble moteur']['actif_fermer']=ordre
|
||||
|
||||
# Ordre utilisateur du capteur barrage IR
|
||||
def ir_emet(ordre):
|
||||
scene.objects['Module emetteur IR']['actif']=ordre
|
||||
# Ordre pour le moteur pour monter le volet
|
||||
def mot_m (order):
|
||||
scene.objects['Moteur']['up']=order
|
||||
|
||||
# Ordre pour le moteur pour descendre le volet
|
||||
def mot_d (order):
|
||||
scene.objects['Moteur']['down']=order
|
||||
|
||||
###############################################################################
|
||||
# Capteurs
|
||||
###############################################################################
|
||||
|
||||
# Compte-rendu utilisateur du capteur fin de course portail ouvert
|
||||
def fdc_o ():
|
||||
return scene.objects['Capteur fdc ouvert']['actif']
|
||||
# Compte-rendu du capteur fin de course volet en haut
|
||||
def fdc_h ():
|
||||
return scene.objects['Microrupteur fdc haut']['activated']
|
||||
|
||||
# Compte-rendu utilisateur du capteur fin de course portail ouvert
|
||||
def fdc_f ():
|
||||
return scene.objects['Capteur fdc ferme']['actif']
|
||||
|
||||
# Compte-rendu utilisateur du capteur barrage IR
|
||||
def ir_recep ():
|
||||
if scene.objects['Module recepteur IR']['actif'] :
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
# Compte-rendu du capteur fin de course volet en bas
|
||||
def fdc_fb():
|
||||
return scene.objects['Microrupteur fdc bas']['activated']
|
||||
|
||||
###############################################################################
|
||||
# Boutons poussoirs
|
||||
###############################################################################
|
||||
|
||||
# Compte-rendu utilisateur du bouton pousssoir coté rue
|
||||
def bp_ext ():
|
||||
return scene.objects['Module bouton cote rue']['actif']
|
||||
# Compte-rendu du bouton monter volet
|
||||
def bp_m ():
|
||||
return scene.objects['Bp monter']['activated']
|
||||
|
||||
# Compte-rendu utilisateur du bouton pousssoir coté cour
|
||||
def bp_int ():
|
||||
return scene.objects['Module bouton cote cour']['actif']
|
||||
# Compte-rendu du bouton arrêt volet
|
||||
def bp_a ():
|
||||
return scene.objects['Bp arret']['activated']
|
||||
|
||||
# Compte-rendu du bouton descendre volet
|
||||
def bp_d ():
|
||||
return scene.objects['Bp descendre']['activated']
|
||||
|
||||
# Compte-rendu du bouton mode automatique
|
||||
def bp_auto ():
|
||||
return scene.objects['Bp auto']['activated']
|
||||
|
||||
###############################################################################
|
||||
# Temporisation
|
||||
@ -302,9 +193,9 @@ def jumeau(pins):
|
||||
|
||||
# UI : étape 2
|
||||
if board =="":
|
||||
scene.objects['Twins-text']['Text'] = "Connection ouverte : "+device+" - "+str(speed)+" baud"
|
||||
scene.objects['Twins-text']['Text'] = "Connection ouverte : "+device+" - "+str(speed)+" baud."
|
||||
else:
|
||||
scene.objects['Twins-text']['Text'] = "Connection ouverte : "+board_name+" sur "+device+" à "+str(speed)+" baud"
|
||||
scene.objects['Twins-text']['Text'] = "Connection ouverte : "+board_name+" sur "+device+" à "+str(speed)+" baud."
|
||||
tempo (0.1)
|
||||
|
||||
# Déclaration des entrées - sorties
|
||||
@ -338,7 +229,7 @@ def jumeau_close():
|
||||
# twins_serial.close() # Fermer proprement le port série
|
||||
board.exit() # Fermer proprement la communication avec la carte
|
||||
scene.objects['System']['twins'] = False
|
||||
scene.objects['Twins-text']['Text'] = "Connection fermée"
|
||||
scene.objects['Twins-text']['Text'] = "Connection fermée."
|
||||
|
||||
# Configuration du port
|
||||
# FIXME
|
||||
|
Loading…
Reference in New Issue
Block a user