Andrews Dajiang using the SDK VirtualStick server-side send data to Flight Control

Function: Aircraft in Nanjing, people in Beijing, then through an analog joystick (the analog sticks is usually play the rocker flight games) control of the aircraft in Nanjing, which is the remote control. The rocker is similar to the remote control joystick the aircraft, Yaw has been output, the value of pitch, roll, and certain multiple customers spread converted back-end software, and to control by virtualstick mode.

Implementation: 1 server and client connections we use MQTT, this project according to the needs of their own choice of their own way

           2. Determine the start virtualstick mode, first setVirtualStickModeEnabled is true, and then to set up a successful set pitch, yaw, roll model

App.getAircraftInstance().getFlightController().setVirtualStickModeEnabled(true, new CommonCallbacks.CompletionCallback() {
                @Override
                public void onResult(DJIError djiError) {
                    if (null == djiError) {
                        isEnableStick = true;
                        Log.e("virstual", "enable success");
                        App.getAircraftInstance().getFlightController().setRollPitchControlMode(RollPitchControlMode.VELOCITY);
                        App.getAircraftInstance().getFlightController().setYawControlMode(YawControlMode.ANGULAR_VELOCITY);
                        App.getAircraftInstance().getFlightController().setVerticalControlMode(VerticalControlMode.VELOCITY);
                        App.getAircraftInstance().getFlightController().setRollPitchCoordinateSystem(FlightCoordinateSystem.BODY);
                    } else {
                        Log.e("virstual", "error = " + djiError.getDescription());
                    }
                }
            });

        3. The docking can then begin parsing the received parameters, to finally send the instructions over sendVirtualStickFlightControlData, official requirements to the transmission frequency if, once the transmission is 40 to 200 milliseconds 5-25HZ.

/**
     * @param rockerX 杆量比[-1, 1], 前/后(+/-)
     * @param rockerY 杆量比[-1, 1], 左/右(-/+)
     * @param rockerZ 杆量比[-1, 1], 上/下(-/+)
     * @param rockerRotate 杆量比[-1, 1], 顺时针/逆时针(+/-)旋转
     */
    private FlightControlData senddata;

    private void fly(VirtualStickPB bean) {
        if (isEnableStick) {
            float rockX = (null == bean.rockerX ? 0 : bean.rockerX);
            float rockY = (null == bean.rockerY ? 0 : bean.rockerY);
            float rockZ = (null == bean.rockerZ ? 0 : bean.rockerZ);
            float rockerRotation = (null == bean.rockerRotation ? 0 : bean.rockerRotation);
            //Log.e("dispatch", "x = " + rockX + "   y = " + rockY + "  z = " + rockZ+"  rockerRotation = "+rockerRotation);

            RollPitchControlMode rollPitchControlMode = App.getAircraftInstance().getFlightController().getRollPitchControlMode();
            VerticalControlMode verticalControlMode = App.getAircraftInstance().getFlightController().getVerticalControlMode();
            YawControlMode yawControlMode = App.getAircraftInstance().getFlightController().getYawControlMode();

            if (rollPitchControlMode == RollPitchControlMode.VELOCITY) {
                // 前/后
                mRoll = rockX * Limits.ROLL_PITCH_CONTROL_MAX_VELOCITY;
                // 左/右
                mPitch = rockY * Limits.ROLL_PITCH_CONTROL_MAX_VELOCITY;
            } else if (rollPitchControlMode == RollPitchControlMode.ANGLE) {
                // 前/后
                mPitch = -rockX * Limits.ROLL_PITCH_CONTROL_MAX_ANGLE;
                // 左/右
                mRoll = rockY * Limits.ROLL_PITCH_CONTROL_MAX_ANGLE;
            } else {
                mPitch = 0;
                mRoll = 0;
            }

            if (verticalControlMode == VerticalControlMode.VELOCITY) {
                // 上/下
                mThrottle = -rockZ * Limits.VERTICAL_CONTROL_MAX_VELOCITY;
            } else if (verticalControlMode == VerticalControlMode.POSITION) {
                // 上/下
                if (rockZ >= 0) {
                    mThrottle = rockZ * Limits.VERTICAL_CONTROL_MAX_HEIGHT;
                } else {
                    mThrottle = 0;
                    //mThrottle = controller.getCurrentState().getAircraftLocation().getAltitude();
                }
            } else {
                mThrottle = 0;
            }

            if (yawControlMode == YawControlMode.ANGULAR_VELOCITY) {
                // 旋转
                mYaw = rockerRotation * Limits.YAW_CONTROL_MAX_ANGULAR_VELOCITY;
            } else if (yawControlMode == YawControlMode.ANGLE) {
                // 旋转
                mYaw = rockerRotation * Limits.YAW_CONTROL_MAX_ANGLE;
            } else {
                mYaw = 0;
            }

            senddata = new FlightControlData(mPitch, mRoll, mYaw, mThrottle);

            if (null == sendVirtualStickDataTimer) {
                sendVirtualStickDataTask = new SendVirtualStickDataTask();
                sendVirtualStickDataTimer = new Timer();
                sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
            }
        } else {
            Log.e("dispatch", "isenablestick = false");
        }
    }

    private class SendVirtualStickDataTask extends TimerTask {
        @Override
        public void run() {
            if (ModuleVerificationUtil.isFlightControllerAvailable()) {
                App.getAircraftInstance().getFlightController().sendVirtualStickFlightControlData(senddata,
                        new CommonCallbacks.CompletionCallback() {
                            @Override
                            public void onResult(DJIError djiError) {

                            }
                        });
            } else {
                Log.e("SendVirtualStickData", "isFlightControllerAvailable = false");
            }
        }
    }

4. Do not forget at the end of the virtualstick mode is set to false, otherwise the remote will not control the airplane looks like, and then release the resources freed

@Override
    public void onDestroy() {
        if (null != sendVirtualStickDataTimer) {
            if (sendVirtualStickDataTask != null) {
                sendVirtualStickDataTask.cancel();

            }
            sendVirtualStickDataTimer.cancel();
            sendVirtualStickDataTimer.purge();
            sendVirtualStickDataTimer = null;
            sendVirtualStickDataTask = null;
        }
        super.onDestroy();
        //Event.unregister(this);
    }

5. Also note things, Dajiang SDK must login account, or limit the flight of 50 meters, I was once engaged in this issue for a long time, the aircraft Why not fly for a while, I always felt that I was the problem code, get two days only to find that not login account, very upset.

I wrote a simple, actually may be some other issues, there are communication problems with me, QQ505057618. Do not like do not spray

Published 24 original articles · won praise 5 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_26923265/article/details/82743941