Salut à tous,

Je développe une solution pour une station de préparation d'aliments à la ferme.

Petit souci, j'ai une fonction qui s'enclenche si une des conditions est valide :
- quantité atteinte
-et/ou temps dépassé
Ceci permet de soit remplir une quantité ou soit avoir un timer pour chaque étape de mon remplissage.

J'ai donc pensé appeler la fonction stoppant l'étape :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
def condition(temps=longtemps, quantité=beaucoup):
par la formule
condition(conditions)
avec conditions un dictionnaire ,{'temps':10}(en secondes) ou {'quantite':100} (en kg).
J'ai vu cela sur l'explication du module thread
(http://python.developpez.com/faq/?page=Thread)
l'argument 'nom' est appelé ainsi sur cette page.
et aurai-je mal compris le système ????


Code complet (c'est pas du jojo sans doute mais je perfectionnerai quand j'aurai le temps) !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
 
 
from tkinter import*
import threading
from time import*
 
 
"importer série et créer fonction arduino"
 
 
 
 
 
 
 
class Pesee():
    "différentes pesée"
poids=Pesee()
poids.total=0
poids.simule=0
poids.partiel=0
poids.tare=0
 
class Process():
    "Etat du process"
process=Process()
process.step=0
process.conditions={}
 
 
class Etat():
    "etat des commandes"
 
 
com=Etat()
 
 
com.nom=["robinet maïs","robinet soja","vis CMV","démarrage broyeur","fonct broyeur","mélangeur","vidange"]
com.marche=len(com.nom)*[0]
 
 
class Produit():
    "Nom et quantité à distribuer"
prod=Produit()
prod.nom=["Maïs", "soja","CMV"]
prod.qte=len(prod.nom)*[0]
 
 
class Capteur():
    "Nom et état des capteurs"
 
 
 
 
 
def informer(texte):
    info.configure(text=texte)
 
 
 
 
 
 
 
def arduino_envoyer(numero,on):
    "A réaliser "
    if 0:
        message=['a','b','c','d','e','f','g','h'][numero]
        if on:
            message=message.upper()
 
        "c'est ici qu'on enverra le message à arduino"
 
 
 
 
 
 
 
def commandevis(self,ch=2):
    "changement d'état de la vis self : éteindre si  ch = 0, allumer si 1, changer si 2"
    fen1.destroy
    if ch==2 and com.marche[self]==0 or ch==1:
        com.marche[self]=1
        widgetslst[self].configure(text="on")
        arduino_envoyer(self, 1)
    else:
        com.marche[self]=0
        widgetslst[self].configure(text="off")
        arduino_envoyer (self, 0)
 
 
 
 
 
 
 
def tare(p=poids.total):
    poids.tare=p
 
 
 
def importerPoids():
    "Aller chercher sur arduino"
 
    return poids.simule
 
 
 
 
PoidsSimul=0
def actualiserpoids():
    ""
    poids.total=importerPoids()
    print("aaaa", poids.total)
    poids.partiel=poids.total-poids.tare
    wtotal.configure(text=str(poids.total)+' Kg')
    wpartiel.configure(text=str(poids.partiel)+' Kg')
 
def simulationPoids(x):
    ""
    simul.configure(text=str(x))
    poids.simule=int(x)
 
 
 
 
 
 
#----------------Définition fenètre choix ration----------------------------
 
 
 
def choixration():
    "entrer les quantités à mettre"
 
 
 
 
 
    def stopfen2():
        fen2.destroy()
        for i in range(len(prod.nom)):
            prod.qte[i]=prod.qte[i].get()
            prod.index[i].configure(text=str(prod.qte[i]))
 
        print (prod.qte)
 
 
 
 
 
    fen2=Toplevel()
    for i in range(len(prod.nom)):
 
 
        Label(fen2,text=prod.nom[i]).grid(row=i+1,column=1)
        prod.qte[i]=IntVar()
        Entry(fen2,textvariable=prod.qte[i]).grid(row=i+1,column=2)
 
 
    Button(fen2, text="Valider", command=stopfen2).grid(row=i+2, column=2)
    fen2.mainloop()
 
 
 
 
 
 
 
 
 
#############################################################################
 
 
 
 
 
 
 
def actualiserparproduit():
    if process.step==2 : i=0
    if process.step==3 : i=1
    if process.step==4 : i=2
    prod.done[i].configure (text=str(poids.partiel))
    prod.percent[i].configure (text=str(poids.partiel))
 
 
 
 
 
# -------------Définition des étapes du remplissage--------------
 
def step(self, commande_externe=False):
 
 
 
    def allumer(text):
        commandevis(com.nom.index(text),1)
 
    def eteindre(text):
        commandevis(com.nom.index(text),0)
 
 
 
 
    if (self==1):# étape 1 allumer le moulin
        informer("allum moulin")
        tare()
 
        allumer("démarrage broyeur")
        process.conditions={'temps':4+time()}
 
    if self==2:
        informer ("remplir maïs")
        eteindre("démarrage broyeur")
        sleep (0.1)
        allumer("fonct broyeur")
        allumer("robinet maïs")
        process.conditions={'quantite':prod.qte[0]}
 
    if self==3:
        tare()
        informer("remplir soja")
        eteindre("robinet maïs")
        sleep(2)
        allumer("robinet soja")
        process.conditions={'quantite':prod.qte[1]}
 
    if self==4:
 
        tare()
        informer("remplir CMV")
        eteindre("robinet soja")
        sleep(2)
        allumer("vis CMV")
        process.conditions={'quantite':prod.qte[2]}
 
    if self==5:
        informer("mélange")
        eteindre("vis CMV")
        allumer ("mélangeur")
        process.conditions={'temps':20+time()}
 
    if self==6 :
        informer("vidange")
        allumer ("vidange")
        process.conditions={'vidange':1}
 
    if self==7:
        informer("fin")
        eteindre ("vidange")
        eteindre ("mélangeur")
        stopprocess()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#  fonction conditionnelle pour fin d'étape remplissage--------------------------------------------------------------
 
 
 
 
 
def condition (temps=time()+10000, quantite=10000, vidange=0):
    if vidange:
        if poids.partiel<=10:
            return True
        else:
            return False
 
    else:
        print(temps,"//",quantite,poids.partiel)
        if (quantite<=poids.partiel) | (temps<=time()) :
            return True
        else:
            return False
 
 
 
 
 
 
 
 
 
 
#------------Définition du thread------------------------------------------------------------------------------------
 
 
def startprocess():
    ""
    process.step=1
    step(1)
 
 
 
 
 
def stopprocess():
    ""
    process.step=0
 
 
 
 
def boucle(w,nom=''):
 
    "thread perpétuel"
    while 1:
        print ("boucle")
        actualiserpoids()
 
 
        if process.step:
            actualiserparproduit
            if condition(process.conditions):
                process.step=process.step+1
                step(process.step)
            else:
                print ("condition faux")
 
 
 
 
 
 
        sleep(0.5)
 
 
 
 
 
T=threading.Thread(None, boucle, None, (200,), {'nom':'thread a'})
 
 
 
 
 
 
 
 
 
#--------------------------------------------------------------------------------------------
#------------Instauration de la fenètre------------------------------------------------------
 
fen1=Tk()
 
#------------Cadres----------------------------------------------------------------------------
 
cadrepesee = Frame(fen1)
 
cadrepesee.grid(row=1,column=1, sticky=N,padx =15, pady =15)
cadreremplissage = Frame(fen1, bg ='#d0d0b0', bd =2)
cadreremplissage.grid(row=2,column=1,sticky=S,padx =15, pady =15)
cadresorties = Frame(fen1)
cadresorties.grid(row=1,column=2,padx =15, pady =15)
cadremenu=Frame(fen1)
cadremenu.grid(row=2,column=2,padx =15, pady =15)
 
 
 
#------------------------------ Pose des boutons de commande---------------------------------
widgetslst=[]
for i in range(len(com.nom)):
    Button(cadresorties, text=com.nom[i], command=lambda x=i: commandevis(x), font=("arial",20)).grid(row=i+1,column=1)
    l = Label(cadresorties, text='off', fg='red', font=("arial", 20))
    l.grid(row=i+1,column=2)
 
    widgetslst.append(l)
 
 
 
#------------Boutons quitter et choix ration--------------------------------------------------
 
def escape():
    "fonction de fin programme"
    stopremplissage
    T._Thread__stop()
    fen1.destroy()
 
 
Button(cadremenu, text='choisir ration', command=lambda:choixration(), font=("arial",20)).pack()
Button(cadremenu, text="quitter", command=fen1.destroy, font=("arial",20)).pack()
 
 
 
 
#------------Pesée et info--------------------------------------------------------------------------
 
Scale(cadrepesee, command=simulationPoids,orient=HORIZONTAL).grid (row=4, column=1)
simul=Label(cadrepesee,text="00", font=("arial", 15))
simul.grid(row=5, column=2)
 
info=Label(cadrepesee,text="",fg='blue', font=("arial", 20))
info.grid(row=6, column=1)
 
 
 
wtotal=Label(cadrepesee,text=poids.total,fg='red', font=("arial", 20))
wpartiel=Label(cadrepesee,text=poids.partiel,fg='red', font=("arial", 20))
wtotal.grid(row=1,column=1)
wpartiel.grid(row=1,column=2)
Button(cadrepesee, text='tare', command=lambda:tare(), font=("arial",20)).grid(row=2,column=2)
 
 
 
#------------% de Remplissage-------------------------------------------------------------------------
 
Label(cadreremplissage,text="Ingrédients",font=("arial",25)).grid(row=0,column=1)
Label(cadreremplissage,text="Qté",font=("arial",25)).grid(row=0,column=2)
Label(cadreremplissage,text="% OK",font=("arial",25)).grid(row=0,column=3)
Label(cadreremplissage,text="OK",font=("arial",25)).grid(row=0,column=4)
prod.index,prod.percent,prod.done=[],[],[]
for i in range (len(prod.nom)):
 
                Label(cadreremplissage,text=prod.nom[i],font=("arial",15)).grid(row=i+1,column=1, sticky=E)
                a=Label(cadreremplissage,text="0 kg",font=("arial",15))
                a.grid(row=i+1,column=2,padx =10, pady =15)
                prod.index.append(a)
                b=Label(cadreremplissage,text="0 %",font=("arial",15))
                b.grid(row=i+1,column=3,padx =10, pady =15)
                prod.percent.append(b)
                c=Label(cadreremplissage,text="0 Kg",font=("arial",15))
                c.grid(row=i+1,column=4,padx =10, pady =15)
                prod.done.append(c)
 
 
Button(cadreremplissage, text='démarrer !', command=lambda:startprocess(), font=("arial",20)).grid(row=i+2,column=1,pady =15)
Button(cadreremplissage, text='STOP!', command=lambda:stopprocess(), font=("arial",20)).grid(row=i+2,column=3,pady =15)
 
T.start()
fen1.mainloop()