天天看點

Mavlink地面站編寫之三--Mission PLanner地面站構架分析之MAVLINK航點讀寫

轉載請注明出處:http://www.amovauto.com 阿木社群 玩也要玩的專業!

3DR這個地面站還是非常專業的,最近研究MAVLINK通信協定,就來分析下Mission planner的構架。至于Msission planner的編譯在前面幾篇文章已經做了較長的描述。

Msission planner有幾十個工程項目組成

Mavlink地面站編寫之三--Mission PLanner地面站構架分析之MAVLINK航點讀寫

AviFile

BaseClasses

BSE.Windows.Forms

Core

GeoUtility

GMap.Net.Core

GMap.Net.WindowsForms

KMLib

MAVLink

MetaDataExtractor

MissionPlanner.Comms

MissionPlanner.Controls

MissionPlanner.Utils

px4uploader

SharpKml

ZedGraph

幾十個工程的調用關系比較複雜,有些庫的使用價值非常高的比如MAVLink,用這個庫就可以搞定所有的MAVLINK協定方面的事情,我也是從這個庫着手寫的地面站,可以重點看看這個庫。

Mission Planner的構架比較複雜,他的功能太全了,是以顯得備援。但是非常專業的地面站,我喜歡........

一 入口函數分析,入口函數在Program.cs中在入口函數中初始化了一堆東西 GMAP等等.......

一直到這裡

try
            {
                //System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.RealTime;
                Thread.CurrentThread.Name = "Base Thread";
                Application.Run(new MainV2());
            }
            catch (Exception ex)
            {
                log.Fatal("Fatal app exception", ex);
                Console.WriteLine(ex.ToString());

                Console.WriteLine("\nPress any key to exit!");
                Console.ReadLine();
            }
      

他啟動了主窗體MainV2,這個主窗體就是要分析的重點了,因為我們的第一步目的是分析MAVLINK的這個協定的使用,其他的一些軟體窗體方面的設定,我們先不考慮。

log.Info("Create FD");
                FlightData = new GCSViews.FlightData();//
                log.Info("Create FP");
                FlightPlanner = new GCSViews.FlightPlanner();
                //Configuration = new GCSViews.ConfigurationView.Setup();
                log.Info("Create SIM");
                Simulation = new GCSViews.Simulation();
      

GCSViews是重要的一個類,這個就是MP的控件類,其中

Mavlink地面站編寫之三--Mission PLanner地面站構架分析之MAVLINK航點讀寫

可以看到FlightData.cs是飛行資料顯示界面,就是MP中的姿态資料顯示界面,FlightPlanner.cs是飛行計劃設定界面,讀取航點和寫入航點都在這個界面裡面,前面的消息解析中我們已經知道怎麼怎麼通過MAVLNK讀取APM的飛控的資料了,下一步我們要實作航點的讀取和寫入,我們來重點分析下這個FlightPlanner.cs。

Mavlink地面站編寫之三--Mission PLanner地面站構架分析之MAVLINK航點讀寫

                                              飛行計劃設定界面

電機讀取航點按鈕,進入實作函數;

internal void BUT_read_Click(object sender, EventArgs e)
        {
            if (Commands.Rows.Count > 0)
            {
                if (sender is FlightData)
                {
                }
                else
                {
                    if (
                        CustomMessageBox.Show("This will clear your existing planned mission, Continue?", "Confirm",
                            MessageBoxButtons.OKCancel) != DialogResult.OK)
                    {
                        return;
                    }
                }
            }

            ProgressReporterDialogue frmProgressReporter = new ProgressReporterDialogue
            {
                StartPosition = FormStartPosition.CenterScreen,
                Text = "Receiving WP's"
            };

            frmProgressReporter.DoWork += getWPs;//擷取航點的委托
            frmProgressReporter.UpdateProgressAndStatus(-1, "Receiving WP's");

            ThemeManager.ApplyThemeTo(frmProgressReporter);

            frmProgressReporter.RunBackgroundOperationAsync();

            frmProgressReporter.Dispose();
        }

        void getWPs(object sender, ProgressWorkerEventArgs e, object passdata = null)
        {
            List cmds = new List();

            try
            {
                MAVLinkInterface port = MainV2.comPort;

                if (!port.BaseStream.IsOpen)
                {
                    throw new Exception("Please Connect First!");
                }

                MainV2.comPort.giveComport = true;

                param = port.MAV.param;

                log.Info("Getting Home");

                ((ProgressReporterDialogue) sender).UpdateProgressAndStatus(0, "Getting WP count");

                if (port.MAV.apname == MAVLink.MAV_AUTOPILOT.PX4)
                {
                    try
                    {
                        cmds.Add(port.getHomePosition());
                    }
                    catch (TimeoutException ex)
                    {
                        // blank home
                        cmds.Add(new Locationwp() { id = (byte)MAVLink.MAV_CMD.WAYPOINT });
                    }
                }

                log.Info("Getting WP #");

                int cmdcount = port.getWPCount();

                for (ushort a = 0; a < cmdcount; a++)
                {
                    if (((ProgressReporterDialogue) sender).doWorkArgs.CancelRequested)
                    {
                        ((ProgressReporterDialogue) sender).doWorkArgs.CancelAcknowledged = true;
                        throw new Exception("Cancel Requested");
                    }

                    log.Info("Getting WP" + a);
                    ((ProgressReporterDialogue) sender).UpdateProgressAndStatus(a*100/cmdcount, "Getting WP " + a);
                    cmds.Add(port.getWP(a));
                }

                port.setWPACK();

                ((ProgressReporterDialogue) sender).UpdateProgressAndStatus(100, "Done");

                log.Info("Done");
            }
            catch
            {
                throw;
            }

            WPtoScreen(cmds);
        }
      

ProgressReporterDialogue這個對話框類中DoWork是個委托,ProgressReporterDialogue的背景運作線程會執行 getWPs(object sender, ProgressWorkerEventArgs e, object passdata = null)這個委托,來看看這個委托

委托裡面定義了 List cmds = new List();航點清單。

MAVLinkInterface port = MainV2.comPort;這個MAVLinkInterface類比較重要,是主界面MainV2的一個成員變量。裡面記錄了關于MAVLINK協定的所有内容,從MAVLINK協定擷取的參數會存入這個成員變量,比如飛控是什麼固件,飛控的狀态,還有對LIB庫裡面的MAVLINK庫的進一步封裝比如得到心跳包函數getHeartBeat(),得到參數清單getParaList(),得到目前航點getRallyPoints();

看來這個成員可以實作所有 MAVLINK的操作,結構圖如下:

Mavlink地面站編寫之三--Mission PLanner地面站構架分析之MAVLINK航點讀寫
Mavlink地面站編寫之三--Mission PLanner地面站構架分析之MAVLINK航點讀寫

 以上可以看到 MAVLinkInterface的一些接口,再看讀取航點的函數:

int cmdcount = port.getWPCount();得到航點個數

cmds.Add(port.getWP(a));存入航點坐标到航點清單,可以在MAVLinkInterface類中看到詳細的getWP(a)得到航點的實作,用while循環讀取,并且做了逾時時間設定,來保證軟體健壯性。

MP裡面還有個航點類來接收航點在locationwp.cs中可以看到詳細的定義,主要是坐标,姿态,和這個航點的command id指令ID。

我們知道了怎麼利用mavlink讀取航點,那麼如果我們要寫入航點,也是一樣的道理。

更多内容請通路:http://www.amovauto.com  阿木社群 玩也要玩的專業!

QQ群:526221258

繼續閱讀