Not a member yet? Why not Sign up today
Create an account  

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 
[2.17] LUA quit working

#1
I was writing my three stage PID
(Altitute-->VerticalVelocity--->Pitch--><<PitchStimuli>> basically)
when LUA Box decided to start doing nothing:
-No control stimuli
-No LogToHud messages
-No errors thrown (even when deliberately introduced)
Blueprint attached
Here is WIP code it is two stage ATM:
Code:
VVKp=3;
VVKi=0;
VVKd=0;
VVSP=15;

pitchKp=1;
pitchKi=0.10;
pitchKd=2;
pitchSP=7;

VVPrevError=0;
VVIntegral=0;

pitchPrevError=0;
pitchIntegral=0;
prevTime=I:GetTime();
function Update(I)
  time=I:GetTime();
  dT=time-prevTime;
  prevTime=time;
  
  if pitchSP>30 then
    pitchSP=30;
  elseif pitchSP<-20 then
    pitchSP=-20;
  end
  VVError=VVSP-I:GetVelocityVector().y;
  VVDerivative=(VVError-VVPrevError)/dT;
  VVIntegral=VVIntegral+VVError*dT;
  VVOut=VVError*VVKp+VVIntegral*VVKi+VVDerivative*VVKd;
  VVPrevError=VVError;
  if VVError>0.2 or VVError<-0.2 then
    VVIntegral=0;
  end

  pitchSP=VVOut;
  if pitchSP>30 then
    pitchSP=30;
  elseif pitchSP<-20 then
    pitchSP=-20;
  end
  pitchError=pitchSP+I:GetConstructPitch();
  pitchDerivative=(pitchError-pitchPrevError)/dT;
  pitchIntegral=pitchIntegral+pitchError*dT;
  pitchOut=pitchError*pitchKp+pitchIntegral*pitchKi+pitchDerivative*pitchKd;
  pitchPrevError=pitchError;
  if pitchError>0.2 or pitchError<-0.2 then
    pitchIntegral=0;
  end
  
  I:LogToHud("VV:"..VVOut);

if pitchOut>0 then
    I:RequestControl(2,4,pitchOut);
  else
    I:RequestControl(2,5,-pitchOut);
  end


end
Reply

#2
Update.
I suppose it was caused by call to I:GetTime() outside of Update(I)
Reply

#3
(2018-04-18, 07:57 PM)Fernir Wrote: Update.
I suppose it was caused by call to I:GetTime() outside of Update(I)

So, is it fixed?
Reply

#4
There is still problem with lack of debug information. I wouldn't know it was running previous version if I hadn't tried to log to hud something. Therefore there are two problems:
1)lack of information on certain types of errors(bug IMO)
2)lack of function that would be called on vessel entering simulation (missing feature I will suggest when I get home)
Reply

#5
(2018-04-19, 10:00 AM)Fernir Wrote: There is still problem with lack of debug information. I wouldn't know it was running previous version if I hadn't tried to log to hud something. Therefore there are two problems:
1)lack of information on certain types of errors(bug IMO)
2)lack of function that would be called on vessel entering simulation (missing feature I will suggest when I get home)

This happens even in functions where I passed I as an argument
commented lines were giving causing bug and vehicle to be moved to void (C menu distance NaNm)
Code:
function Update(I)
PID={lastCall=0,Kp=0,Ki=0,Kd=0,lastError=0,integral=0,setPoint=0}
function createPID(Kp,Ki,Kd,setPoint,I)
--PID.lastCall=I:GetTime()
PID.Kp=Kp
PID.Ki=Ki
PID.Kd=Kd
PID.setPoint=setPoint
return PID
end
function updatePID(procesVariable,PID,I)
--dT=I:GetTime()-PID.lastCall
dT=1/40
error=procesVariable-PID.setPoint
derivative=(error-PID.lastError)/dT
PID.lastError=error
PID.integral=PID.integral+dT*error
return error*PID.Kp+PID.integral*PID.Ki+derivative*PID.Kd
end
init=true
function Update(I)
if init==true then
  PitchPID=createPID(1,0,0,2,I)
  init=false
end
pitchControl=updatePID(I:GetConstructPitch(),PitchPID,I)
I:LogToHud(pitchControl)  
if pitchControl>0 then
  I:RequestControl(2,4,pitchControl)
else
  I:RequestControl(2,5,-pitchControl)
end
end
end
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)