Skip to content

Commit 02703f9

Browse files
committed
Fix #3123: add support for custom axis controls
1 parent 43d636e commit 02703f9

4 files changed

Lines changed: 69 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Add `ACTUATION` suffix to steeringmanager (thanks @DBooots) [pr](https://github.com/KSP-KOS/KOS/pull/3162)
1010
- `VESSEL:ELEMENTS` now considers ModuleGrappleNode (i.e. the Klaw) to separate elements
1111
- `PART:CONTROLFROM` can now be called on a part that has ModuleGrappleNode
12+
- Add support for custom axis control
1213

1314
### Performance Improvements
1415

doc/source/commands/flight/pilot.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ what kOS is doing.
127127
- :ref:`Boolean <boolean>`
128128
- Are the pilot's controls zeroed, including trim?
129129

130+
* - `PILOTCUSTOMAXISCOUNT`
131+
- Get-only
132+
- Number of custom axes available from the pilot's controls
133+
134+
* - `PILOTCUSTOMAXIS`
135+
- Get-only
136+
- :ref:`scalar <scalar>` [-1,1]
137+
- Param: the index of the custom axis, from 0 to `PILOTCUSTOMAXISCOUNT-1`.
138+
- Returns: the value of the custom axis
139+
130140

131141
.. _SHIP CONTROL PILOTMAINTHROTTLE:
132142
.. object:: SHIP:CONTROL:PILOTMAINTHROTTLE

doc/source/commands/flight/raw.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,27 @@ These "Raw" controls allow you the direct control of flight parameters while the
158158
- :ref:`Boolean <boolean>`
159159
- Releases Control
160160

161+
* - `CUSTOMAXISCOUNT <SHIP CONTROL CUSTOMAXISCOUNT>`
162+
- Get-only
163+
- Number of custom axes available from the ship's controls
161164

165+
* - `CUSTOMAXIS <SHIP CONTROL CUSTOMAXIS>`
166+
- Get-only
167+
- param: the index of the custom axis, from 0 to `CUSTOMAXISCOUNT-1`.
168+
- :ref:`scalar <scalar>` [-1,1]
169+
- Returns: the value of the custom axis
170+
171+
* - `SETCUSTOMAXIS <SHIP CONTROL SETCUSTOMAXIS>`
172+
- Set-only
173+
- param: the index of the custom axis, from 0 to `CUSTOMAXISCOUNT-1`.
174+
- param: the value to set the custom axis to
175+
- :ref:`scalar <scalar>` [-1,1]
176+
- Sets the value of the custom axis. After a custom axis has been set, the pilot's custom axes values will be ignored until `NEUTRALIZECUSTOMAXES` is set to true.
162177

178+
* - `NEUTRALIZECUSTOMAXES`
179+
- Set-only
180+
- :ref:`Boolean <boolean>`
181+
- Setting this to true causes all custom axes to be set to zero. Setting it to false has no effect.
163182

164183
.. _SHIP CONTROL MAINTHROTTLE:
165184
.. object:: SHIP:CONTROL:MAINTHROTTLE

src/kOS/Suffixed/FlightControl.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using kOS.Communication;
1212
using kOS.Control;
1313
using Smooth.Delegates;
14+
using UnityEngine;
1415

1516
namespace kOS.Suffixed
1617
{
@@ -33,6 +34,9 @@ public class FlightControl : Structure , IDisposable, IFlightControlParameter
3334
private float wheelThrottle;
3435
private float wheelThrottleTrim;
3536
private float mainThrottle;
37+
private readonly float[] customAxes;
38+
private bool customAxesActive;
39+
3640
private bool bound;
3741
private readonly List<string> floatSuffixes;
3842
private readonly List<string> vectorSuffixes;
@@ -41,6 +45,7 @@ public FlightControl(Vessel vessel)
4145
{
4246
bound = false;
4347
Vessel = vessel;
48+
customAxes = new float[vessel.ctrlState.custom_axes.Length];
4449

4550
floatSuffixes = new List<string> { "YAW", "PITCH", "ROLL", "STARBOARD", "TOP", "FORE", "MAINTHROTTLE", "PILOTMAINTHROTTLE", "WHEELTHROTTLE", "WHEELSTEER" };
4651
vectorSuffixes = new List<string> { "ROTATION", "TRANSLATION" };
@@ -115,6 +120,9 @@ private void InitializePilotSuffixes()
115120
FlightInputHandler.state.mainThrottle = value;
116121
}
117122
}, 0, 1));
123+
124+
AddSuffix("PILOTCUSTOMAXISCOUNT", new Suffix<ScalarIntValue>(() => new ScalarIntValue(Vessel == FlightGlobals.ActiveVessel ? FlightInputHandler.state.custom_axes.Length : 0)));
125+
AddSuffix("PILOTCUSTOMAXIS", new OneArgsSuffix<ScalarValue, ScalarIntValue>((index) => Vessel == FlightGlobals.ActiveVessel ? FlightInputHandler.state.custom_axes[index.GetIntValue()] : 0f));
118126
}
119127

120128
private float ReadPilot(ref float flightInputValue)
@@ -143,18 +151,28 @@ private void InitializeSuffixes()
143151
AddSuffix(new[] { "FORE" }, new ClampSetSuffix<ScalarValue>(() => fore, value => fore = value, -1, 1));
144152
AddSuffix(new[] { "STARBOARD" }, new ClampSetSuffix<ScalarValue>(() => starboard, value => starboard = value, -1, 1));
145153
AddSuffix(new[] { "TOP" }, new ClampSetSuffix<ScalarValue>(() => top, value => top = value, -1, 1));
146-
AddSuffix(new[] { "TRANSLATION" }, new SetSuffix<Vector>(() => new Vector(starboard, top, fore) , SetTranslation));
154+
AddSuffix(new[] { "TRANSLATION" }, new SetSuffix<Vector>(() => new Vector(starboard, top, fore), SetTranslation));
147155

148156
//ROVER
149157
AddSuffix(new[] { "WHEELSTEER" }, new ClampSetSuffix<ScalarValue>(() => wheelSteer, value => wheelSteer = value, -1, 1));
150158
AddSuffix(new[] { "WHEELSTEERTRIM" }, new ClampSetSuffix<ScalarValue>(() => wheelSteerTrim, value => wheelSteerTrim = value, -1, 1));
151-
159+
152160

153161
//THROTTLE
154162
AddSuffix(new[] { "MAINTHROTTLE" }, new ClampSetSuffix<ScalarValue>(() => mainThrottle, value => mainThrottle = value, 0, 1));
155163
AddSuffix(new[] { "WHEELTHROTTLE" }, new ClampSetSuffix<ScalarValue>(() => wheelThrottle, value => wheelThrottle = value, -1, 1));
156164
AddSuffix(new[] { "WHEELTHROTTLETRIM" }, new ClampSetSuffix<ScalarValue>(() => wheelThrottleTrim, value => wheelThrottleTrim = value, -1, 1));
157165

166+
//CUSTOM AXES
167+
AddSuffix("CUSTOMAXISCOUNT", new Suffix<ScalarIntValue>(() => new ScalarIntValue(customAxes.Length)));
168+
AddSuffix("CUSTOMAXIS", new OneArgsSuffix<ScalarValue, ScalarIntValue>((index) => customAxes[index.GetIntValue()]));
169+
AddSuffix("SETCUSTOMAXIS", new TwoArgsSuffix<ScalarIntValue, ScalarValue>((index, value) =>
170+
{
171+
customAxesActive = true;
172+
customAxes[index.GetIntValue()] = Mathf.Clamp(value, -1.0f, 1.0f);
173+
}));
174+
AddSuffix("NEUTRALIZECUSTOMAXES", new OneArgsSuffix<BooleanValue>(v => NeutralizeCustomAxes(v)));
175+
158176
//OTHER
159177
AddSuffix(new[] { "BOUND" }, new SetSuffix<BooleanValue>(() => bound, value => bound = value));
160178
AddSuffix(new[] { "NEUTRAL", "NEUTRALIZE" }, new SetSuffix<BooleanValue>(IsNeutral, v => {if (v) Neutralize();}));
@@ -275,6 +293,18 @@ private void ResetControls()
275293
mainThrottle = default(float);
276294
}
277295

296+
private void NeutralizeCustomAxes(BooleanValue v)
297+
{
298+
customAxesActive = v;
299+
if (!customAxesActive)
300+
{
301+
for (int i = 0; i < customAxes.Length; i++)
302+
{
303+
customAxes[i] = 0f;
304+
}
305+
}
306+
}
307+
278308
private BooleanValue IsNeutral()
279309
{
280310
return (yaw == yawTrim && pitch == pitchTrim && roll == rollTrim &&
@@ -308,6 +338,13 @@ private void PushNewSetting(ref FlightCtrlState st)
308338
if(Math.Abs(wheelSteerTrim) > SETTING_EPILSON) st.wheelSteerTrim = wheelSteerTrim;
309339
if(Math.Abs(wheelThrottleTrim) > SETTING_EPILSON) st.wheelThrottleTrim = wheelThrottleTrim;
310340

341+
if (customAxesActive)
342+
{
343+
for (int i = 0; i < customAxes.Length; i++)
344+
{
345+
st.custom_axes[i] = customAxes[i];
346+
}
347+
}
311348
}
312349

313350
bool IFlightControlParameter.Enabled

0 commit comments

Comments
 (0)