Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android APP with DroneKit-Android connects to PX4 SITL JMAVSim fails #440

Open
seantobe opened this issue Sep 9, 2016 · 1 comment
Open

Comments

@seantobe
Copy link

seantobe commented Sep 9, 2016

Dear all:
I am trying to use Dronekit-Android to develop an Android APP that can take off the plane on JMAVSim simulator. However, I cannot even connect the drone. If anybody knows, please reply, thanks. I posted my code as below. When I run the APP and click on the button, it crashes on the socket connection part. Am I using it right?

1. run JMAVSim on ubuntu sucessfully #2. run my APP and press the button to connect

package com.o3dr.hellodrone;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

import com.o3dr.android.client.ControlTower;
import com.o3dr.android.client.Drone;
import com.o3dr.android.client.interfaces.DroneListener;
import com.o3dr.android.client.interfaces.TowerListener;
import com.o3dr.services.android.lib.drone.attribute.AttributeEvent;
import com.o3dr.services.android.lib.drone.connection.ConnectionParameter;
import com.o3dr.services.android.lib.drone.connection.ConnectionResult;
import com.o3dr.services.android.lib.drone.connection.ConnectionType;
import com.o3dr.services.android.lib.drone.property.Type;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

//listen for events sent from the Drone library to APP
public class MainActivity extends ActionBarActivity implements DroneListener, TowerListener {

@Override
public void onDroneEvent(String event, Bundle extras) {
    switch (event) {
        case AttributeEvent.STATE_CONNECTED:
            alertUser("Drone Connected");
            updateConnectedButton(this.drone.isConnected());
            break;

        case AttributeEvent.STATE_DISCONNECTED:
            alertUser("Drone Disconnected");
            updateConnectedButton(this.drone.isConnected());
            break;

        default:
            break;
    }
}

/*
@OverRide
public void onDroneConnectionFailed(ConnectionResult result) {

}

*/
@OverRide
public void onDroneServiceInterrupted(String errorMsg) {

}

private ControlTower controlTower;
private Drone drone;
private int droneType = Type.TYPE_UNKNOWN;

//In order to register with the control tower, the drone instance needs a generic Android handler.
//Go ahead and add a handler where you declare your instance variables.
private final Handler handler = new Handler();

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

    // Initialize the service manager, to manage the communication to the Client library
    this.controlTower = new ControlTower(getApplicationContext());
    this.drone = new Drone(getApplicationContext());

    Toast.makeText(getApplicationContext(), "onCreate", Toast.LENGTH_LONG).show();
}

@Override
public void onStart() {
    super.onStart();
    this.controlTower.connect(this);

    Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_LONG).show();
}

@Override
public void onStop() {
    super.onStop();
    if (this.drone.isConnected()) {
        this.drone.disconnect();
        updateConnectedButton(false);
    }
    this.controlTower.unregisterDrone(this.drone);
    this.controlTower.disconnect();

    Toast.makeText(getApplicationContext(), "onStop", Toast.LENGTH_LONG).show();
}

// DroneKit-Android Listener
@Override
public void onTowerConnected() {
    this.controlTower.registerDrone(this.drone, this.handler);
    this.drone.registerDroneListener(this);

    Toast.makeText(getApplicationContext(), "onTowerConnected", Toast.LENGTH_LONG).show();
}

@Override
public void onTowerDisconnected() {
    Toast.makeText(getApplicationContext(), "onTowerDisconnected", Toast.LENGTH_LONG).show();
}

//1. If the drone is connected, use this button to disconnect.
//2. If the drone isn’t connected, build a set of connection parameters and connect.
public void onBtnConnectTap(View view) {
    if(this.drone.isConnected()) {
        this.drone.disconnect();
        Toast.makeText(getApplicationContext(), "onBtnConnectTap, drone disconnect", Toast.LENGTH_LONG).show();
    } else {
        int timeout = 10000;

        System.out.printf("Attempting: %s port: %s ....\n", "127.0.0.1", "14550" );

        Socket socket = new Socket();
        InetSocketAddress endPoint = new InetSocketAddress( "127.0.0.1",
                Integer.parseInt( "14550" )  );

        if ( endPoint.isUnresolved() ) {

            System.out.println("Failure " + endPoint );

        } else try {

            socket.connect(  endPoint , timeout );
            System.out.printf("Success:    %s  \n",  endPoint );

        } catch( IOException ioe ) {

            System.out.printf("Failure:    %s message: %s - %s \n",
                    endPoint , ioe.getClass().getSimpleName(),  ioe.getMessage());

        } finally {

            if ( socket != null ) try {
                socket.close();
            } catch( IOException ioe ) {}
        }

        //Bundle extraParams = new Bundle();

        //set to 14560, same as in PX4 code "sitl_run.sh", and "simulator_mavlink.cpp"
        //set to 14550, same as in PX4 code, mavlink_main.cpp DEFAULT_REMOTE_PORT_UDP = _remote_port
        //set to 14540, mavlink_main.cpp has set _remote_port to 14540
        //extraParams.putInt(ConnectionType.EXTRA_UDP_SERVER_PORT, ConnectionType.DEFAULT_UDP_SERVER_PORT/*14550*//*14560*//*14550*/); // Set default port to 14550

        //ConnectionParameter connectionParams = new ConnectionParameter(ConnectionType.TYPE_UDP, extraParams, null);//--->this line compile error
        /*ConnectionParameter connectionParams = selectedConnectionType == ConnectionType.TYPE_USB
                ? ConnectionParameter.newUsbConnection(null)
                : ConnectionParameter.newUdpConnection(null);*/
        ConnectionParameter connectionParams = ConnectionParameter.newUdpConnection(ConnectionType.DEFAULT_UDP_SERVER_PORT, null);
        //ConnectionParameter connectionParams = ConnectionParameter.newTcpConnection("127.0.0.1", null);

        //ConnectionParameter connectionParams = ConnectionParameter.newUdpConnection(ConnectionType.DEFAULT_UDP_SERVER_PORT, null, 10000);

        this.drone.connect(connectionParams);

        Toast.makeText(getApplicationContext(), "onBtnConnectTap, drone connect", Toast.LENGTH_LONG).show();
    }
}

protected void alertUser(String message) {
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}

protected void updateConnectedButton(Boolean isConnected) {
    Button connectButton = (Button)findViewById(R.id.btnConnect);
    if (isConnected) {
        connectButton.setText("Disconnect");
    } else {
        connectButton.setText("Connect");
    }
    Toast.makeText(getApplicationContext(), "updateConnectedButton", Toast.LENGTH_LONG).show();
}

}

@abencomo
Copy link

@seantobe, were you able to get it to work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants