Communication entre Blender et Qt5 (point d'étape)

This commit is contained in:
Philippe Roy 2023-01-15 16:56:46 +01:00
parent af09b80de2
commit 298337663b
13 changed files with 141 additions and 22 deletions

View File

@ -51,7 +51,7 @@ system_card_description.update({"firmata-card" : [card_firmata_title, card_firma
# Monitoring
card_plot_title="Chronogramme"
card_plot_text=""" plot([valeurs]) \n -> Affiche le chronogramme de la liste des \n valeurs spécifiées.
card_plot_text=""" plot([valeurs]) \n -> Affiche le chronogramme des valeurs \n spécifiées. Blender étant cadencé à 60 fps, \n la fréquence d'acquisition est 16 ms.
Par exemple : plot(['ba_0', 'cabine_z']).\n
Les entrées/sorties peuvent être suivies : \n ba_0', 'ba_1', 'pc_0', 'pc_1', 'mot_m', 'mot_d', \n 'voy_0' et 'voy_1'.\n
Les autres valeurs pouvant être suivies : \n 'moteur_angle', 'moteur_vitesse', 'cabine_z'."""

View File

@ -0,0 +1 @@
/home/phroy/Bureau/seriousgames/blender-edutech/git/digital_twin/twin_plot_qt.py

1
poppy_ergo_jr/twin_plot.py Symbolic link
View File

@ -0,0 +1 @@
/home/phroy/Bureau/seriousgames/blender-edutech/git/digital_twin/twin_plot.py

View File

@ -0,0 +1 @@
/home/phroy/Bureau/seriousgames/blender-edutech/git/digital_twin/twin_plot_qt.py

View File

@ -17,16 +17,19 @@ import time
scene = bge.logic.getCurrentScene()
# Configuration du brochage du jumeau réel
# 'nom_variable' : [['type de broche (d,a,p) ','mode de la broche (i,o)'],
# ['Objet 3D','pin'],
# ['marque', 'type de ligne', 'couleur', linewidth]] (matplotlib)
pin_config = {
'bp_ext' : [['d','i'],['Bp cote rue','pin']],
'bp_int' : [['d','i'],['Bp cote cour','pin']],
'fdc_o' : [['d','i'],['Microrupteur fdc ouvert','pin']],
'fdc_f' : [['d','i'],['Microrupteur fdc ferme','pin']],
'mot_o' : [['d','o'],['Moteur','pin_o']],
'mot_f' : [['d','o'],['Moteur','pin_f']],
'gyr' : [['d','o'],['Led','pin']],
'ir_emet' : [['d','o'],['Emetteur IR','pin']],
'ir_recep' : [['d','i'],['Recepteur IR','pin']]}
'bp_ext' : [['d','i'],['Bp cote rue','pin'],['o','-', 'blue', 1]],
'bp_int' : [['d','i'],['Bp cote cour','pin'],['o','-', 'darkblue', 1]],
'fdc_o' : [['d','i'],['Microrupteur fdc ouvert','pin'],['o','-', 'violet', 1]],
'fdc_f' : [['d','i'],['Microrupteur fdc ferme','pin'],['o','-', 'darkviolet', 1]],
'mot_o' : [['d','o'],['Moteur','pin_o'],['o','-', 'green', 1]],
'mot_f' : [['d','o'],['Moteur','pin_f'],['o','-', 'darkgreen', 1]],
'gyr' : [['d','o'],['Led','pin'],['o','-', 'orange', 1]],
'ir_emet' : [['d','o'],['Emetteur IR','pin'],['o','-', 'red', 1]],
'ir_recep' : [['d','i'],['Recepteur IR','pin'],['o','-', 'darkred', 1]]}
# Couleurs
color_passive = (0.800, 0.005, 0.315,1) # bouton non activable : magenta

View File

@ -51,7 +51,8 @@ def commandes():
# mot_f(False)
# gyr(False)
plot(['Led'])
plot(['gyr'])
# plot(['gyr', 'bp_ext', 'bp_int'])
while True:
gyr(True)
tempo(0.25)

View File

@ -53,7 +53,7 @@ system_card_description.update({"firmata-card" : [card_firmata_title, card_firma
# Monitoring
card_plot_title="Chronogramme"
card_plot_text=""" plot([valeurs]) \n -> Affiche le chronogramme de la liste des \n valeurs spécifiées.
card_plot_text=""" plot([valeurs]) \n -> Affiche le chronogramme des valeurs \n spécifiées. Blender étant cadencé à 60 fps, \n la fréquence d'acquisition est 16 ms.
Par exemple : plot(['bp_ext', 'portail_x']).\n
Les entrées/sorties peuvent être suivies : \n 'bp_ext', 'bp_int', 'fdc_o', 'fdc_f', 'mot_o',\n 'mot_f', 'gyr', 'ir_emet' et 'ir_recep'.\n
Les autres valeurs pouvant être suivies : \n 'moteur_angle', 'moteur_vitesse', 'portail_x'."""

View File

@ -0,0 +1 @@
/home/phroy/Bureau/seriousgames/blender-edutech/git/digital_twin/twin_plot_qt.py

View File

@ -31,7 +31,7 @@ class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
self.subplot = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)
class MainWindow(QtWidgets.QMainWindow):
@ -50,33 +50,44 @@ class MainWindow(QtWidgets.QMainWindow):
self.show()
# Timer pour le update_plot
fps = 60
self.timer = QtCore.QTimer()
self.timer.setInterval(100)
self.timer.setInterval(1000/fps)
self.timer.timeout.connect(self.update_plot)
self.timer.start()
# Lecture des données et mise à jour du graphique
def update_plot(self):
plt = self.canvas.subplot
# Données
# donnees = ([0,1,2,3,4], [10,1,20,3,40])
# print (self.xdata[len(self.xdata)-1]+1)
new_x=self.xdata[len(self.xdata)-1]+1
self.xdata.append(new_x)
self.ydata.append(random.randint(0, 10))
# new_x=self.xdata[len(self.xdata)-1]+1
# self.xdata.append(new_x)
# self.ydata.append(random.randint(0, 10))
# Lecture du Pipe
for line in sys.stdin:
print('Output:', line.rstrip())
for line in sys.stdin:
msg = line.rstrip()
break
# X et Y
# FIXME : temps et une valeur
msg_list=msg.split(',')
print('Output:', msg, msg_list)
self.xdata = self.xdata + [float(msg_list[0])]
self.ydata = self.ydata + [float(msg_list[1])]
# self.ydata = self.ydata + [random.randint(0, 10)]
# Drop off the first y element, append a new one.
# self.ydata = self.ydata[1:] + [random.randint(0, 10)]
# Redraw
self.canvas.axes.cla()
self.canvas.axes.plot(self.xdata, self.ydata, 'r')
plt.cla()
plt.plot(self.xdata, self.ydata, 'r')
self.canvas.draw()
###############################################################################

99
twin_plot_qt.py Normal file
View File

@ -0,0 +1,99 @@
import sys
import random
import matplotlib
matplotlib.use('Qt5Agg')
from PyQt5 import QtCore, QtGui, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
###############################################################################
# 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
# scene = bge.logic.getCurrentScene()
# Récupérer le brochage du jumeau réel
# system=importlib.import_module(scene.objects['Doc']['system']) # Système
# pin_config = system.get_pin_config()
###############################################################################
# Fenêtre dynamique
###############################################################################
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.subplot = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)
class MainWindow(QtWidgets.QMainWindow):
# Création du graphique
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
self.setCentralWidget(self.canvas)
n_data = 50
# self.xdata = list(range(n_data))
# self.ydata = [random.randint(0, 10) for i in range(n_data)]
self.xdata = [0]
self.ydata = [0]
self.update_plot()
self.show()
# Timer pour le update_plot
fps = 60
self.timer = QtCore.QTimer()
self.timer.setInterval(1000/fps)
self.timer.timeout.connect(self.update_plot)
self.timer.start()
# Lecture des données et mise à jour du graphique
def update_plot(self):
plt = self.canvas.subplot
# Données
# donnees = ([0,1,2,3,4], [10,1,20,3,40])
# print (self.xdata[len(self.xdata)-1]+1)
# new_x=self.xdata[len(self.xdata)-1]+1
# self.xdata.append(new_x)
# self.ydata.append(random.randint(0, 10))
# Lecture du Pipe
for line in sys.stdin:
msg = line.rstrip()
break
# X et Y
# FIXME : temps et une valeur
msg_list=msg.split(',')
print('Output:', msg, msg_list)
self.xdata = self.xdata + [float(msg_list[0])]
self.ydata = self.ydata + [float(msg_list[1])]
# self.ydata = self.ydata + [random.randint(0, 10)]
# Drop off the first y element, append a new one.
# self.ydata = self.ydata[1:] + [random.randint(0, 10)]
# Redraw
plt.cla()
plt.plot(self.xdata, self.ydata, 'r')
self.canvas.draw()
###############################################################################
# Application
###############################################################################
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
app.exec_()

View File

@ -0,0 +1 @@
/home/phroy/Bureau/seriousgames/blender-edutech/git/digital_twin/twin_plot_qt.py

View File

@ -51,7 +51,7 @@ system_card_description.update({"firmata-card" : [card_firmata_title, card_firma
# Monitoring
card_plot_title="Chronogramme"
card_plot_text=""" plot([valeurs]) \n -> Affiche le chronogramme de la liste des \n valeurs spécifiées.
card_plot_text=""" plot([valeurs]) \n -> Affiche le chronogramme des valeurs \n spécifiées. Blender étant cadencé à 60 fps, \n la fréquence d'acquisition est 16 ms.
Par exemple : plot(['bp_m', 'moteur_angle']).\n
Les entrées/sorties peuvent être suivies : \n 'bp_m', 'bp_a', 'bp_d', 'mot_m', 'mot_d', \n 'fdc_h', 'fdc_b', 'bp_auto', 'voy_auto' et 'lum'.\n
Les autres valeurs pouvant être suivies : \n 'moteur_angle', 'moteur_vitesse'."""