Beispiel #1
0
        private void PauseResumeStartMission(Uav uav, System.Windows.Forms.Button btn)
        {
            string[] args      = new string[] { uav.ConnectionType, uav.Ip, uav.Port };
            string   app       = "";
            int      newStatus = 0;

            switch (uav.MissionStatus)
            {
            case 0:
                app       = "StartMission.py"; // not sure
                newStatus = 1;
                break;

            case 1:
                app       = "PauseMission.py";
                newStatus = 2;
                break;

            case 2:
                app       = "ResumeMission.py";
                newStatus = 1;
                break;
            }

            Log(RunPythonProc(@"Apps\" + app, new string[] { uav.ConnectionType, uav.Ip, uav.Port }));
            // check if everything is ok
            uav.MissionStatus = newStatus;
            btn.Text          = newStatus == 1 ? "Pause mission" : "Resume mission";
        }
Beispiel #2
0
 internal void SendMissionStatusToSup(Uav uav)
 {
     foreach (var i in Connections)
     {
         if (!(i is Coordinator))
         {
             continue;
         }
         (i as Coordinator).SendMissionStatus(uav);
     }
 }
Beispiel #3
0
        public void SendMissionStatus(Uav uav)
        {
            if (!Connected || uav.MissionFileName == "")
            {
                return;
            }

            string message = "Mission;";

            message += uav.Name + "," + uav.Ip + "," + uav.Port + "," + uav.ConnectionType + ";";
            message += uav.MissionCode + ";";
            message += uav.MissionStatus;
            SendMsg(message);
        }
Beispiel #4
0
        private void LoadMission(Uav uav)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = fileDialog.FileName;

                // Generate mission code randomly:
                var rnd  = new Random();
                var code = new string(Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 16).Select(s => s[rnd.Next(s.Length)]).ToArray());

                LoadMission(uav, fileName, code);
            }
        }
Beispiel #5
0
        internal void LoadMission(Uav uav, string fileName, string code)
        {
            string[]           lines = File.ReadAllLines(fileName);
            List <PointLatLng> wps   = new List <PointLatLng>();

            for (int i = 1; i < lines.Length; i++)
            {
                string[] line = lines[i].Split('\t');
                wps.Add(new PointLatLng(ParseDouble(line[8]), ParseDouble(line[9])));
            }
            uav.WaypointsLL     = wps;
            uav.MissionFileName = fileName;
            uav.MissionCode     = code;

            Log(RunPythonProc(@"Apps\UploadMission.py", new string[] { uav.ConnectionType, uav.Ip, uav.Port, fileName }));
        }
Beispiel #6
0
        public void SendMission(Uav uav)
        {
            if (!Connected)
            {
                return;
            }

            string message = "Mission;";

            message += uav.Name + "," + uav.Ip + "," + uav.Port + "," + uav.ConnectionType + ";";
            message += uav.MissionCode + ";";
            if (uav.MissionFileName != "")
            {
                message += File.ReadAllText(uav.MissionFileName);
            }
            SendMsg(message);
        }
        private void PaintOnMap(object sender, PaintEventArgs e)
        {
            foreach (var o_i in Connections)
            {
                if (!(o_i is Uav))
                {
                    continue;
                }

                Uav   i = o_i as Uav;
                Color c = i.UavColor;

                if (i.WaypointsAP != null)
                {
                    for (int j = 0; j < i.WaypointsAP.Count - 1; j++)
                    {
                        PointF p1 = PixelPosition(i.WaypointsAP[j]),
                               p2 = PixelPosition(i.WaypointsAP[j + 1]);
                        e.Graphics.DrawLine(new Pen(c, 3), p1, p2);
                    }
                    int count = 1;
                    foreach (var wp in i.WaypointsAP)
                    {
                        DrawWaypoint(count++, c, PixelPosition(wp), e.Graphics);
                    }
                }
            }
            foreach (var o_i in Connections)
            {
                if (!(o_i is Uav))
                {
                    continue;
                }

                Uav i = o_i as Uav;

                if (i.HasPosition)
                {
                    PointF p = PixelPosition(i.CurrentPixelPosition);
                    e.Graphics.DrawImage(i.UavBitmap, new PointF(p.X - 33, p.Y - 33));
                }
            }
        }
Beispiel #8
0
        private object[] GetUavInfo(Uav uav)
        {
            PointLatLng pos;
            float       heading;
            int         currWp;

            try
            {
                string[] uavInfo = RunPythonProc(@"Apps\CurrentState.py", new string[] { uav.ConnectionType, uav.Ip, uav.Port }).Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                pos     = new PointLatLng(ParseDouble(uavInfo[0]), ParseDouble(uavInfo[1]));
                heading = ParseFloat(uavInfo[4]);
                currWp  = int.Parse(uavInfo[5]);
            }
            catch (Exception)
            {
                throw new Exception();
            }

            return(new object[] { pos, heading, currWp });
        }
Beispiel #9
0
 private void ReturnToHome(Uav uav)
 {
     Log(RunPythonProc(@"Apps\ReturnToHome.py", new string[] { uav.ConnectionType, uav.Ip, uav.Port }));
 }
Beispiel #10
0
 private void TakeOff(Uav uav)
 {
     Log(RunPythonProc(@"Apps\ArmTakeoffAuto.py", new string[] { uav.ConnectionType, uav.Ip, uav.Port }));
 }
 private void ConnectUav(Uav uav)
 {
     //
 }
Beispiel #12
0
        public MainForm()
        {
            Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
            InitializeComponent();
            ResizeRedraw = true;
            Size         = new Size(640, 640);
            InitMap();
            Paint += DrawTopPanel;
            InitHiddenComponents();
            LoadWorkspace();
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            for (int i = 0; i < 5; i++)
            {
                TopBtnIcons.Add(new Bitmap(@"Images\top-btn-" + i + ".png"));
            }
            for (int i = 0; i < 5; i++)
            {
                TopBtnIcons.Add(new Bitmap(@"Images\top-btn-" + i + "-active.png"));
            }

            new Thread(new ThreadStart(() => {
                while (true)
                {
                    ConnectionsListMutex.WaitOne();
                    foreach (var i in Connections)
                    {
                        if (!(i is Uav))
                        {
                            continue;
                        }
                        try
                        {
                            var uavInfo = GetUavInfo(i as Uav);
                            (i as Uav).CurrentPosition = (PointLatLng)uavInfo[0];
                            (i as Uav).Angle           = (float)uavInfo[1];
                            (i as Uav).CurrentWpNum    = (int)uavInfo[2];
                        }
                        catch (Exception)
                        {
                            /*LogBox.Invoke((MethodInvoker)delegate {
                             *  var text = "Could not obtain " + i.Name + "'s state\n";
                             *  if (LogBox.Text == "None") LogBox.Text = text;
                             *  else LogBox.Text += text;
                             * });*/
                        }
                    }
                    ConnectionsListMutex.ReleaseMutex();
                    Thread.Sleep(100);
                }
            })).Start();

            // Example
            foreach (var i in Connections)
            {
                if (!(i is Uav))
                {
                    continue;
                }
                Uav j = i as Uav;
                if (j.Name != "UAV 2")
                {
                    continue;
                }
                j.CurrentPosition = new PointLatLng(-35.362988, 149.1651245);
                j.Angle           = 45f;
                break;
            }
        }
Beispiel #13
0
        private Connection AddConnection(string name, string ip, string port, int type, Color uavColor, string fileName, string missionCode)
        {
            int panelHeight = 74;

            Panel conPanel = new Panel();

            conPanel.Location = new Point(0, ConnectionsContainer.Controls.Count * panelHeight);
            conPanel.Size     = new Size(ConnectionsContainer.ClientSize.Width, panelHeight);

            var labels = new Label[] { new Label(), new Label() };

            labels[0].Text = name;
            labels[1].Text = "IP: " + ip + ", port: " + port;

            var f = new Font(new FontFamily("Arial"), 11);

            foreach (var i in labels)
            {
                i.ForeColor = Color.White;
                i.Font      = f;
                i.Width     = TextRenderer.MeasureText(i.Text, f).Width;
                conPanel.Controls.Add(i);
            }

            int y = panelHeight - (labels[0].Height + labels[1].Height + 4);

            labels[0].Location = new Point(6 + 74 + 6, y);
            labels[1].Location = new Point(6 + 74 + 6, y + labels[0].Height + 4);

            Connection con = null;

            int btnWidth = 95;

            List <System.Windows.Forms.Button> btns = new List <System.Windows.Forms.Button>();

            btns.Add(new System.Windows.Forms.Button());
            btns[0].Text     = "Delete";
            btns[0].Width    = btnWidth;
            btns[0].Location = new Point(conPanel.ClientSize.Width - btns[0].Width, panelHeight / 2 - btns[0].Height / 2);

            if (type == 0 || type == 1)
            {
                con = new Uav(type == 0 ? "udp" : "tcp", ip, port, name, uavColor, this);
                if (fileName != "" && missionCode != "")
                {
                    LoadMission(con as Uav, fileName, missionCode);
                }
                Connections.Add(con);

                var bmp = new Bitmap(74, 74);
                Graphics.FromImage(bmp).DrawImage((con as Uav).UavDrawingBmp, new Point(6, 8 + 4));
                conPanel.BackgroundImage       = bmp;
                conPanel.BackgroundImageLayout = ImageLayout.None;

                btns[0].Location = new Point(conPanel.ClientSize.Width - btns[0].Width, panelHeight / 2 - btns[0].Height - 1);

                btns.Add(new System.Windows.Forms.Button());
                btns[1].Text  = "Start mission";
                btns[1].Width = btnWidth;
                if ((con as Uav).MissionFileName == "")
                {
                    btns[1].Enabled = false;
                }
                btns[1].Location = new Point(btns[0].Location.X - 2 - btns[1].Width, btns[0].Location.Y);
                btns[1].Click   += (e, sender) => {
                    PauseResumeStartMission(con as Uav, btns[1]);
                };

                btns.Add(new System.Windows.Forms.Button());
                btns[2].Text     = "Take off";
                btns[2].Width    = btnWidth;
                btns[2].Location = new Point(conPanel.ClientSize.Width - btns[2].Width, btns[0].Location.Y + 2 + btns[0].Height);
                btns[2].Click   += (e, sender) => { TakeOff(con as Uav); };

                btns.Add(new System.Windows.Forms.Button());
                btns[3].Text     = "Home";
                btns[3].Width    = btnWidth;
                btns[3].Location = new Point(btns[2].Location.X - 2 - btns[3].Width, btns[2].Location.Y);
                btns[3].Click   += (e, sender) => { ReturnToHome(con as Uav); };

                btns.Add(new System.Windows.Forms.Button());
                btns[4].Text     = (con as Uav).MissionFileName == "" ? "Load mission" : "Clear mission";
                btns[4].Width    = btnWidth;
                btns[4].Location = new Point(btns[1].Location.X - 2 - btns[4].Width, btns[1].Location.Y);
                btns[4].Click   += (sender, e) => {
                    try
                    {
                        if ((con as Uav).MissionFileName == "")
                        {
                            LoadMission(con as Uav);
                            btns[1].Enabled = true;
                            btns[1].Text    = "Start mission";
                            btns[4].Text    = "Clear mission";
                        }
                        else
                        {
                            (con as Uav).MissionFileName = "";
                            (con as Uav).MissionCode     = "";
                            (con as Uav).MissionStatus   = 0;
                            (con as Uav).WaypointsLL     = null;
                            btns[1].Enabled = false;
                            btns[1].Text    = "Start mission";
                            btns[4].Text    = "Load mission";
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Invalid file format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                };
            }
            else
            {
                con = new Coordinator(ip, port, name, this);
                Connections.Add(con);

                btns.Add(new System.Windows.Forms.Button());
                btns[1].Text     = "Connect";
                btns[1].Width    = btnWidth;
                btns[1].Location = new Point(btns[0].Location.X - 2 - btns[1].Width, btns[0].Location.Y);
                btns[1].Click   += (e, sender) => {
                    Cursor = Cursors.WaitCursor;

                    Coordinator c = con as Coordinator;
                    try
                    {
                        if (!c.Connected)
                        {
                            c.Connect();
                        }
                        else
                        {
                            c.Disconnect();
                        }
                        btns[1].Text = c.Connected ? "Disconnect" : "Connect";
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    Cursor = Cursors.Default;
                };
            }

            foreach (var i in btns)
            {
                i.Anchor    = AnchorStyles.Right;
                i.BackColor = SystemColors.Control;
                conPanel.Controls.Add(i);
            }

            btns[0].Click += (e, sender) => {
                btns[0].Enabled = false;
                new Thread(new ThreadStart(() => {
                    ConnectionsListMutex.WaitOne();
                    Connections = new ConcurrentBag <Connection>(Connections.Except(new[] { con }));
                    ConnectionsListMutex.ReleaseMutex();
                    Invoke((MethodInvoker) delegate {
                        var ind = ConnectionsContainer.Controls.IndexOf(conPanel);
                        for (int i = ind; i < ConnectionsContainer.Controls.Count; i++)
                        {
                            ConnectionsContainer.Controls[i].Location = new Point(0, ConnectionsContainer.Controls[i].Location.Y - panelHeight);
                        }
                        ConnectionsContainer.Controls.Remove(conPanel);
                    });
                })).Start();
            };

            ConnectionsContainer.Controls.Add(conPanel);

            return(con);
        }