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,6 +91,7 @@ def commandes():
# " rad/s - portail_pas : " +str(round(portail_pas, 3))+" mm/impulsion - moteur_pas : " +str(round(mot_pas, 3))+" rad/impulsion") # " 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']]) 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 fin() # A garder
############################################################################### ###############################################################################
@ -99,7 +100,6 @@ def commandes():
############################################################################### ###############################################################################
if __name__=='start': if __name__=='start':
thread_cmd_start(commandes) start(commandes)
if __name__=='stop': if __name__=='stop':
stop() stop()

View File

@ -1,5 +1,5 @@
import bge # Bibliothèque Blender Game Engine (UPBGE) 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_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 from twin_daq import get, daq, csv_generate, plot, plot_generate # Acquisition des données
import time import time
@ -23,6 +23,12 @@ JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
ACTIVATE = bge.logic.KX_INPUT_ACTIVE ACTIVATE = bge.logic.KX_INPUT_ACTIVE
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED # JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
# Threads de commandes
threads_cmd=[]
# Threads de visualisation de données
threads_plot=[]
############################################################################### ###############################################################################
# Gyrophare # Gyrophare
############################################################################### ###############################################################################
@ -123,6 +129,13 @@ def set_t (date):
def reset_t (): def reset_t ():
scene.objects['System']['time']=0 scene.objects['System']['time']=0
##
# Départ
##
def start(fct):
twin_threading.start(threads_cmd, "commands", fct)
## ##
# Arrêt # Arrêt
## ##
@ -138,35 +151,21 @@ def stop():
if scene.objects['System']['daq']: if scene.objects['System']['daq']:
csv_generate() csv_generate()
if scene.objects['System']['plot']: if scene.objects['System']['plot']:
plot_generate() plot_generate(threads_plot)
scene.objects['System']['plot_draw']=False # Plot
# Thread # Thread
thread_cmd_stop() twin_threading.stop(threads_cmd, "commands")
## ##
# Fin naturelle # Fin naturelle
## ##
def end(): def end():
fin()
# 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()
def fin(): def fin():
end() if scene.objects['System']['debug_thread']:
print ("Thread commands is arrived.")
def quit(): time.sleep(0.125)
end() scene.objects['System']['thread_cmd']=False
time.sleep(0.125)

View File

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

View File

@ -2,6 +2,7 @@ import bge # Bibliothèque Blender Game Engine (UPBGE)
import os import os
import sys import sys
import importlib import importlib
import threading # Multithreading
import subprocess # Multiprocessus import subprocess # Multiprocessus
import time import time
import csv import csv
@ -155,17 +156,34 @@ def plot(data_groups):
# Génération du graphique # 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 # Terminer le processus plot précédent
if ('plot_proc' in scene.objects['System']): # if ('plot_proc' in scene.objects['System']):
if scene.objects['System']['plot_proc'].poll()==None: # if scene.objects['System']['plot_proc'].poll()==None:
scene.objects['System']['plot_proc'].terminate() # scene.objects['System']['plot_proc'].terminate()
# Démarrer le processus plot # Démarrer le processus plot
fichier_csv=os.path.join(os.path.split((scene.objects['System']['script']))[0], scene.objects['System']['system']+'.csv') # 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_proc'] = subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot_qt.py"), fichier_csv])
scene.objects['System']['plot']=False # scene.objects['System']['plot']=False
############################################################################### ###############################################################################
# Graphique interactif # Graphique interactif

View File

@ -25,6 +25,8 @@ import matplotlib.pyplot as plts
twin_config = ET.parse('twin_config.xml').getroot() twin_config = ET.parse('twin_config.xml').getroot()
plot_config_tree = ET.parse('plot_config.xml').getroot() plot_config_tree = ET.parse('plot_config.xml').getroot()
plot_config={} plot_config={}
# csv_file= locals()['csv_file']
# print(locals()['file_csv'])
############################################################################### ###############################################################################
# Configuration des plots # Configuration des plots
@ -219,10 +221,11 @@ class MainWindow(QtWidgets.QMainWindow):
# Lecture fichier CSV # Lecture fichier CSV
fields = [] fields = []
rows = [] rows = []
with open(sys.argv[1], newline='') as csvfile: # with open(csv_file, newline='') as csv_buff:
csvreader = csv.reader(csvfile, delimiter=';') with open(sys.argv[1], newline='') as csv_buff:
fields = next(csvreader) csv_reader = csv.reader(csv_buff, delimiter=';')
for row in csvreader: fields = next(csv_reader)
for row in csv_reader:
rows.append(row) rows.append(row)
# Mise en tableau à deux colonnes (xdata,ydata) # 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 threading # Multithreading
import trace import trace
import sys import sys
import time
############################################################################### ###############################################################################
# twin_threading.py # twin_threading.py
@ -17,12 +16,6 @@ import time
scene = bge.logic.getCurrentScene() scene = bge.logic.getCurrentScene()
debug_thread = scene.objects['System']['debug_thread'] 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) # 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) # 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.append(thread_with_trace(target = fct))
threads[len(threads)-1].start() threads[len(threads)-1].start()
if (debug_thread): if (debug_thread):
print ("Thread",type_txt, "#", len(threads)-1, "open.") print ("Thread",type_txt, "#", len(threads)-1, "open.")
# Stop des threads # Stop des threads
def thread_stop(threads, type_txt): def stop(threads, type_txt):
i=0 i=0
zombie_flag=False zombie_flag=False
for t in threads: for t in threads:
@ -97,34 +90,3 @@ def thread_stop(threads, type_txt):
if (debug_thread): if (debug_thread):
print ("There are zombies threads",type_txt, ".") print ("There are zombies threads",type_txt, ".")
return False 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.")