39
39
import dji .sdk .flightcontroller .FlightController ;
40
40
import dji .sdk .flightcontroller .Simulator ;
41
41
42
- //TODO: Refactor needed
43
42
44
43
/**
45
44
* Class for virtual stick.
46
45
*/
47
- public class VirtualStickView extends RelativeLayout
48
- implements View .OnClickListener , CompoundButton .OnCheckedChangeListener , PresentableView {
49
-
50
- private boolean yawControlModeFlag = true ;
51
- private boolean rollPitchControlModeFlag = true ;
52
- private boolean verticalControlModeFlag = true ;
53
- private boolean horizontalCoordinateFlag = true ;
54
-
46
+ public class VirtualStickView extends RelativeLayout implements View .OnClickListener , CompoundButton .OnCheckedChangeListener , PresentableView {
55
47
private Button btnEnableVirtualStick ;
56
48
private Button btnDisableVirtualStick ;
57
49
private Button btnHorizontalCoordinate ;
@@ -73,7 +65,9 @@ public class VirtualStickView extends RelativeLayout
73
65
private float roll ;
74
66
private float yaw ;
75
67
private float throttle ;
76
- private FlightControllerKey isSimulatorActived ;
68
+ private boolean isSimulatorActived = false ;
69
+ private FlightController flightController = null ;
70
+ private Simulator simulator = null ;
77
71
78
72
public VirtualStickView (Context context ) {
79
73
super (context );
@@ -111,13 +105,28 @@ protected void onDetachedFromWindow() {
111
105
private void init (Context context ) {
112
106
LayoutInflater layoutInflater = (LayoutInflater ) context .getSystemService (Service .LAYOUT_INFLATER_SERVICE );
113
107
layoutInflater .inflate (R .layout .view_virtual_stick , this , true );
114
-
115
- initAllKeys ();
108
+ initParams ();
116
109
initUI ();
117
110
}
118
111
119
- private void initAllKeys () {
120
- isSimulatorActived = FlightControllerKey .create (FlightControllerKey .IS_SIMULATOR_ACTIVE );
112
+ private void initParams () {
113
+ // We recommand you use the below settings, a standard american hand style.
114
+ if (flightController == null ) {
115
+ if (ModuleVerificationUtil .isFlightControllerAvailable ()) {
116
+ flightController = DJISampleApplication .getAircraftInstance ().getFlightController ();
117
+ }
118
+ }
119
+ flightController .setVerticalControlMode (VerticalControlMode .VELOCITY );
120
+ flightController .setRollPitchControlMode (RollPitchControlMode .VELOCITY );
121
+ flightController .setYawControlMode (YawControlMode .ANGULAR_VELOCITY );
122
+ flightController .setRollPitchCoordinateSystem (FlightCoordinateSystem .BODY );
123
+
124
+ // Check if the simulator is activated.
125
+ if (simulator == null ) {
126
+ simulator = ModuleVerificationUtil .getSimulator ();
127
+ }
128
+ isSimulatorActived = simulator .isSimulatorActive ();
129
+
121
130
}
122
131
123
132
private void initUI () {
@@ -145,15 +154,13 @@ private void initUI() {
145
154
btnTakeOff .setOnClickListener (this );
146
155
btnSimulator .setOnCheckedChangeListener (VirtualStickView .this );
147
156
148
- Boolean isSimulatorOn = (Boolean ) KeyManager .getInstance ().getValue (isSimulatorActived );
149
- if (isSimulatorOn != null && isSimulatorOn ) {
157
+ if (isSimulatorActived ) {
150
158
btnSimulator .setChecked (true );
151
159
textView .setText ("Simulator is On." );
152
160
}
153
161
}
154
162
155
163
private void setUpListeners () {
156
- Simulator simulator = ModuleVerificationUtil .getSimulator ();
157
164
if (simulator != null ) {
158
165
simulator .setStateCallback (new SimulatorState .Callback () {
159
166
@ Override
@@ -173,7 +180,7 @@ public void onUpdate(@NonNull final SimulatorState simulatorState) {
173
180
}
174
181
});
175
182
} else {
176
- ToastUtils .setResultToToast ("Disconnected !" );
183
+ ToastUtils .setResultToToast ("Simulator disconnected !" );
177
184
}
178
185
179
186
screenJoystickLeft .setJoystickListener (new OnScreenJoystickListener () {
@@ -190,15 +197,8 @@ public void onTouch(OnScreenJoystick joystick, float pX, float pY) {
190
197
float pitchJoyControlMaxSpeed = 10 ;
191
198
float rollJoyControlMaxSpeed = 10 ;
192
199
193
- if (horizontalCoordinateFlag ) {
194
- if (rollPitchControlModeFlag ) {
195
- pitch = (float ) (pitchJoyControlMaxSpeed * pX );
196
- roll = (float ) (rollJoyControlMaxSpeed * pY );
197
- } else {
198
- pitch = - (float ) (pitchJoyControlMaxSpeed * pY );
199
- roll = (float ) (rollJoyControlMaxSpeed * pX );
200
- }
201
- }
200
+ pitch = pitchJoyControlMaxSpeed * pY ;
201
+ roll = rollJoyControlMaxSpeed * pX ;
202
202
203
203
if (null == sendVirtualStickDataTimer ) {
204
204
sendVirtualStickDataTask = new SendVirtualStickDataTask ();
@@ -219,7 +219,7 @@ public void onTouch(OnScreenJoystick joystick, float pX, float pY) {
219
219
if (Math .abs (pY ) < 0.02 ) {
220
220
pY = 0 ;
221
221
}
222
- float verticalJoyControlMaxSpeed = 2 ;
222
+ float verticalJoyControlMaxSpeed = 4 ;
223
223
float yawJoyControlMaxSpeed = 20 ;
224
224
225
225
yaw = yawJoyControlMaxSpeed * pX ;
@@ -270,72 +270,45 @@ public void onResult(DJIError djiError) {
270
270
break ;
271
271
272
272
case R .id .btn_roll_pitch_control_mode :
273
- if (rollPitchControlModeFlag ) {
273
+ if (flightController . getRollPitchControlMode () == RollPitchControlMode . VELOCITY ) {
274
274
flightController .setRollPitchControlMode (RollPitchControlMode .ANGLE );
275
- rollPitchControlModeFlag = false ;
276
275
} else {
277
276
flightController .setRollPitchControlMode (RollPitchControlMode .VELOCITY );
278
- rollPitchControlModeFlag = true ;
279
- }
280
- try {
281
- ToastUtils .setResultToToast (flightController .getRollPitchControlMode ().name ());
282
- } catch (Exception ex ) {
283
277
}
278
+ ToastUtils .setResultToToast (flightController .getRollPitchControlMode ().name ());
284
279
break ;
285
-
286
280
case R .id .btn_yaw_control_mode :
287
- if (yawControlModeFlag ) {
281
+ if (flightController . getYawControlMode () == YawControlMode . ANGULAR_VELOCITY ) {
288
282
flightController .setYawControlMode (YawControlMode .ANGLE );
289
- yawControlModeFlag = false ;
290
283
} else {
291
284
flightController .setYawControlMode (YawControlMode .ANGULAR_VELOCITY );
292
- yawControlModeFlag = true ;
293
- }
294
- try {
295
- ToastUtils .setResultToToast (flightController .getYawControlMode ().name ());
296
- } catch (Exception ex ) {
297
285
}
286
+ ToastUtils .setResultToToast (flightController .getYawControlMode ().name ());
298
287
break ;
299
-
300
288
case R .id .btn_vertical_control_mode :
301
- if (verticalControlModeFlag ) {
289
+ if (flightController . getVerticalControlMode () == VerticalControlMode . VELOCITY ) {
302
290
flightController .setVerticalControlMode (VerticalControlMode .POSITION );
303
- verticalControlModeFlag = false ;
304
291
} else {
305
292
flightController .setVerticalControlMode (VerticalControlMode .VELOCITY );
306
- verticalControlModeFlag = true ;
307
- }
308
- try {
309
- ToastUtils .setResultToToast (flightController .getVerticalControlMode ().name ());
310
- } catch (Exception ex ) {
311
293
}
294
+ ToastUtils .setResultToToast (flightController .getVerticalControlMode ().name ());
312
295
break ;
313
-
314
296
case R .id .btn_horizontal_coordinate :
315
- if (horizontalCoordinateFlag ) {
297
+ if (flightController . getRollPitchCoordinateSystem () == FlightCoordinateSystem . BODY ) {
316
298
flightController .setRollPitchCoordinateSystem (FlightCoordinateSystem .GROUND );
317
- horizontalCoordinateFlag = false ;
318
299
} else {
319
300
flightController .setRollPitchCoordinateSystem (FlightCoordinateSystem .BODY );
320
- horizontalCoordinateFlag = true ;
321
- }
322
- try {
323
- ToastUtils .setResultToToast (flightController .getRollPitchCoordinateSystem ().name ());
324
- } catch (Exception ex ) {
325
301
}
302
+ ToastUtils .setResultToToast (flightController .getRollPitchCoordinateSystem ().name ());
326
303
break ;
327
-
328
304
case R .id .btn_take_off :
329
-
330
305
flightController .startTakeoff (new CommonCallbacks .CompletionCallback () {
331
306
@ Override
332
307
public void onResult (DJIError djiError ) {
333
308
DialogUtils .showDialogBasedOnError (getContext (), djiError );
334
309
}
335
310
});
336
-
337
311
break ;
338
-
339
312
default :
340
313
break ;
341
314
}
@@ -349,29 +322,27 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
349
322
}
350
323
351
324
private void onClickSimulator (boolean isChecked ) {
352
- Simulator simulator = ModuleVerificationUtil .getSimulator ();
353
325
if (simulator == null ) {
354
326
return ;
355
327
}
356
328
if (isChecked ) {
357
-
358
329
textView .setVisibility (VISIBLE );
359
-
360
- simulator . start ( InitializationData . createInstance ( new LocationCoordinate2D ( 23 , 113 ), 10 , 10 ),
361
- new CommonCallbacks . CompletionCallback ( ) {
362
- @ Override
363
- public void onResult ( DJIError djiError ) {
364
-
365
- }
366
- });
330
+ simulator . start ( InitializationData . createInstance ( new LocationCoordinate2D ( 23 , 113 ), 10 , 10 ), new CommonCallbacks . CompletionCallback () {
331
+ @ Override
332
+ public void onResult ( DJIError djiError ) {
333
+ if ( djiError != null ) {
334
+ ToastUtils . setResultToToast ( djiError . getDescription ());
335
+ }
336
+ }
337
+ });
367
338
} else {
368
-
369
339
textView .setVisibility (INVISIBLE );
370
-
371
340
simulator .stop (new CommonCallbacks .CompletionCallback () {
372
341
@ Override
373
342
public void onResult (DJIError djiError ) {
374
-
343
+ if (djiError != null ) {
344
+ ToastUtils .setResultToToast (djiError .getDescription ());
345
+ }
375
346
}
376
347
});
377
348
}
@@ -383,22 +354,18 @@ public int getDescription() {
383
354
}
384
355
385
356
private class SendVirtualStickDataTask extends TimerTask {
386
-
387
357
@ Override
388
358
public void run () {
389
- if (ModuleVerificationUtil .isFlightControllerAvailable ()) {
390
- DJISampleApplication .getAircraftInstance ()
391
- .getFlightController ()
392
- .sendVirtualStickFlightControlData (new FlightControlData (pitch ,
393
- roll ,
394
- yaw ,
395
- throttle ),
396
- new CommonCallbacks .CompletionCallback () {
397
- @ Override
398
- public void onResult (DJIError djiError ) {
399
-
400
- }
401
- });
359
+ if (flightController != null ) {
360
+ //接口写反了,setPitch()应该传入roll值,setRoll()应该传入pitch值
361
+ flightController .sendVirtualStickFlightControlData (new FlightControlData (roll , pitch , yaw , throttle ), new CommonCallbacks .CompletionCallback () {
362
+ @ Override
363
+ public void onResult (DJIError djiError ) {
364
+ if (djiError != null ) {
365
+ ToastUtils .setResultToToast (djiError .getDescription ());
366
+ }
367
+ }
368
+ });
402
369
}
403
370
}
404
371
}
0 commit comments