转载请注明出处:http://www.amovauto.com 阿木社区 玩也要玩的专业!
3DR这个地面站还是非常专业的,最近研究MAVLINK通信协议,就来分析下Mission planner的构架。至于Msission planner的编译在前面几篇文章已经做了详细描述。
Msission planner有几十个工程项目组成
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的控件类,其中

可以看到FlightData.cs是飞行数据显示界面,就是MP中的姿态数据显示界面,FlightPlanner.cs是飞行计划设置界面,读取航点和写入航点都在这个界面里面,前面的消息解析中我们已经知道怎么怎么通过MAVLNK读取APM的飞控的数据了,下一步我们要实现航点的读取和写入,我们来重点分析下这个FlightPlanner.cs。
     
                                         飞行计划设置界面
电机读取航点按钮,进入实现函数;
 
  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的操作,结构图如下:

 以上可以看到 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
Mission PLanner地面站构架分析之MAVLINK航点读写
原文:http://blog.csdn.net/msq19895070/article/details/51065992