@@ -60,7 +60,8 @@ public ZOPIDController AltitudeHoldPID {
6060 Ki = 15 ,
6161 Kd = 3 ,
6262 MaximumOutputValue = 100.0f ,
63- DeadBandEpsilon = 0.0f
63+ DeadBandEpsilon = 0.0f ,
64+ SetPoint = 0
6465 } ;
6566
6667 public ZOPIDController AltitudeClimbPID {
@@ -88,7 +89,8 @@ public AltitudeControlStateEnum AltitudeControlState {
8889 Ki = 0 ,
8990 Kd = 0.1f ,
9091 MaximumOutputValue = 100.0f ,
91- DeadBandEpsilon = 0.0f
92+ DeadBandEpsilon = 0.0f ,
93+ SetPoint = 0
9294 } ;
9395
9496 public ZOPIDController RollControllPID {
@@ -101,15 +103,30 @@ public ZOPIDController RollControllPID {
101103 Ki = 0 ,
102104 Kd = 0.1f ,
103105 MaximumOutputValue = 100.0f ,
104- DeadBandEpsilon = 0.0f
106+ DeadBandEpsilon = 0.0f ,
107+ SetPoint = 0
105108 } ;
106109
107- public ZOPIDController PitchControllPID {
110+ public ZOPIDController PitchControlPID {
108111 get => _pitchControlPID ;
109112 }
110113
114+ public ZOPIDController _yawControlPID = new ZOPIDController {
115+ Kp = 1 ,
116+ Ki = 0 ,
117+ Kd = 0.1f ,
118+ MaximumOutputValue = 100.0f ,
119+ DeadBandEpsilon = 0.0f ,
120+ SetPoint = 0
121+ } ;
122+
123+ public ZOPIDController YawControlPID {
124+ get => _yawControlPID ;
125+ }
126+
111127 private float _pitchForce = 0.0f ;
112128 private float _rollForce = 0.0f ;
129+ private float _yawTorque = 0.0f ;
113130
114131
115132 private float _altitudeForce = 0.0f ;
@@ -141,8 +158,6 @@ protected override void ZOStart() {
141158 AltitudeHoldPID . SetPoint = Altimeter . AltitudeMeters ;
142159 }
143160
144- PitchControllPID . SetPoint = 0 ;
145- RollControllPID . SetPoint = 0 ;
146161 }
147162
148163
@@ -156,21 +171,28 @@ protected override void ZOUpdate() {
156171 AltitudeHoldPID . SetPoint -= _altitudeHeightIncrement ;
157172 }
158173 if ( Input . GetKeyDown ( "w" ) ) {
159- PitchControllPID . SetPoint = 10.0f ;
174+ PitchControlPID . SetPoint = 10.0f ;
160175 }
161176 if ( Input . GetKeyDown ( "x" ) ) {
162- PitchControllPID . SetPoint = - 10.0f ;
177+ PitchControlPID . SetPoint = - 10.0f ;
163178 }
164179 if ( Input . GetKeyDown ( "d" ) ) {
165180 RollControllPID . SetPoint = - 10.0f ;
166181 }
167182 if ( Input . GetKeyDown ( "a" ) ) {
168183 RollControllPID . SetPoint = 10.0f ;
169184 }
185+ if ( Input . GetKeyDown ( "j" ) ) {
186+ YawControlPID . SetPoint = - 5 ;
187+ }
188+ if ( Input . GetKeyDown ( "l" ) ) {
189+ YawControlPID . SetPoint = 5 ;
190+ }
170191
171192 if ( Input . GetKeyDown ( "s" ) ) {
172- PitchControllPID . SetPoint = 0.0f ;
193+ PitchControlPID . SetPoint = 0.0f ;
173194 RollControllPID . SetPoint = 0.0f ;
195+ YawControlPID . SetPoint = 0.0f ;
174196 }
175197
176198 }
@@ -212,7 +234,7 @@ protected override void ZOFixedUpdate() {
212234 Motors [ 3 ] . globalForceVector += backLeftForce ;
213235
214236 // pitch control
215- _pitchForce = PitchControllPID . Update ( IMU . OrientationEulerDegrees . x , Time . deltaTime ) ;
237+ _pitchForce = PitchControlPID . Update ( IMU . OrientationEulerDegrees . x , Time . deltaTime ) ;
216238 forwardRightForce = BaseRigidBody . transform . up * - _pitchForce ;
217239 forwardLeftForce = BaseRigidBody . transform . up * - _pitchForce ;
218240 backRightForce = BaseRigidBody . transform . up * _pitchForce ;
@@ -237,7 +259,6 @@ protected override void ZOFixedUpdate() {
237259 Motors [ 3 ] . globalForceVector += backLeftForce ;
238260
239261
240-
241262 } else if ( QuadCopterConfiguration == ZOQuadCopterMotorConfiguration . CrossConfiguration ) {
242263 //TODO:
243264 }
@@ -247,6 +268,10 @@ protected override void ZOFixedUpdate() {
247268 BaseRigidBody . AddForceAtPosition ( motor . globalForceVector , motor . globalPosition ) ;
248269 }
249270
271+ // apply yaw torque
272+ _yawTorque = YawControlPID . Update ( IMU . AngularVelocity . y , Time . deltaTime ) ;
273+ BaseRigidBody . AddTorque ( BaseRigidBody . transform . up * _yawTorque ) ;
274+
250275 }
251276
252277
@@ -261,6 +286,9 @@ private void OnGUI() {
261286 GUI . TextField ( new Rect ( 10 , y += yInc , 300 , 22 ) , $ "Orientation: { IMU . OrientationEulerDegrees . x . ToString ( "n2" ) } { IMU . OrientationEulerDegrees . y . ToString ( "n2" ) } { IMU . OrientationEulerDegrees . z . ToString ( "n2" ) } ") ;
262287 GUI . TextField ( new Rect ( 10 , y += yInc , 300 , 22 ) , $ "Pitch Force: { _pitchForce . ToString ( "n2" ) } ") ;
263288
289+ GUI . TextField ( new Rect ( 10 , y += yInc , 300 , 22 ) , $ "Angular Velocity: { IMU . AngularVelocity . x . ToString ( "n2" ) } { IMU . AngularVelocity . y . ToString ( "n2" ) } { IMU . AngularVelocity . z . ToString ( "n2" ) } ") ;
290+ GUI . TextField ( new Rect ( 10 , y += yInc , 300 , 22 ) , $ "Yaw Torque: { _yawTorque . ToString ( "n2" ) } ") ;
291+
264292 foreach ( Motor motor in Motors ) {
265293 Debug . DrawRay ( motor . globalPosition , motor . globalForceVector , Color . green , 0.1f ) ;
266294 }
0 commit comments