import bge # Bibliothèque Blender Game Engine (UPBGE) import twin_threading # Multithreading (multitâches) from twin_serial import jumeau, jumeau_stop, serial_close # Liaison série from twin_daq import get, daq, csv_generate, plot, plot_generate # Acquisition des données import time ############################################################################### # porcou_lib.py # @title: Bibliothèque utilisateur du portail coulissant # @project: Blender-EduTech # @lang: fr # @authors: Philippe Roy # @copyright: Copyright (C) 2020-2023 Philippe Roy # @license: GNU GPL ############################################################################### # Récupérer la scène UPBGE scene = bge.logic.getCurrentScene() # 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 # Threads de commandes threads_cmd=[] # Threads de visualisation de données threads_plot=[] ############################################################################### # Gyrophare ############################################################################### # Ordre pour allumer le gyrophare def gyr (order): scene.objects['Led']['activated']=order ############################################################################### # Actionneurs ############################################################################### # Ordre pour le moteur phase ouvrir def mot_o (order): scene.objects['Moteur']['open']=order # Ordre pour le moteur phase fermer def mot_f (order): scene.objects['Moteur']['close']=order # Réglage de la vitesse du moteur numérique def mot_digitset (speed=None): if speed==None: scene.objects['Moteur']['speed_setting']=125.6 # Vitesse du moteur numérique : 125.6 rad /s ( 20 tr / s ) else: scene.objects['Moteur']['speed_setting']=speed # Ordre pour le capteur barrage IR def ir_emet(order): scene.objects['Emetteur IR']['active']=order ############################################################################### # Capteurs ############################################################################### # Compte-rendu du capteur fin de course portail ouvert def fdc_o (): if scene.objects['Microrupteur fdc ouvert']['prior'] and scene.objects['Microrupteur fdc ouvert']['activated']: return True if scene.objects['Microrupteur fdc ouvert']['prior_real'] and scene.objects['Microrupteur fdc ouvert']['activated_real']: return True return False # Compte-rendu du capteur fin de course portail fermé def fdc_f (): if scene.objects['Microrupteur fdc ferme']['prior'] and scene.objects['Microrupteur fdc ferme']['activated']: return True if scene.objects['Microrupteur fdc ferme']['prior_real'] and scene.objects['Microrupteur fdc ferme']['activated_real']: return True return False # Compte-rendu du capteur barrage IR # Absence d'obstacle -> True, présence d'obstacle -> False def ir_recep (): 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 ############################################################################### # Boutons poussoirs ############################################################################### # Compte-rendu du bouton pousssoir coté rue def bp_ext (): if scene.objects['Bp cote rue']['prior'] and scene.objects['Bp cote rue']['activated']: return True if scene.objects['Bp cote rue']['prior_real'] and scene.objects['Bp cote rue']['activated_real']: return True return False # Compte-rendu du bouton pousssoir coté cour def bp_int (): 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']: return True 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 else: 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 ############################################################################### # Cycle ############################################################################### ## # Temporisation ## def tempo (duree): time.sleep(duree) ## # t (temps) ## def get_t (): # return truncate(scene.objects['System']['time'], 3) return round(scene.objects['System']['time'], 3) def set_t (date): scene.objects['System']['time']=date def reset_t (): scene.objects['System']['time']=0 ## # Départ ## def start(fct): twin_threading.start(threads_cmd, "commands", fct) ## # Arrêt ## def stop(): # Jumeau if scene.objects['System']['twins']: serial_close(scene.objects['System']['board']) time.sleep(1) # Suivi de données if scene.objects['System']['daq']: csv_generate() if scene.objects['System']['plot']: plot_generate(threads_plot) # Thread twin_threading.stop(threads_cmd, "commands") ## # Fin naturelle ## def end(): fin() def fin(): 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)