AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
AC_BaseAirplane_Input.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using Communicator;
5 using UnityEditor;
6 using Commons;
7 namespace AirControl
8 {
12  public class AC_BaseAirplane_Input : MonoBehaviour
13  {
14  #region Variable
15 
16  protected float pitch = 0f;
17  protected float roll = 0f;
18  protected float yaw = 0f;
19  protected float throttle = 0f;
20  protected float brake = 0f;
21  public int maxFlapIncrements=2; // maximum increment allowed to flaps
22  protected int flaps = 0;
23 
24  protected KeyCode cameraKey = KeyCode.C;
25  protected bool camerSwitch = false;
26 
27  // Slowly move the throttle
28 
29  [Header("Sticky throttle value control how the throttle can be moved")]
30 
31  protected float throttleSpeed;
32  protected float stickyThrottle;
33 
34  #endregion
35 
36  #region Properties
37  public float Pitch{
38  get{return pitch;}
39  }
40  public float Roll{
41  get{return roll;}
42  }
43  public float Yaw{
44  get{return yaw;}
45  }
46  public float Throttle{
47  get{return throttle;}
48  }
49  public int Flaps{
50  get{return flaps;}
51  }
52  public float NormalizedFlaps{
53  get{
54  return (float)flaps / maxFlapIncrements;
55  }
56  }
57  public float Brake{
58  get{return brake;}
59  }
60  public float StickyThrottle {
61  get{return stickyThrottle;}
62  }
63  public bool CameraSwitch{
64  get{return camerSwitch;}
65  }
66  #endregion
67 
68 
69  #region Builtin Methods
70  // Update is called once per frame
71 
72 
73  void Start(){
74 
75  throttleSpeed = (float)CommonFunctions.airplanePreset[CommonFunctions.activeAirplane+"/throttleSpeed"];
76 #if !UNITY_EDITOR && UNITY_WEBGL
77  // disable WebGLInput.captureAllKeyboardInput so elements in web page can handle keabord inputs
78  WebGLInput.captureAllKeyboardInput = false;
79 #endif
80  }
81  void Update()
82  {
83  // HandleInputOld();
85  StickyThrottleControl();
86  ClampInputs();
87 
88  // Keeping Get connection in the update loop is essential to avoid the lag
89  IOSwitch();
90  }
91  #endregion
92 
93  #region Custom Methods
94  protected virtual void HandleInputOld(){
98  // Process pitch, roll, yaw and throttle
99  pitch = Input.GetAxis("Vertical");
100  roll = Input.GetAxis("Horizontal");
101  yaw = Input.GetAxis("yaw");
102  throttle = Input.GetAxis("throttle");
103 
104  StickyThrottleControl();
105  // Process brakes bool
106  brake = Input.GetKey(KeyCode.Space)?1f:0f;
107  // Process flaps
108  // get GetKeyDown is used because it fires only once when key pressed. GetKey constantly fire events
109  if(Input.GetKeyDown(KeyCode.F)){
110  flaps+=1;
111  }
112  if(Input.GetKeyDown(KeyCode.G)){
113  flaps-=1;
114  }
115  flaps = Mathf.Clamp(flaps, 0,maxFlapIncrements);
116 
117  //camera switch key
118  camerSwitch = Input.GetKeyDown(cameraKey);
119 
120  }
121 
122  protected virtual void HandleInputNew(){
123 
124  }
125 
126 
130  void StickyThrottleControl(){
131  stickyThrottle = stickyThrottle + (throttle*throttleSpeed*Time.deltaTime);
132  stickyThrottle = Mathf.Clamp01(stickyThrottle);
133  }
137  protected void ClampInputs()
138  {
139  pitch = Mathf.Clamp(pitch,-1f,1f);
140  roll = Mathf.Clamp(roll,-1f,1f);
141  yaw = Mathf.Clamp(yaw,-1f,1f);
142  throttle = Mathf.Clamp(throttle,-1f,1f);
143  brake = Mathf.Clamp(brake,0f,1f);
144  flaps = Mathf.Clamp(flaps, 0, maxFlapIncrements);
145  }
149  protected void IOSwitch()
150  {
151  string DBInputControlType = StaticControlSchema.InputControlType;
152  // if control type is code then lock the controls and fly it
153  // else let user fly manually
154  if(DBInputControlType == "Code")
155  {
156  throttle = StaticControlSchema.Throttle;
157  stickyThrottle = StaticControlSchema.StickyThrottle;
158  pitch = StaticControlSchema.Pitch;
159  roll = StaticControlSchema.Roll;
160  yaw = StaticControlSchema.Yaw;
161  brake = StaticControlSchema.Brake;
162  flaps = StaticControlSchema.Flaps;
163  }
164  }
165 
166  }
167 
168  #endregion
169 }
170 
171 
172 
AirControl.AC_BaseAirplane_Input.IOSwitch
void IOSwitch()
Receive input from external program like python
Definition: AC_BaseAirplane_Input.cs:149
AirControl.AC_BaseAirplane_Input
Base class to listen for keyboard Inputs
Definition: AC_BaseAirplane_Input.cs:12
AirControl.AC_BaseAirplane_Input.ClampInputs
void ClampInputs()
Clamping inputs between limits
Definition: AC_BaseAirplane_Input.cs:137
AirControl.AC_BaseAirplane_Input.HandleInputOld
virtual void HandleInputOld()
Take input such as Pitch, Yaw, Roll etc
Definition: AC_BaseAirplane_Input.cs:97
AirControl
Definition: AirplaneSelector.cs:8
Communicator.StaticControlSchema
Input control class, acts a dictionary. This class can be accessed anywhere in the code as dict....
Definition: IOSchema.cs:41
Communicator
Definition: InputHandle.cs:10
Commons
Definition: AirplaneProperties.cs:14