From ace823fccf546cd64075ae1ab04d6f2f8fbdb8d3 Mon Sep 17 00:00:00 2001 From: Michael Oborne Date: Wed, 21 Dec 2011 08:22:28 +0800 Subject: [PATCH] APM Planner 1.1.12 touch up geofence. remove reset tab --- .../GCSViews/FlightPlanner.Designer.cs | 30 ++- .../GCSViews/FlightPlanner.cs | 250 ++++++++++++++---- .../GCSViews/FlightPlanner.resx | 30 ++- .../Properties/AssemblyInfo.cs | 2 +- .../Setup/Setup.Designer.cs | 2 +- 5 files changed, 247 insertions(+), 67 deletions(-) diff --git a/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.Designer.cs b/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.Designer.cs index 33a007d093..d4b7fcea02 100644 --- a/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.Designer.cs +++ b/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.Designer.cs @@ -121,12 +121,14 @@ this.geoFenceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.GeoFenceuploadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.GeoFencedownloadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.setReturnLocationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.loadFromFileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.trackBar1 = new ArdupilotMega.MyTrackBar(); this.label11 = new System.Windows.Forms.Label(); this.panelBASE = new System.Windows.Forms.Panel(); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); this.timer1 = new System.Windows.Forms.Timer(this.components); - this.setReturnLocationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveToFileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)(this.Commands)).BeginInit(); this.panel5.SuspendLayout(); this.panel1.SuspendLayout(); @@ -833,7 +835,9 @@ this.geoFenceToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.GeoFenceuploadToolStripMenuItem, this.GeoFencedownloadToolStripMenuItem, - this.setReturnLocationToolStripMenuItem}); + this.setReturnLocationToolStripMenuItem, + this.loadFromFileToolStripMenuItem, + this.saveToFileToolStripMenuItem}); this.geoFenceToolStripMenuItem.Name = "geoFenceToolStripMenuItem"; resources.ApplyResources(this.geoFenceToolStripMenuItem, "geoFenceToolStripMenuItem"); // @@ -849,6 +853,18 @@ resources.ApplyResources(this.GeoFencedownloadToolStripMenuItem, "GeoFencedownloadToolStripMenuItem"); this.GeoFencedownloadToolStripMenuItem.Click += new System.EventHandler(this.GeoFencedownloadToolStripMenuItem_Click); // + // setReturnLocationToolStripMenuItem + // + this.setReturnLocationToolStripMenuItem.Name = "setReturnLocationToolStripMenuItem"; + resources.ApplyResources(this.setReturnLocationToolStripMenuItem, "setReturnLocationToolStripMenuItem"); + this.setReturnLocationToolStripMenuItem.Click += new System.EventHandler(this.setReturnLocationToolStripMenuItem_Click); + // + // loadFromFileToolStripMenuItem + // + this.loadFromFileToolStripMenuItem.Name = "loadFromFileToolStripMenuItem"; + resources.ApplyResources(this.loadFromFileToolStripMenuItem, "loadFromFileToolStripMenuItem"); + this.loadFromFileToolStripMenuItem.Click += new System.EventHandler(this.loadFromFileToolStripMenuItem_Click); + // // trackBar1 // resources.ApplyResources(this.trackBar1, "trackBar1"); @@ -885,11 +901,11 @@ this.timer1.Interval = 1000; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // - // setReturnLocationToolStripMenuItem + // saveToFileToolStripMenuItem // - this.setReturnLocationToolStripMenuItem.Name = "setReturnLocationToolStripMenuItem"; - resources.ApplyResources(this.setReturnLocationToolStripMenuItem, "setReturnLocationToolStripMenuItem"); - this.setReturnLocationToolStripMenuItem.Click += new System.EventHandler(this.setReturnLocationToolStripMenuItem_Click); + this.saveToFileToolStripMenuItem.Name = "saveToFileToolStripMenuItem"; + resources.ApplyResources(this.saveToFileToolStripMenuItem, "saveToFileToolStripMenuItem"); + this.saveToFileToolStripMenuItem.Click += new System.EventHandler(this.saveToFileToolStripMenuItem_Click); // // FlightPlanner // @@ -1009,5 +1025,7 @@ private System.Windows.Forms.ToolStripMenuItem GeoFenceuploadToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem GeoFencedownloadToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem setReturnLocationToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem loadFromFileToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem saveToFileToolStripMenuItem; } } \ No newline at end of file diff --git a/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.cs b/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.cs index 91c67c324e..6ec962c51c 100644 --- a/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.cs +++ b/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.cs @@ -660,17 +660,22 @@ namespace ArdupilotMega.GCSViews isonline = false; } - + // setup geofence List polygonPoints = new List(); gf = new GMapPolygon(polygonPoints, "geofence"); gf.Stroke = new Pen(Color.Pink, 5); + //setup drawnpolgon + List polygonPoints2 = new List(); + drawnpolygon = new GMapPolygon(polygonPoints2, "drawnpoly"); + drawnpolygon.Stroke = new Pen(Color.Red, 2); + updateCMDParams(); // mono panelMap.Dock = DockStyle.None; panelMap.Dock = DockStyle.Fill; - panelMap_Resize(null,null); + panelMap_Resize(null, null); writeKML(); } @@ -701,45 +706,46 @@ namespace ArdupilotMega.GCSViews //Console.WriteLine("feat " + feat.GetType()); //processKML((Element)feat); } - } else - if (folder != null ) - { - foreach (Feature feat in folder.Features) + } + else + if (folder != null) { - //Console.WriteLine("feat "+feat.GetType()); - //processKML(feat); + foreach (Feature feat in folder.Features) + { + //Console.WriteLine("feat "+feat.GetType()); + //processKML(feat); + } } - } - else if (pm != null) - { - - } - else if (polygon != null) - { - GMapPolygon kmlpolygon = new GMapPolygon(new List(), "kmlpolygon"); - - kmlpolygon.Stroke.Color = Color.Purple; - - foreach (var loc in polygon.OuterBoundary.LinearRing.Coordinates) + else if (pm != null) { - kmlpolygon.Points.Add(new PointLatLng(loc.Latitude, loc.Longitude)); + } - - kmlpolygons.Polygons.Add(kmlpolygon); - } - else if (ls != null) - { - GMapRoute kmlroute = new GMapRoute(new List(), "kmlroute"); - - kmlroute.Stroke.Color = Color.Purple; - - foreach (var loc in ls.Coordinates) + else if (polygon != null) { - kmlroute.Points.Add(new PointLatLng(loc.Latitude, loc.Longitude)); - } + GMapPolygon kmlpolygon = new GMapPolygon(new List(), "kmlpolygon"); - kmlpolygons.Routes.Add(kmlroute); - } + kmlpolygon.Stroke.Color = Color.Purple; + + foreach (var loc in polygon.OuterBoundary.LinearRing.Coordinates) + { + kmlpolygon.Points.Add(new PointLatLng(loc.Latitude, loc.Longitude)); + } + + kmlpolygons.Polygons.Add(kmlpolygon); + } + else if (ls != null) + { + GMapRoute kmlroute = new GMapRoute(new List(), "kmlroute"); + + kmlroute.Stroke.Color = Color.Purple; + + foreach (var loc in ls.Coordinates) + { + kmlroute.Points.Add(new PointLatLng(loc.Latitude, loc.Longitude)); + } + + kmlpolygons.Routes.Add(kmlroute); + } } private void ChangeColumnHeader(string command) @@ -791,7 +797,7 @@ namespace ArdupilotMega.GCSViews if (tcell.Value == null) tcell.Value = "0"; } - } + } DataGridViewComboBoxCell cell = Commands.Rows[e.RowIndex].Cells[Command.Index] as DataGridViewComboBoxCell; if (cell.Value == null) @@ -985,8 +991,8 @@ namespace ArdupilotMega.GCSViews { if (Commands.Rows[a].HeaderCell.Value == null) { - Commands.Rows[a].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; - Commands.Rows[a].HeaderCell.Value = (a + 1).ToString(); + Commands.Rows[a].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; + Commands.Rows[a].HeaderCell.Value = (a + 1).ToString(); } // skip rows with the correct number string rowno = Commands.Rows[a].HeaderCell.Value.ToString(); @@ -2175,7 +2181,7 @@ namespace ArdupilotMega.GCSViews } private void comboBoxMapType_SelectedValueChanged(object sender, EventArgs e) - { + { MainMap.MapType = (MapType)comboBoxMapType.SelectedItem; FlightData.mymap.MapType = (MapType)comboBoxMapType.SelectedItem; MainV2.config["MapType"] = comboBoxMapType.Text; @@ -2701,7 +2707,7 @@ namespace ArdupilotMega.GCSViews { if (polygongridmode == false) { - MessageBox.Show("You will remain in polygon mode until you clear the polygon or create a grid"); + MessageBox.Show("You will remain in polygon mode until you clear the polygon or create a grid/upload a fence"); } polygongridmode = true; @@ -2709,8 +2715,7 @@ namespace ArdupilotMega.GCSViews List polygonPoints = new List(); if (drawnpolygons.Polygons.Count == 0) { - drawnpolygon = new GMapPolygon(polygonPoints, "drawnpoly"); - drawnpolygon.Stroke = new Pen(Color.Red, 2); + drawnpolygon.Points.Clear(); drawnpolygons.Polygons.Add(drawnpolygon); } drawnpolygon.Points.Add(new PointLatLng(start.Lat, start.Lng)); @@ -2869,8 +2874,8 @@ namespace ArdupilotMega.GCSViews { // this is a mono fix for the zoom bar //Console.WriteLine("panelmap "+panelMap.Size.ToString()); - MainMap.Size = new Size(panelMap.Size.Width - 50,panelMap.Size.Height); - trackBar1.Location = new System.Drawing.Point(panelMap.Size.Width - 50,trackBar1.Location.Y); + MainMap.Size = new Size(panelMap.Size.Width - 50, panelMap.Size.Height); + trackBar1.Location = new System.Drawing.Point(panelMap.Size.Width - 50, trackBar1.Location.Y); trackBar1.Size = new System.Drawing.Size(trackBar1.Size.Width, panelMap.Size.Height - trackBar1.Location.Y); label11.Location = new System.Drawing.Point(panelMap.Size.Width - 50, label11.Location.Y); } @@ -2918,7 +2923,7 @@ namespace ArdupilotMega.GCSViews var parser = new SharpKml.Base.Parser(); parser.ElementAdded += parser_ElementAdded; - parser.ParseString(kml,true); + parser.ParseString(kml, true); if (DialogResult.Yes == MessageBox.Show("Do you want to load this into the flight data screen?", "Load data", MessageBoxButtons.YesNo)) { @@ -2989,6 +2994,17 @@ namespace ArdupilotMega.GCSViews return; } + // check if return is inside polygon + List plll = new List(drawnpolygon.Points.ToArray()); + // close it + plll.Add(plll[0]); + // check it + if (!pnpoly(plll.ToArray(), start.Lat, start.Lng)) + { + MessageBox.Show("Your return location is outside the polygon"); + return; + } + string minalts = (int.Parse(MainV2.comPort.param["FENCE_MINALT"].ToString()) * MainV2.cs.multiplierdist).ToString("0"); Common.InputBox("Min Alt", "Box Minimum Altitude?", ref minalts); @@ -2998,13 +3014,13 @@ namespace ArdupilotMega.GCSViews int minalt = 0; int maxalt = 0; - if (!int.TryParse(minalts,out minalt)) + if (!int.TryParse(minalts, out minalt)) { MessageBox.Show("Bad Min Alt"); return; } - if (!int.TryParse(maxalts,out maxalt)) + if (!int.TryParse(maxalts, out maxalt)) { MessageBox.Show("Bad Max Alt"); return; @@ -3015,9 +3031,10 @@ namespace ArdupilotMega.GCSViews MainV2.comPort.setParam("FENCE_MINALT", minalt); MainV2.comPort.setParam("FENCE_MAXALT", maxalt); } - catch { - MessageBox.Show("Failed to set min/max fence alt"); - return; + catch + { + MessageBox.Show("Failed to set min/max fence alt"); + return; } try @@ -3053,11 +3070,21 @@ namespace ArdupilotMega.GCSViews drawnpolygons.Polygons.Clear(); drawnpolygons.Markers.Clear(); + geofence.Polygons.Clear(); + gf.Points.Clear(); - gf.Points.AddRange(drawnpolygon.Points); + gf.Points.AddRange(drawnpolygon.Points.ToArray()); drawnpolygon.Points.Clear(); + geofence.Polygons.Add(gf); + + // update flightdata + FlightData.geofence.Markers.Clear(); + FlightData.geofence.Polygons.Clear(); + FlightData.geofence.Polygons.Add(gf); + FlightData.geofence.Markers.Add(geofence.Markers[0]); + MainMap.UpdatePolygonLocalPosition(gf); MainMap.Invalidate(); @@ -3082,12 +3109,8 @@ namespace ArdupilotMega.GCSViews geofence.Polygons.Clear(); geofence.Markers.Clear(); - gf.Points.Clear(); - geofence.Polygons.Add(gf); - FlightData.geofence.Polygons.Clear(); - FlightData.geofence.Polygons.Add(gf); for (int a = 0; a < count; a++) { @@ -3099,7 +3122,14 @@ namespace ArdupilotMega.GCSViews geofence.Markers.Add(new GMapMarkerGoogleRed(new PointLatLng(gf.Points[0].Lat, gf.Points[0].Lng)) { ToolTipMode = MarkerTooltipMode.OnMouseOver, ToolTipText = "GeoFence Return" }); gf.Points.RemoveAt(0); - MainMap.UpdatePolygonLocalPosition(gf); + // add now - so local points are calced + geofence.Polygons.Add(gf); + + // update flight data + FlightData.geofence.Markers.Clear(); + FlightData.geofence.Polygons.Clear(); + FlightData.geofence.Polygons.Add(gf); + FlightData.geofence.Markers.Add(geofence.Markers[0]); MainMap.Invalidate(); } @@ -3111,5 +3141,113 @@ namespace ArdupilotMega.GCSViews MainMap.Invalidate(); } + + /// + /// from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html + /// + /// a closed polygon + /// + /// + /// true = outside + bool pnpoly(PointLatLng[] array, double testx, double testy) + { + int nvert = array.Length; + int i, j = 0; + bool c = false; + for (i = 0, j = nvert - 1; i < nvert; j = i++) + { + if (((array[i].Lng > testy) != (array[j].Lng > testy)) && + (testx < (array[j].Lat - array[i].Lat) * (testy - array[i].Lng) / (array[j].Lng - array[i].Lng) + array[i].Lat)) + c = !c; + } + return c; + } + + private void loadFromFileToolStripMenuItem_Click(object sender, EventArgs e) + { + OpenFileDialog fd = new OpenFileDialog(); + fd.Filter = "Fence (*.fen)|*.fen"; + fd.ShowDialog(); + if (File.Exists(fd.FileName)) + { + StreamReader sr = new StreamReader(fd.OpenFile()); + + drawnpolygons.Markers.Clear(); + drawnpolygons.Polygons.Clear(); + drawnpolygon.Points.Clear(); + + int a = 0; + + while (!sr.EndOfStream) + { + string line = sr.ReadLine(); + if (line.StartsWith("#")) + { + continue; + } + else + { + string[] items = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); + + if (a == 0) + { + geofence.Markers.Add(new GMapMarkerGoogleRed(new PointLatLng( double.Parse(items[0]),double.Parse(items[1]))) { ToolTipMode = MarkerTooltipMode.OnMouseOver, ToolTipText = "GeoFence Return" }); + } + else + { + drawnpolygon.Points.Add(new PointLatLng(double.Parse(items[0]), double.Parse(items[1]))); + addpolygonmarkergrid(drawnpolygon.Points.Count.ToString(), double.Parse(items[1]), double.Parse(items[0]), 0); + } + a++; + } + } + + drawnpolygons.Polygons.Add(drawnpolygon); + + MainMap.Invalidate(); + } + } + + private void saveToFileToolStripMenuItem_Click(object sender, EventArgs e) + { + if (geofence.Markers.Count == 0) + { + MessageBox.Show("Please set a return location"); + return; + } + + + SaveFileDialog sf = new SaveFileDialog(); + sf.Filter = "Fence (*.fen)|*.fen"; + sf.ShowDialog(); + if (sf.FileName != "") + { + try + { + StreamWriter sw = new StreamWriter(sf.OpenFile()); + + sw.WriteLine("#saved by APM Planner " + Application.ProductVersion); + + sw.WriteLine(geofence.Markers[0].Position.Lat + " " + geofence.Markers[0].Position.Lng); + if (drawnpolygon.Points.Count > 0) + { + foreach (var pll in drawnpolygon.Points) + { + sw.WriteLine(pll.Lat + " " + pll.Lng); + } + } + else + { + foreach (var pll in gf.Points) + { + sw.WriteLine(pll.Lat + " " + pll.Lng); + } + } + + sw.Close(); + } + catch { MessageBox.Show("Failed to write fence file"); } + } + } } } \ No newline at end of file diff --git a/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.resx b/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.resx index 0dcf3a930a..8db443f0a2 100644 --- a/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.resx +++ b/Tools/ArdupilotMegaPlanner/GCSViews/FlightPlanner.resx @@ -1704,6 +1704,18 @@ Set Return Location + + 177, 22 + + + Load from File + + + 177, 22 + + + Save to File + 167, 22 @@ -2200,6 +2212,18 @@ System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + setReturnLocationToolStripMenuItem + + + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + loadFromFileToolStripMenuItem + + + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + toolTip1 @@ -2212,10 +2236,10 @@ System.Windows.Forms.Timer, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - setReturnLocationToolStripMenuItem + + saveToFileToolStripMenuItem - + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 diff --git a/Tools/ArdupilotMegaPlanner/Properties/AssemblyInfo.cs b/Tools/ArdupilotMegaPlanner/Properties/AssemblyInfo.cs index 3fac338c3e..096d96d522 100644 --- a/Tools/ArdupilotMegaPlanner/Properties/AssemblyInfo.cs +++ b/Tools/ArdupilotMegaPlanner/Properties/AssemblyInfo.cs @@ -34,5 +34,5 @@ using System.Resources; // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.1.11")] +[assembly: AssemblyFileVersion("1.1.12")] [assembly: NeutralResourcesLanguageAttribute("")] diff --git a/Tools/ArdupilotMegaPlanner/Setup/Setup.Designer.cs b/Tools/ArdupilotMegaPlanner/Setup/Setup.Designer.cs index 7b8bcc53a6..a88422cce5 100644 --- a/Tools/ArdupilotMegaPlanner/Setup/Setup.Designer.cs +++ b/Tools/ArdupilotMegaPlanner/Setup/Setup.Designer.cs @@ -200,7 +200,7 @@ // // tabControl1 // - this.tabControl1.Controls.Add(this.tabReset); + //this.tabControl1.Controls.Add(this.tabReset); this.tabControl1.Controls.Add(this.tabRadioIn); this.tabControl1.Controls.Add(this.tabModes); this.tabControl1.Controls.Add(this.tabHardware);