Skip to content

Commit e7f7f35

Browse files
author
daniel.chen
committed
update 4.16.2
1 parent a25f670 commit e7f7f35

File tree

7 files changed

+300
-104
lines changed

7 files changed

+300
-104
lines changed

Sample Code/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ android {
7777
dependencies {
7878
implementation 'androidx.multidex:multidex:2.0.0'
7979
implementation 'com.squareup:otto:1.3.8'
80-
implementation('com.dji:dji-sdk:4.16.1', {
80+
implementation('com.dji:dji-sdk:4.16.2', {
8181
/**
8282
* Uncomment the "library-anti-distortion" if your app does not need Anti Distortion for Mavic 2 Pro and Mavic 2 Zoom.
8383
* Uncomment the "fly-safe-database" if you need database for release, or we will download it when DJISDKManager.getInstance().registerApp
@@ -87,7 +87,7 @@ dependencies {
8787
exclude module: 'library-anti-distortion'
8888
//exclude module: 'fly-safe-database'
8989
})
90-
compileOnly 'com.dji:dji-sdk-provided:4.16.1'
90+
compileOnly 'com.dji:dji-sdk-provided:4.16.2'
9191

9292
implementation 'androidx.appcompat:appcompat:1.0.0'
9393
implementation 'androidx.core:core:1.0.0'

Sample Code/app/src/main/java/com/dji/sdk/sample/demo/flightcontroller/VirtualStickView.java

Lines changed: 57 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,11 @@
3939
import dji.sdk.flightcontroller.FlightController;
4040
import dji.sdk.flightcontroller.Simulator;
4141

42-
//TODO: Refactor needed
4342

4443
/**
4544
* Class for virtual stick.
4645
*/
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 {
5547
private Button btnEnableVirtualStick;
5648
private Button btnDisableVirtualStick;
5749
private Button btnHorizontalCoordinate;
@@ -73,7 +65,9 @@ public class VirtualStickView extends RelativeLayout
7365
private float roll;
7466
private float yaw;
7567
private float throttle;
76-
private FlightControllerKey isSimulatorActived;
68+
private boolean isSimulatorActived = false;
69+
private FlightController flightController = null;
70+
private Simulator simulator = null;
7771

7872
public VirtualStickView(Context context) {
7973
super(context);
@@ -111,13 +105,28 @@ protected void onDetachedFromWindow() {
111105
private void init(Context context) {
112106
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
113107
layoutInflater.inflate(R.layout.view_virtual_stick, this, true);
114-
115-
initAllKeys();
108+
initParams();
116109
initUI();
117110
}
118111

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+
121130
}
122131

123132
private void initUI() {
@@ -145,15 +154,13 @@ private void initUI() {
145154
btnTakeOff.setOnClickListener(this);
146155
btnSimulator.setOnCheckedChangeListener(VirtualStickView.this);
147156

148-
Boolean isSimulatorOn = (Boolean) KeyManager.getInstance().getValue(isSimulatorActived);
149-
if (isSimulatorOn != null && isSimulatorOn) {
157+
if (isSimulatorActived) {
150158
btnSimulator.setChecked(true);
151159
textView.setText("Simulator is On.");
152160
}
153161
}
154162

155163
private void setUpListeners() {
156-
Simulator simulator = ModuleVerificationUtil.getSimulator();
157164
if (simulator != null) {
158165
simulator.setStateCallback(new SimulatorState.Callback() {
159166
@Override
@@ -173,7 +180,7 @@ public void onUpdate(@NonNull final SimulatorState simulatorState) {
173180
}
174181
});
175182
} else {
176-
ToastUtils.setResultToToast("Disconnected!");
183+
ToastUtils.setResultToToast("Simulator disconnected!");
177184
}
178185

179186
screenJoystickLeft.setJoystickListener(new OnScreenJoystickListener() {
@@ -190,15 +197,8 @@ public void onTouch(OnScreenJoystick joystick, float pX, float pY) {
190197
float pitchJoyControlMaxSpeed = 10;
191198
float rollJoyControlMaxSpeed = 10;
192199

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;
202202

203203
if (null == sendVirtualStickDataTimer) {
204204
sendVirtualStickDataTask = new SendVirtualStickDataTask();
@@ -219,7 +219,7 @@ public void onTouch(OnScreenJoystick joystick, float pX, float pY) {
219219
if (Math.abs(pY) < 0.02) {
220220
pY = 0;
221221
}
222-
float verticalJoyControlMaxSpeed = 2;
222+
float verticalJoyControlMaxSpeed = 4;
223223
float yawJoyControlMaxSpeed = 20;
224224

225225
yaw = yawJoyControlMaxSpeed * pX;
@@ -270,72 +270,45 @@ public void onResult(DJIError djiError) {
270270
break;
271271

272272
case R.id.btn_roll_pitch_control_mode:
273-
if (rollPitchControlModeFlag) {
273+
if (flightController.getRollPitchControlMode() == RollPitchControlMode.VELOCITY) {
274274
flightController.setRollPitchControlMode(RollPitchControlMode.ANGLE);
275-
rollPitchControlModeFlag = false;
276275
} else {
277276
flightController.setRollPitchControlMode(RollPitchControlMode.VELOCITY);
278-
rollPitchControlModeFlag = true;
279-
}
280-
try {
281-
ToastUtils.setResultToToast(flightController.getRollPitchControlMode().name());
282-
} catch (Exception ex) {
283277
}
278+
ToastUtils.setResultToToast(flightController.getRollPitchControlMode().name());
284279
break;
285-
286280
case R.id.btn_yaw_control_mode:
287-
if (yawControlModeFlag) {
281+
if (flightController.getYawControlMode() == YawControlMode.ANGULAR_VELOCITY) {
288282
flightController.setYawControlMode(YawControlMode.ANGLE);
289-
yawControlModeFlag = false;
290283
} else {
291284
flightController.setYawControlMode(YawControlMode.ANGULAR_VELOCITY);
292-
yawControlModeFlag = true;
293-
}
294-
try {
295-
ToastUtils.setResultToToast(flightController.getYawControlMode().name());
296-
} catch (Exception ex) {
297285
}
286+
ToastUtils.setResultToToast(flightController.getYawControlMode().name());
298287
break;
299-
300288
case R.id.btn_vertical_control_mode:
301-
if (verticalControlModeFlag) {
289+
if (flightController.getVerticalControlMode() == VerticalControlMode.VELOCITY) {
302290
flightController.setVerticalControlMode(VerticalControlMode.POSITION);
303-
verticalControlModeFlag = false;
304291
} else {
305292
flightController.setVerticalControlMode(VerticalControlMode.VELOCITY);
306-
verticalControlModeFlag = true;
307-
}
308-
try {
309-
ToastUtils.setResultToToast(flightController.getVerticalControlMode().name());
310-
} catch (Exception ex) {
311293
}
294+
ToastUtils.setResultToToast(flightController.getVerticalControlMode().name());
312295
break;
313-
314296
case R.id.btn_horizontal_coordinate:
315-
if (horizontalCoordinateFlag) {
297+
if (flightController.getRollPitchCoordinateSystem() == FlightCoordinateSystem.BODY) {
316298
flightController.setRollPitchCoordinateSystem(FlightCoordinateSystem.GROUND);
317-
horizontalCoordinateFlag = false;
318299
} else {
319300
flightController.setRollPitchCoordinateSystem(FlightCoordinateSystem.BODY);
320-
horizontalCoordinateFlag = true;
321-
}
322-
try {
323-
ToastUtils.setResultToToast(flightController.getRollPitchCoordinateSystem().name());
324-
} catch (Exception ex) {
325301
}
302+
ToastUtils.setResultToToast(flightController.getRollPitchCoordinateSystem().name());
326303
break;
327-
328304
case R.id.btn_take_off:
329-
330305
flightController.startTakeoff(new CommonCallbacks.CompletionCallback() {
331306
@Override
332307
public void onResult(DJIError djiError) {
333308
DialogUtils.showDialogBasedOnError(getContext(), djiError);
334309
}
335310
});
336-
337311
break;
338-
339312
default:
340313
break;
341314
}
@@ -349,29 +322,27 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
349322
}
350323

351324
private void onClickSimulator(boolean isChecked) {
352-
Simulator simulator = ModuleVerificationUtil.getSimulator();
353325
if (simulator == null) {
354326
return;
355327
}
356328
if (isChecked) {
357-
358329
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+
});
367338
} else {
368-
369339
textView.setVisibility(INVISIBLE);
370-
371340
simulator.stop(new CommonCallbacks.CompletionCallback() {
372341
@Override
373342
public void onResult(DJIError djiError) {
374-
343+
if (djiError != null) {
344+
ToastUtils.setResultToToast(djiError.getDescription());
345+
}
375346
}
376347
});
377348
}
@@ -383,22 +354,18 @@ public int getDescription() {
383354
}
384355

385356
private class SendVirtualStickDataTask extends TimerTask {
386-
387357
@Override
388358
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+
});
402369
}
403370
}
404371
}

0 commit comments

Comments
 (0)