2023-01-16 07:48:01 +01:00
|
|
|
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
|
|
|
import os
|
2023-01-14 07:49:53 +01:00
|
|
|
import sys
|
2023-01-16 07:48:01 +01:00
|
|
|
import importlib
|
|
|
|
import subprocess # Multiprocessus
|
|
|
|
import time
|
2023-01-14 07:49:53 +01:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# twin_plot.py
|
|
|
|
# @title: Visualisation des données
|
|
|
|
# @project: Blender-EduTech
|
|
|
|
# @lang: fr
|
|
|
|
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
|
|
|
# @copyright: Copyright (C) 2023 Philippe Roy
|
|
|
|
# @license: GNU GPL
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
# UPBGE scene
|
2023-01-16 07:48:01 +01:00
|
|
|
scene = bge.logic.getCurrentScene()
|
2023-01-14 07:49:53 +01:00
|
|
|
|
2023-01-16 07:48:01 +01:00
|
|
|
# Récupérer la configuration du graphique
|
|
|
|
system=importlib.import_module(scene.objects['Doc']['system']) # Système
|
2023-01-18 18:21:12 +01:00
|
|
|
plot_config = system.get_public_vars()
|
2023-01-14 07:49:53 +01:00
|
|
|
|
2023-01-18 06:06:15 +01:00
|
|
|
###############################################################################
|
2023-01-18 18:21:12 +01:00
|
|
|
# Accès aux variables publiques du système
|
2023-01-18 06:06:15 +01:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
def get(data):
|
2023-01-18 18:21:12 +01:00
|
|
|
if data in plot_config:
|
2023-01-19 15:35:57 +01:00
|
|
|
if len (plot_config[data][0])>2: # Echelle à prendre en compte
|
|
|
|
return(scene.objects[plot_config[data][0][0]][plot_config[data][0][1]]*plot_config[data][0][2])
|
|
|
|
else:
|
|
|
|
return(scene.objects[plot_config[data][0][0]][plot_config[data][0][1]])
|
2023-01-18 18:21:12 +01:00
|
|
|
else:
|
|
|
|
print ("Erreur sur l'accès aux variables par get("+data+"), la variable '"+data+"' absente du système.")
|
2023-01-19 02:27:47 +01:00
|
|
|
return None
|
2023-01-18 06:06:15 +01:00
|
|
|
|
2023-01-14 07:49:53 +01:00
|
|
|
###############################################################################
|
2023-01-16 07:48:01 +01:00
|
|
|
# Création du graphique
|
2023-01-14 07:49:53 +01:00
|
|
|
###############################################################################
|
|
|
|
|
2023-01-16 07:48:01 +01:00
|
|
|
def plot(data):
|
|
|
|
# subprocess.run([sys.executable, os.path.join(os.getcwd(), "twin_plot.py")], , stdin=subprocess.PIPE) # Process bloquant
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# Configuration des données
|
|
|
|
scene.objects['System']['plot_data'] =[]
|
|
|
|
for obj in data:
|
2023-01-18 18:21:12 +01:00
|
|
|
scene.objects['System']['plot_data'].append(plot_config[obj][0][0])
|
2023-01-16 07:48:01 +01:00
|
|
|
|
|
|
|
# Démarrer le processus
|
2023-01-17 01:24:21 +01:00
|
|
|
scene.objects['System']['plot_proc'] = subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot_qt.py")], stdin=subprocess.PIPE, encoding = 'utf8', universal_newlines=True)
|
2023-01-18 06:06:15 +01:00
|
|
|
# # scene.objects['System']['plot_proc'] = subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot_qt.py")], stdin=subprocess.PIPE, stdout=subprocess.PIPE, encoding = 'utf8')
|
|
|
|
# # stout = scene.objects['System']['plot_proc'].communicate() # FIXME : attente du message retour pour lancer l'acquisition
|
|
|
|
# # print("Blender stout : ", stout)
|
2023-01-16 07:48:01 +01:00
|
|
|
scene.objects['System']['plot']=True
|
2023-01-14 14:03:15 +01:00
|
|
|
|
2023-01-14 07:49:53 +01:00
|
|
|
###############################################################################
|
2023-01-16 07:48:01 +01:00
|
|
|
# Mise à jour du graphique
|
2023-01-14 07:49:53 +01:00
|
|
|
###############################################################################
|
2023-01-15 10:05:41 +01:00
|
|
|
|
2023-01-17 01:24:21 +01:00
|
|
|
def truncate(n, decimals=0):
|
|
|
|
multiplier = 10**decimals
|
|
|
|
return int(n* multiplier)/multiplier
|
|
|
|
|
2023-01-16 07:48:01 +01:00
|
|
|
def plot_maj(cont):
|
|
|
|
if cont.sensors['Plot'].positive :
|
|
|
|
|
|
|
|
# Affichage du graphique
|
|
|
|
if scene.objects['System']['plot_draw']:
|
2023-01-18 06:06:15 +01:00
|
|
|
if scene.objects['System']['plot_time'] != truncate(scene.objects['System']['time'], 0) or True:
|
2023-01-16 07:48:01 +01:00
|
|
|
|
2023-01-17 01:24:21 +01:00
|
|
|
# Préparation du message
|
|
|
|
# FIXME : ajouter les valeurs réelles et valeurs numériques ('activated_real')
|
2023-01-18 06:06:15 +01:00
|
|
|
scene.objects['System']['plot_time'] = truncate(scene.objects['System']['time'], 0)
|
2023-01-17 01:24:21 +01:00
|
|
|
# msg=str(round(scene.objects['System']['plot_time'], 1))
|
|
|
|
# msg=format(scene.objects['System']['plot_time'],".2f")
|
2023-01-18 06:06:15 +01:00
|
|
|
msg=format(scene.objects['System']['plot_time'],".2f")
|
2023-01-17 01:24:21 +01:00
|
|
|
for obj in scene.objects['System']['plot_data']:
|
|
|
|
if scene.objects[obj]['activated']:
|
|
|
|
msg = msg+",1"
|
|
|
|
else:
|
|
|
|
msg = msg+",0"
|
|
|
|
msg = msg+"\n"
|
2023-01-16 07:48:01 +01:00
|
|
|
|
2023-01-17 01:24:21 +01:00
|
|
|
# Envoi (Pipe)
|
|
|
|
if scene.objects['System']['plot_proc'].poll()==None:
|
2023-01-18 06:06:15 +01:00
|
|
|
# # scene.objects['System']['plot_proc'].communicate(input=time_send.encode())[0] # Communication bloquante
|
|
|
|
# scene.objects['System']['plot_proc'].communicate(input=msg.encode())[0] # Communication bloquante
|
|
|
|
scene.objects['System']['plot_proc'].communicate(input=msg.encode()) # Communication bloquante
|
|
|
|
# scene.objects['System']['plot_proc'].stdin.write(msg)
|
2023-01-17 01:24:21 +01:00
|
|
|
print ("twin : ", msg)
|
|
|
|
else:
|
|
|
|
# Arrêt
|
|
|
|
print ("Stop")
|
|
|
|
scene.objects['System']['plot']=False
|
|
|
|
scene.objects['System']['plot_draw'] =False
|
|
|
|
scene.objects['System']['plot_proc'].terminate()
|
2023-01-16 07:48:01 +01:00
|
|
|
|
2023-01-17 01:24:21 +01:00
|
|
|
# Arrêt de l'affichage du graphique
|
2023-01-16 07:48:01 +01:00
|
|
|
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()
|