Problem with MavLink


#1

Product: [Bebop]
Product version: [HW_00]
SDK version: [4.0.6]
Use of libARController: [YES]
SDK platform: [Android]

Hi everyone i’m trying to create the autonomous navigation drone system in android devices.

I used the MavLink to fly the drone but i couldn’t fly the drone. I’m having a problem with the saving of mavlink file at these section

public String generateMavlinkFile(LatLng position, float altitude, float yaw) {

    final ARMavlinkFileGenerator generator;

    try {
        generator = new ARMavlinkFileGenerator();
    } catch (ARMavlinkException e) {
        Log.e(Tag, "generateMavlinkFile: " + e.getMessage(), e);
        return "";
    }

    generator.addMissionItem(ARMavlinkMissionItem.CreateMavlinkDelay(10));
    // Take off coordinates
    generator.addMissionItem(ARMavlinkMissionItem.CreateMavlinkTakeoffMissionItem((float) position.latitude, (float) position.longitude, altitude, yaw, 0));
    // Way points - coordinates to travel
    generator.addMissionItem(ARMavlinkMissionItem.CreateMavlinkNavWaypointMissionItem((float) position.latitude + 0.00002000f, (float) position.longitude + 0.00002000f, altitude, yaw));
    // Landing coordinates
    generator.addMissionItem(ARMavlinkMissionItem.CreateMavlinkLandMissionItem((float) position.latitude,(float) position.longitude, 0, 0));
    //lat,long,alt,yaw

    // save our mavlink file
    final File file = new File(Environment.getExternalStoragePublicDirectory(Environment.MAVLINK_STORAGE_DIRECTORY));
    //noinspection ResultOfMethodCallIgnored
    file.mkdirs();

    final String filename = MAVLINK_STORAGE_DIRECTORY + File.separator + "flightPlan.mavlink";
    final File mavFile = new File(filename);

    //noinspection ResultOfMethodCallIgnored
    mavFile.delete();
    generator.CreateMavlinkFile(filename);

    return filename;
}

public void autoFlight(String filename){
    mDeviceController.getFeatureCommon().sendMavlinkStart(filename, ARCOMMANDS_COMMON_MAVLINK_START_TYPE_ENUM.ARCOMMANDS_COMMON_MAVLINK_START_TYPE_FLIGHTPLAN);
    Log.d(TAG,"sending autoFlight Mavlink file");
}

I’m also having problem with the home return function also.

Can someone help me please?


#2

The following are the codes that I wrote.

AutonomousController.java

public class AutonomousController {
private static final String TAG = “BebopDrone”;

private static final int DEVICE_PORT = 21;

public interface Listener {
   
    void onDroneConnectionChanged(ARCONTROLLER_DEVICE_STATE_ENUM state);

    void onBatteryChargeChanged(int batteryPercentage);

    void onPilotingStateChanged(ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_ENUM state);


    void onPictureTaken(ARCOMMANDS_ARDRONE3_MEDIARECORDEVENT_PICTUREEVENTCHANGED_ERROR_ENUM error);

    void configureDecoder(ARControllerCodec codec);

    void onFrameReceived(ARFrame frame);

    void onMatchingMediasFound(int nbMedias);

    void onDownloadComplete(String mediaName);

    void onGPSChange(double latitude, double longtitude, double altitude);

    void onSpeedChange(float north, float east, float down);

    void onAltitudeChange(double altitude);

    void onHomeReturn(double latitude, double longtitude, double altitude);

    void onFlightPath(ARCOMMANDS_COMMON_FLIGHTPLANSTATE_COMPONENTSTATELISTCHANGED_COMPONENT_ENUM component, byte State);

    void onAutoStateChanged(ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM state);

    void noOfSaterllite(Integer gpsSatellite);

    void onAutoAvailableState(byte availabilityState);
}

private final List<AutonomousController.Listener> mListeners;

private final Handler mHandler;

private ARDeviceController mDeviceController;
private SDCardModule mSDCardModule;
private ARCONTROLLER_DEVICE_STATE_ENUM mState;
private ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_ENUM mFlyingState;
private String mCurrentRunId;
private ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM mAutoFlyingState;

public AutonomousController(Context context, @NonNull ARDiscoveryDeviceService deviceService) {

    mListeners = new ArrayList<>();

    // needed because some callbacks will be called on the main thread
    mHandler = new Handler(context.getMainLooper());

    mState = ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_STOPPED;

    // if the product type of the deviceService match with the types supported
    ARDISCOVERY_PRODUCT_ENUM productType = ARDiscoveryService.getProductFromProductID(deviceService.getProductID());
    ARDISCOVERY_PRODUCT_FAMILY_ENUM family = ARDiscoveryService.getProductFamily(productType);
    if (ARDISCOVERY_PRODUCT_FAMILY_ENUM.ARDISCOVERY_PRODUCT_FAMILY_ARDRONE.equals(family)) {

        ARDiscoveryDevice discoveryDevice = createDiscoveryDevice(deviceService, productType);
        if (discoveryDevice != null) {
            mDeviceController = createDeviceController(discoveryDevice);
            discoveryDevice.dispose();
        }

        try
        {
            String productIP = ((ARDiscoveryDeviceNetService)(deviceService.getDevice())).getIp();

            ARUtilsManager ftpListManager = new ARUtilsManager();
            ARUtilsManager ftpQueueManager = new ARUtilsManager();

            ftpListManager.initWifiFtp(productIP, DEVICE_PORT, ARUtilsManager.FTP_ANONYMOUS, "");
            ftpQueueManager.initWifiFtp(productIP, DEVICE_PORT, ARUtilsManager.FTP_ANONYMOUS, "");

            mSDCardModule = new SDCardModule(ftpListManager, ftpQueueManager);
            mSDCardModule.addListener(mSDCardModuleListener);
        }
        catch (ARUtilsException e)
        {
            Log.e(TAG, "Exception", e);
        }

    } else {
        Log.e(TAG, "DeviceService type is not supported by BebopDrone");
    }
}

public void dispose()
{
    if (mDeviceController != null)
        mDeviceController.dispose();
}

//region Listener functions
public void addListener(AutonomousController.Listener listener) {
    mListeners.add(listener);
}

public void removeListener(AutonomousController.Listener listener) {
    mListeners.remove(listener);
}
//endregion Listener

public boolean connect() {
    boolean success = false;
    if ((mDeviceController != null) && (ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_STOPPED.equals(mState))) {
        ARCONTROLLER_ERROR_ENUM error = mDeviceController.start();
        if (error == ARCONTROLLER_ERROR_ENUM.ARCONTROLLER_OK) {
            success = true;
        }
    }
    return success;
}

public boolean disconnect() {
    boolean success = false;
    if ((mDeviceController != null) && (ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING.equals(mState))) {
        ARCONTROLLER_ERROR_ENUM error = mDeviceController.stop();
        if (error == ARCONTROLLER_ERROR_ENUM.ARCONTROLLER_OK) {
            success = true;
        }
    }
    return success;
}

public ARCONTROLLER_DEVICE_STATE_ENUM getConnectionState() {
    return mState;
}

public ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_ENUM getFlyingState() {
    return mFlyingState;
}

public ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM getAutoFlyingState(){
    return mAutoFlyingState;
}

public void pauseMavLink(){
    mDeviceController.getFeatureCommon().sendMavlinkPause();
}

public void stopMavLink(){
    mDeviceController.getFeatureCommon().sendMavlinkStop();
}

public String generateMavlinkFile(LatLng position, float altitude, float yaw) {

    final ARMavlinkFileGenerator generator;

    try {
        generator = new ARMavlinkFileGenerator();
    } catch (ARMavlinkException e) {
        Log.e(Tag, "generateMavlinkFile: " + e.getMessage(), e);
        return "";
    }

    generator.addMissionItem(ARMavlinkMissionItem.CreateMavlinkDelay(10));
    // Take off coordinates
    generator.addMissionItem(ARMavlinkMissionItem.CreateMavlinkTakeoffMissionItem((float) position.latitude, (float) position.longitude, altitude, yaw, 0));
    // Way points - coordinates to travel
    generator.addMissionItem(ARMavlinkMissionItem.CreateMavlinkNavWaypointMissionItem((float) position.latitude + 0.00002000f, (float) position.longitude + 0.00002000f, altitude, yaw));
    // Landing coordinates
    generator.addMissionItem(ARMavlinkMissionItem.CreateMavlinkLandMissionItem((float) position.latitude,(float) position.longitude, 0, 0));
    //lat,long,alt,yaw

    // save our mavlink file
    final File file = new File(Environment.getExternalStoragePublicDirectory(Environment.MAVLINK_STORAGE_DIRECTORY));
    //noinspection ResultOfMethodCallIgnored
    file.mkdirs();

    final String filename = MAVLINK_STORAGE_DIRECTORY + File.separator + "flightPlan.mavlink";
    final File mavFile = new File(filename);

    //noinspection ResultOfMethodCallIgnored
    mavFile.delete();
    generator.CreateMavlinkFile(filename);

    return filename;
}

public void autoFlight(String filename){
    mDeviceController.getFeatureCommon().sendMavlinkStart(filename, ARCOMMANDS_COMMON_MAVLINK_START_TYPE_ENUM.ARCOMMANDS_COMMON_MAVLINK_START_TYPE_FLIGHTPLAN);
    Log.d(TAG,"sending autoFlight Mavlink file");
}

public void takeOff() {
    if ((mDeviceController != null) && (mState.equals(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING))) {
        mDeviceController.getFeatureARDrone3().sendPilotingTakeOff();
    }
}

public void land() {
    if ((mDeviceController != null) && (mState.equals(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING))) {
        mDeviceController.getFeatureARDrone3().sendPilotingLanding();
    }
}

public void emergency() {
    if ((mDeviceController != null) && (mState.equals(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING))) {
        mDeviceController.getFeatureARDrone3().sendPilotingEmergency();
    }
}

public void home(){
    if ((mDeviceController != null) || (mState.equals(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING))) {
        mDeviceController.getFeatureARDrone3().sendPilotingNavigateHome((byte) 1);
    }
}

public void takePicture() {
    if ((mDeviceController != null) && (mState.equals(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING))) {
        mDeviceController.getFeatureARDrone3().sendMediaRecordPictureV2();
    }
}


public void setPitch(byte pitch) {
    if ((mDeviceController != null) && (mState.equals(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING))) {
        mDeviceController.getFeatureARDrone3().setPilotingPCMDPitch(pitch);
    }
}


public void setRoll(byte roll) {
    if ((mDeviceController != null) && (mState.equals(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING))) {
        mDeviceController.getFeatureARDrone3().setPilotingPCMDRoll(roll);
    }
}

public void setYaw(byte yaw) {
    if ((mDeviceController != null) && (mState.equals(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING))) {
        mDeviceController.getFeatureARDrone3().setPilotingPCMDYaw(yaw);
    }
}

public void setGaz(byte gaz) {
    if ((mDeviceController != null) && (mState.equals(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING))) {
        mDeviceController.getFeatureARDrone3().setPilotingPCMDGaz(gaz);
    }
}


public void setFlag(byte flag) {
    if ((mDeviceController != null) && (mState.equals(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING))) {
        mDeviceController.getFeatureARDrone3().setPilotingPCMDFlag(flag);
    }
}

public void getLastFlightMedias() {
    String runId = mCurrentRunId;
    if ((runId != null) && !runId.isEmpty()) {
        mSDCardModule.getFlightMedias(runId);
    } else {
        Log.e(TAG, "RunID not available, fallback to the day's medias");
        mSDCardModule.getTodaysFlightMedias();
    }
}

public void cancelGetLastFlightMedias() {
    mSDCardModule.cancelGetFlightMedias();
}

private ARDiscoveryDevice createDiscoveryDevice(@NonNull ARDiscoveryDeviceService service, ARDISCOVERY_PRODUCT_ENUM productType) {
    ARDiscoveryDevice device = null;
    try {
        device = new ARDiscoveryDevice();

        ARDiscoveryDeviceNetService netDeviceService = (ARDiscoveryDeviceNetService) service.getDevice();
        device.initWifi(productType, netDeviceService.getName(), netDeviceService.getIp(), netDeviceService.getPort());

    } catch (ARDiscoveryException e) {
        Log.e(TAG, "Exception", e);
        Log.e(TAG, "Error: " + e.getError());
    }

    return device;
}

private ARDeviceController createDeviceController(@NonNull ARDiscoveryDevice discoveryDevice) {
    ARDeviceController deviceController = null;
    try {
        deviceController = new ARDeviceController(discoveryDevice);

        deviceController.addListener(mDeviceControllerListener);
        deviceController.addStreamListener(mStreamListener);
    } catch (ARControllerException e) {
        Log.e(TAG, "Exception", e);
    }

    return deviceController;
}

//region notify listener block
private void notifyConnectionChanged(ARCONTROLLER_DEVICE_STATE_ENUM state) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onDroneConnectionChanged(state);
    }
}

private void notifyBatteryChanged(int battery) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onBatteryChargeChanged(battery);
    }
}

private void notifyPilotingStateChanged(ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_ENUM state) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onPilotingStateChanged(state);
    }
}

private void notifyAutoStateChange(ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM state) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onAutoStateChanged(state);
    }
}

private void notifyPictureTaken(ARCOMMANDS_ARDRONE3_MEDIARECORDEVENT_PICTUREEVENTCHANGED_ERROR_ENUM error) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onPictureTaken(error);
    }
}

private void notifyConfigureDecoder(ARControllerCodec codec) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.configureDecoder(codec);
    }
}

private void notifyFrameReceived(ARFrame frame) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onFrameReceived(frame);
    }
}

private void notifyMatchingMediasFound(int nbMedias) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onMatchingMediasFound(nbMedias);
    }
}

private void notifyDownloadProgressed(String mediaName, int progress) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onDownloadProgressed(mediaName, progress);
    }
}

private void notifyDownloadComplete(String mediaName) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onDownloadComplete(mediaName);
    }
}

private void notifyGPSChange(double latitude, double longtitude, double altitude) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onGPSChange(latitude, longtitude, altitude);
    }
}

private void notifySpeedChange(float speedX, float speedY, float speedZ){
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onSpeedChange(speedX, speedY, speedZ);
    }
}

private void notifyAltitudeChange(double altitude){
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onAltitudeChange(altitude);
    }
}

private void notifyHomeReturn(double latitude, double longtitude, double altitude){
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onHomeReturn(latitude, longtitude, altitude);
    }
}

private void notifyAutoFlightPath(ARCOMMANDS_COMMON_FLIGHTPLANSTATE_COMPONENTSTATELISTCHANGED_COMPONENT_ENUM component, byte State){
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onFlightPath(component, State);
    }
}

private void notifyAvailabiltyState(byte availabilityState) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.onAutoAvailableState(availabilityState);
    }
}

private void onUpdateBebopGpsSatellite(Integer gpsSatellite) {
    List<AutonomousController.Listener> listenersCpy = new ArrayList<>(mListeners);
    for (AutonomousController.Listener listener : listenersCpy) {
        listener.noOfSaterllite(gpsSatellite);
    }
}

private final SDCardModule.Listener mSDCardModuleListener = new SDCardModule.Listener() {
    @Override
    public void onMatchingMediasFound(final int nbMedias) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                notifyMatchingMediasFound(nbMedias);
            }
        });
    }

    @Override
    public void onDownloadProgressed(final String mediaName, final int progress) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                notifyDownloadProgressed(mediaName, progress);
            }
        });
    }

    @Override
    public void onDownloadComplete(final String mediaName) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                notifyDownloadComplete(mediaName);
            }
        });
    }
};

private final ARDeviceControllerListener mDeviceControllerListener = new ARDeviceControllerListener() {
    @Override
    public void onStateChanged(ARDeviceController deviceController, ARCONTROLLER_DEVICE_STATE_ENUM newState, ARCONTROLLER_ERROR_ENUM error) {
        mState = newState;
        if (ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING.equals(mState)) {
            mDeviceController.getFeatureARDrone3().sendMediaStreamingVideoEnable((byte) 1);
        } else if (ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_STOPPED.equals(mState)) {
            mSDCardModule.cancelGetFlightMedias();
        }
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                notifyConnectionChanged(mState);
            }
        });
    }

    @Override
    public void onExtensionStateChanged(ARDeviceController deviceController, ARCONTROLLER_DEVICE_STATE_ENUM newState, ARDISCOVERY_PRODUCT_ENUM product, String name, ARCONTROLLER_ERROR_ENUM error) {
    }

    @Override
    public void onCommandReceived(ARDeviceController deviceController, ARCONTROLLER_DICTIONARY_KEY_ENUM commandKey, ARControllerDictionary elementDictionary) {
        if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_COMMON_COMMONSTATE_BATTERYSTATECHANGED) && (elementDictionary != null)) {
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if (args != null) {
                final int battery = (Integer) args.get(ARFeatureCommon.ARCONTROLLER_DICTIONARY_KEY_COMMON_COMMONSTATE_BATTERYSTATECHANGED_PERCENT);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        notifyBatteryChanged(battery);
                    }
                });
            }
        }
        else if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED) && (elementDictionary != null)) {
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if (args != null) {
                final ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_ENUM state = ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_ENUM.getFromValue((Integer) args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE));

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mFlyingState = state;
                        notifyPilotingStateChanged(state);
                    }
                });
            }
        }
        else if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_MEDIARECORDEVENT_PICTUREEVENTCHANGED) && (elementDictionary != null)){
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if (args != null) {
                final ARCOMMANDS_ARDRONE3_MEDIARECORDEVENT_PICTUREEVENTCHANGED_ERROR_ENUM error = ARCOMMANDS_ARDRONE3_MEDIARECORDEVENT_PICTUREEVENTCHANGED_ERROR_ENUM.getFromValue((Integer)args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_MEDIARECORDEVENT_PICTUREEVENTCHANGED_ERROR));
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        notifyPictureTaken(error);
                    }
                });
            }
        }
        else if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_COMMON_RUNSTATE_RUNIDCHANGED) && (elementDictionary != null)){
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if (args != null) {
                final String runID = (String) args.get(ARFeatureCommon.ARCONTROLLER_DICTIONARY_KEY_COMMON_RUNSTATE_RUNIDCHANGED_RUNID);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCurrentRunId = runID;
                    }
                });
            }
        }
        else if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_POSITIONCHANGED) && (elementDictionary != null)){
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if(args != null){
                final double latitude = (double)args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_POSITIONCHANGED_LATITUDE);
                final double longtitude = (double)args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_POSITIONCHANGED_LONGITUDE);
                final double altitude = (double)args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_POSITIONCHANGED_ALTITUDE);
                //Log.d(TAG, "Position changed lat= " + String.valueOf(latitude) + ", lon= " + String.valueOf(longtitude) + ", alt= " + String.valueOf(altitude));

                mHandler.post(new Runnable(){
                    @Override
                    public void run() {
                        notifyGPSChange(latitude,longtitude,altitude);
                    }
                });
            }
        }
        else if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_SPEEDCHANGED) && (elementDictionary != null)) {
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if (args != null) {
                final float speedX = (float) ((Double) args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_SPEEDCHANGED_SPEEDX)).doubleValue();
                final float speedY = (float) ((Double) args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_SPEEDCHANGED_SPEEDY)).doubleValue();
                final float speedZ = (float) ((Double) args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_SPEEDCHANGED_SPEEDZ)).doubleValue();
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        notifySpeedChange(speedX, speedY, speedZ);
                    }
                });
            }
        }
        else if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_ALTITUDECHANGED) && (elementDictionary != null)){
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if (args != null) {
                final double altitude = (double)args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_PILOTINGSTATE_ALTITUDECHANGED_ALTITUDE);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        notifyAltitudeChange(altitude);
                    }
                });
            }
        }
        else if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_GPSSETTINGSSTATE_HOMECHANGED) && (elementDictionary != null)){
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if (args != null) {
                final double latitude = (double)args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_GPSSETTINGSSTATE_HOMECHANGED_LATITUDE);
                final double longitude = (double)args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_GPSSETTINGSSTATE_HOMECHANGED_LONGITUDE);
                final double altitude = (double)args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_GPSSETTINGSSTATE_HOMECHANGED_ALTITUDE);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        notifyHomeReturn(latitude, longitude, altitude);
                    }
                });
            }
        }
        else if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_COMMON_FLIGHTPLANSTATE_COMPONENTSTATELISTCHANGED) && (elementDictionary != null)){
            Iterator<ARControllerArgumentDictionary<Object>> itr = elementDictionary.values().iterator();
            while (itr.hasNext()) {
                ARControllerArgumentDictionary<Object> args = itr.next();
                if (args != null) {
                    final ARCOMMANDS_COMMON_FLIGHTPLANSTATE_COMPONENTSTATELISTCHANGED_COMPONENT_ENUM component = ARCOMMANDS_COMMON_FLIGHTPLANSTATE_COMPONENTSTATELISTCHANGED_COMPONENT_ENUM.getFromValue((Integer)args.get(ARFeatureCommon.ARCONTROLLER_DICTIONARY_KEY_COMMON_FLIGHTPLANSTATE_COMPONENTSTATELISTCHANGED_COMPONENT));
                    final byte State = (byte)((Integer)args.get(ARFeatureCommon.ARCONTROLLER_DICTIONARY_KEY_COMMON_FLIGHTPLANSTATE_COMPONENTSTATELISTCHANGED_STATE)).intValue();
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            notifyAutoFlightPath(component, State);
                        }
                    });
                }
            }
        }
        else if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_COMMON_FLIGHTPLANSTATE_AVAILABILITYSTATECHANGED) && (elementDictionary != null)){
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARControllerDictionary.ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if (args != null) {
                final byte AvailabilityState = (byte)((Integer)args.get(ARFeatureCommon.ARCONTROLLER_DICTIONARY_KEY_COMMON_FLIGHTPLANSTATE_AVAILABILITYSTATECHANGED_AVAILABILITYSTATE)).intValue();
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        notifyAvailabiltyState(AvailabilityState);
                    }
                });
            }
        }
        else if ((commandKey == ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED) && (elementDictionary != null)){
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if (args != null) {
                final ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM state = ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM.getFromValue((Integer)args.get(ARFeatureCommon.ARCONTROLLER_DICTIONARY_KEY_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE));
                String filepath = (String)args.get(ARFeatureCommon.ARCONTROLLER_DICTIONARY_KEY_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_FILEPATH);
                ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_TYPE_ENUM type = ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_TYPE_ENUM.getFromValue((Integer)args.get(ARFeatureCommon.ARCONTROLLER_DICTIONARY_KEY_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_TYPE));
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mAutoFlyingState = state;
                        notifyAutoStateChange(state);
                    }
                });
            }
        }
        else if (commandKey ==  ARCONTROLLER_DICTIONARY_KEY_ENUM.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_GPSSTATE_NUMBEROFSATELLITECHANGED)
        {
            ARControllerArgumentDictionary<Object> args = elementDictionary.get(ARCONTROLLER_DICTIONARY_SINGLE_KEY);
            if (args != null) {
                final Integer gpsSatellite = (Integer)  args.get(ARFeatureARDrone3.ARCONTROLLER_DICTIONARY_KEY_ARDRONE3_GPSSTATE_NUMBEROFSATELLITECHANGED_NUMBEROFSATELLITE);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        onUpdateBebopGpsSatellite(gpsSatellite);  // show value in TextView
                    }
                });
            }
        }
    }
};

private final ARDeviceControllerStreamListener mStreamListener = new ARDeviceControllerStreamListener() {
    @Override
    public ARCONTROLLER_ERROR_ENUM configureDecoder(ARDeviceController deviceController, final ARControllerCodec codec) {
        notifyConfigureDecoder(codec);
        return ARCONTROLLER_ERROR_ENUM.ARCONTROLLER_OK;
    }

    @Override
    public ARCONTROLLER_ERROR_ENUM onFrameReceived(ARDeviceController deviceController, final ARFrame frame) {
        notifyFrameReceived(frame);
        return ARCONTROLLER_ERROR_ENUM.ARCONTROLLER_OK;
    }

    @Override
    public void onFrameTimeout(ARDeviceController deviceController) {}
};

}


#3

AutonomousBebopActivity.java

package com.parrot.sdksample.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.media.Image;
import android.os.Bundle;
import android.os.Process;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.ads.formats.NativeAd;
import com.google.android.gms.maps.model.LatLng;
import com.parrot.arsdk.arcommands.ARCOMMANDS_ARDRONE3_MEDIARECORDEVENT_PICTUREEVENTCHANGED_ERROR_ENUM;
import com.parrot.arsdk.arcommands.ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_ENUM;
import com.parrot.arsdk.arcommands.ARCOMMANDS_COMMON_FLIGHTPLANSTATE_COMPONENTSTATELISTCHANGED_COMPONENT_ENUM;
import com.parrot.arsdk.arcommands.ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM;
import com.parrot.arsdk.arcontroller.ARCONTROLLER_DEVICE_STATE_ENUM;
import com.parrot.arsdk.arcontroller.ARControllerCodec;
import com.parrot.arsdk.arcontroller.ARFrame;
import com.parrot.arsdk.ardiscovery.ARDiscoveryDeviceService;
import com.parrot.sdksample.controller.AutonomousController;
import com.parrot.sdksample.location.Locations;
import com.parrot.sdksample.R;
import com.parrot.sdksample.view.BebopVideoView;

import java.util.ArrayList;

import static com.parrot.arsdk.arcommands.ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_ENUM.ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_HOVERING;
import static com.parrot.arsdk.arcommands.ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM.ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_LOADED;
import static com.parrot.arsdk.arcommands.ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM.ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_PAUSED;
import static com.parrot.arsdk.arcommands.ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM.ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_PLAYING;

public class AutonomousBebopActivity extends AppCompatActivity {

private static final String TAG = "AutonomousBebopActivity";
//private BebopDrone mBebopDrone;
private AutonomousController mBebopDrone;

private float curNorth = 0.0f;
private float curEast = 0.0f;
private float curDown = 0.0f;
private double curAltitude = 0.0;

private ProgressDialog mConnectionProgressDialog;
private ProgressDialog mDownloadProgressDialog;

private BebopVideoView mVideoView;

private TextView mBatteryLabel, mSpeedChange, mAltitudeChange;
private TextView mTakeOffLand, mSaterllite;
private Button mTakeOffLandBt, backBtn;
private Button mDownloadBt;
private ImageView mGpsImage;

private int mNbMaxDownload;
private int mCurrentDownloadIndex;
private double droneLat, droneLng, droneAlt;

ArrayList<Locations> locationList;
ArrayList<Locations> locationList1 = new ArrayList<Locations>();;

ARDiscoveryDeviceService service;

public static final String EXTRA_DEVICE_SERVICE = "EXTRA_DEVICE_SERVICE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_autonomous_bebop);


    Intent intent = getIntent();
    locationList =(ArrayList<Locations>)getIntent().getSerializableExtra("Location_List");
    for(int i = 0; i<locationList.size(); i++){
        Log.i("Auto Location List: ", Integer.toString(locationList.size()));
        Log.i("Location ID: ", Integer.toString(locationList.get(i).getId()));
        Log.i("Location Lat: ", Double.toString(locationList.get(i).getLatitude()));
        Log.i("Location Lng: ", Double.toString(locationList.get(i).getLongtitude()));
    }

    initIHM();

    service = intent.getParcelableExtra(DeviceListActivity.EXTRA_DEVICE_SERVICE);
    mBebopDrone = new AutonomousController(this, service);
    mBebopDrone.addListener(mBebopListener);
    //sleep(60);
    //command();

}

@Override
protected void onStart() {
    super.onStart();

    // show a loading view while the bebop drone is connecting
    if ((mBebopDrone != null) && !(ARCONTROLLER_DEVICE_STATE_ENUM.ARCONTROLLER_DEVICE_STATE_RUNNING.equals(mBebopDrone.getConnectionState())))
    {
        mConnectionProgressDialog = new ProgressDialog(this, R.style.AppCompatAlertDialogStyle);
        mConnectionProgressDialog.setIndeterminate(true);
        mConnectionProgressDialog.setMessage("Connecting ...");
        mConnectionProgressDialog.setCancelable(false);
        mConnectionProgressDialog.show();

        // if the connection to the Bebop fails, finish the activity
        if (!mBebopDrone.connect()) {
            finish();
        }
    }
}

@Override
public void onBackPressed() {
    if (mBebopDrone != null)
    {
        mConnectionProgressDialog = new ProgressDialog(this, R.style.AppCompatAlertDialogStyle);
        mConnectionProgressDialog.setIndeterminate(true);
        mConnectionProgressDialog.setMessage("Disconnecting ...");
        mConnectionProgressDialog.setCancelable(false);
        mConnectionProgressDialog.show();

        if (!mBebopDrone.disconnect()) {
            finish();
        }
    }
}

@Override
public void onDestroy()
{
    mBebopDrone.dispose();
    Process.killProcess(Process.myPid());
    super.onDestroy();
}

public void onClickBack(View v){
    backBtn = (Button) v;

    if (mBebopDrone != null)
    {
        mConnectionProgressDialog = new ProgressDialog(this, R.style.AppCompatAlertDialogStyle);
        mConnectionProgressDialog.setIndeterminate(true);
        mConnectionProgressDialog.setMessage("Disconnecting ...");
        mConnectionProgressDialog.setCancelable(false);
        mConnectionProgressDialog.show();

        if (!mBebopDrone.disconnect()) {

// Intent intent = null;
// intent = new Intent(AutonomousBebopActivity.this, ModeSelectionActivity.class);
//
// if (intent != null) {
// if (service != null) {
// intent.putExtra(EXTRA_DEVICE_SERVICE, service);
// startActivity(intent);
// } else {
// Toast.makeText(getApplicationContext(), “Force shut down the app!”, Toast.LENGTH_SHORT).show();
// }
// }
}
}
}

private void initIHM() {
    mVideoView = (BebopVideoView) findViewById(R.id.videoView);

    // Emergency Landing button on click
    findViewById(R.id.emergencyBt).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mBebopDrone.emergency();
        }
    });

    findViewById(R.id.homeBtn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBebopDrone.home();
        }
    });

    //double lat, lng;

// all give 0.0 value
// if(droneLat != 500 && droneLng != 500 && droneAlt != 500) {
// Log.i("Drone Lng: ", “Drone signal is not accurate!”);
// Log.i("Drone : ", "lat = " + droneLat);
// Log.i("Drone : ", "lng = " + droneLng);
// Log.i("Drone : ", "alt = " + droneAlt);
// //Toast.makeText(getApplicationContext(),“Drone signal is not accurate!”, Toast.LENGTH_SHORT).show();
// }
// else{
// Locations loc = new Locations();
// loc.setId(0);
// loc.setLatitude(droneLat);
// loc.setLongtitude(droneLng);
// locationList1.add(loc);
// for (int i = 1; i < locationList.size(); i++) {
// Locations loc1 = new Locations();
// loc1.setId(i);
// loc1.setLatitude(locationList.get(i).getLatitude());
// loc1.setLongtitude(locationList.get(i).getLongtitude());
// locationList1.add(loc1);
// }
// Log.i("Location Lng: ", “Drone signal is accurate!”);
// }

    mTakeOffLand = (TextView)findViewById(R.id.tvTakeoffLand);
    mTakeOffLandBt = (Button) findViewById(R.id.takeOffOrLandBt);
    mTakeOffLandBt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            LatLng latLng;
            String fileName;
        switch (mBebopDrone.getAutoFlyingState()) {
            case ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_LOADED:
                latLng = new LatLng(droneLat,droneLng);
                fileName = mBebopDrone.generateMavlinkFile(latLng, 1.5f, 1.5f);
                mBebopDrone.autoFlight(fileName);
                break;
            case ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_PLAYING:

// for (int i = 0; i < locationList.size(); i++) {
// LatLng latLng = new LatLng(locationList.get(i).getLatitude(), locationList.get(i).getLongtitude());
// //mBebopDrone.goToPosition(latLng, 5.0f, 20.0f);
// }
latLng = new LatLng(droneLat,droneLng);
// mBebopDrone.goToPosition(getBaseContext(),latLng,1.5f, 1.0f);
fileName = mBebopDrone.generateMavlinkFile(latLng, 1.5f, 1.5f);
mBebopDrone.autoFlight(fileName);
break;
case ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_PAUSED:
case ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_STOPPED:
default:
}
}

// switch (mBebopDrone.getFlyingState()) {
// case ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_LANDED:
// mBebopDrone.takeOff();
// break;
// case ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_FLYING:
// case ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_HOVERING:
// mBebopDrone.land();
// break;
// default:
// }
// }
});

    // Take picture button on click
    findViewById(R.id.takePictureBt).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mBebopDrone.takePicture();
        }
    });

    // Download button on click
    mDownloadBt = (Button)findViewById(R.id.downloadBt);
    mDownloadBt.setEnabled(false);
    mDownloadBt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mBebopDrone.getLastFlightMedias();

            mDownloadProgressDialog = new ProgressDialog(AutonomousBebopActivity.this, R.style.AppCompatAlertDialogStyle);
            mDownloadProgressDialog.setIndeterminate(true);
            mDownloadProgressDialog.setMessage("Fetching medias");
            mDownloadProgressDialog.setCancelable(false);
            mDownloadProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mBebopDrone.cancelGetLastFlightMedias();
                }
            });
            mDownloadProgressDialog.show();
        }
    });

    //  Go up button
    /*findViewById(R.id.gazUpBt).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.setPressed(true);
                    mBebopDrone.setGaz((byte) 50);
                    break;

                case MotionEvent.ACTION_UP:
                    v.setPressed(false);
                    mBebopDrone.setGaz((byte) 0);
                    break;

                default:

                    break;
            }

            return true;
        }
    });*/

    // Go down button
    /*findViewById(R.id.gazDownBt).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.setPressed(true);
                    mBebopDrone.setGaz((byte) -50);
                    break;

                case MotionEvent.ACTION_UP:
                    v.setPressed(false);
                    mBebopDrone.setGaz((byte) 0);
                    break;

                default:

                    break;
            }

            return true;
        }
    });*/

    // yaw left
    /*findViewById(R.id.yawLeftBt).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.setPressed(true);
                    mBebopDrone.setYaw((byte) -50);
                    break;

                case MotionEvent.ACTION_UP:
                    v.setPressed(false);
                    mBebopDrone.setYaw((byte) 0);
                    break;

                default:

                    break;
            }

            return true;
        }
    });*/

    // yaw right
    /*findViewById(R.id.yawRightBt).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.setPressed(true);
                    mBebopDrone.setYaw((byte) 50);
                    break;

                case MotionEvent.ACTION_UP:
                    v.setPressed(false);
                    mBebopDrone.setYaw((byte) 0);
                    break;

                default:

                    break;
            }

            return true;
        }
    });*/

    // roll forward
    /*findViewById(R.id.forwardBt).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.setPressed(true);
                    mBebopDrone.setPitch((byte) 50);
                    mBebopDrone.setFlag((byte) 1);
                    break;

                case MotionEvent.ACTION_UP:
                    v.setPressed(false);
                    mBebopDrone.setPitch((byte) 0);
                    mBebopDrone.setFlag((byte) 0);
                    break;

                default:

                    break;
            }

            return true;
        }
    });*/

    // roll back
    /*findViewById(R.id.backBt).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.setPressed(true);
                    mBebopDrone.setPitch((byte) -50);
                    mBebopDrone.setFlag((byte) 1);
                    break;

                case MotionEvent.ACTION_UP:
                    v.setPressed(false);
                    mBebopDrone.setPitch((byte) 0);
                    mBebopDrone.setFlag((byte) 0);
                    break;

                default:

                    break;
            }

            return true;
        }
    });*/

    // roll left
    /*findViewById(R.id.rollLeftBt).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.setPressed(true);
                    mBebopDrone.setRoll((byte) -50);
                    mBebopDrone.setFlag((byte) 1);
                    break;

                case MotionEvent.ACTION_UP:
                    v.setPressed(false);
                    mBebopDrone.setRoll((byte) 0);
                    mBebopDrone.setFlag((byte) 0);
                    break;

                default:

                    break;
            }

            return true;
        }
    });*/

    //roll right
    /*findViewById(R.id.rollRightBt).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.setPressed(true);
                    mBebopDrone.setRoll((byte) 50);
                    mBebopDrone.setFlag((byte) 1);
                    break;

                case MotionEvent.ACTION_UP:
                    v.setPressed(false);
                    mBebopDrone.setRoll((byte) 0);
                    mBebopDrone.setFlag((byte) 0);
                    break;

                default:

                    break;
            }

            return true;
        }
    });*/

    // percentage symbol
    mBatteryLabel = (TextView) findViewById(R.id.batteryLabel);
    mSpeedChange = (TextView) findViewById(R.id.tbSpeed);
    mAltitudeChange = (TextView) findViewById(R.id.tbAltitude);
    mGpsImage = (ImageView) findViewById(R.id.imgDrone);
    mSaterllite = (TextView) findViewById(R.id.tvTakeoffLand);

}

private final AutonomousController.Listener mBebopListener = new AutonomousController.Listener() {
    //Change of drone connection
    @Override
    public void onDroneConnectionChanged(ARCONTROLLER_DEVICE_STATE_ENUM state) {
        switch (state)
        {
            case ARCONTROLLER_DEVICE_STATE_RUNNING:
                mConnectionProgressDialog.dismiss();
                break;

            case ARCONTROLLER_DEVICE_STATE_STOPPED:
                // if the deviceController is stopped, go back to the previous activity
                mConnectionProgressDialog.dismiss();
                finish();
                break;

            default:
                break;
        }
    }

    // Drone Battery Level indicator
    @Override
    public void onBatteryChargeChanged(int batteryPercentage) {
        mBatteryLabel.setText(String.format("%d%%", batteryPercentage));
        if(batteryPercentage <= 25){
            mBatteryLabel.setTextColor(Color.BLUE);
            if((mBebopDrone.getFlyingState().equals(ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_PLAYING)
                    || mBebopDrone.getAutoFlyingState().equals(ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_LOADED)
                    || mBebopDrone.getAutoFlyingState().equals(ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_PAUSED))
                    && batteryPercentage <= 23){
                mBebopDrone.home();
            }
        }
        else if(batteryPercentage <= 15){
            mBatteryLabel.setTextColor(Color.RED);
            if((mBebopDrone.getFlyingState().equals(ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_PLAYING)
                    || mBebopDrone.getAutoFlyingState().equals(ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_LOADED)
                    || mBebopDrone.getAutoFlyingState().equals(ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_PAUSED))
                    && batteryPercentage <= 13) {
                mBebopDrone.emergency();
            }
        }
        else{
            mBatteryLabel.setTextColor(Color.GREEN);
        }
    }

    // Display Pilot state change
    @Override
    public void onPilotingStateChanged(ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_ENUM state) {
        //mTakeOffLand = (TextView) findViewById(R.id.tvTakeoffLand);
        switch (state) {
            case ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_LANDED:
                //textview
                //mTakeOffLand.setText("Take Off");
                //mTakeOffLand.setEnabled(true);
                //button
                mTakeOffLandBt.setText("Take off");
                mTakeOffLandBt.setEnabled(true);
                mDownloadBt.setEnabled(true);
                break;
            case ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_FLYING:
            case ARCOMMANDS_ARDRONE3_PILOTINGSTATE_FLYINGSTATECHANGED_STATE_HOVERING:
                //texteview
                //mTakeOffLand.setText("Land");
                //mTakeOffLand.setEnabled(true);
                //button
                mTakeOffLandBt.setText("Land");
                mTakeOffLandBt.setEnabled(true);
                mDownloadBt.setEnabled(false);
                break;
            default:
                //textview
                //mTakeOffLand.setEnabled(false);
                //buttton
                mTakeOffLandBt.setEnabled(false);
                mDownloadBt.setEnabled(false);
        }
    }

    @Override
    public void onPictureTaken(ARCOMMANDS_ARDRONE3_MEDIARECORDEVENT_PICTUREEVENTCHANGED_ERROR_ENUM error) {
        Toast.makeText(getApplicationContext(),"Picture has been taken", Toast.LENGTH_SHORT).show();
        //mPictComment.setText("Picture has been taken");
        //Log.i(TAG, "Picture has been taken");
    }

    @Override
    public void configureDecoder(ARControllerCodec codec) {
        mVideoView.configureDecoder(codec);
    }

    @Override
    public void onFrameReceived(ARFrame frame) {
        mVideoView.displayFrame(frame);
    }

    // download medias
    @Override
    public void onMatchingMediasFound(int nbMedias) {
        mDownloadProgressDialog.dismiss();

        mNbMaxDownload = nbMedias;
        mCurrentDownloadIndex = 1;

        if (nbMedias > 0) {
            mDownloadProgressDialog = new ProgressDialog(AutonomousBebopActivity.this, R.style.AppCompatAlertDialogStyle);
            mDownloadProgressDialog.setIndeterminate(false);
            mDownloadProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mDownloadProgressDialog.setMessage("Downloading medias");
            mDownloadProgressDialog.setMax(mNbMaxDownload * 100);
            mDownloadProgressDialog.setSecondaryProgress(mCurrentDownloadIndex * 100);
            mDownloadProgressDialog.setProgress(0);
            mDownloadProgressDialog.setCancelable(false);
            mDownloadProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mBebopDrone.cancelGetLastFlightMedias();
                }
            });
            mDownloadProgressDialog.show();
        }
    }

    @Override
    public void onDownloadProgressed(String mediaName, int progress) {
        mDownloadProgressDialog.setProgress(((mCurrentDownloadIndex - 1) * 100) + progress);
    }

    @Override
    public void onDownloadComplete(String mediaName) {
        mCurrentDownloadIndex++;
        mDownloadProgressDialog.setSecondaryProgress(mCurrentDownloadIndex * 100);

        if (mCurrentDownloadIndex > mNbMaxDownload) {
            mDownloadProgressDialog.dismiss();
            mDownloadProgressDialog = null;
        }
    }

    @Override
    public void onGPSChange(double latitude, double longtitude, double altitude) {
        droneLat = latitude;
        droneLng = longtitude;
        droneAlt = altitude;
        Toast.makeText(getApplicationContext(),"Lat = " + latitude + ", lon = " + longtitude + ", alt = " + altitude, Toast.LENGTH_SHORT).show();
        //mPictComment.setText("Picture has been taken");
        //Log.i(TAG, "Picture has been taken");
        if(latitude == 500.0 && longtitude == 500.0 && altitude == 500.0){
            mGpsImage.setImageResource(R.drawable.gps_lost_icon);
        }
        else{
            mGpsImage.setImageResource(R.drawable.gps_icon);
        }
    }

    @Override
    public void onSpeedChange(float north, float east, float down){
        if (curDown < down || curDown > down){
            curDown = down;
            mSpeedChange.setText(String.format("%.0f%%", curDown));
        }
        else if(curEast < east || curEast > east){
            curEast = east;
            mSpeedChange.setText(String.format("%.0f%%", curEast));
        }
        else if(curNorth < north || curNorth > north){
            curNorth = north;
            mSpeedChange.setText(String.format("%.0f%%", curNorth));
        }
    }

    @Override
    public void onAltitudeChange(double altitude) {
        if (curAltitude < altitude){
            curAltitude = altitude;
            mAltitudeChange.setText(String.format("%.0f%%", curAltitude));
        }
    }

    @Override
    public void onHomeReturn(double latitude, double longtitude, double altitude) {

    }

    @Override
    public void onFlightPath(ARCOMMANDS_COMMON_FLIGHTPLANSTATE_COMPONENTSTATELISTCHANGED_COMPONENT_ENUM component, byte State) {

    }

    @Override
    public void onAutoStateChanged(ARCOMMANDS_COMMON_MAVLINKSTATE_MAVLINKFILEPLAYINGSTATECHANGED_STATE_ENUM state) {

    }

    @Override
    public void noOfSaterllite(Integer gpsSatellite) {
        mSaterllite.setText(String.format("%d", gpsSatellite));
    }

    @Override
    public void onAutoAvailableState(byte availabilityState) {

    }
};

public boolean gazAndYaw(String direction){

    if(direction == "gazUp")
    {
        mBebopDrone.setGaz((byte) 50);
        return true;
    }
    else if(direction == "gazDown")
    {
        mBebopDrone.setGaz((byte) -50);
        return true;
    }
    else if(direction == "stopGaz")
    {
        mBebopDrone.setGaz((byte) 0);
        return true;
    }
    else if(direction == "yawLeft")
    {
        mBebopDrone.setYaw((byte) -50);
        return true;
    }
    else if(direction == "yawRight")
    {
        mBebopDrone.setYaw((byte) 50);
        return true;
    }
    else if(direction == "stopYaw")
    {
        mBebopDrone.setYaw((byte) 0);
        return true;
    }
    else
    {
        mConnectionProgressDialog.setMessage("Wrong direction for Gaz/Yaw ...");
    }
    return false;
}

public boolean roll(String direction){
    if(direction == "rollForward")
    {
        mBebopDrone.setPitch((byte) 50);
        mBebopDrone.setFlag((byte) 1);
        return true;
    }
    else if(direction == "rollBack")
    {
        mBebopDrone.setPitch((byte) -50);
        mBebopDrone.setFlag((byte) 1);
        return true;
    }
    else if (direction == "stopForwardBack")
    {
        mBebopDrone.setPitch((byte) 0);
        mBebopDrone.setFlag((byte) 0);
        return true;
    }
    else if (direction == "rollLeft")
    {
        mBebopDrone.setRoll((byte) -50);
        mBebopDrone.setFlag((byte) 1);
        return true;
    }
    else if (direction == "rollRight")
    {
        mBebopDrone.setRoll((byte) 50);
        mBebopDrone.setFlag((byte) 1);
        return true;
    }
    else if (direction == "stopRoll")
    {
        mBebopDrone.setRoll((byte) 0);
        mBebopDrone.setFlag((byte) 0);
        return true;
    }
    else
    {
        mConnectionProgressDialog.setMessage("Wrong direction for Roll ...");
    }
    return false;
}

public void command(){

}

}