Bugfix : génération des plot bugée, simplification de la gestion des threads

This commit is contained in:
Philippe Roy 2023-01-28 17:24:21 +01:00
parent 1272fad6b3
commit 95fbbfa09c
7 changed files with 67 additions and 83 deletions

View File

@ -91,15 +91,15 @@ def commandes():
# " rad/s - portail_pas : " +str(round(portail_pas, 3))+" mm/impulsion - moteur_pas : " +str(round(mot_pas, 3))+" rad/impulsion")
plot([['bp_ext', 'bp_ext_r'], ['bp_int', 'bp_int_r'], ['fdc_o', 'fdc_o_r'], ['fdc_f', 'fdc_f_r'], 'mot_o', 'mot_f', 'gyr', ['mot_angle', 'mot_vitesse', 'portail_x', 'portail_vitesse']])
fin() # A garder
###############################################################################
# En: External call << DONT CHANGE THIS SECTION >>
# Fr: Appel externe << NE PAS MODIFIER CETTE SECTION >>
###############################################################################
###############################################################################
# En: External call << DONT CHANGE THIS SECTION >>
# Fr: Appel externe << NE PAS MODIFIER CETTE SECTION >>
###############################################################################
if __name__=='start':
thread_cmd_start(commandes)
start(commandes)
if __name__=='stop':
stop()

View File

@ -1,5 +1,5 @@
import bge # Bibliothèque Blender Game Engine (UPBGE)
from twin_threading import thread_cmd_start, thread_cmd_stop, thread_cmd_end # Multithreading (multitâches)
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
@ -23,6 +23,12 @@ 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
###############################################################################
@ -123,12 +129,19 @@ def set_t (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'])
@ -138,35 +151,21 @@ def stop():
if scene.objects['System']['daq']:
csv_generate()
if scene.objects['System']['plot']:
plot_generate()
scene.objects['System']['plot_draw']=False # Plot
plot_generate(threads_plot)
# Thread
thread_cmd_stop()
twin_threading.stop(threads_cmd, "commands")
##
# Fin naturelle
##
def end():
# 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()
scene.objects['System']['plot_draw']=False # Plot
# Thread
thread_cmd_end()
fin()
def fin():
end()
def quit():
end()
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)

View File

@ -537,6 +537,8 @@ def cycle_run ():
system.system_reset()
time.sleep(0.125)
scene.objects['System']['time']=0
scene.objects['System']['daq']=False
scene.objects['System']['plot']=False
module_name=os.path.split((scene.objects['System']['script']))[1][:-3]
if python_validation(scene.objects['System']['script']):
runpy.run_path(scene.objects['System']['script'], run_name='start') # Execution du script utilisateur
@ -555,7 +557,7 @@ def cycle_stop ():
system.system_reset()
##
# Fin naturelle du cycle
# Fin du cycle
##
def cycle_end (cont):

View File

@ -2,6 +2,7 @@ import bge # Bibliothèque Blender Game Engine (UPBGE)
import os
import sys
import importlib
import threading # Multithreading
import subprocess # Multiprocessus
import time
import csv
@ -155,17 +156,34 @@ def plot(data_groups):
# Génération du graphique
##
def plot_generate():
def plot_generate_thread(csv_file):
command = sys.executable + " " + os.path.join(sys.executable, os.getcwd(), "twin_plot_qt.py") + " "+ csv_file
os.system(command)
def plot_generate(threads):
# Threading -> reste actif après le Stop
csv_file=os.path.join(os.path.split((scene.objects['System']['script']))[0], scene.objects['System']['system']+'.csv')
threads.append(threading.Thread(target=plot_generate_thread, args=(csv_file,)))
threads[len(threads)-1].start()
# exec : Bloquant
# csv_file=os.path.join(os.path.split((scene.objects['System']['script']))[0], scene.objects['System']['system']+'.csv')
# command = sys.executable + " " + os.path.join(sys.executable, os.getcwd(), "twin_plot_qt.py") + " "+ csv_file
# command2 = os.path.join(sys.executable, os.getcwd(), "twin_plot_qt.py") + " "+ csv_file
# command3 = os.path.join(sys.executable, os.getcwd(), "twin_plot_qt.py")
# exec(open(command3).read(), {}, {'csv_file': csv_file})
# subprocess : Bug
# Terminer le processus plot précédent
if ('plot_proc' in scene.objects['System']):
if scene.objects['System']['plot_proc'].poll()==None:
scene.objects['System']['plot_proc'].terminate()
# if ('plot_proc' in scene.objects['System']):
# if scene.objects['System']['plot_proc'].poll()==None:
# scene.objects['System']['plot_proc'].terminate()
# Démarrer le processus plot
fichier_csv=os.path.join(os.path.split((scene.objects['System']['script']))[0], 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
# fichier_csv=os.path.join(os.path.split((scene.objects['System']['script']))[0], 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
###############################################################################
# Graphique interactif

View File

@ -25,6 +25,8 @@ import matplotlib.pyplot as plts
twin_config = ET.parse('twin_config.xml').getroot()
plot_config_tree = ET.parse('plot_config.xml').getroot()
plot_config={}
# csv_file= locals()['csv_file']
# print(locals()['file_csv'])
###############################################################################
# Configuration des plots
@ -219,10 +221,11 @@ class MainWindow(QtWidgets.QMainWindow):
# Lecture fichier CSV
fields = []
rows = []
with open(sys.argv[1], newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=';')
fields = next(csvreader)
for row in csvreader:
# with open(csv_file, newline='') as csv_buff:
with open(sys.argv[1], newline='') as csv_buff:
csv_reader = csv.reader(csv_buff, delimiter=';')
fields = next(csv_reader)
for row in csv_reader:
rows.append(row)
# Mise en tableau à deux colonnes (xdata,ydata)

View File

@ -2,7 +2,6 @@ import bge # Bibliothèque Blender Game Engine (UPBGE)
import threading # Multithreading
import trace
import sys
import time
###############################################################################
# twin_threading.py
@ -17,12 +16,6 @@ import time
scene = bge.logic.getCurrentScene()
debug_thread = scene.objects['System']['debug_thread']
# Threads de commandes
threads_cmd=[]
# # Threads de visualisation de données
# threads_plot=[]
###############################################################################
# Méthode kill pour les tâches (threads)
###############################################################################
@ -61,14 +54,14 @@ class thread_with_trace(threading.Thread):
# Start et stop des tâches (threads)
###############################################################################
def thread_start(threads, type_txt, fct):
def 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.")
# Stop des threads
def thread_stop(threads, type_txt):
def stop(threads, type_txt):
i=0
zombie_flag=False
for t in threads:
@ -97,34 +90,3 @@ def thread_stop(threads, type_txt):
if (debug_thread):
print ("There are zombies threads",type_txt, ".")
return False
###############################################################################
# Fonctions utilisateur
###############################################################################
def thread_cmd_start(fct):
thread_start(threads_cmd, "commands", fct)
def thread_cmd_stop():
thread_stop(threads_cmd, "commands")
def thread_cmd_end():
if (debug_thread):
print ("Thread commands is arrived.")
time.sleep(0.125)
scene.objects['System']['thread_cmd']=False
time.sleep(0.125)
###############################################################################
# Fonctions visualisation de données
###############################################################################
# def thread_plot_start(fct):
# thread_start(threads_plot, "plot", fct)
# def thread_plot_stop():
# thread_stop(threads_plot, "plot")
# def thread_plot_end():
# if (debug_thread):
# print ("Thread plot is arrived.")