AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
GPSEncoder.cs
1 //Copyright 2013 MichaelTaylor3D
2 //www.michaeltaylor3d.com
3 using UnityEngine;
4 using Communicator;
5 using Commons;
6 namespace AirControl
7 {
8 
9  //Copyright 2013 MichaelTaylor3D
10  //www.michaeltaylor3d.com
11  //https://github.com/MichaelTaylor3D/UnityGPSConverter/
12 
13  public class GPSEncoder : MonoBehaviour
14  {
15 
16  #region Instance Variables
17  public Rigidbody rb;
18 
19  private Vector2 _localOrigin = Vector2.zero;
20  private float _LatOrigin { get{ return _localOrigin.x; }}
21  private float _LonOrigin { get{ return _localOrigin.y; }}
22  private float MaxLongitude = 180f;
23  private float MaxLatitude = 90f;
24 
25  private float metersPerLat;
26  private float metersPerLon;
27  #endregion
28 
29  void Update(){
30  HandleLocation();
31  }
32 
33  void Start()
34  {
35  rb = GameObject.Find(CommonFunctions.ActiveAirplane).GetComponent<Rigidbody>();
36  }
37 
38  #region Instance Functions
39  public void FindMetersPerLat(float lat) // Compute lengths of degrees
40  {
41  float m1 = 1000f; // reducing the gloabe equatorail diameter, original : 111132.92f; // latitude calculation term 1
42  float m2 = -559.82f; // latitude calculation term 2
43  float m3 = 1.175f; // latitude calculation term 3
44  float m4 = -0.0023f; // latitude calculation term 4
45  float p1 = 1000f; // reducing the globe polar diameter, original : 111412.84f; // longitude calculation term 1
46  float p2 = -93.5f; // longitude calculation term 2
47  float p3 = 0.118f; // longitude calculation term 3
48 
49  lat = lat * Mathf.Deg2Rad;
50 
51  // Calculate the length of a degree of latitude and longitude in meters
52  metersPerLat = m1 + (m2 * Mathf.Cos(2 * (float)lat)) + (m3 * Mathf.Cos(4 * (float)lat)) + (m4 * Mathf.Cos(6 * (float)lat));
53  metersPerLon = (p1 * Mathf.Cos((float)lat)) + (p2 * Mathf.Cos(3 * (float)lat)) + (p3 * Mathf.Cos(5 * (float)lat));
54  }
55 
56  public Vector3 ConvertGPStoUCS(Vector2 gps)
57  {
58  FindMetersPerLat(_LatOrigin);
59  float zPosition = metersPerLat * (gps.x - _LatOrigin); //Calc current lat
60  float xPosition = metersPerLon * (gps.y - _LonOrigin); //Calc current lat
61  return new Vector3((float)xPosition, 0, (float)zPosition);
62  }
63 
64  public Vector2 ConvertUCStoGPS(Vector3 position)
65  {
66  FindMetersPerLat(_LatOrigin);
67  Vector2 geoLocation = new Vector2(0,0);
68  geoLocation.x = (_LatOrigin + (position.z)/metersPerLat); //Calc current lat
69  geoLocation.y = (_LonOrigin + (position.x)/metersPerLon); //Calc current lon
70  return geoLocation;
71  }
72 
73 
77  void HandleLocation()
78  {
79  //future functionality to provide the xyz location of the plane
80  Vector3 currentLocation = rb.position;
81  Vector2 latlong = ConvertUCStoGPS(currentLocation);
82  // Debug.Log(latlong);
83  float normalizedLatitude = latlong.x/MaxLatitude;
84  float normalizedLongitude = latlong.y/MaxLongitude;
85  // Add location to static function
86  StaticOutputSchema.Latitude = normalizedLatitude;
87  StaticOutputSchema.Longitude = normalizedLongitude;
88  }
89  #endregion
90  }
91 }
AirControl.GPSEncoder
Definition: GPSEncoder.cs:13
AirControl
Definition: AirplaneSelector.cs:8
Communicator
Definition: InputHandle.cs:10
Commons
Definition: AirplaneProperties.cs:14