Advertisement
agmike

Lse.Std.KinematicNeedle

Sep 25th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. class LseStdKinematicNeedle isclass LseDevice
  2. {
  3.     public string ControlName;
  4.     public float ValueMin, ValueMax;
  5.    
  6.     public float W0 = 1.0;
  7.     public float Y = 1.0;
  8.     public float Rk = 0.0;
  9.     public float Rb = 0.0;
  10.     public float E = 1.0;
  11.    
  12.     public float Value = 0.0;
  13.    
  14.     public float KinematicValue = 0.0;
  15.     public float Velocity = 0.0;
  16.  
  17.     int controlId = -1;
  18.  
  19.     public void Init(LseDeviceManager mgr, LseLocomotive loco)
  20.     {
  21.         inherited(mgr, loco);
  22.  
  23.         mgr.SetInvokeMethod(me, LseDeviceUpdate.InvokePerFrame);
  24.  
  25.         controlId = loco.GetControlId(ControlName);
  26.     }
  27.    
  28.    
  29.     float RK4Accel(float kinValue, float v)
  30.     {
  31.         float x = kinValue - Value;
  32.        
  33.         return -(W0*W0) * x - 2.0 * Y * v - LlMath.Sign(v) * (Rk * v * v + Rb);
  34.     }
  35.    
  36.     float[] rk4dx = new float[4];
  37.     float[] rk4dv = new float[4];
  38.    
  39.     void RK4Eval(float dt, int step)
  40.     {
  41.         float x = KinematicValue;
  42.         float v = Velocity;
  43.        
  44.         if (step > 0)
  45.         {
  46.             int prevStep = step - 1;
  47.             x = x + rk4dx[prevStep] * dt;
  48.             v = v + rk4dv[prevStep] * dt;
  49.         }
  50.    
  51.         rk4dx[step] = v;
  52.         rk4dv[step] = RK4Accel(x, v);
  53.     }
  54.    
  55.     float RK4Sample(float[] step)
  56.     {
  57.         return 1.0 / 6.0 * (step[0] + 2.0 * (step[1] + step[2]) + step[3]);
  58.     }
  59.    
  60.     void RK4Integrate(float dt)
  61.     {
  62.         int step = 0;
  63.         RK4Eval(0.0, step++);
  64.         RK4Eval(dt * 0.5, step++);
  65.         RK4Eval(dt * 0.5, step++);
  66.         RK4Eval(dt, step++);
  67.        
  68.         float dx = RK4Sample(rk4dx);
  69.         float dv = RK4Sample(rk4dv);
  70.        
  71.         KinematicValue = KinematicValue + dx * dt;
  72.         Velocity = Velocity + dv * dt;
  73.     }
  74.  
  75.     public void Update(LseDeviceUpdate u)
  76.     {
  77.         RK4Integrate(u.dt);
  78.        
  79.         if (KinematicValue > maxValue)
  80.         {
  81.             KinematicValue = maxValue;
  82.             Velocity = -Velocity * E;
  83.         }
  84.         else if (KinematicValue < minValue)
  85.         {
  86.             KinematicValue = minValue;
  87.             Velocity = -Velocity * E;
  88.         }
  89.        
  90.         Loco.GetControlState(controlId).Value = KinematicValue;
  91.         Loco.UpdateControlState(controlId);
  92.     }
  93.  
  94.     public LseStdNeedle Clone()
  95.     {
  96.         LseStdNeedle ret = new LseStdNeedle();
  97.  
  98.         ret.ControlName = ControlName;
  99.         ret.ValueMin = ValueMin;
  100.         ret.ValueMax = ValueMax;
  101.         ret.W0 = W0;
  102.         ret.Y  = Y
  103.         ret.Rk = Rk
  104.         ret.Rb = Rb
  105.         ret.E  = E
  106.  
  107.         return ret;
  108.     }
  109.    
  110.     public LseDevice CloneDevice() { return cast<LseDevice> Clone(); }
  111. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement