forked from Summerfirefly/ASW
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPIDSystem.cs
85 lines (76 loc) · 2.14 KB
/
PIDSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using UnityEngine;
namespace AntiSubmarineWeapon
{
public class PIDData
{
private float Lasterror;
public float lasterror
{
get { return Lasterror; }
set { Lasterror = value; }
}
private float Integral;
public float integral
{
get { return Integral; }
set { Integral = value; }
}
private float Derivative;
public float derivative
{
get { return Derivative; }
set { Derivative = value; }
}
private float Error;
public float error
{
get { return Error; }
set { Error = value; }
}
private float MaxOut;
public float maxOut
{
get { return MaxOut; }
set { MaxOut = value; }
}
private float K;
public float k
{
get { return K; }
set { K = value; }
}
private float Kintegral;
public float kIntegral
{
get { return Kintegral; }
set { Kintegral = value; }
}
private float KDerivative;
public float kDerivative
{
get { return KDerivative; }
set { KDerivative = value; }
}
}
public class PIDSystem
{
PIDData Data = new PIDData();
public float PIDControl(float target, float present, float deltaTime)
{
Data.error = target - present;
Data.integral = Data.integral + Data.error * deltaTime;
Data.derivative = (Data.error - Data.lasterror) / deltaTime;
float outPut = Data.k * Data.error + Data.kIntegral * Data.integral + Data.kDerivative * Data.derivative;
Data.lasterror = Data.error;
return Mathf.Clamp(outPut, -Data.maxOut, Data.maxOut);
}
public void PIDDataWrite(float maxOut, float k, float kIntergral, float kDerivative)
{
Data.maxOut = maxOut;
Data.k = k;
Data.kIntegral = kIntergral;
Data.kDerivative = kDerivative;
}
}
}