Skip to content

Commit

Permalink
update 4.16.2
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel.chen committed Aug 1, 2022
1 parent a25f670 commit e7f7f35
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 104 deletions.
4 changes: 2 additions & 2 deletions Sample Code/app/build.gradle
Expand Up @@ -77,7 +77,7 @@ android {
dependencies {
implementation 'androidx.multidex:multidex:2.0.0'
implementation 'com.squareup:otto:1.3.8'
implementation('com.dji:dji-sdk:4.16.1', {
implementation('com.dji:dji-sdk:4.16.2', {
/**
* Uncomment the "library-anti-distortion" if your app does not need Anti Distortion for Mavic 2 Pro and Mavic 2 Zoom.
* Uncomment the "fly-safe-database" if you need database for release, or we will download it when DJISDKManager.getInstance().registerApp
Expand All @@ -87,7 +87,7 @@ dependencies {
exclude module: 'library-anti-distortion'
//exclude module: 'fly-safe-database'
})
compileOnly 'com.dji:dji-sdk-provided:4.16.1'
compileOnly 'com.dji:dji-sdk-provided:4.16.2'

implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.core:core:1.0.0'
Expand Down
Expand Up @@ -39,19 +39,11 @@
import dji.sdk.flightcontroller.FlightController;
import dji.sdk.flightcontroller.Simulator;

//TODO: Refactor needed

/**
* Class for virtual stick.
*/
public class VirtualStickView extends RelativeLayout
implements View.OnClickListener, CompoundButton.OnCheckedChangeListener, PresentableView {

private boolean yawControlModeFlag = true;
private boolean rollPitchControlModeFlag = true;
private boolean verticalControlModeFlag = true;
private boolean horizontalCoordinateFlag = true;

public class VirtualStickView extends RelativeLayout implements View.OnClickListener, CompoundButton.OnCheckedChangeListener, PresentableView {
private Button btnEnableVirtualStick;
private Button btnDisableVirtualStick;
private Button btnHorizontalCoordinate;
Expand All @@ -73,7 +65,9 @@ public class VirtualStickView extends RelativeLayout
private float roll;
private float yaw;
private float throttle;
private FlightControllerKey isSimulatorActived;
private boolean isSimulatorActived = false;
private FlightController flightController = null;
private Simulator simulator = null;

public VirtualStickView(Context context) {
super(context);
Expand Down Expand Up @@ -111,13 +105,28 @@ protected void onDetachedFromWindow() {
private void init(Context context) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.view_virtual_stick, this, true);

initAllKeys();
initParams();
initUI();
}

private void initAllKeys() {
isSimulatorActived = FlightControllerKey.create(FlightControllerKey.IS_SIMULATOR_ACTIVE);
private void initParams() {
// We recommand you use the below settings, a standard american hand style.
if (flightController == null) {
if (ModuleVerificationUtil.isFlightControllerAvailable()) {
flightController = DJISampleApplication.getAircraftInstance().getFlightController();
}
}
flightController.setVerticalControlMode(VerticalControlMode.VELOCITY);
flightController.setRollPitchControlMode(RollPitchControlMode.VELOCITY);
flightController.setYawControlMode(YawControlMode.ANGULAR_VELOCITY);
flightController.setRollPitchCoordinateSystem(FlightCoordinateSystem.BODY);

// Check if the simulator is activated.
if (simulator == null) {
simulator = ModuleVerificationUtil.getSimulator();
}
isSimulatorActived = simulator.isSimulatorActive();

}

private void initUI() {
Expand Down Expand Up @@ -145,15 +154,13 @@ private void initUI() {
btnTakeOff.setOnClickListener(this);
btnSimulator.setOnCheckedChangeListener(VirtualStickView.this);

Boolean isSimulatorOn = (Boolean) KeyManager.getInstance().getValue(isSimulatorActived);
if (isSimulatorOn != null && isSimulatorOn) {
if (isSimulatorActived) {
btnSimulator.setChecked(true);
textView.setText("Simulator is On.");
}
}

private void setUpListeners() {
Simulator simulator = ModuleVerificationUtil.getSimulator();
if (simulator != null) {
simulator.setStateCallback(new SimulatorState.Callback() {
@Override
Expand All @@ -173,7 +180,7 @@ public void onUpdate(@NonNull final SimulatorState simulatorState) {
}
});
} else {
ToastUtils.setResultToToast("Disconnected!");
ToastUtils.setResultToToast("Simulator disconnected!");
}

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

if (horizontalCoordinateFlag) {
if (rollPitchControlModeFlag) {
pitch = (float) (pitchJoyControlMaxSpeed * pX);
roll = (float) (rollJoyControlMaxSpeed * pY);
} else {
pitch = - (float) (pitchJoyControlMaxSpeed * pY);
roll = (float) (rollJoyControlMaxSpeed * pX);
}
}
pitch = pitchJoyControlMaxSpeed * pY;
roll = rollJoyControlMaxSpeed * pX;

if (null == sendVirtualStickDataTimer) {
sendVirtualStickDataTask = new SendVirtualStickDataTask();
Expand All @@ -219,7 +219,7 @@ public void onTouch(OnScreenJoystick joystick, float pX, float pY) {
if (Math.abs(pY) < 0.02) {
pY = 0;
}
float verticalJoyControlMaxSpeed = 2;
float verticalJoyControlMaxSpeed = 4;
float yawJoyControlMaxSpeed = 20;

yaw = yawJoyControlMaxSpeed * pX;
Expand Down Expand Up @@ -270,72 +270,45 @@ public void onResult(DJIError djiError) {
break;

case R.id.btn_roll_pitch_control_mode:
if (rollPitchControlModeFlag) {
if (flightController.getRollPitchControlMode() == RollPitchControlMode.VELOCITY) {
flightController.setRollPitchControlMode(RollPitchControlMode.ANGLE);
rollPitchControlModeFlag = false;
} else {
flightController.setRollPitchControlMode(RollPitchControlMode.VELOCITY);
rollPitchControlModeFlag = true;
}
try {
ToastUtils.setResultToToast(flightController.getRollPitchControlMode().name());
} catch (Exception ex) {
}
ToastUtils.setResultToToast(flightController.getRollPitchControlMode().name());
break;

case R.id.btn_yaw_control_mode:
if (yawControlModeFlag) {
if (flightController.getYawControlMode() == YawControlMode.ANGULAR_VELOCITY) {
flightController.setYawControlMode(YawControlMode.ANGLE);
yawControlModeFlag = false;
} else {
flightController.setYawControlMode(YawControlMode.ANGULAR_VELOCITY);
yawControlModeFlag = true;
}
try {
ToastUtils.setResultToToast(flightController.getYawControlMode().name());
} catch (Exception ex) {
}
ToastUtils.setResultToToast(flightController.getYawControlMode().name());
break;

case R.id.btn_vertical_control_mode:
if (verticalControlModeFlag) {
if (flightController.getVerticalControlMode() == VerticalControlMode.VELOCITY) {
flightController.setVerticalControlMode(VerticalControlMode.POSITION);
verticalControlModeFlag = false;
} else {
flightController.setVerticalControlMode(VerticalControlMode.VELOCITY);
verticalControlModeFlag = true;
}
try {
ToastUtils.setResultToToast(flightController.getVerticalControlMode().name());
} catch (Exception ex) {
}
ToastUtils.setResultToToast(flightController.getVerticalControlMode().name());
break;

case R.id.btn_horizontal_coordinate:
if (horizontalCoordinateFlag) {
if (flightController.getRollPitchCoordinateSystem() == FlightCoordinateSystem.BODY) {
flightController.setRollPitchCoordinateSystem(FlightCoordinateSystem.GROUND);
horizontalCoordinateFlag = false;
} else {
flightController.setRollPitchCoordinateSystem(FlightCoordinateSystem.BODY);
horizontalCoordinateFlag = true;
}
try {
ToastUtils.setResultToToast(flightController.getRollPitchCoordinateSystem().name());
} catch (Exception ex) {
}
ToastUtils.setResultToToast(flightController.getRollPitchCoordinateSystem().name());
break;

case R.id.btn_take_off:

flightController.startTakeoff(new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError djiError) {
DialogUtils.showDialogBasedOnError(getContext(), djiError);
}
});

break;

default:
break;
}
Expand All @@ -349,29 +322,27 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
}

private void onClickSimulator(boolean isChecked) {
Simulator simulator = ModuleVerificationUtil.getSimulator();
if (simulator == null) {
return;
}
if (isChecked) {

textView.setVisibility(VISIBLE);

simulator.start(InitializationData.createInstance(new LocationCoordinate2D(23, 113), 10, 10),
new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError djiError) {

}
});
simulator.start(InitializationData.createInstance(new LocationCoordinate2D(23, 113), 10, 10), new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError djiError) {
if (djiError != null) {
ToastUtils.setResultToToast(djiError.getDescription());
}
}
});
} else {

textView.setVisibility(INVISIBLE);

simulator.stop(new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError djiError) {

if (djiError != null) {
ToastUtils.setResultToToast(djiError.getDescription());
}
}
});
}
Expand All @@ -383,22 +354,18 @@ public int getDescription() {
}

private class SendVirtualStickDataTask extends TimerTask {

@Override
public void run() {
if (ModuleVerificationUtil.isFlightControllerAvailable()) {
DJISampleApplication.getAircraftInstance()
.getFlightController()
.sendVirtualStickFlightControlData(new FlightControlData(pitch,
roll,
yaw,
throttle),
new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError djiError) {

}
});
if (flightController != null) {
//接口写反了,setPitch()应该传入roll值,setRoll()应该传入pitch值
flightController.sendVirtualStickFlightControlData(new FlightControlData(roll, pitch, yaw, throttle), new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError djiError) {
if (djiError != null) {
ToastUtils.setResultToToast(djiError.getDescription());
}
}
});
}
}
}
Expand Down

0 comments on commit e7f7f35

Please sign in to comment.