Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DIM SHARED AS BYTE    stayZ =0, stayXY 'boolean
  2. DIM SHARED AS DOUBLE  XUserInput=0, YUserInput=0, ZUserInput=0
  3. CONST      AS DOUBLE  rotKp=30, rotKi=0, rotKd=6
  4. CONST      AS DOUBLE  posKp=30, posKi=0, posKd=4
  5. DIM SHARED AS DOUBLE  targetRoll =0 , rollError =0, rollPreviousError =0, rollIntegral =0, rollDerivative =0, rollOutput =0
  6. DIM SHARED AS DOUBLE  targetPitch=0 , pitchError=0, pitchPreviousError=0, pitchIntegral=0, pitchDerivative=0, pitchOutput=0
  7. DIM SHARED AS DOUBLE  targetyaw  =0 , yawError  =0, yawPreviousError  =0, yawIntegral  =0, yawDerivative  =0, yawOutput  =0
  8. DIM SHARED AS DOUBLE  targetX    =0 , xError    =0, xPreviousError    =0, xIntegral    =0, xDerivative    =0, xOutput    =0
  9. DIM SHARED AS DOUBLE  targetY    =0 , yError    =0, yPreviousError    =0, yIntegral    =0, yDerivative    =0, yOutput    =0
  10. DIM SHARED AS DOUBLE  targetZ    =20, zError    =0, zPreviousError    =0, zIntegral    =0, zDerivative    =0, zOutput    =0
  11. DIM SHARED AS INTEGER funcoutput(0 TO 3) = {0,0,0,0}
  12. SUB PID(funcouput() AS INTEGER,_
  13.     QuadcopterX     AS DOUBLE=1/3, QuadcopterY    AS DOUBLE=1/3, QuadcopterZ   AS DOUBLE=1/3,_'position
  14.     QuadcopterPitch AS DOUBLE=1/3, QuadcopterRoll AS DOUBLE=1/3, QuadcopterYaw AS DOUBLE=1/3,_'rotation in °
  15.     XUserInput      AS DOUBLE=0  , YUserInput     AS DOUBLE=0  , ZUserInput    AS DOUBLE=0)  _'input: -1 to 1
  16.    
  17.     IF QuadcopterPitch=1/3 THEN QuadcopterX=targetPitch
  18.     IF QuadcopterRoll =1/3 THEN QuadcopterX=targetRoll
  19.     IF QuadcopterYaw  =1/3 THEN QuadcopterX=targetYaw
  20.     IF QuadcopterX    =1/3 THEN QuadcopterX=targetX
  21.     IF QuadcopterY    =1/3 THEN QuadcopterX=targetY
  22.     IF QuadcopterZ    =1/3 THEN QuadcopterX=targetZ
  23.     IF LBOUND(funcouput)>0 OR LBOUND(funcouput)<4 THEN PRINT"ERROR in func PID: array too small or out of bounds" : EXIT SUB
  24.    
  25.     rollError = (targetRoll - QuadcopterRoll)/360 'normalize: 360° error => 1
  26.     rollIntegral = rollIntegral + rollError*0.0001 '--------------\
  27.     rollDerivative = (rollError - rollPreviousError)/0.0001 'make it relative to the average deltaT in seconds
  28.     rollOutput = (rotKp*rollError + rotKi*rollIntegral + rotKd*rollDerivative)
  29.     rollPreviousError = rollError
  30.    
  31.     pitchError = (targetPitch - QuadcopterPitch)/360
  32.     pitchIntegral = pitchIntegral + pitchError*0.0001
  33.     pitchDerivative = (pitchError - pitchPreviousError)/0.0001
  34.     pitchOutput = (rotKp*pitchError + rotKi*pitchIntegral + rotKd*pitchDerivative)
  35.     pitchPreviousError = pitchError
  36.    
  37.     yawError = (targetyaw - QuadcopterYaw)/360
  38.     yawIntegral = yawIntegral + yawError*0.0001
  39.     yawDerivative = (yawError - yawPreviousError)/0.0001
  40.     yawOutput = (rotKp*yawError + rotKi*yawIntegral + rotKd*yawDerivative)
  41.     yawPreviousError = yawError
  42.    
  43.     xError = (targetX - QuadcopterX)/1000 'normalize...
  44.     xIntegral = xIntegral + xError/0.0001
  45.     xDerivative = (xError - xPreviousError)/0.0001
  46.     xOutput = (posKp*xError + posKi*xIntegral + posKd*xDerivative)
  47.     xPreviousError = xError
  48.    
  49.     yError = (targetY - QuadcopterY)/1000
  50.     yIntegral = yIntegral + yError/0.0001
  51.     yDerivative = (yError - yPreviousError)/0.0001
  52.     yOutput = (posKp*yError + posKi*yIntegral + posKd*yDerivative)
  53.     yPreviousError = yError
  54.    
  55.     zError = ((targetZ+40*ZUserInput+0) - QuadcopterZ)/1000
  56.     zIntegral = zIntegral + zError/0.0001
  57.     zDerivative = (zError - zPreviousError)/0.0001
  58.     zOutput = (posKp*zError + posKi*zIntegral + posKd*zDerivative)
  59.     zPreviousError = zError
  60.     '***debug***
  61.     'print(posKp*zError & " " & posKd*zDerivative & " " & zOutput & " " & QuadcopterZ & "  " & targetZ)
  62.     'print(rollOutput & "  " & pitchOutput & "  " & yawOutput & "  " & xOutput & "  " & yOutput & "  " & zOutput & "  " & staypos)
  63.     'print(QuadcopterZ & "   " & zOutput & "   " & ZAccel & "   " & ZVelo & "  " & QuadcopterRoll & "  " & QuadcopterPitch)
  64.     '***debug***
  65.     targetPitch=6*XUserInput
  66.     targetRoll=6*YUserInput
  67.     IF NOT stayZ THEN targetZ=QuadcopterZ
  68.    
  69.     funcoutput(0) = INT(50+  rollOutput +  pitchOutput +  yawOutput + (-xOutput+ yOutput)*stayXY +3*zOutput)
  70.     funcoutput(1) = INT(50+ -rollOutput +  pitchOutput + -yawOutput + (-xOutput+-yOutput)*stayXY +3*zOutput)
  71.     funcoutput(2) = INT(50+  rollOutput + -pitchOutput + -yawOutput + ( xOutput+ yOutput)*stayXY +3*zOutput)
  72.     funcoutput(3) = INT(50+ -rollOutput + -pitchOutput +  yawOutput + ( xOutput+-yOutput)*stayXY +3*zOutput)
  73. END SUB
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement