from class_4d import xd
import mem_lista as ml
from class_master import master,uscita_exp,ingresso_exp
from machine import Pin, Timer, WDT, mem32
from class_timer import tm
import time
from class_PID import PID
from class_scale import scale
from class_fall import fall
from class_contaimpulsi import contaimpulsi
from filtro_analogico import af
import _thread
#from umodbus.serial import Serial as ModbusRTUMaster
import gc
gc.enable()
time.sleep(5)
'''
#ModbusRTU
rtu_pins = (Pin(4), Pin(5)) # (TX, RX)
uart_id = 1
host = ModbusRTUMaster(pins=rtu_pins, uart_id=uart_id)
'''
#temporizzatori
t_olio=tm(10000)
t_start_kg=tm(2000)
t_modbus=tm(100)
t_modbus.start()
t_scr_file=tm(60000)
t_scr_file.start()
#display
vmd=ml.read('vmd') #arrey Variabili Modificabili da Display
dsp=xd(vmd,0,1,0) #lista,n°uart,rx pin,tx pin
dsp.w_leddigits() #scrive tutta la lista sul display
#pid scalature filtro analogico
pid=PID(kP=vmd[7],kI=vmd[8],kD=1,tI=1000,tD=0,bmin=vmd[10],bmax=vmd[9],outmax=10000,outmin=0,reverse=True)
pid.I_value=vmd[13]
pid_in=af(27,20,10000,-50) #n°pin,n°campioni,valore max di scalatura,offset
kg_present=scale(200,0,10000,0,0) #scalatura analogica a kg (max out, min out, max in, min in, offset)
kg_set=scale(10000,0,200,0,0) #scalatura kg per set pid
#ingressi master
s_metri=fall(21,'PULL_DOWN') #differenziale ingresso
guasto_inv=Pin(20,Pin.IN,Pin.PULL_DOWN)
p_jog=Pin(19,Pin.IN,Pin.PULL_DOWN)
p_stop=Pin(18,Pin.IN,Pin.PULL_DOWN)
p_start=Pin(17,Pin.IN,Pin.PULL_DOWN)
sel_start=Pin(16,Pin.IN,Pin.PULL_DOWN)
sel_freno=Pin(15,Pin.IN,Pin.PULL_DOWN)
v_bus=Pin(24,Pin.IN)
#ingressi slave e init slave
sm=master(0) #avvio state machine spi master-slave
ordito=ingresso_exp(3)
trama=ingresso_exp(4)
carter=ingresso_exp(5) #contatto chiuso a coperchio chiuso
apporto=ingresso_exp(28)
pres_olio=ingresso_exp(27) #contatto chiuso in pressione
antiavvolgimento=ingresso_exp(26) #contatto chiuso in riposo
start_avv_sx=ingresso_exp(22)
stop_avv_sx=ingresso_exp(21)
led_slave_in=ingresso_exp(25)
#uscite master
led=Pin(25,Pin.OUT)
led.off()
mot_telaio=Pin(7,Pin.OUT)
mot_telaio.off()
mot_tiro=Pin(8,Pin.OUT)
mot_tiro.off()
mot_tamb=Pin(9,Pin.OUT)
mot_tamb.off()
marcia_lenta=Pin(10,Pin.OUT)
freno=Pin(11,Pin.OUT)
luce=Pin(12,Pin.OUT)
mot_avv_sx=Pin(13,Pin.OUT)
#uscite slave
led_slave=uscita_exp(25)
#variabili
k_mt=vmd[4]/1000
present_mt=vmd[0]
messaggio=0 #n°stringa per scrivere su display
frequenza_tiro=0
frequenza_tamburo=vmd[13]
mem_frequenza_tamburo=frequenza_tamburo
now_ms=0
analogica_filtrata=0
def display():
ultimo_messaggio=0
#ultima_trasmissione=True
schedulatore=1
schedulatore_modbus=1
global frequenza_tiro
global frequenza_tamburo
global messaggio
global present_mt
while True:
gc.collect()
if dsp.uart.any() > 0:
time.sleep(0.1)
dsp.r_dsp() #deve essere nel ciclo while del programma
if dsp.dato_modificato: #flag per scrivere file
ml.write(vmd,'vmd') #scrive lista su file
if dsp.n_ogg==0:
present_mt=vmd[0]
pid.kp=vmd[7]
pid.kI=vmd[8]
pid.bmin=vmd[10]*-1
pid.bmax=vmd[9]
dsp.dato_modificato=False
if not dsp.input_format: # and ultima_trasmissione:
if not dsp.uart_busy:
if schedulatore==1:
dsp.w_dsp(vmd[11],dsp.leddigits,11)
if schedulatore==2:
dsp.w_dsp(vmd[0],dsp.leddigits,0)
if schedulatore==3:
dsp.w_dsp(vmd[5],dsp.leddigits,5)
if schedulatore==4:
dsp.w_dsp(pid.P_value,dsp.leddigits,12)
if schedulatore==5:
dsp.w_dsp(pid.I_value,dsp.leddigits,13)
if schedulatore==6:
dsp.w_dsp(pid.out,dsp.leddigits,14)
if schedulatore==7:
if ultimo_messaggio != messaggio:
if messaggio!=8:
dsp.w_dsp(messaggio,dsp.string,0)
elif messaggio==8:
dsp.w_string(0,'AVV.NASTRO')
ultimo_messaggio=messaggio
if schedulatore==8:
dsp.w_dsp(vmd[11],dsp.igauge,0)
ultima_trasmissione=False
schedulatore=0
schedulatore+=1
#if mot_telaio.value() or mot_tiro.value():
#ultima_trasmissione=True
if t_modbus.check():
if schedulatore_modbus==1:
try:
host.write_single_register(slave_addr=1, register_address=0x02, register_value=frequenza_tiro, signed= True)
except:
pass
if schedulatore_modbus==2:
try:
host.write_single_register(slave_addr=2, register_address=0x02, register_value=frequenza_tamburo, signed= True)
except:
pass
schedulatore_modbus=0
schedulatore_modbus+=1
t_modbus.restart()
_thread.start_new_thread(display,())
def contabattute(_):
bpm.reset()
global vmd
global now_ms
tempo=time.ticks_ms()-now_ms
vmd[5]=int(60000/tempo*200)
now_ms=time.ticks_ms()
bpm=contaimpulsi(1,2000,22,200,contabattute) #n°sm, frequenza sm, pin ingresso, n°impulsi, funzione interrupt
def scrittura_file():
ml.write(vmd,'vmd')
def telaio_run():
mot_telaio.on()
mot_tiro.on()
mot_tamb.on()
freno.off()
luce.off()
pid.I_on=True
marcia_lenta.off()
def telaio_stop():
mot_telaio.off()
mot_tiro.off()
mot_tamb.off()
freno.on()
luce.on()
pid.I_on=False
marcia_lenta.off()
def slave():
if led.value():
if not led_slave_in.value():
telaio_stop()
else:
if led_slave_in.value():
telaio_stop()
led.toggle()
if led.value():
led_slave.on()
else:
led_slave.off()
sm.rw_exp() #aggiorno lettura-scrittura slave
def start_mode():
global messaggio
global frequenza_tiro
frequenza_tiro=vmd[2]
if p_start.value() and carter.value() and guasto_inv.value() and not dsp.input_format:
telaio_run()
messaggio=0
if not p_stop.value():
telaio_stop()
if dsp.input_format:
telaio_stop()
if not p_start.value() and mot_telaio.value():
if trama.value():
telaio_stop()
messaggio=2
if ordito.value():
telaio_stop()
messaggio=3
if apporto.value():
telaio_stop()
messaggio=4
if not carter.value():
telaio_stop()
messaggio=5
if not antiavvolgimento.value():
telaio_stop()
messaggio=8
if not guasto_inv.value():
telaio_stop()
messaggio=7
if not pres_olio.value():
t_olio.start()
if t_olio.check():
telaio_stop()
messaggio=6
else:
t_olio.reset()
def jog_mode():
global messaggio
global frequenza_tiro
frequenza_tiro=vmd[3]
mot_telaio.off()
freno.on()
luce.on()
pid.I_on=False
if p_jog.value():
time.sleep_ms(30)
if p_jog.value():
marcia_lenta.on()
mot_tiro.on()
messaggio=0
if kg_present.out(pid_in.value())>(vmd[6]+5):
mot_tamb.on()
else:
mot_tamb.off()
else:
marcia_lenta.off()
mot_tamb.off()
mot_tiro.off()
def pid_tamburo():
global frequenza_tamburo
global mem_frequenza_tamburo
analogica_filtrata=pid_in.value2()
vmd[11]=kg_present.out(analogica_filtrata)
if mot_telaio.value():
frequenza_tamburo=(pid.outpid((kg_set.out(vmd[6])),analogica_filtrata))
vmd[13]=pid.I_value
mem_frequenza_tamburo=frequenza_tamburo
if vmd[11]>(vmd[6]+50) or vmd[11]<(vmd[6]-50):
t_start_kg.start()
if t_start_kg.check():
telaio_stop()
else:
t_start_kg.reset()
else:
t_start_kg.reset()
frequenza_tamburo=mem_frequenza_tamburo//10
def avvolgitore():
if mot_telaio.value():
if start_avv_sx.value():
mot_avv_sx.on()
if stop_avv_sx.value():
mot_avv_sx.off()
else:
mot_avv_sx.off()
def contametri():
global messaggio
global present_mt
if s_metri.up():
present_mt+=k_mt
vmd[0]=int(present_mt)
if present_mt>=vmd[1]:
telaio_stop()
messaggio=1
if not v_bus:
wdt=WDT(timeout=100)
while True:
if not v_bus:
wdt.feed()
slave()
if sel_start.value():
start_mode()
if sel_freno.value():
freno.off()
if mot_telaio.value():
telaio_stop()
if not sel_freno.value() and not sel_start.value():
jog_mode()
pid_tamburo()
avvolgitore()
contametri()
if t_scr_file.check():
scrittura_file()
t_scr_file.restart()