BigBot/SmoothValue.cs

61 lines
1.0 KiB
C#

using System;
using sharp.json.attributes;
namespace BigBot
{
[JSONClassPolicy( Policy = JSONPolicy.ATTRIBUTED)]
public class SmoothValue
{
[JSONField(Alias = "last")]
public double LastValue { get; private set; }
[JSONField( Alias = "current")]
public double CurrentValue { get; private set; }
[JSONField(Alias = "Kp")]
public double Kp { get; set; }
[JSONField(Alias = "sum")]
private double sum;
public SmoothValue()
{
}
public SmoothValue(double Kp)
{
this.Kp = Kp;
}
public void Clear()
{
this.sum = 0;
}
public void Set(double value){
this.sum = (value / this.Kp) - value;
this.CurrentValue = value;
this.LastValue = value;
}
public void Add(double value)
{
LastValue = CurrentValue;
this.sum += value;
CurrentValue = this.sum * this.Kp;
this.sum -= CurrentValue;
}
public double dT {
get {
return this.CurrentValue - this.LastValue;
}
}
public static implicit operator double(SmoothValue svalue){
return svalue.CurrentValue;
}
}
}