forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPIDControllerFragment.cs
More file actions
195 lines (174 loc) · 10.1 KB
/
Copy pathPIDControllerFragment.cs
File metadata and controls
195 lines (174 loc) · 10.1 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using RemoteTech.FlightComputer;
using RemoteTech.FlightComputer.Commands;
using System;
using System.Collections;
using UnityEngine;
using KSP.Localization;
namespace RemoteTech.UI
{
public class PIDControllerFragment : IFragment
{
private FlightComputer.FlightComputer mFlightComputer;
private Action mOnClickQueue;
private string kp = "0",
ki = "0",
kd = "0";
public PIDControllerFragment(FlightComputer.FlightComputer fc, Action queue)
{
mFlightComputer = fc;
mOnClickQueue = queue;
LoadFlightPIDValues();
}
public void Draw()
{
float width3 = 156 / 3 - GUI.skin.button.margin.right * 2.0f / 3.0f;
GUILayout.BeginVertical();
{
Vector3 Torque = SteeringHelper.TorqueAvailable;
var MoI = mFlightComputer.Vessel.MOI;
////////////////
//PITCH INFO
////////////////
GUILayout.Label(new GUIContent("<b>" + Localizer.Format("#RT_PIDControllerFragment_Pitch") + "</b>"));//"Pitch"
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_TorqueMoI"), Localizer.Format("#RT_PIDControllerFragment_TorqueMoI_desc")));//"Torque-MoI Rate: ", "Current rate of torque to mass of inertia"
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent((Torque[0] / MoI[0]).ToString("F3")));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_DeviationError"), Localizer.Format("#RT_PIDControllerFragment_DeviationError_desc")));//"Deviation Error: ", "Deviation from the target point"
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent(mFlightComputer.PIDController.getDeviationErrors()[0].ToString("F2")));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_Output"), Localizer.Format("#RT_PIDControllerFragment_Output_desc")));//"Output: ", "Output of Flight Control State"
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent(mFlightComputer.Vessel.ctrlState.pitch.ToString("F2")));
}
GUILayout.EndHorizontal();
////////////////
//ROLL INFO
////////////////
GUILayout.Label(new GUIContent("<b>" + Localizer.Format("#RT_PIDControllerFragment_Roll") + "</b>"));//"Roll"
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_TorqueMoI"), Localizer.Format("#RT_PIDControllerFragment_TorqueMoI_desc")));//"Torque-MoI Rate: ", "Current rate of torque to mass of inertia"
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent((Torque[1] / MoI[1]).ToString("F3")));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_DeviationError"), Localizer.Format("#RT_PIDControllerFragment_DeviationError_desc")));//"Deviation Error: ", "Deviation from the target point"
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent(mFlightComputer.PIDController.getDeviationErrors()[1].ToString("F2")));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_Output"), Localizer.Format("#RT_PIDControllerFragment_Output_desc")));//"Output: ", "Output of Flight Control State"
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent(mFlightComputer.Vessel.ctrlState.roll.ToString("F2")));
}
GUILayout.EndHorizontal();
////////////////
//YAW INFO
////////////////
GUILayout.Label(new GUIContent("<b>" + Localizer.Format("#RT_PIDControllerFragment_Yaw") + "</b>"));//"Yaw"
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_TorqueMoI"), Localizer.Format("#RT_PIDControllerFragment_TorqueMoI_desc")));//"Torque-MoI Rate: ", "Current rate of torque to mass of inertia"
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent((Torque[2] / MoI[2]).ToString("F3")));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_DeviationError"), Localizer.Format("#RT_PIDControllerFragment_DeviationError_desc")));//"Deviation Error: ", "Deviation from the target point"
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent(mFlightComputer.PIDController.getDeviationErrors()[2].ToString("F2")));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_Output"), Localizer.Format("#RT_PIDControllerFragment_Output_desc")));//"Output: ", "Output of Flight Control State"
GUILayout.FlexibleSpace();
GUILayout.Label(new GUIContent(mFlightComputer.Vessel.ctrlState.yaw.ToString("F2")));
}
GUILayout.EndHorizontal();
GUILayout.Space(10);
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_PIDHelp")));//"See ni.com/white-paper/3782/en"
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_Kp"), Localizer.Format("#RT_PIDControllerFragment_Kp_desc")));//"Proportional gain", "(1) With I and D terms set to 0, increase until the output of the loop oscillates."
RTUtil.TextField(ref kp, GUILayout.Width(50), GUILayout.ExpandWidth(false));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_Ki"), Localizer.Format("#RT_PIDControllerFragment_Ki_desc")));//"Integral", "(2) Increase to stop the oscillations."
RTUtil.TextField(ref ki, GUILayout.Width(50), GUILayout.ExpandWidth(false));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_Kd"), Localizer.Format("#RT_PIDControllerFragment_Kd_desc")));//"Derivative", "(3) Increase until the loop is acceptably quick to its target point."
RTUtil.TextField(ref kd, GUILayout.Width(50), GUILayout.ExpandWidth(false));
}
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
{
RTUtil.Button(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_button1"), Localizer.Format("#RT_PIDControllerFragment_button1_desc")),//"SAVE", "Save all values persistently."
OnSaveClick, GUILayout.Width(width3));
RTUtil.Button(new GUIContent(Localizer.Format("#RT_PIDControllerFragment_button2"), Localizer.Format("#RT_PIDControllerFragment_button2_desc")),//"APLY", "Interface all values to Flight PID Controller."
() => RTCore.Instance.StartCoroutine(OnApplyClick()), GUILayout.Width(width3));
RTUtil.Button(new GUIContent(">>", Localizer.Format("#RT_PIDControllerFragment_Queue_desc")),//"Toggles the queue and delay functionality."
mOnClickQueue, GUILayout.Width(width3));
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
private IEnumerator OnApplyClick()
{
yield return null;
if (mFlightComputer.InputAllowed)
{
kp = RTUtil.ConstrictNum(kp, false);
ki = RTUtil.ConstrictNum(ki, false);
kd = RTUtil.ConstrictNum(kd, false);
mFlightComputer.Enqueue(PIDCommand.WithNewChanges(Double.Parse(kp), Double.Parse(ki), Double.Parse(kd)));
}
}
private void OnSaveClick()
{
if (RTSettings.Instance != null)
{
RTSettings.Instance.FlightTermP = Double.Parse(kp);
RTSettings.Instance.FlightTermI = Double.Parse(ki);
RTSettings.Instance.FlightTermD = Double.Parse(kd);
}
FlightComputer.FlightComputer.PIDKp = Double.Parse(kp);
FlightComputer.FlightComputer.PIDKi = Double.Parse(ki);
FlightComputer.FlightComputer.PIDKd = Double.Parse(kd);
}
private void LoadFlightPIDValues()
{
if (RTSettings.Instance != null)
{
FlightComputer.FlightComputer.PIDKp = RTSettings.Instance.FlightTermP;
FlightComputer.FlightComputer.PIDKi = RTSettings.Instance.FlightTermI;
FlightComputer.FlightComputer.PIDKd = RTSettings.Instance.FlightTermD;
}
kp = FlightComputer.FlightComputer.PIDKp.ToString();
ki = FlightComputer.FlightComputer.PIDKi.ToString();
kd = FlightComputer.FlightComputer.PIDKd.ToString();
}
}
}