Ejemplo n.º 1
0
        public override bool OnKeyUp(Keycode keyCode, KeyEvent e)
        {
            InputDevice device = e.Device;

            if (device != null && device.Id == current_device_id)
            {
                int index = ButtonMapping.OrdinalValue(keyCode);
                if (index >= 0)
                {
                    buttons[index] = 0;
                    if (index == 7)
                    {
                        Tello.takeOff();
                    }
                    if (index == 6)
                    {
                        Tello.land();
                    }
                    axes[4] = buttons[5];
                    Tello.setAxis(axes);

                    //controller_view.Invalidate();
                }
                return(true);
            }
            return(base.OnKeyUp(keyCode, e));
        }
Ejemplo n.º 2
0
        public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
        {
            InputDevice device = e.Device;

            if (device != null && device.Id == current_device_id)
            {
                if (IsGamepad(device))
                {
                    if (keyCode == Preferences.takeoffButtonCode && e.RepeatCount == 7)
                    {
                        if (Tello.connected && !Tello.state.flying)
                        {
                            Tello.takeOff();
                        }
                        else if (Tello.connected && Tello.state.flying)
                        {
                            Tello.land();
                        }
                        return(true);
                    }
                    if (keyCode == Preferences.landButtonCode && e.RepeatCount == 7)
                    {
                        Tello.land();
                        return(true);
                    }
                    if (keyCode == Preferences.pictureButtonCode && e.RepeatCount == 0)
                    {
                        Tello.takePicture();
                        cameraShutterSound.Play();
                        return(true);
                    }
                    ;
                    if (keyCode == Preferences.recButtonCode && e.RepeatCount == 0)
                    {
                        toggleRecording = true;
                        return(true);
                    }
                    ;
                    //controller_view.Invalidate();
                    if (keyCode == Preferences.speedButtonCode)
                    {
                        Tello.controllerState.setSpeedMode(1);
                        Tello.sendControllerUpdate();
                        return(true);
                    }

                    //if joy button return handled.
                    if (keyCode >= Keycode.ButtonA && keyCode <= Keycode.ButtonMode)
                    {
                        return(true);
                    }
                    ;
                }
            }
            return(base.OnKeyDown(keyCode, e));
        }
        //This is called when you press 'T'
        public void AutoTakeOff()
        {
            Debug.Log("TakeOff!");
            var preFlightPanel = GameObject.Find("Pre Flight Panel");

            if (preFlightPanel)
            {
                preFlightPanel.SetActive(false);
            }
            Tello.takeOff();
            sceneManager.flightStatus = SceneManager.FlightStatus.Launching;
        }
Ejemplo n.º 4
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            Tello.takeOff();
        }
        else if (Input.GetKeyDown(KeyCode.L))
        {
            Tello.land();
        }

        float lx = 0f;
        float ly = 0f;
        float rx = 0f;
        float ry = 0f;

        if (Input.GetKey(KeyCode.UpArrow))
        {
            ry = 1;
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            ry = -1;
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            rx = 1;
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            rx = -1;
        }
        if (Input.GetKey(KeyCode.W))
        {
            ly = 1;
        }
        if (Input.GetKey(KeyCode.S))
        {
            ly = -1;
        }
        if (Input.GetKey(KeyCode.D))
        {
            lx = 1;
        }
        if (Input.GetKey(KeyCode.A))
        {
            lx = -1;
        }
        Tello.controllerState.setAxis(lx, ly, rx, ry);
    }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            //subscribe to Tello connection events
            Tello.onConnection += (Tello.ConnectionState newState) =>
            {
                if (newState == Tello.ConnectionState.Connected)
                {
                    //When connected update maxHeight to 5 meters
                    Tello.setMaxHeight(5);
                }
                //Show connection messages.
                Console.WriteLine("Tello " + newState.ToString());
            };

            //subscribe to Tello update events. Called when update data arrives from drone.
            Tello.onUpdate += (int cmdId) =>
            {
                if (cmdId == 86)//ac update
                {
                    Console.WriteLine("FlyMode:" + Tello.state.flyMode + " Height:" + Tello.state.height);
                }
            };

            Tello.startConnecting();//Start trying to connect.

            var str = "";

            while (str != "exit")
            {
                str = Console.ReadLine().ToLower();
                if (str == "takeoff" && Tello.connected && !Tello.state.flying)
                {
                    Tello.takeOff();
                }
                if (str == "land" && Tello.connected && Tello.state.flying)
                {
                    Tello.land();
                }
            }
        }
Ejemplo n.º 6
0
        void keyDown(object sender, KeyEventArgs e)
        {
            float[] axis = new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
            switch (e.Key)
            {
            case Key.W:
            {
                axis[3] = sensitivity;
            }
            break;

            case Key.A:
            {
                axis[2] = -sensitivity;
            }
            break;

            case Key.S:
            {
                axis[3] = -sensitivity;
            }
            break;

            case Key.D:
            {
                axis[2] = sensitivity;
            }
            break;

            case Key.Q:
            {
                axis[0] = sensitivity;
            }
            break;

            case Key.E:
            {
                axis[0] = -sensitivity;
            }
            break;

            case Key.Z:
            {
                axis[1] = sensitivity;
            }
            break;

            case Key.X:
            {
                axis[1] = -sensitivity;
            }
            break;

            case Key.D1:
            {
                if (sensitivity + 0.25f <= 1f)
                {
                    sensitivity += 0.25f;
                }
            }
            break;

            case Key.D2:
            {
                if (sensitivity - 0.25f >= 0.0f)
                {
                    sensitivity -= 0.25f;
                }
            }
            break;

            case Key.Tab:
            {
                if (Tello.connected)
                {
                    if (Tello.state.flying)
                    {
                        Tello.land();
                    }
                    else
                    {
                        Tello.takeOff();
                    }
                }
            }
            break;
            }
            Tello.controllerState.setAxis(axis[0], axis[1], axis[2], axis[3]);
        }
Ejemplo n.º 7
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            //force max brightness on screen.
            Window.Attributes.ScreenBrightness = 1f;

            //Full screen and hide nav bar.
            View decorView    = Window.DecorView;
            var  uiOptions    = (int)decorView.SystemUiVisibility;
            var  newUiOptions = (int)uiOptions;

            newUiOptions |= (int)SystemUiFlags.LowProfile;
            newUiOptions |= (int)SystemUiFlags.Fullscreen;
            newUiOptions |= (int)SystemUiFlags.HideNavigation;
            newUiOptions |= (int)SystemUiFlags.Immersive;
            // This option will make bars disappear by themselves
            newUiOptions |= (int)SystemUiFlags.ImmersiveSticky;
            decorView.SystemUiVisibility = (StatusBarVisibility)newUiOptions;

            //Keep screen from dimming.
            this.Window.SetFlags(WindowManagerFlags.KeepScreenOn, WindowManagerFlags.KeepScreenOn);


            onScreenJoyL = FindViewById <JoystickView>(Resource.Id.joystickViewL);
            onScreenJoyR = FindViewById <JoystickView>(Resource.Id.joystickViewR);

            takeoffButton      = FindViewById <ImageButton>(Resource.Id.takeoffButton);
            throwTakeoffButton = FindViewById <ImageButton>(Resource.Id.throwTakeoffButton);

            //subscribe to Tello connection events
            Tello.onConnection += (Tello.ConnectionState newState) =>
            {
                //Update state on screen
                Button cbutton = FindViewById <Button>(Resource.Id.connectButton);

                //If not connected check to see if connected to tello network.
                if (newState != Tello.ConnectionState.Connected)
                {
                    WifiManager wifiManager = (WifiManager)Application.Context.GetSystemService(Context.WifiService);
                    string      ip          = Formatter.FormatIpAddress(wifiManager.ConnectionInfo.IpAddress);
                    if (!ip.StartsWith("192.168.10."))
                    {
                        //CrossTextToSpeech.Current.Speak("No network found.");
                        //Not connected to network.
                        RunOnUiThread(() => {
                            cbutton.Text = "Not Connected. Touch Here.";
                            cbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#55ff3333"));
                        });
                        return;
                    }
                }
                if (newState == Tello.ConnectionState.Connected)
                {
                    //Tello.queryMaxHeight();
                    //Override max hei on connect.
                    Tello.setMaxHeight(30);//meters
                    Tello.queryMaxHeight();

                    //Tello.queryAttAngle();
                    Tello.setAttAngle(25);
                    //Tello.queryAttAngle();

                    Tello.setJpgQuality(Preferences.jpgQuality);

                    CrossTextToSpeech.Current.Speak("Connected");

                    Tello.setPicVidMode(picMode);//0=picture(960x720)

                    Tello.setEV(Preferences.exposure);
                }
                if (newState == Tello.ConnectionState.Disconnected)
                {
                    //if was connected then warn.
                    if (Tello.connectionState == Tello.ConnectionState.Connected)
                    {
                        CrossTextToSpeech.Current.Speak("Disconnected");
                    }
                }
                //update connection state button.
                RunOnUiThread(() => {
                    cbutton.Text = newState.ToString();
                    if (newState == Tello.ConnectionState.Connected)
                    {
                        cbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#6090ee90"));//transparent light green.
                    }
                    else
                    {
                        cbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#ffff00"));//yellow
                    }
                });
            };
            var modeTextView   = FindViewById <TextView>(Resource.Id.modeTextView);
            var hSpeedTextView = FindViewById <TextView>(Resource.Id.hSpeedTextView);
            var vSpeedTextView = FindViewById <TextView>(Resource.Id.vSpeedTextView);
            var heiTextView    = FindViewById <TextView>(Resource.Id.heiTextView);
            var batTextView    = FindViewById <TextView>(Resource.Id.batTextView);
            var wifiTextView   = FindViewById <TextView>(Resource.Id.wifiTextView);

            //Log file setup.
            var logPath      = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "aTello/logs/");;
            var logStartTime = DateTime.Now;
            var logFilePath  = logPath + logStartTime.ToString("yyyy-dd-M--HH-mm-ss") + ".csv";

            if (doStateLogging)
            {
                //write header for cols in log.
                System.IO.Directory.CreateDirectory(logPath);
                File.WriteAllText(logFilePath, "time," + Tello.state.getLogHeader());
            }

            //subscribe to Tello update events
            Tello.onUpdate += (Tello.FlyData newState) =>
            {
                if (doStateLogging)
                {
                    //write update to log.
                    var elapsed = DateTime.Now - logStartTime;
                    File.AppendAllText(logFilePath, elapsed.ToString(@"mm\:ss\:ff\,") + newState.getLogLine());
                }

                RunOnUiThread(() => {
                    //Update state on screen

                    modeTextView.Text   = "FM:" + newState.flyMode;
                    hSpeedTextView.Text = string.Format("HS:{0: 0.0;-0.0}m/s", (float)newState.flySpeed / 10);
                    vSpeedTextView.Text = string.Format("VS:{0: 0.0;-0.0}m/s", (float)newState.verticalSpeed / 10);
                    heiTextView.Text    = string.Format("Hei:{0: 0.0;-0.0}m", (float)newState.height / 10);

                    if (Tello.controllerState.speed > 0)
                    {
                        hSpeedTextView.SetBackgroundColor(Android.Graphics.Color.IndianRed);
                    }
                    else
                    {
                        hSpeedTextView.SetBackgroundColor(Android.Graphics.Color.Transparent);
                    }

                    batTextView.Text  = "Bat:" + newState.batteryPercentage;
                    wifiTextView.Text = "Wifi:" + newState.wifiStrength;

                    //acstat.Text = str;
                    if (Tello.state.flying)
                    {
                        takeoffButton.SetImageResource(Resource.Drawable.land);
                    }
                    else if (!Tello.state.flying)
                    {
                        takeoffButton.SetImageResource(Resource.Drawable.takeoff_white);
                    }
                });
            };

            var videoFrame  = new byte[100 * 1024];
            var videoOffset = 0;

            Video.Decoder.surface = FindViewById <SurfaceView>(Resource.Id.surfaceView).Holder.Surface;

            var path = "aTello/video/";

            System.IO.Directory.CreateDirectory(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path + "cache/"));
            videoFilePath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path + "cache/" + DateTime.Now.ToString("MMMM dd yyyy HH-mm-ss") + ".h264");

            FileStream videoStream = null;

            startUIUpdateThread();
            //updateUI();//hide record light etc.

            //subscribe to Tello video data
            Tello.onVideoData += (byte[] data) =>
            {
                totalVideoBytesReceived += data.Length;
                //Handle recording.
                if (true)                                                             //videoFilePath != null)
                {
                    if (data[2] == 0 && data[3] == 0 && data[4] == 0 && data[5] == 1) //if nal
                    {
                        var nalType = data[6] & 0x1f;
                        //                       if (nalType == 7 || nalType == 8)
                        {
                            if (toggleRecording)
                            {
                                if (videoStream != null)
                                {
                                    videoStream.Close();
                                }
                                videoStream = null;

                                isRecording     = !isRecording;
                                toggleRecording = false;
                                if (isRecording)
                                {
                                    videoFilePath      = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path + DateTime.Now.ToString("MMMM dd yyyy HH-mm-ss") + ".h264");
                                    startRecordingTime = DateTime.Now;
                                    CrossTextToSpeech.Current.Speak("Recording");
                                    updateUI();
                                }
                                else
                                {
                                    videoFilePath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path + "cache/" + DateTime.Now.ToString("MMMM dd yyyy HH-mm-ss") + ".h264");
                                    CrossTextToSpeech.Current.Speak("Recording stopped");
                                    updateUI();
                                }
                            }
                        }
                    }

                    if ((isRecording || Preferences.cacheVideo))
                    {
                        if (videoStream == null)
                        {
                            videoStream = new FileStream(videoFilePath, FileMode.Append);
                        }

                        if (videoStream != null)
                        {
                            //Save raw data minus sequence.
                            videoStream.Write(data, 2, data.Length - 2);//Note remove 2 byte seq when saving.
                        }
                    }
                }

                //Handle video display.
                if (true)//video decoder tests.
                {
                    //Console.WriteLine("1");

                    if (data[2] == 0 && data[3] == 0 && data[4] == 0 && data[5] == 1)//if nal
                    {
                        var nalType = data[6] & 0x1f;
                        if (nalType == 7 || nalType == 8)
                        {
                        }
                        if (videoOffset > 0)
                        {
                            aTello.Video.Decoder.decode(videoFrame.Take(videoOffset).ToArray());
                            videoOffset = 0;
                        }
                        //var nal = (received.bytes[6] & 0x1f);
                        //if (nal != 0x01 && nal != 0x07 && nal != 0x08 && nal != 0x05)
                        //    Console.WriteLine("NAL type:" + nal);
                    }
                    //todo. resquence frames.
                    Array.Copy(data, 2, videoFrame, videoOffset, data.Length - 2);
                    videoOffset += (data.Length - 2);
                }
            };

            onScreenJoyL.onUpdate += OnTouchJoystickMoved;
            onScreenJoyR.onUpdate += OnTouchJoystickMoved;



            Tello.startConnecting();//Start trying to connect.

            //Clicking on network state button will show wifi connection page.
            Button button = FindViewById <Button>(Resource.Id.connectButton);

            button.Click += delegate {
                WifiManager wifiManager = (WifiManager)Application.Context.GetSystemService(Context.WifiService);
                string      ip          = Formatter.FormatIpAddress(wifiManager.ConnectionInfo.IpAddress);
                if (!ip.StartsWith("192.168.10."))//Already connected to network?
                {
                    StartActivity(new Intent(Android.Net.Wifi.WifiManager.ActionPickWifiNetwork));
                }
            };


            takeoffButton.Click += delegate {
                if (Tello.connected && !Tello.state.flying)
                {
                    Tello.takeOff();
                }
                else if (Tello.connected && Tello.state.flying)
                {
                    Tello.land();
                }
            };
            throwTakeoffButton.Click += delegate {
                if (Tello.connected && !Tello.state.flying)
                {
                    Tello.throwTakeOff();
                }
                else if (Tello.connected && Tello.state.flying)
                {
                    //Tello.land();
                }
            };
            var pictureButton = FindViewById <ImageButton>(Resource.Id.pictureButton);

            Tello.picPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "aTello/pics/");
            System.IO.Directory.CreateDirectory(Tello.picPath);


            cameraShutterSound.Load("cameraShutterClick.mp3");
            pictureButton.Click += delegate
            {
                Tello.takePicture();
                cameraShutterSound.Play();
            };
            pictureButton.LongClick += delegate
            {
                //Toggle
                picMode = picMode == 1?0:1;
                Tello.setPicVidMode(picMode);
                aTello.Video.Decoder.reconfig();
            };

            var recordButton = FindViewById <ImageButton>(Resource.Id.recordButton);

            recordButton.Click += delegate
            {
                toggleRecording = true;
            };

            var galleryButton = FindViewById <ImageButton>(Resource.Id.galleryButton);

            galleryButton.Click += async delegate
            {
                //var uri = Android.Net.Uri.FromFile(new Java.IO.File(Tello.picPath));
                //shareImage(uri);
                //return;
                Intent intent = new Intent();
                intent.PutExtra(Intent.ActionView, Tello.picPath);
                intent.SetType("image/*");
                intent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), 1);
            };
            //Settings button
            ImageButton settingsButton = FindViewById <ImageButton>(Resource.Id.settingsButton);

            settingsButton.Click += delegate
            {
                StartActivity(typeof(SettingsActivity));
            };


            //Init joysticks.
            input_manager = (InputManager)GetSystemService(Context.InputService);
            CheckGameControllers();
        }
Ejemplo n.º 8
0
        string videoFilePath;//file to save raw h264 to.

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            //force max brightness on screen.
            Window.Attributes.ScreenBrightness = 1f;

            takeoffButton      = FindViewById <ImageButton>(Resource.Id.takeoffButton);
            throwTakeoffButton = FindViewById <ImageButton>(Resource.Id.throwTakeoffButton);

            var path = "aTello/video/";

            System.IO.Directory.CreateDirectory(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path));
            videoFilePath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".h264");

            //subscribe to Tello connection events
            Tello.onConnection += (Tello.ConnectionState newState) =>
            {
                //Update state on screen
                Button cbutton = FindViewById <Button>(Resource.Id.connectButton);

                //If not connected check to see if connected to tello network.
                if (newState != Tello.ConnectionState.Connected)
                {
                    WifiManager wifiManager = (WifiManager)Application.Context.GetSystemService(Context.WifiService);
                    string      ip          = Formatter.FormatIpAddress(wifiManager.ConnectionInfo.IpAddress);
                    if (!ip.StartsWith("192.168.10."))
                    {
                        //CrossTextToSpeech.Current.Speak("No network found.");

                        //Not connected to network.
                        RunOnUiThread(() => {
                            cbutton.Text = "Not Connected. Touch Here.";
                            cbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#55ff3333"));
                        });
                        return;
                    }
                }
                if (newState == Tello.ConnectionState.Connected)
                {
                    //Tello.queryMaxHeight();
                    //Override max hei on connect.
                    Tello.setMaxHeight(25);//meters
                    Tello.queryMaxHeight();

                    CrossTextToSpeech.Current.Speak("Connected");

                    //Tello.setPicVidMode(0);//0=picture(960x720)

                    //Set new video file name based on date.
                    //var path = "aTello/video/";
                    //System.IO.Directory.CreateDirectory(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path));
                    //videoFilePath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".h264");
                }
                if (newState == Tello.ConnectionState.Disconnected)
                {
                    //if was connected then warn.
                    if (Tello.connectionState == Tello.ConnectionState.Connected)
                    {
                        CrossTextToSpeech.Current.Speak("Disconnected");
                    }
                }
                //update connection state button.
                RunOnUiThread(() => {
                    cbutton.Text = newState.ToString();
                    if (newState == Tello.ConnectionState.Connected)
                    {
                        cbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#6090ee90"));//transparent light green.
                    }
                    else
                    {
                        cbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#ffff00"));//yellow
                    }
                });
            };
            var modeTextView   = FindViewById <TextView>(Resource.Id.modeTextView);
            var hSpeedTextView = FindViewById <TextView>(Resource.Id.hSpeedTextView);
            var vSpeedTextView = FindViewById <TextView>(Resource.Id.vSpeedTextView);
            var heiTextView    = FindViewById <TextView>(Resource.Id.heiTextView);
            var batTextView    = FindViewById <TextView>(Resource.Id.batTextView);
            var wifiTextView   = FindViewById <TextView>(Resource.Id.wifiTextView);

            //Log file setup.
            var logPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "aTello/logs/");;

            System.IO.Directory.CreateDirectory(logPath);
            var logStartTime = DateTime.Now;
            var logFilePath  = logPath + logStartTime.ToString("yyyy-dd-M--HH-mm-ss") + ".csv";

            //write header for cols in log.
            File.WriteAllText(logFilePath, "time," + Tello.state.getLogHeader());

            //subscribe to Tello update events
            Tello.onUpdate += (Tello.FlyData newState) =>
            {
                //write update to log.
                var elapsed = DateTime.Now - logStartTime;
                File.AppendAllText(logFilePath, elapsed.ToString(@"mm\:ss\:ff\,") + newState.getLogLine());

                RunOnUiThread(() => {
                    //Update state on screen
                    //var acstat = FindViewById<TextView>(Resource.Id.ac_state);

                    modeTextView.Text   = "FM:" + newState.flyMode;
                    hSpeedTextView.Text = "HS:" + newState.flySpeed;
                    vSpeedTextView.Text = "VS:" + newState.verticalSpeed;
                    heiTextView.Text    = "Hei:" + newState.height;
                    batTextView.Text    = "Bat:" + newState.batteryPercentage;
                    wifiTextView.Text   = "Wifi:" + newState.wifiStrength;

                    //acstat.Text = str;
                    if (Tello.state.flying)
                    {
                        takeoffButton.SetImageResource(Resource.Drawable.land);
                    }
                    else if (!Tello.state.flying)
                    {
                        takeoffButton.SetImageResource(Resource.Drawable.takeoff_white);
                    }
                });
            };

            var videoFrame  = new byte[100 * 1024];
            var videoOffset = 0;

            Video.Decoder.surface = FindViewById <SurfaceView>(Resource.Id.surfaceView).Holder.Surface;

            //subscribe to Tello video data
            Tello.onVideoData += (byte[] data) =>
            {
                if (false)//videoFilePath != null)
                {
                    //Save raw data minus sequence.
                    using (var stream = new FileStream(videoFilePath, FileMode.Append))
                    {
                        stream.Write(data, 2, data.Length - 2);//Note remove 2 byte seq when saving.
                    }
                }
                if (true)                                                             //video decoder tests.
                {
                    if (data[2] == 0 && data[3] == 0 && data[4] == 0 && data[5] == 1) //if nal
                    {
                        var nalType = data[6] & 0x1f;
                        if (nalType == 7 || nalType == 8)
                        {
                        }
                        if (videoOffset > 0)
                        {
                            aTello.Video.Decoder.decode(videoFrame.Take(videoOffset).ToArray());
                            videoOffset = 0;
                        }
                        //var nal = (received.bytes[6] & 0x1f);
                        //if (nal != 0x01 && nal != 0x07 && nal != 0x08 && nal != 0x05)
                        //    Console.WriteLine("NAL type:" + nal);
                    }
                    Array.Copy(data, 2, videoFrame, videoOffset, data.Length - 2);
                    videoOffset += (data.Length - 2);
                }
            };

            var onScreenJoyL = FindViewById <JoystickView>(Resource.Id.joystickViewL);
            var onScreenJoyR = FindViewById <JoystickView>(Resource.Id.joystickViewR);

            Tello.getControllerCallback = () => {
                if (current_device_id > -1)
                {
                    RunOnUiThread(() =>
                    {
                        onScreenJoyL.Visibility = ViewStates.Invisible;
                        onScreenJoyR.Visibility = ViewStates.Invisible;
                    });
                    return(axes);//joystick
                }
                else
                {
                    RunOnUiThread(() =>
                    {
                        onScreenJoyL.Visibility = ViewStates.Visible;
                        onScreenJoyR.Visibility = ViewStates.Visible;
                    });
                    var touchAxis = new float[] { onScreenJoyL.curX, onScreenJoyL.curY, onScreenJoyR.curX, onScreenJoyR.curY, 0 };
                    return(touchAxis);
                }
            };

            Tello.startConnecting();//Start trying to connect.

            //Clicking on network state button will show wifi connection page.
            Button button = FindViewById <Button>(Resource.Id.connectButton);

            button.Click += delegate {
                WifiManager wifiManager = (WifiManager)Application.Context.GetSystemService(Context.WifiService);
                string      ip          = Formatter.FormatIpAddress(wifiManager.ConnectionInfo.IpAddress);
                if (!ip.StartsWith("192.168.10."))//Already connected to network?
                {
                    StartActivity(new Intent(Android.Net.Wifi.WifiManager.ActionPickWifiNetwork));
                }
            };


            takeoffButton.Click += delegate {
                if (Tello.connected && !Tello.state.flying)
                {
                    Tello.takeOff();
                }
                else if (Tello.connected && Tello.state.flying)
                {
                    Tello.land();
                }
            };
            throwTakeoffButton.Click += delegate {
                if (Tello.connected && !Tello.state.flying)
                {
                    Tello.throwTakeOff();
                }
                else if (Tello.connected && Tello.state.flying)
                {
                    //Tello.land();
                }
            };
            var pictureButton = FindViewById <ImageButton>(Resource.Id.pictureButton);

            Tello.picPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "aTello/pics/");
            System.IO.Directory.CreateDirectory(Tello.picPath);


            var cameraShutterSound = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;

            cameraShutterSound.Load("cameraShutterClick.mp3");
            pictureButton.Click += delegate
            {
                Tello.takePicture();
                cameraShutterSound.Play();
            };

            var galleryButton = FindViewById <ImageButton>(Resource.Id.galleryButton);

            galleryButton.Click += delegate
            {
                Intent intent = new Intent();
                intent.PutExtra(Intent.ActionView, Tello.picPath);
                intent.SetType("image/*");
                intent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), 1);
            };
            //Settings button
            ImageButton settingsButton = FindViewById <ImageButton>(Resource.Id.settingsButton);

            settingsButton.Click += delegate
            {
                StartActivity(typeof(SettingsActivity));
            };


            //Init joysticks.
            input_manager = (InputManager)GetSystemService(Context.InputService);
            CheckGameControllers();
        }
Ejemplo n.º 9
0
    // Update is called once per frame 上記のファンクションを使ってドローンをコントロールしていきます。
    void Update()
    {
        if (Input.GetKey(KeyCode.T) || (OSC_Receiver.GetComponent <OSC_Receiver>().Y_controller1_b1_pressed > 0))
        {
            Tello.takeOff();
            isLanding = true;
            Invoke("setFlyingTrue", 0.2f);
            TelloInitialPos_X = Tello.state.posX;
            TelloInitialPos_Y = Tello.state.posY;
            TelloInitialPos_Z = Tello.state.posZ;
            Invoke("setFinishLandingTrue", 4.5f);
        }
        else if ((Input.GetKeyDown(KeyCode.L)) || (OSC_Receiver.GetComponent <OSC_Receiver>().X_controller1_b3_pressed > 0))
        {
            Tello.land();

            isFlying = false;
        }
        //Debug.Log(OSC_Receiver.GetComponent<OSC_Receiver>().Y_controller1_b1_pressed);

        float lx = 0f;
        float ly = 0f;
        float rx = 0f;
        float ry = 0f;

        RelativeVectorZ();
        //ドローンの高さを対象のゲームオブジェクトに揃えます。&& isFlying && !isLandingで離陸後安定後の飛行中にのみ値が0以外になるように制限します。
        //if ((realDestinationZ > 0.15f) && isFlying && !isLanding)
        //{
        //    ly = 0.3f;
        //    Debug.Log("上昇");
        //}
        //else if ((realDestinationZ < -0.15f) && isFlying && !isLanding)
        //{
        //    ly = -0.3f;
        //    Debug.Log("降下");

        //}
        //else if ((realDestinationZ > 0.075f) && isFlying && !isLanding)
        //{
        //    ly = 0.1f;
        //    Debug.Log("ちょっと上昇");
        //}
        //else if ((realDestinationZ < -0.075f) && isFlying && !isLanding)
        //{
        //    ly = -0.1f;
        //    Debug.Log("ちょっと降下");
        //}
        //else
        //{
        //    ly = 0f;
        //}

        //バーチャルドローンは高さYのみリアルに依存させます。XZはリアルドローンを逆に依存(追従)させます。
        //Drone_Yup_Virtual.transform.position = new Vector3(Drone_Yup_Virtual.transform.position.x, Drone_Yup_Real.transform.position.y, Drone_Yup_Virtual.transform.position.z);


        RelativeRotationY();
        ////ドローンの正面を対象のゲームオブジェクトに向けます。
        //if (((realRotationDifferenceY.y > 0.15f) && (realRotationDifferenceY.w >= 0) && isFlying && !isLanding) || ((realRotationDifferenceY.y < -0.15f) && (realRotationDifferenceY.w < 0) && isFlying && !isLanding))
        //{
        //    lx = 0.8f;
        //    //lx = 0.6f;
        //    isForwardDirectionMatched = false;
        //    Debug.Log("時計回りに回転");
        //}
        //else if (((realRotationDifferenceY.y < -0.15f) && (realRotationDifferenceY.w >= 0) && isFlying && !isLanding) || ((realRotationDifferenceY.y > 0.15f) && (realRotationDifferenceY.w < 0) && isFlying && !isLanding))
        //{
        //    lx = -0.8f;
        //    //lx = -0.6f;
        //    isForwardDirectionMatched = false;
        //    Debug.Log("反対の時計回りに回転");
        //}
        //else if (((realRotationDifferenceY.y > 0.075f) && (realRotationDifferenceY.w >= 0) && isFlying && !isLanding) || ((realRotationDifferenceY.y < -0.075f) && (realRotationDifferenceY.w < 0) && isFlying && !isLanding))
        //{
        //    lx = 0.1f;
        //    isForwardDirectionMatched = true;
        //    Debug.Log("ちょっと時計回りに回転");
        //}
        //else if (((realRotationDifferenceY.y < -0.075f) && (realRotationDifferenceY.w >= 0) && isFlying && !isLanding) || ((realRotationDifferenceY.y > 0.075f && (realRotationDifferenceY.w < 0) && isFlying && !isLanding)))
        //{
        //    lx = -0.1f;
        //    isForwardDirectionMatched = true;
        //    Debug.Log("ちょっと反対の時計回りに回転");
        //}
        //else
        //{
        //    lx = 0f;
        //}

        //デバッグ用
        RelativeForwardDistanceZ();
        //本番
        //if (isFlying && !isLanding)
        //{
        //    RelativeForwardDistanceZ();
        //}


        ////ドローンと対象のゲームオブジェクト距離を一定以下に保ちます。
        //if ((distanceOfRealAndVirtual > 0.5f) && ((realForwardDistanceZbyVirtuallDrone - realForwardDistanceZbyRealDrone) < 0.1f) && isFlying && !isLanding)
        //{
        //    ry = 0.5f;
        //    //ry = 0.2f;
        //    Debug.Log("前に進む");
        //}
        //else if ((distanceOfRealAndVirtual > 0.5f) && ((realForwardDistanceZbyVirtuallDrone - realForwardDistanceZbyRealDrone) > -0.1f) && isFlying && !isLanding)
        //{
        //    ry = -0.5f;
        //    //ry = -0.2f;
        //    Debug.Log("後ろに下がる");
        //}
        //else if ((distanceOfRealAndVirtual > 0.2f) && ((realForwardDistanceZbyVirtuallDrone - realForwardDistanceZbyRealDrone) < 0.1f) && isFlying && !isLanding)
        //{
        //    ry = 0.1f;
        //    Debug.Log("ちょっと前に進む");
        //}
        //else if ((distanceOfRealAndVirtual > 0.2f) && ((realForwardDistanceZbyVirtuallDrone - realForwardDistanceZbyRealDrone) > -0.1f) && isFlying && !isLanding)
        //{
        //    ry = -0.1f;
        //    Debug.Log("ちょっと後ろに下がる");
        //}
        //else
        //{
        //    ry = 0f;
        //}

        //Go Up!
        if (OSC_Receiver.GetComponent <OSC_Receiver>().B_controller2_b1_pressed > 0 || Input.GetKey(KeyCode.W))
        {
            ly = 1;
        }
        //Go Down!
        if (OSC_Receiver.GetComponent <OSC_Receiver>().A_controller2_b3_pressed > 0 || Input.GetKey(KeyCode.S))
        {
            ly = -1;
        }
        //Turn Right!
        if (OSC_Receiver.GetComponent <OSC_Receiver>().stick_controller2_a1x > 0.1f || Input.GetKey(KeyCode.D))
        {
            lx = 1;
        }
        //Turn Left!
        if (OSC_Receiver.GetComponent <OSC_Receiver>().stick_controller2_a1x < -0.1f || Input.GetKey(KeyCode.A))
        {
            lx = -1;
        }
        //Go Right or Left!
        if (Input.GetKey(KeyCode.RightArrow))
        {
            rx = 1;
        }
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            rx = -1;
        }
        else
        {
            rx += OSC_Receiver.GetComponent <OSC_Receiver>().stick_controller1_a1x;
        }

        //Go Forward or  Back!
        if (Input.GetKey(KeyCode.UpArrow))
        {
            ry = 1;
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            ry = -1;
        }
        else
        {
            ry += OSC_Receiver.GetComponent <OSC_Receiver>().stick_controller1_a1y;
        }

        //Debug.Log("ry = " + ry);
        if (isFlying && !isLanding)
        {
            Debug.Log("lx = " + lx + ", ly = " + ly + ", rx = " + rx + ", ry = " + ry);
        }
        //Debug.Log("Tello.state.posX = " + Tello.state.posX + ", Tello.state.posY = " + Tello.state.posY + ", Tello.state.posZ = " + Tello.state.posZ);

        Tello.controllerState.setAxis(lx, ly, rx, ry); //float values
        //Tello.controllerState.setSpeedMode(int mode);


        //センサーからのデータの外れ値をスキップします。
        if ((Mathf.Abs(Tello.state.posX) < 0.05f) && (Mathf.Abs(Tello.state.posY) < 0.05f) && (Mathf.Abs(Tello.state.posZ) < 0.05f) && isFlying && !isLanding)
        {
            Debug.Log("〇〇〇TimeFrame = " + Time.frameCount + " Tello.state.posX = " + Tello.state.posX + ", Tello.state.posY = " + Tello.state.posY + ", Tello.state.posZ = " + Tello.state.posZ + ", Data Collection Error: x = " + TelloCurrentPos_X.ToString("f2") + ", y = " + TelloCurrentPos_Y.ToString("f2") + ", z = " + TelloCurrentPos_Z.ToString("f2") + ", use previous flame: TelloPreviousPos_X = " + TelloPreviousPos_X + ", TelloPreviousPos_Y = " + TelloPreviousPos_Y + ", TelloPreviousPos_Z = " + TelloPreviousPos_Z);
            TelloCurrentPos_X        = TelloPreviousPos_X;
            TelloCurrentPos_Y        = TelloPreviousPos_Y;
            TelloCurrentPos_Z        = TelloPreviousPos_Z;
            TelloCurrentQuaternion_X = TelloPreviousQuaternion_X;
            TelloCurrentQuaternion_Y = TelloPreviousQuaternion_Y;
            TelloCurrentQuaternion_Z = TelloPreviousQuaternion_Z;
            TelloCurrentQuaternion_W = TelloPreviousQuaternion_W;
            //TelloCurrentPos_X = Tello.state.posX;
            //TelloCurrentPos_Y = Tello.state.posY;
            //TelloCurrentPos_Z = Tello.state.posZ;
            //TelloCurrentQuaternion_X = Tello.state.quatX;
            //TelloCurrentQuaternion_Y = Tello.state.quatY;
            //TelloCurrentQuaternion_Z = Tello.state.quatZ;
            //TelloCurrentQuaternion_W = Tello.state.quatW;
        }
        else
        {
            TelloCurrentPos_X        = Tello.state.posX;
            TelloCurrentPos_Y        = Tello.state.posY;
            TelloCurrentPos_Z        = Tello.state.posZ;
            TelloCurrentQuaternion_X = Tello.state.quatX;
            TelloCurrentQuaternion_Y = Tello.state.quatY;
            TelloCurrentQuaternion_Z = Tello.state.quatZ;
            TelloCurrentQuaternion_W = Tello.state.quatW;
        }

        //Debug.Log("Tello_onUpdate : " + "x = " + TelloCurrentPos_X.ToString("f2") + ", y = " + TelloCurrentPos_Y.ToString("f2") + ", z = " + TelloCurrentPos_Z.ToString("f2"));
        TelloPreviousPos_X        = TelloCurrentPos_X;
        TelloPreviousPos_Y        = TelloCurrentPos_Y;
        TelloPreviousPos_Z        = TelloCurrentPos_Z;
        TelloPreviousQuaternion_X = TelloCurrentQuaternion_X;
        TelloPreviousQuaternion_Y = TelloCurrentQuaternion_Y;
        TelloPreviousQuaternion_Z = TelloCurrentQuaternion_Z;
        TelloPreviousQuaternion_W = TelloCurrentQuaternion_W;
    }
Ejemplo n.º 10
0
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            float[] axis = new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
            switch (keyData)
            {
            case Keys.W:       // Forward
                axis[3] = sensitivity;
                break;

            case Keys.A:       // Left
                axis[2] = -sensitivity;
                break;

            case Keys.S:        // Bakcwards
                axis[3] = -sensitivity;
                break;

            case Keys.D:        // Right
                axis[2] = sensitivity;
                break;

            case Keys.Right:        // Look Right
                axis[0] = sensitivity;
                break;

            case Keys.Left:         // Look Left
                axis[0] = -sensitivity;
                break;

            case Keys.Up:         // Up
                axis[1] = sensitivity;
                break;

            case Keys.Down:      // Down
                axis[1] = -sensitivity;
                break;

            case Keys.D1:           // Faster
                if (sensitivity + 0.25f <= 1f)
                {
                    sensitivity += 0.25f;
                }
                break;

            case Keys.Add:         // Faster
                if (sensitivity + 0.25f <= 1f)
                {
                    sensitivity += 0.25f;
                }
                break;

            case Keys.D2:          // Slower
                if (sensitivity - 0.25f >= 0.0f)
                {
                    sensitivity -= 0.25f;
                }
                break;

            case Keys.Subtract:     // Slower
                if (sensitivity - 0.25f >= 0.0f)
                {
                    sensitivity -= 0.25f;
                }
                break;

            case Keys.Enter:
                takepic();
                break;

            case Keys.P:
                sensitivity = 0.5f;
                break;

            case Keys.Tab:
            {
                if (Tello.connected)
                {
                    if (Tello.state.flying)
                    {
                        Tello.land();
                    }
                    else
                    {
                        Tello.takeOff();
                    }
                }
            }
            break;
            }
            Tello.controllerState.setAxis(axis[0], axis[1], axis[2], axis[3]);

            return(base.ProcessCmdKey(ref msg, keyData));
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            //subscribe to Tello connection events
            Tello.onConnection += (Tello.ConnectionState newState) =>
            {
                if (newState != Tello.ConnectionState.Connected)
                {
                }
                if (newState == Tello.ConnectionState.Connected)
                {
                    Tello.queryAttAngle();
                    Tello.setMaxHeight(50);

                    clearConsole();
                }
                printAt(0, 0, "Tello " + newState.ToString());
            };

            //Log file setup.
            var logPath = "logs/";

            System.IO.Directory.CreateDirectory(Path.Combine("../", logPath));
            var logStartTime = DateTime.Now;
            var logFilePath  = Path.Combine("../", logPath + logStartTime.ToString("yyyy-dd-M--HH-mm-ss") + ".csv");

            //write header for cols in log.
            File.WriteAllText(logFilePath, "time," + Tello.state.getLogHeader());

            //subscribe to Tello update events.
            Tello.onUpdate += (Tello.FlyData newState) =>
            {
                //write update to log.
                var elapsed = DateTime.Now - logStartTime;
                File.AppendAllText(logFilePath, elapsed.ToString(@"mm\:ss\:ff\,") + newState.getLogLine());

                //display state in console.
                var outStr = newState.ToString();//ToString() = Formated state
                printAt(0, 2, outStr);
            };

            //subscribe to Joystick update events. Called ~10x second.
            PCJoystick.onUpdate += (SharpDX.DirectInput.JoystickState joyState) =>
            {
                var rx = ((float)joyState.RotationX / 0x8000) - 1;
                var ry = (((float)joyState.RotationY / 0x8000) - 1);
                var lx = ((float)joyState.X / 0x8000) - 1;
                var ly = (((float)joyState.Y / 0x8000) - 1);
                //var boost = joyState.Z
                float[] axes   = new float[] { lx, ly, rx, ry, 0 };
                var     outStr = string.Format("JOY {0: 0.00;-0.00} {1: 0.00;-0.00} {2: 0.00;-0.00} {3: 0.00;-0.00} {4: 0.00;-0.00}", axes[0], axes[1], axes[2], axes[3], axes[4]);
                printAt(0, 22, outStr);
                Tello.controllerState.setAxis(lx, ly, rx, ry);
                Tello.sendControllerUpdate();
            };
            PCJoystick.init();

            //Connection to send raw video data to local udp port.
            //To play: ffplay -probesize 32 -sync ext udp://127.0.0.1:7038
            //To play with minimum latency:ffmpeg -i udp://127.0.0.1:7038 -f sdl "Tello"
            var videoClient = UdpUser.ConnectTo("127.0.0.1", 7038);

            //subscribe to Tello video data
            Tello.onVideoData += (byte[] data) =>
            {
                try
                {
                    videoClient.Send(data.Skip(2).ToArray());//Skip 2 byte header and send to ffplay.
                    //Console.WriteLine("Video size:" + data.Length);
                }catch (Exception ex)
                {
                }
            };

            Tello.startConnecting();//Start trying to connect.

            clearConsole();

            var str = "";

            while (str != "exit")
            {
                str = Console.ReadLine().ToLower();
                if (str == "takeoff" && Tello.connected && !Tello.state.flying)
                {
                    Tello.takeOff();
                }
                if (str == "land" && Tello.connected && Tello.state.flying)
                {
                    Tello.land();
                }
                if (str == "cls")
                {
                    Tello.setMaxHeight(9);
                    Tello.queryMaxHeight();
                    clearConsole();
                }
            }
        }
Ejemplo n.º 12
0
        void glControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.T)
            {
                Takeoff.BackColor = Color.Green;
                Takeoff.ForeColor = Color.White;
                // Instrutions to the drone
                Tello.takeOff();
            }

            if (e.KeyCode == Keys.Y)
            {
                Land.BackColor = Color.Green;
                Land.ForeColor = Color.White;
                // Instrutions to the drone
                Tello.land();
            }

            if (e.KeyCode == Keys.R)
            {
                markOrigin.PerformClick();
                markOrigin.BackColor = Color.Green;
                markOrigin.ForeColor = Color.White;
            }

            if (e.KeyCode == Keys.Space)
            {
                Hover.BackColor = Color.Green;
                Hover.ForeColor = Color.White;
                // Instrutions to the drone
                Tello.hover();
            }

            float lx = 0f;
            float ly = 0f;
            float rx = 0f;
            float ry = 0f;

            if (e.KeyCode == Keys.W)
            {
                Forward.BackColor = Color.Green;
                Forward.ForeColor = Color.White;
                // Instrutions to the drone
                ry = 0.5f;
            }

            if (e.KeyCode == Keys.S)
            {
                Backward.BackColor = Color.Green;
                Backward.ForeColor = Color.White;
                // Instrutions to the drone
                ry = -0.5f;
            }

            if (e.KeyCode == Keys.A)
            {
                Right.BackColor = Color.Green;
                Right.ForeColor = Color.White;
                // Instrutions to the drone
                rx = -0.5f;
            }

            if (e.KeyCode == Keys.D)
            {
                Left.BackColor = Color.Green;
                Left.ForeColor = Color.White;
                // Instrutions to the drone
                rx = 0.5f;
            }

            if (e.KeyCode == Keys.I)
            {
                Upward.BackColor = Color.Green;
                Upward.ForeColor = Color.White;
                // Instrutions to the drone
                // Instrutions to the drone
                ly = 0.5f;
            }

            if (e.KeyCode == Keys.K)
            {
                Downward.BackColor = Color.Green;
                Downward.ForeColor = Color.White;
                // Instrutions to the drone
                ly = -0.5f;
            }

            if (e.KeyCode == Keys.J)
            {
                Counter.BackColor = Color.Green;
                Counter.ForeColor = Color.White;
                // Instrutions to the drone
                lx = -0.5f;
            }

            if (e.KeyCode == Keys.L)
            {
                Clockwise.BackColor = Color.Green;
                Clockwise.ForeColor = Color.White;
                // Instrutions to the drone
                lx = 0.5f;
            }
            Tello.controllerState.setAxis(lx, ly, rx, ry);
        }
Ejemplo n.º 13
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            //force max brightness on screen.
            Window.Attributes.ScreenBrightness = 1f;

            //Full screen and hide nav bar.
            View decorView    = Window.DecorView;
            var  uiOptions    = (int)decorView.SystemUiVisibility;
            var  newUiOptions = (int)uiOptions;

            newUiOptions |= (int)SystemUiFlags.LowProfile;
            newUiOptions |= (int)SystemUiFlags.Fullscreen;
            newUiOptions |= (int)SystemUiFlags.HideNavigation;
            newUiOptions |= (int)SystemUiFlags.Immersive;
            // This option will make bars disappear by themselves
            newUiOptions |= (int)SystemUiFlags.ImmersiveSticky;
            decorView.SystemUiVisibility = (StatusBarVisibility)newUiOptions;

            //Keep screen from dimming.
            this.Window.SetFlags(WindowManagerFlags.KeepScreenOn, WindowManagerFlags.KeepScreenOn);


            onScreenJoyL = FindViewById <JoystickView>(Resource.Id.joystickViewL);
            onScreenJoyR = FindViewById <JoystickView>(Resource.Id.joystickViewR);

            takeoffButton      = FindViewById <ImageButton>(Resource.Id.takeoffButton);
            throwTakeoffButton = FindViewById <ImageButton>(Resource.Id.throwTakeoffButton);

            rthButton = FindViewById <ImageButton>(Resource.Id.rthButton);

            //subscribe to Tello connection events
            Tello.onConnection += (Tello.ConnectionState newState) =>
            {
                //Update state on screen
                Button cbutton = FindViewById <Button>(Resource.Id.connectButton);

                //If not connected check to see if connected to tello network.
                if (newState != Tello.ConnectionState.Connected &&
                    newState != Tello.ConnectionState.Paused)
                {
                    WifiManager wifiManager = (WifiManager)Application.Context.GetSystemService(Context.WifiService);
                    string      ip          = Formatter.FormatIpAddress(wifiManager.ConnectionInfo.IpAddress);
                    if (!ip.StartsWith("192.168.10."))
                    {
                        //CrossTextToSpeech.Current.Speak("No network found.");
                        //Not connected to network.
                        RunOnUiThread(() => {
                            cbutton.Text = "Not Connected. Touch Here.";
                            cbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#55ff3333"));
                        });
                        return;
                    }
                }
                if (newState == Tello.ConnectionState.Paused)
                {
                }
                if (newState == Tello.ConnectionState.UnPausing)
                {
                }
                if (newState == Tello.ConnectionState.Connected)
                {
                    //Tello.queryMaxHeight();
                    //Override max hei on connect.
                    Tello.setMaxHeight(30);//meters
                    Tello.queryMaxHeight();

                    //Tello.queryAttAngle();
                    Tello.setAttAngle(25);
                    //Tello.queryAttAngle();

                    Tello.setJpgQuality(Preferences.jpgQuality);

                    CrossTextToSpeech.Current.Speak("Connected");

                    Tello.setPicVidMode(picMode);//0=picture(960x720)
                    //updateVideoSize();

                    Tello.setEV(Preferences.exposure);

                    Tello.setVideoBitRate(Preferences.videoBitRate);
                    Tello.setVideoDynRate(1);

                    if (forceSpeedMode)
                    {
                        Tello.controllerState.setSpeedMode(1);
                    }
                    else
                    {
                        Tello.controllerState.setSpeedMode(0);
                    }
                }
                if (newState == Tello.ConnectionState.Disconnected)
                {
                    //if was connected then warn.
                    if (Tello.connectionState == Tello.ConnectionState.Connected)
                    {
                        CrossTextToSpeech.Current.Speak("Disconnected");
                    }
                }
                //update connection state button.
                RunOnUiThread(() => {
                    if (newState == Tello.ConnectionState.UnPausing)//Fix. Don't show "unpausing" string.
                    {
                        cbutton.Text = Tello.ConnectionState.Connected.ToString();
                    }
                    else
                    {
                        cbutton.Text = newState.ToString();
                    }

                    if (newState == Tello.ConnectionState.Connected || newState == Tello.ConnectionState.UnPausing)
                    {
                        cbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#6090ee90"));//transparent light green.
                    }
                    else
                    {
                        cbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#ffff00"));//yellow
                    }
                });
            };
            var modeTextView   = FindViewById <TextView>(Resource.Id.modeTextView);
            var hSpeedTextView = FindViewById <TextView>(Resource.Id.hSpeedTextView);
            var vSpeedTextView = FindViewById <TextView>(Resource.Id.vSpeedTextView);
            var heiTextView    = FindViewById <TextView>(Resource.Id.heiTextView);
            var batTextView    = FindViewById <TextView>(Resource.Id.batTextView);
            var wifiTextView   = FindViewById <TextView>(Resource.Id.wifiTextView);

            //Log file setup.
            var logPath      = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "aTello/logs/");;
            var logStartTime = DateTime.Now;
            var logFilePath  = logPath + logStartTime.ToString("yyyy-dd-M--HH-mm-ss") + ".csv";

            if (doStateLogging)
            {
                //write header for cols in log.
                System.IO.Directory.CreateDirectory(logPath);
                File.WriteAllText(logFilePath, "time," + Tello.state.getLogHeader());
            }

            //Long click vert speed to force fast mode.
            hSpeedTextView.LongClick += delegate {
                forceSpeedMode = !forceSpeedMode;
                if (forceSpeedMode)
                {
                    Tello.controllerState.setSpeedMode(1);
                }
                else
                {
                    Tello.controllerState.setSpeedMode(0);
                }
            };

            cameraShutterSound.Load("cameraShutterClick.mp3");
            //subscribe to Tello update events
            Tello.onUpdate += (int cmdId) =>
            {
                if (doStateLogging)
                {
                    //write update to log.
                    var elapsed = DateTime.Now - logStartTime;
                    File.AppendAllText(logFilePath, elapsed.ToString(@"mm\:ss\:ff\,") + Tello.state.getLogLine());
                }

                RunOnUiThread(() => {
                    if (cmdId == 86)//ac status update.
                    {
                        //Update state on screen
                        modeTextView.Text   = "FM:" + Tello.state.flyMode;
                        hSpeedTextView.Text = string.Format("HS:{0: 0.0;-0.0}m/s", (float)Tello.state.flySpeed / 10);
                        vSpeedTextView.Text = string.Format("VS:{0: 0.0;-0.0}m/s", -(float)Tello.state.verticalSpeed / 10);//Note invert so negative means moving down.
                        heiTextView.Text    = string.Format("Hei:{0: 0.0;-0.0}m", (float)Tello.state.height / 10);

                        if (Tello.controllerState.speed > 0)
                        {
                            hSpeedTextView.SetBackgroundColor(Android.Graphics.Color.IndianRed);
                        }
                        else
                        {
                            hSpeedTextView.SetBackgroundColor(Android.Graphics.Color.DarkGreen);
                        }

                        batTextView.Text  = "Bat:" + Tello.state.batteryPercentage;
                        wifiTextView.Text = "Wifi:" + Tello.state.wifiStrength;

                        //Autopilot debugging.
                        if (/*!bAutopilot &&*/ Tello.state.flying)
                        {
                            RunOnUiThread(() =>
                            {
                                var eular = Tello.state.toEuler();
                                var yaw   = eular[2];

                                var deltaPosX   = autopilotTarget.X - Tello.state.posX;
                                var deltaPosY   = autopilotTarget.Y - Tello.state.posY;
                                var dist        = Math.Sqrt(deltaPosX * deltaPosX + deltaPosY * deltaPosY);
                                var normalizedX = deltaPosX / dist;
                                var normalizedY = deltaPosY / dist;

                                var targetYaw = Math.Atan2(normalizedY, normalizedX);
                                var deltaYaw  = targetYaw - yaw;

                                var str = string.Format("x {0:0.00; -0.00} y {1:0.00; -0.00} yaw {2:0.00; -0.00} targetYaw {3:0.00; -0.00} targetDist {4:0.00; -0.00} On:{5}",
                                                        Tello.state.posX, Tello.state.posY,
                                                        (((yaw * (180.0 / Math.PI)) + 360.0) % 360.0),
                                                        (((targetYaw * (180.0 / Math.PI)) + 360.0) % 360.0), dist,
                                                        bAutopilot.ToString());

                                TextView joystat = FindViewById <TextView>(Resource.Id.joystick_state);
                                joystat.Text     = str;
                            });
                        }



                        //acstat.Text = str;
                        if (Tello.state.flying)
                        {
                            takeoffButton.SetImageResource(Resource.Drawable.land);
                        }
                        else if (!Tello.state.flying)
                        {
                            takeoffButton.SetImageResource(Resource.Drawable.takeoff_white);
                        }
                    }
                    if (cmdId == 48)//ack picture start.
                    {
                        cameraShutterSound.Play();
                    }
                    if (cmdId == 98)//start picture download.
                    {
                    }
                    if (cmdId == 100)                      //picture piece downloaded.
                    {
                        if (Tello.picDownloading == false) //if done downloading.
                        {
                            if (remainingExposures >= 0)
                            {
                                var exposureSet = new int[] { 0, -2, 8 };
                                Tello.setEV(Preferences.exposure + exposureSet[remainingExposures]);
                                remainingExposures--;
                                Tello.takePicture();
                            }
                            if (remainingExposures == -1)//restore exposure.
                            {
                                Tello.setEV(Preferences.exposure);
                            }
                        }
                    }
                });

                //Do autopilot input.
                handleAutopilot();
            };



            var videoFrame  = new byte[100 * 1024];
            var videoOffset = 0;

            updateVideoSize();
            Video.Decoder.surface = FindViewById <SurfaceView>(Resource.Id.surfaceView).Holder.Surface;

/*
 *          var textureView = FindViewById<TextureView>(Resource.Id.textureView);
 *          mSurfaceTextureListener = new SurfaceTextureListener(this);
 *          textureView.SurfaceTextureListener = mSurfaceTextureListener;
 *
 *          //var st = new Surface(textureView.SurfaceTexture);
 *          //Video.Decoder.surface = st;
 *
 */

            var path = "aTello/video/";

            System.IO.Directory.CreateDirectory(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path + "cache/"));
            videoFilePath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path + "cache/" + DateTime.Now.ToString("MMMM dd yyyy HH-mm-ss") + ".h264");

            FileStream videoStream = null;

            startUIUpdateThread();
            //updateUI();//hide record light etc.

            //subscribe to Tello video data
            var vidCount = 0;

            Tello.onVideoData += (byte[] data) =>
            {
                totalVideoBytesReceived += data.Length;
                //Handle recording.
                if (true)                                                             //videoFilePath != null)
                {
                    if (data[2] == 0 && data[3] == 0 && data[4] == 0 && data[5] == 1) //if nal
                    {
                        var nalType = data[6] & 0x1f;
                        //                       if (nalType == 7 || nalType == 8)
                        {
                            if (toggleRecording)
                            {
                                if (videoStream != null)
                                {
                                    videoStream.Close();
                                }
                                videoStream = null;

                                isRecording     = !isRecording;
                                toggleRecording = false;
                                if (isRecording)
                                {
                                    videoFilePath      = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path + DateTime.Now.ToString("MMMM dd yyyy HH-mm-ss") + ".h264");
                                    startRecordingTime = DateTime.Now;
//                                    Tello.setVideoRecord(vidCount++);
                                    CrossTextToSpeech.Current.Speak("Recording");
                                    updateUI();
                                }
                                else
                                {
                                    videoFilePath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, path + "cache/" + DateTime.Now.ToString("MMMM dd yyyy HH-mm-ss") + ".h264");
                                    CrossTextToSpeech.Current.Speak("Recording stopped");
                                    updateUI();
                                }
                            }
                        }
                    }

                    if ((isRecording || Preferences.cacheVideo))
                    {
                        if (videoStream == null)
                        {
                            videoStream = new FileStream(videoFilePath, FileMode.Append);
                        }

                        if (videoStream != null)
                        {
                            //Save raw data minus sequence.
                            videoStream.Write(data, 2, data.Length - 2);//Note remove 2 byte seq when saving.
                        }
                    }
                }

                //Handle video display.
                if (true)//video decoder tests.
                {
                    //Console.WriteLine("1");

                    if (data[2] == 0 && data[3] == 0 && data[4] == 0 && data[5] == 1)//if nal
                    {
                        var nalType = data[6] & 0x1f;
                        if (nalType == 7 || nalType == 8)
                        {
                        }
                        if (videoOffset > 0)
                        {
                            aTello.Video.Decoder.decode(videoFrame.Take(videoOffset).ToArray());
                            videoOffset = 0;
                        }
                        //var nal = (received.bytes[6] & 0x1f);
                        //if (nal != 0x01 && nal != 0x07 && nal != 0x08 && nal != 0x05)
                        //    Console.WriteLine("NAL type:" + nal);
                    }
                    //todo. resquence frames.
                    Array.Copy(data, 2, videoFrame, videoOffset, data.Length - 2);
                    videoOffset += (data.Length - 2);
                }
            };

            onScreenJoyL.onUpdate += OnTouchJoystickMoved;
            onScreenJoyR.onUpdate += OnTouchJoystickMoved;



            Tello.startConnecting();//Start trying to connect.

            //Clicking on network state button will show wifi connection page.
            Button button = FindViewById <Button>(Resource.Id.connectButton);

            button.Click += delegate {
                WifiManager wifiManager = (WifiManager)Application.Context.GetSystemService(Context.WifiService);
                string      ip          = Formatter.FormatIpAddress(wifiManager.ConnectionInfo.IpAddress);
                if (!ip.StartsWith("192.168.10."))//Already connected to network?
                {
                    StartActivity(new Intent(Android.Net.Wifi.WifiManager.ActionPickWifiNetwork));
                }
            };

            rthButton.LongClick += delegate {
                bAutopilot = !bAutopilot;//Toggle autopilot
            };
            rthButton.Click += delegate {
                bAutopilot = false;//Stop if going.
                Tello.controllerState.setAxis(0, 0, 0, 0);
                Tello.sendControllerUpdate();

                //set new home point
                setAutopilotTarget(new PointF(Tello.state.posX, Tello.state.posY));
            };

            takeoffButton.LongClick += delegate {
                if (Tello.connected && !Tello.state.flying)
                {
                    Tello.takeOff();
                }
                else if (Tello.connected && Tello.state.flying)
                {
                    Tello.land();
                }
            };
            throwTakeoffButton.LongClick += delegate {
                if (Tello.connected && !Tello.state.flying)
                {
                    Tello.throwTakeOff();
                }
                else if (Tello.connected && Tello.state.flying)
                {
                    //Tello.land();
                }
            };
            var pictureButton = FindViewById <ImageButton>(Resource.Id.pictureButton);

            Tello.picPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "aTello/pics/");
            System.IO.Directory.CreateDirectory(Tello.picPath);

            pictureButton.Click += delegate
            {
                remainingExposures = -1;
                Tello.takePicture();
            };

            /*
             * Multiple exposure. Not working yet.
             * pictureButton.LongClick += delegate
             * {
             *  remainingExposures = 2;
             *  Tello.takePicture();
             * };
             */

            var recordButton = FindViewById <ImageButton>(Resource.Id.recordButton);

            recordButton.Click += delegate
            {
                toggleRecording = true;
            };

            recordButton.LongClick += delegate
            {
                //Toggle
                picMode = picMode == 1 ? 0 : 1;
                Tello.setPicVidMode(picMode);
                updateVideoSize();
                aTello.Video.Decoder.reconfig();
            };
            var galleryButton = FindViewById <ImageButton>(Resource.Id.galleryButton);

            galleryButton.Click += async delegate
            {
                //var uri = Android.Net.Uri.FromFile(new Java.IO.File(Tello.picPath));
                //shareImage(uri);
                //return;
                Intent intent = new Intent();
                intent.PutExtra(Intent.ActionView, Tello.picPath);
                intent.SetType("image/*");
                intent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), 1);
            };
            //Settings button
            ImageButton settingsButton = FindViewById <ImageButton>(Resource.Id.settingsButton);

            settingsButton.Click += delegate
            {
                StartActivity(typeof(SettingsActivity));
            };


            //Init joysticks.
            input_manager = (InputManager)GetSystemService(Context.InputService);
            CheckGameControllers();
        }
Ejemplo n.º 14
0
        private void ConnectDroneButton_Click(object sender, EventArgs e)
        {
            //subscribe to Tello connection events
            Tello.onConnection += (Tello.ConnectionState newState) =>
            {
                if (newState != Tello.ConnectionState.Connected)
                {
                }
                if (newState == Tello.ConnectionState.Connected)
                {
                    Tello.queryAttAngle();
                    Tello.setMaxHeight(50);
                }
            };

            //Log file setup.
            var logPath = "logs/";

            System.IO.Directory.CreateDirectory(Path.Combine("../", logPath));
            var logStartTime = DateTime.Now;
            var logFilePath  = Path.Combine("../", logPath + logStartTime.ToString("yyyy-dd-M--HH-mm-ss") + ".csv");

            //write header for cols in log.
            File.WriteAllText(logFilePath, "time," + Tello.state.getLogHeader());

            //subscribe to Tello update events.
            Tello.onUpdate += (cmdId) =>
            {
                if (cmdId == 86)//ac update
                {
                    //write update to log.
                    var elapsed = DateTime.Now - logStartTime;
                    File.AppendAllText(logFilePath, elapsed.ToString(@"mm\:ss\:ff\,") + Tello.state.getLogLine());

                    //display state in console.
                    var outStr = Tello.state.ToString();//ToString() = Formated state
                }
            };



            //Connection to send raw video data to local udp port.
            //To play: ffplay -probesize 32 -sync ext udp://127.0.0.1:7038
            //To play with minimum latency:ffmpeg -i udp://127.0.0.1:7038 -f sdl "Tello"
            var videoClient = UdpUser.ConnectTo("127.0.0.1", 7038);

            //subscribe to Tello video data
            Tello.onVideoData += (byte[] data) =>
            {
                try
                {
                    videoClient.Send(data.Skip(2).ToArray());//Skip 2 byte header and send to ffplay.
                    //Console.WriteLine("Video size:" + data.Length);
                }
                catch (Exception ex)
                {
                }
            };

            Tello.startConnecting();//Start trying to connect.
            Tello.takeOff();
        }