forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrackerHomeGPS.cs
148 lines (130 loc) · 5.41 KB
/
TrackerHomeGPS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using MissionPlanner;
using MissionPlanner.Plugin;
using MissionPlanner.Utilities;
using TrackerHomeGPS;
public class TrackerHome : Plugin
{
private string _Name = "GPS Tracker Home Plugin";
private string _Version = "1.0";
private string _Author = "Will Bryan";
public override string Name { get { return _Name; } }
public override string Version { get { return _Version; } }
public override string Author { get { return _Author; } }
private bool _Available = false;
public override bool Init() { loopratehz = 0.0f; return true; }
public override bool Loaded()
{
/* Register with Device Change event */
Host.DeviceChanged += deviceChanged;
/* Add to Flight Planner Map Menu */
ToolStripMenuItem trkrHome = new ToolStripMenuItem(Strings.TrackerHome)
{
Name = "trkrHomeMenuItem"
};
ToolStripMenuItem obtainFrmMod = new ToolStripMenuItem(Strings.ObtainFromModule);
obtainFrmMod.Click += setTrackerHomeFromModule;
ToolStripMenuItem setAtLoc = new ToolStripMenuItem(Strings.SetHere);
setAtLoc.Click += setFromPlannerLocation;
trkrHome.DropDownItems.AddRange( new ToolStripItem[] { obtainFrmMod, setAtLoc } );
ToolStripItemCollection col = Host.FPMenuMap.Items;
int index = col.Count;
foreach (ToolStripItem item in col)
{
if (item.Text.Equals(Strings.TrackerHome))
{
index = col.IndexOf(item);
col.Remove(item);
break;
}
}
if (index != col.Count) col.Insert(index, trkrHome);
else col.Add(trkrHome);
if (getDevice() != null) _Available = true;
return true;
}
void deviceChanged(MainV2.WM_DEVICECHANGE_enum cause)
{
GPSDevice gpsModule = getDevice();
if ((gpsModule != null))
{
if (_Available == false)
{
_Available = true;
if (CustomMessageBox.Show("A GPS module was detected on your system. Would you like to use it to set your tracker home location?", "Tracker Home", MessageBoxButtons.YesNo) == (int)DialogResult.Yes)
{
GPSPosition pos = gpsModule.GetCoordinates();
double alt = getGEAlt(pos.Lat, pos.Lng);
Host.comPort.MAV.cs.TrackerLocation = new PointLatLngAlt(pos.Lat, pos.Lng, alt, "Tracker Home");
}
}
}
else if (_Available == true) _Available = false;
}
public override bool Exit()
{
return true;
}
private GPSDevice getDevice()
{
if (GarminUSBGPS.DevicePresent())
{
return new GarminUSBGPS();
}
else
{
return null;
}
}
void setFromPlannerLocation(object sender, EventArgs e)
{
Host.comPort.MAV.cs.TrackerLocation = new PointLatLngAlt(this.Host.FPMenuMapPosition);
Host.comPort.MAV.cs.TrackerLocation.Alt = srtm.getAltitude(Host.comPort.MAV.cs.TrackerLocation.Lat, Host.comPort.MAV.cs.TrackerLocation.Lng).alt;
}
void setTrackerHomeFromModule(object sender, EventArgs e)
{
GPSDevice gpsModule = getDevice();
if (gpsModule != null)
{
_Available = true;
GPSPosition pos = gpsModule.GetCoordinates();
double alt = getGEAlt(pos.Lat, pos.Lng);
Host.comPort.MAV.cs.TrackerLocation = new PointLatLngAlt(pos.Lat, pos.Lng, alt, "Tracker Home");
}
else
{
_Available = false;
CustomMessageBox.Show("No GPS Device connected. Please verify it is connected and try again.");
}
}
double getGEAlt(double lat, double lng)
{
double alt = 0;
//http://maps.google.com/maps/api/elevation/xml
try
{
using (XmlTextReader xmlreader = new XmlTextReader("http://maps.google.com/maps/api/elevation/xml?locations=" + lat.ToString(new System.Globalization.CultureInfo("en-US")) + "," + lng.ToString(new System.Globalization.CultureInfo("en-US")) + "&sensor=true"))
{
while (xmlreader.Read())
{
xmlreader.MoveToElement();
switch (xmlreader.Name)
{
case "elevation":
alt = double.Parse(xmlreader.ReadString(), new System.Globalization.CultureInfo("en-US"));
break;
default:
break;
}
}
}
}
catch { }
return alt * CurrentState.multiplierdist;
}
}