Memorandum-Code für Raspeltorte
filename.py
import pigpio
import time
import signal
import csv
#▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ..
wfilename='wlogfile_1.csv'
rfilename='rlogfile_1.csv'
time_HI_STR_N=1520#HOHE Zeit im neutralen Zustand des lenkseitigen Eingangs[us]
time_HI_STR_MAX=2020
time_HI_STR_MIN=1020
time_HI_DRV_N=1520#Antriebsseitiger Eingang Neutralzustand HIGH-Zeit[us]
time_HI_DRV_MAX=2020
time_HI_DRV_MIN=1020
DUTY_MOT_MAX=20
frec_PWM_STR=50#Hz
frec_PWM_DRV=50#Hz
frec_logic=50#Hz
GAIN_DRV=DUTY_MOT_MAX/(time_HI_DRV_N-time_HI_DRV_MIN) #Höchste Zeit[us]× Konstante = Tastverhältnis
DRV_TH=1*10000 #Totzonenschwelle[%*10000]
#▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ ed Abstimmelement
#▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ Keine Änderung
#Einstellung der PIN-Nummer
#Zwei Hardware-PWM-kompatible Pins für den Ausgang können nicht geändert werden
pin_PWM_STR=12
pin_PWM_DRVF=13
pin_PWM_DRVR=19
#Kann für die Eingabe geändert werden
pin_IN_STR=16
pin_IN_DRV=20
#Erstellung einer Pigpio-Instanz
pi=pigpio.pi()
#output pin setup
pi.set_mode(pin_PWM_STR,pigpio.OUTPUT)
pi.set_mode(pin_PWM_DRVF,pigpio.OUTPUT)
pi.set_mode(pin_PWM_DRVR,pigpio.OUTPUT)
#input pin setup
pi.set_mode(pin_IN_STR,pigpio.INPUT)
pi.set_pull_up_down(pin_IN_STR,pigpio.PUD_DOWN)
pi.set_mode(pin_IN_DRV,pigpio.INPUT)
pi.set_pull_up_down(pin_IN_DRV,pigpio.PUD_DOWN)
HITIME_TO_OUTDUTY=frec_PWM_STR/(1000*1000)*100*10000#Höchste Zeit[us]× Konstante = Einschaltbefehlswert[%*10^4]Was
#▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ ed Festeinstellung Keine Änderung
#▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
#Eingangsleistungsberechnungsvariable
time_UP_STR=[0,0]
time_DW_STR=[0,0]
time_UP_DRV=[0,0]
time_DW_DRV=[0,0]
#Aufzeichnungsvariable für das Eingangs Tastverhältnis
duty_IN_STR=0
duty_IN_DRV=0
#Geben Sie die HIGH-Zeitaufzeichnungsvariable ein
time_HI_STR=0
time_HI_DRV=0
#Staatsverwaltung
mod_req=0
mod_state=0
MODE_INIT=0
MODE_LOGING=1
MODE_REPLAY=2
duty_OUT_STR=0
duty_OUT_DRV=0
#▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ ed Variable Einstellung
#▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
#PWM-Eingang für Lenkinterrupt-Rückruf
def callback_IN(gpio,level,tick):
    #Verfahren zur Verwendung globaler Variablen
    global time_UP_STR
    global time_DW_STR
    global duty_IN_STR
    global time_HI_STR
    global time_UP_DRV
    global time_DW_DRV
    global duty_IN_DRV
    global time_HI_DRV
    
    if gpio==pin_IN_STR:#Unterbrechung durch Lenkung
        if level == 1:#Erhebt euch
            time_UP_STR[1]=time_UP_STR[0]
            time_UP_STR[0]=tick
            duty_IN_STR=(time_DW_STR[0]-time_UP_STR[1])/(time_UP_STR[0]-time_UP_STR[1])*100
            time_HI_STR=(time_DW_STR[0]-time_UP_STR[1])
        else:#Herunterfallen
            time_DW_STR[1]=time_DW_STR[0]
            time_DW_STR[0]=tick
    else:#Unterbrechung wird nicht durch Lenken verursacht (verursacht durch Fahren)
        if level == 1:#Erhebt euch
            time_UP_DRV[1]=time_UP_DRV[0]
            time_UP_DRV[0]=tick
            duty_IN_DRV=(time_DW_DRV[0]-time_UP_DRV[1])/(time_UP_DRV[0]-time_UP_DRV[1])*100
            time_HI_DRV=(time_DW_DRV[0]-time_UP_DRV[1])
        else:#Herunterfallen
            time_DW_DRV[1]=time_DW_DRV[0]
            time_DW_DRV[0]=tick
        
    #print(time_HI_STR,time_HI_DRV,duty_IN_STR,duty_IN_DRV)
#▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ ed ed ed edal
#▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ unterbrechen
def logic_main(arg1, arg2):
    #Globales Verfahren zur Verwendung von Variablen
    global time_HI_STR#[us]
    global time_HI_DRV#[us]
    global time_HI_STRtmp#[us]
    global time_HI_DRVtmp#[us]
    global index,index_lim
    #▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
    
    duty_OUT_STR=0
    duty_OUT_DRV=0
    
    if mod_state==MODE_REPLAY:
        if index<index_lim:
            duty_OUT_STR=int(data[index][0])
            duty_OUT_DRV=int(data[index][1])
            index=index+1
        elif index==index_lim:
            print("Ende der Wiedergabe! Drücken Sie die Eingabetaste")
            index=index+1
        else:
            duty_OUT_STR=0
            duty_OUT_DRV=0
            
    else:
        #Betriebsmenge steuern
        duty_OUT_STR=int(max(min(time_HI_STRtmp*HITIME_TO_OUTDUTY,100*10000),0))#Ober- und Untergrenze und Umrechnung
        #Antriebsmenge
        duty_OUT_DRV=int(max(min((time_HI_DRVtmp-time_HI_DRV_N)*GAIN_DRV,DUTY_MOT_MAX),-DUTY_MOT_MAX)*10000)
    
    #Ausgabe
    pi.hardware_PWM(pin_PWM_STR,frec_PWM_STR,duty_OUT_STR)
    if duty_OUT_DRV>DRV_TH:#Vorwärtsrotation
        pi.hardware_PWM(pin_PWM_DRVF,frec_PWM_DRV,duty_OUT_DRV)
        pi.write(pin_PWM_DRVR,0)
        #print(duty_OUT_STR,duty_OUT_DRV,"F")
    elif duty_OUT_DRV<-DRV_TH:#Umkehrung
        pi.write(pin_PWM_DRVF,0)
        pi.hardware_PWM(pin_PWM_DRVR,frec_PWM_DRV,-duty_OUT_DRV)
        #print(duty_OUT_STR,duty_OUT_DRV,"R")
    else:#Halt
        pi.write(pin_PWM_DRVF,0)
        pi.write(pin_PWM_DRVR,0)
        #print(duty_OUT_STR,duty_OUT_DRV)
    
    if mod_state==MODE_LOGING:
        w.writerow([duty_OUT_STR,duty_OUT_DRV])
    
    #▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
#▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ unterbrechen
    
    
#▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
pi.callback(pin_IN_STR,pigpio.EITHER_EDGE,callback_IN)#Seite lenken
pi.callback(pin_IN_DRV,pigpio.EITHER_EDGE,callback_IN)#Fahrseite
signal.signal(signal.SIGALRM,logic_main)#Ausführung und Einstellung der Hauptlogik (Timer-Interrupt), wenn ein Signal kommt
signal.setitimer(signal.ITIMER_REAL,0.1,0.02)#Timer-Interrupt mit 50-Hz-Signal
#▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ ed Interrupt-Einstellung
#▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
time_HI_STRtmp=5000
time_HI_DRVtmp=5000
while 1:
    print('1:Protokollierung 2:Wiedergabe 3:Ende')
    try:
        tmp_req=input('> ')
    except KeyboardInterrupt:
        print("stop!")
        break
    
    try:
        mod_req=int(tmp_req)
    except:
        print("miss")
        break
#Start
    if mod_req==MODE_LOGING:
        file=open(wfilename,'w')
        w=csv.writer(file)
        mod_state=MODE_LOGING
        print("Protokollierung...")
        print("Drücken Sie die Eingabetaste, um anzuhalten")
    elif mod_req==MODE_REPLAY:
        file=open(rfilename,'r')
        r=csv.reader(file)
        data=[row for row in r]
        index=0
        index_lim=len(data)
        mod_state=MODE_REPLAY
        print("Spielen...")
    else:
        print("END!")
        break
    
#Ende
    try:
        input('> ')
    except KeyboardInterrupt:
        print("stop!")
        break
   
    if mod_req==MODE_LOGING:
        mod_state=MODE_INIT
        mod_req=MODE_INIT
        file.close()
    elif mod_req==MODE_REPLAY:
        mod_state=MODE_INIT
        mod_req=MODE_INIT
        file.close()
    else:
        print("END!")
        break
    
time.sleep(0.1)
#▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
pi.set_mode(pin_PWM_STR,pigpio.INPUT)
pi.set_mode(pin_PWM_DRVF,pigpio.INPUT)
pi.set_mode(pin_PWM_DRVR,pigpio.INPUT)
pi.stop()
signal.setitimer(signal.ITIMER_REAL,0)
#▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ ed Termination Verfahren
Recommended Posts