Facebook
From Gray Motmot, 5 Years ago, written in Java.
This paste is a reply to Untitled from Tacky Armadillo - view diff
Embed
Download Paste or View Raw
Hits: 372
  1. public class MainActivity extends Activity implements JoystickView.RightJoystickListener
  2.         ,JoystickView.LeftJoystickListener, AttitudeListener {
  3.  
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_main);
  8.         JoystickView joystick = findViewById(R.id.left_joystick);
  9.         joystick.setZOrderOnTop(true);
  10.         joystick.getHolder().setFormat(PixelFormat.TRANSLUCENT);
  11.         JoystickView joystick2 = findViewById(R.id.right_joystick);
  12.         joystick2.setZOrderOnTop(true);
  13.         joystick2.getHolder().setFormat(PixelFormat.TRANSLUCENT);
  14.  
  15.  
  16.         WifiManager wifi = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  17.         final TextView textView = findViewById(R.id.text_init);
  18.         textView.setText("\n Connected to " + wifi.getConnectionInfo().getSSID());
  19.  
  20.         initialize();
  21.  
  22.         initButtons();
  23.  
  24.         Toast.makeText(this, "Touch and hold the buttons", Toast.LENGTH_SHORT).show();
  25.  
  26.     }
  27.     private void initialize()
  28.     {
  29.         final TextView text = findViewById(R.id.text_init);
  30.  
  31.         DroneApp app = (DroneApp)getApplication();
  32.         IARDrone drone = app.getARDrone();
  33.  
  34.         try
  35.         {
  36.             drone.start();
  37.         }
  38.         catch(Exception exc)
  39.         {
  40.             exc.printStackTrace();
  41.  
  42.             if (drone != null)
  43.                 drone.stop();
  44.         }
  45.     }
  46.     public boolean onKeyDown(int keyCode, KeyEvent event)
  47.     {
  48.         if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
  49.         {
  50.             new AlertDialog.Builder(this).setMessage("Upon exiting, drone will be disconnected !").setTitle("Exit YADrone ?").setCancelable(false).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  51.                 public void onClick(DialogInterface dialog, int whichButton)
  52.                 {
  53.                     DroneApp app = (DroneApp)getApplication();
  54.                     IARDrone drone = app.getARDrone();
  55.                     drone.stop();
  56.  
  57.                     finish();
  58.                 }
  59.             }).setNeutralButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
  60.                 public void onClick(DialogInterface dialog, int whichButton)
  61.                 {
  62.                     // User selected Cancel, nothing to do here.
  63.                 }
  64.             }).show();
  65.  
  66.             return true;
  67.         }
  68.  
  69.         return super.onKeyDown(keyCode, event);
  70.     }
  71.  
  72.  
  73.     @Override
  74.     public void onLeftJoystickMoved(float xPercent, float yPercent, int id) {
  75.         DroneApp app = (DroneApp) getApplication();
  76.         IARDrone drone = app.getARDrone();
  77.         if(id == R.id.right_joystick) {
  78.             if ((xPercent > -0.3 && xPercent < 0.3)&& (yPercent < 0.3 && yPercent > -0.3 )){
  79.                 drone.getCommandManager().hover();
  80.             }
  81.             if(xPercent > 0.3) {
  82.                 drone.getCommandManager().goRight(20);
  83.             }
  84.             if(xPercent < -0.3) {
  85.                 drone.getCommandManager().goLeft(20);
  86.             }
  87.             if (yPercent > 0.3) {
  88.                 drone.getCommandManager().backward(20);
  89.             }
  90.             if (yPercent < -0.3) {
  91.                 drone.getCommandManager().forward(20);
  92.             }
  93.             if(yPercent > 0.3 && xPercent > 0.3) {
  94.                 drone.getCommandManager().move(0.2f, 0.2f,0f , 0f );
  95.             }
  96.             if(yPercent < -0.3 && xPercent > 0.3) {
  97.                 drone.getCommandManager().move(0.2f, -0.2f,0f , 0f );
  98.             }
  99.             if(yPercent < -0.3 && xPercent < -0.3) {
  100.                 drone.getCommandManager().move(-0.2f, -0.2f,0f , 0f );
  101.             }
  102.             if(yPercent > 0.3 && xPercent < -0.3) {
  103.                 drone.getCommandManager().move(-0.2f, 0.2f,0f , 0f );
  104.             }
  105.  
  106.         }
  107.     }
  108.  
  109.     @Override
  110.     public void onRightJoystickMoved(float xPercent, float yPercent, int id) {
  111.         DroneApp app = (DroneApp) getApplication();
  112.         IARDrone drone = app.getARDrone();
  113.  
  114.  
  115.         if(id == R.id.left_joystick) {
  116.             if ((xPercent > -0.3 && xPercent < 0.3)&& (yPercent < 0.3 && yPercent > -0.3 )){
  117.                 drone.getCommandManager().hover();
  118.             }
  119.             if(xPercent > 0.3) {
  120.                 drone.getCommandManager().spinRight(50);
  121.             }
  122.             if(xPercent < -0.3) {
  123.                 drone.getCommandManager().spinLeft(50);
  124.             }
  125.             if (yPercent > 0.3) {
  126.                 drone.getCommandManager().down(50);
  127.             }
  128.             if (yPercent < -0.3) {
  129.                 drone.getCommandManager().up(50);
  130.             }
  131.  
  132.         }
  133.     }
  134.  
  135.  
  136.     public void onResume()
  137.     {
  138.         super.onResume();
  139.         DroneApp app = (DroneApp) getApplication();
  140.         IARDrone drone = app.getARDrone();
  141.  
  142.         drone.getNavDataManager().addAttitudeListener(this);
  143.     }
  144.  
  145.     public void onPause()
  146.     {
  147.         super.onPause();
  148.         DroneApp app = (DroneApp) getApplication();
  149.         IARDrone drone = app.getARDrone();
  150.  
  151.         drone.getNavDataManager().removeAttitudeListener(this);
  152.     }
  153.  
  154.     public void attitudeUpdated(final float pitch, final float roll, final float yaw)
  155.     {
  156.         final TextView text = (TextView)findViewById(R.id.text_navdata);
  157.  
  158.         runOnUiThread(new Runnable() {
  159.             public void run()
  160.             {
  161.                 text.setText("Pitch: " + pitch + " Roll: " + roll + " Yaw: " + yaw  );
  162.             }
  163.         });
  164.     }
  165.  
  166.     @Override
  167.     public void attitudeUpdated(float v, float v1) {
  168.  
  169.     }
  170.  
  171.     @Override
  172.     public void windCompensation(float v, float v1) {
  173.  
  174.     }
  175.     private void initButtons() {
  176.         DroneApp app = (DroneApp)getApplication();
  177.         final IARDrone drone = app.getARDrone();
  178.  
  179.         Button emergency = (Button)findViewById(R.id.emergency);
  180.         emergency.setOnClickListener(new View.OnClickListener() {
  181.             public void onClick(View v)
  182.             {
  183.                 drone.reset();
  184.             }
  185.         });
  186.  
  187.         final Button landing = (Button)findViewById(R.id.take_off);
  188.         landing.setOnClickListener(new View.OnClickListener() {
  189.             boolean isFlying = false;
  190.             public void onClick(View v)
  191.             {
  192.                 if (!isFlying)
  193.                 {
  194.                     drone.takeOff();
  195.                     drone.hover();
  196.                     landing.setText("LAND");
  197.                 }
  198.                 else
  199.                 {
  200.                     drone.landing();
  201.                     landing.setText("TAKE OFF");
  202.                 }
  203.                 isFlying = !isFlying;
  204.             }
  205.         });
  206.  
  207.  
  208.     }
  209.  
  210. }