package org.vishia.exampleJava2C.java4c;


/**This class provides a simple PID-Controller generateable to C with 16-bit arithmetic and 32-bit-integrator.
 * 
 * @author JcHartmut
 *
 */
public class PID_controller
{
  /**The integral value of the controller. */
  int intVal = 0;
  
  /**The last value to build the difference. */
  short lastForDiff;
  
  /**The I amplification, unit per sample step. Range -1.00000 to 0.99998 coded with 0x8000...0x7FFF*/
  short kI = 0x4000;
  
  /**The P amplification. Range -32.768 to 32.767 coded with 0x8000...0x7FFF. The value 1.0 is 0x0400. */
  short kP = 0x400;
  
  /**The D amplification. Range -32.768 to 32.767 coded with 0x8000...0x7FFF.  The value 1.0 is 0x0400. */
  float kD = 0;
  
  public short process(short input)
  {
    intVal += (input * kI);
    
    return (short)( (intVal >> 16) + input);  
  }
  
  /**Gets the value of the internal integrator for displayment. The value is shown in float with position after decimal
    point
   * as low value. The 1.0-unit is equal to 1 mV output value while controlling.
   * @return
   */ 
  public float getIntg()  { return intVal/65536.0F; }
  
}