Open and close hand in variable speed with data


I have a hand simulator running at a constant speed. Hand opens and closes according to EMG data from USB connection. I added the project running at constant speed below. I have to make this simulator running at variable speed depending on EMG amplitude. I have the mathematical model of the hand in my hand. The Input is EMG amplitude and Output is hand speed. The model that generates output speed depending on input EMG is:

Output(k) = 0.6Input(k) + 2Output(k-1) - Output(k-2) So the formula will be in my code like that:

Velocity(k) = 0.6emg(k) + 2Velocity(k-1) - Velocity(k-2)

I’ve thought adding this model to script. I generated the code as below but it did not work. I think Blender runs scripts over and over so my variables do not store.What do you think? I would be very happy if I could get suggestions from you.

import bge
cont = bge.logic.getCurrentController()
own = cont.owner

if cont.sensors['action_true_prp'].positive:
    own['action'] = 0
        #emg_1, emg_2 = bge.logic.line.replace(',','.').strip('\n').split(' ')
        emg_1, emg_2 = bge.logic.line.replace(',','.').strip('\n').split('\t')
        own['ch1'] = float(emg_1)
        own['ch2'] = float(emg_2)
        own['velocity'] = 0
        own['velo_before'] = 0
        own['velo_2before'] = 0

        print("error 003: parse error")
        print (bge.logic.line)  
        # CH1 ACTION control
        if (own['ch1']>bge.logic.globalDict['ch1_emg_threshold']):
            own['velocity'] = 0.6*own['ch1'] + 2*own['velo_before'] - own['velo_2before'];  
            if (own['time1']<190):
                own['time1'] = own['time1'] + own['velocity']
                if (own['time1']>190):
                    own['time1'] = 190

        # CH2 ACTION control
        if (own['ch2']>bge.logic.globalDict['ch2_emg_threshold']):
            own['velocity'] = 0.6*own['ch2'] + 2*own['velo_before'] - own['velo_2before']; 
            if (own['time1']>100):
                own['time1'] = own['time1'] + own['velocity']
                if (own['time1']<100):
                    own['time1'] = 100
        print ("CH_0:%7.4f  CH_1:%7.4f  STEP:%+3d  TIME:%+4.3f second"% (own['ch1'], own['ch2'], own['velocity'], bge.logic.delay_t))
        print ("error 008: undefined error")
        scene = bge.logic.getCurrentScene()
        Finger1 = scene.objects['Finger1E']
        Finger2 = scene.objects['Finger2E']
        Finger3 = scene.objects['Finger3E']
        Finger4 = scene.objects['Finger4E']
        Finger5 = scene.objects['Finger5E']

        Finger1['Finger-Prop'] = own['time1']
        Finger2['Finger-Prop'] = own['time1']   
        Finger3['Finger-Prop'] = own['time1']
        Finger4['Finger-Prop'] = own['time1'] 
        Finger5['Finger-Prop'] = own['time1']
        print ("error 004: undefined error")