'How to actually control a drone using VirtualSticks

I'm struggling with sending commands to drone using DJI SDK. I'm using Phantom 4 V2.0 and DJI SDK version 4.15. There's a tutorial on how to use VirtualSticks to command the drone but only applicable to simulator. I tried using the same code to fly an actual drone but to no success. If I start the motors via provided method they will stop immediately, probably because of the commands my app is sending to the drone while sticks are idle. This is the code:

screenJoystickLeft.setJoystickListener((joystick, pX, pY) -> {
        if (Math.abs(pX) < 0.02) {
            pX = 0;
        }

        if (Math.abs(pY) < 0.02) {
            pY = 0;
        }
        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);
            }
        }

        if (null == sendVirtualStickDataTimer) {
            sendVirtualStickDataTask = new SendVirtualStickDataTask();
            sendVirtualStickDataTimer = new Timer();
            sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 100, 200);
        }
    });

    screenJoystickRight.setJoystickListener((joystick, pX, pY) -> {
        if (Math.abs(pX) < 0.02) {
            pX = 0;
        }

        if (Math.abs(pY) < 0.02) {
            pY = 0;
        }
        float verticalJoyControlMaxSpeed = 2;
        float yawJoyControlMaxSpeed = 3;

        yaw = yawJoyControlMaxSpeed * pX;
        throttle = (verticalJoyControlMaxSpeed * pY) < 1 ? 1 : (verticalJoyControlMaxSpeed * pY);

        if (null == sendVirtualStickDataTimer) {
            sendVirtualStickDataTask = new SendVirtualStickDataTask();
            sendVirtualStickDataTimer = new Timer();
            sendVirtualStickDataTimer.schedule(sendVirtualStickDataTask, 0, 200);
        }

        Log.d(TAG, "Right vertical: " + pY);
    });

Any ideas on how to modify my code to fly an actual drone successfully? Has anyone done it some other way maybe? This code is taken from DJI Demo GitHub.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source