2023-01-21 06:56:54 +01:00
|
|
|
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import importlib
|
|
|
|
import subprocess # Multiprocessus
|
|
|
|
import time
|
|
|
|
import csv
|
2023-01-25 06:03:02 +01:00
|
|
|
import xml.etree.ElementTree as ET # Creating/parsing XML file
|
2023-01-21 06:56:54 +01:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# twin_daq.py
|
|
|
|
# @title: Enregistement et 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
|
|
|
|
scene = bge.logic.getCurrentScene()
|
|
|
|
|
|
|
|
# Récupérer la configuration du graphique
|
|
|
|
system=importlib.import_module(scene.objects['System']['system']) # Système
|
|
|
|
daq_config = system.get_public_vars()
|
|
|
|
|
|
|
|
# CSV
|
|
|
|
daq_data=[]
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Accès aux variables publiques du système
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
def get(data):
|
|
|
|
if data in daq_config:
|
|
|
|
if len (daq_config[data][0])>2: # Echelle à prendre en compte
|
|
|
|
return(scene.objects[daq_config[data][0][0]][daq_config[data][0][1]]*daq_config[data][0][2])
|
|
|
|
else:
|
|
|
|
return(scene.objects[daq_config[data][0][0]][daq_config[data][0][1]])
|
|
|
|
else:
|
|
|
|
print ("Erreur sur l'accès aux variables par get("+data+"), la variable '"+data+"' absente du système.")
|
|
|
|
return None
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Enregistrement des données
|
|
|
|
###############################################################################
|
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
2023-01-21 10:39:24 +01:00
|
|
|
# Activation du mode DAQ
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
def daq(data):
|
2023-01-21 10:39:24 +01:00
|
|
|
daq_data.clear()
|
2023-01-21 06:56:54 +01:00
|
|
|
scene.objects['System']['daq_var'] =[]
|
2023-01-25 06:03:02 +01:00
|
|
|
row = ['t']
|
2023-01-21 06:56:54 +01:00
|
|
|
for var in data:
|
|
|
|
scene.objects['System']['daq_var'].append([daq_config[var][0][0], daq_config[var][0][1]])
|
2023-01-25 06:03:02 +01:00
|
|
|
row.append(var)
|
|
|
|
# print ("Acquisition des données :", scene.objects['System']['daq_var'])
|
|
|
|
daq_data.append(row)
|
2023-01-21 06:56:54 +01:00
|
|
|
scene.objects['System']['daq']=True
|
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
2023-01-21 06:56:54 +01:00
|
|
|
# Ajout des données (fps = 60)
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
def daq_add(cont):
|
2023-01-25 06:03:02 +01:00
|
|
|
row = [round(scene.objects['System']['time'], 3)]
|
2023-01-21 06:56:54 +01:00
|
|
|
for var in scene.objects['System']['daq_var']:
|
2023-01-25 06:03:02 +01:00
|
|
|
row.append(round(scene.objects[var[0]][var[1]], 3))
|
|
|
|
daq_data.append(row)
|
2023-01-21 06:56:54 +01:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Tableau CSV
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
# Format français des décimaux
|
|
|
|
def localize_floats(row):
|
|
|
|
return [
|
2023-01-21 10:39:24 +01:00
|
|
|
str(el).replace('.', ',') if isinstance(el, float) else el
|
2023-01-21 06:56:54 +01:00
|
|
|
for el in row
|
|
|
|
]
|
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
2023-01-21 06:56:54 +01:00
|
|
|
# Génération du fichier
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
def csv_generate():
|
|
|
|
scene.objects['System']['daq']=False
|
|
|
|
fichier_csv=scene.objects['System']['system']+'.csv'
|
2023-01-21 10:39:24 +01:00
|
|
|
scene.objects['Cmd-text']['modal']= True
|
|
|
|
scene.objects['Cmd-text']['Text']= "Génération du fichier : "+fichier_csv
|
|
|
|
scene.objects['Cmd-text'].setVisible(True,False)
|
2023-01-21 06:56:54 +01:00
|
|
|
with open(fichier_csv, 'w') as csvfile:
|
|
|
|
writer = csv.writer(csvfile, delimiter = ';', lineterminator = '\n')
|
|
|
|
for t_data in daq_data:
|
|
|
|
writer.writerow(localize_floats(t_data))
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Graphique statique
|
|
|
|
###############################################################################
|
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
|
|
|
# Création du fichier twin_plot.xml (configuration du graphique)
|
|
|
|
##
|
|
|
|
|
|
|
|
def plot_config_generate(data_groups):
|
|
|
|
|
|
|
|
# Création du XML
|
|
|
|
daq_config_list=list(daq_config)
|
|
|
|
xml_data = ET.Element('data')
|
|
|
|
xml_figure = ET.SubElement(xml_data, 'figure')
|
|
|
|
xml_plot = ET.SubElement(xml_figure, 'plot')
|
|
|
|
for var in daq_config_list:
|
|
|
|
xml_var = ET.SubElement(xml_plot, 'var')
|
|
|
|
xml_var.text=var
|
|
|
|
if len(daq_config[var][2]) >=4:
|
|
|
|
xml_mark = ET.SubElement(xml_var, 'marker')
|
|
|
|
xml_mark.text=daq_config[var][2][0]
|
|
|
|
xml_line = ET.SubElement(xml_var, 'linestyle')
|
|
|
|
xml_line.text=daq_config[var][2][1]
|
|
|
|
xml_color = ET.SubElement(xml_var, 'color')
|
|
|
|
xml_color.text=daq_config[var][2][2]
|
|
|
|
xml_size = ET.SubElement(xml_var, 'linewidth')
|
|
|
|
xml_size.text=str(daq_config[var][2][3])
|
|
|
|
|
|
|
|
# Détection de groupe de graphique
|
|
|
|
data_group_i=0
|
|
|
|
xml_group = ET.SubElement(xml_var, 'group')
|
|
|
|
xml_group.text="-1" # Pas de groupe par défaut
|
|
|
|
for data_group in data_groups:
|
|
|
|
if isinstance(data_group, list):
|
|
|
|
data_group_i+=1
|
|
|
|
for data_subgroup in data_group: # Scan du sous-groupe
|
|
|
|
if data_subgroup == var:
|
|
|
|
xml_group.text=str(data_group_i)
|
|
|
|
else:
|
|
|
|
if data_group == var: # Pas de groupe
|
|
|
|
xml_group.text="0"
|
|
|
|
|
|
|
|
# Ecriture fichier
|
|
|
|
plot_config = ET.ElementTree(xml_data)
|
|
|
|
with open("plot_config.xml", "wb") as f: # XML généré plat (not pretty print)
|
|
|
|
plot_config.write(f)
|
|
|
|
|
|
|
|
##
|
2023-01-21 10:39:24 +01:00
|
|
|
# Activation du mode Plot
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
|
|
|
|
|
|
|
def plot(data_groups):
|
|
|
|
plot_config_generate(data_groups)
|
2023-01-21 10:39:24 +01:00
|
|
|
scene.objects['System']['plot']=True
|
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
2023-01-21 10:39:24 +01:00
|
|
|
# Fermer le processus du graphique
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
|
|
|
|
2023-01-21 10:39:24 +01:00
|
|
|
def plot_close():
|
|
|
|
if scene.objects['System']['plot_proc'] is not None:
|
|
|
|
if scene.objects['System']['plot_proc'].poll()==None:
|
|
|
|
scene.objects['System']['plot_proc'].terminate()
|
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
2023-01-21 10:39:24 +01:00
|
|
|
# Génération du graphique
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
def plot_generate():
|
2023-01-21 10:39:24 +01:00
|
|
|
plot_close()
|
|
|
|
fichier_csv=scene.objects['System']['system']+'.csv'
|
|
|
|
scene.objects['System']['plot_proc'] = subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot_qt.py"), fichier_csv])
|
|
|
|
scene.objects['System']['plot']=False
|
2023-01-21 06:56:54 +01:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Graphique interactif
|
2023-01-21 10:39:24 +01:00
|
|
|
# FIXME : ne marche pas
|
2023-01-21 06:56:54 +01:00
|
|
|
###############################################################################
|
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
2023-01-21 06:56:54 +01:00
|
|
|
# Mise en forme de décimaux
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
def truncate(n, decimals=0):
|
|
|
|
multiplier = 10**decimals
|
|
|
|
return int(n* multiplier)/multiplier
|
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
2023-01-21 06:56:54 +01:00
|
|
|
# Création du graphique interactif
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
|
|
|
|
2023-01-21 10:39:24 +01:00
|
|
|
def plot_start(data):
|
2023-01-21 06:56:54 +01:00
|
|
|
# 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:
|
|
|
|
scene.objects['System']['plot_data'].append(daq_config[obj][0][0])
|
|
|
|
|
|
|
|
# Démarrer le processus
|
|
|
|
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)
|
|
|
|
# # 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)
|
|
|
|
scene.objects['System']['plot']=True
|
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
2023-01-21 06:56:54 +01:00
|
|
|
# Ajout des données (fps = 60)
|
2023-01-25 06:03:02 +01:00
|
|
|
##
|
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
def plot_add(cont):
|
|
|
|
if cont.sensors['Plot'].positive :
|
|
|
|
|
|
|
|
# Affichage du graphique
|
|
|
|
if scene.objects['System']['plot_draw']:
|
|
|
|
if scene.objects['System']['plot_time'] != truncate(scene.objects['System']['time'], 0) or True:
|
|
|
|
|
|
|
|
# Préparation du message
|
|
|
|
# FIXME : ajouter les valeurs réelles et valeurs numériques ('activated_real')
|
|
|
|
scene.objects['System']['plot_time'] = truncate(scene.objects['System']['time'], 0)
|
|
|
|
# msg=str(round(scene.objects['System']['plot_time'], 1))
|
|
|
|
# msg=format(scene.objects['System']['plot_time'],".2f")
|
|
|
|
msg=format(scene.objects['System']['plot_time'],".2f")
|
|
|
|
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'].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)
|
|
|
|
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-21 10:39:24 +01:00
|
|
|
|
2023-01-21 06:56:54 +01:00
|
|
|
# Arrêt de l'affichage du graphique
|
|
|
|
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()
|