Jump to content
The simFlight Network Forums

Recommended Posts

Posted (edited)

Hey Team,

 

I have been racking my brain for the past 2 days trying to deep dive into programming an accurate and effective ETA system. 

Using VB.net and the harvestine theory formula to calculate the remaining time until destination. I am pulling the user's current Position (latitude and longitude) via a timer then inputting that into the following:

 

Public Sub CalculateAndDisplayETA()


        Dim xmlFile As String = My.Settings.ApplicationDirectory + "\Routes\" + IFETabletLrg.DepartureLbl.Text + IFETabletLrg.ArrivalLbl.Text + ".xml"

        ' Load the XML file that contains route information
        Dim xmlDoc As New XmlDocument()
        xmlDoc.Load(xmlFile)

        ' Find the <destination> element
        Dim destinationNode As XmlNode = xmlDoc.SelectSingleNode("/OFP/destination")

        ' Check if the <destination> elements exist
        If destinationNode IsNot Nothing Then
            ' Extract the values from the <destination> element
            Dim destinationAirportLat As Double = Double.Parse(destinationNode.SelectSingleNode("pos_lat").InnerText)
            Dim destinationAirportLon As Double = Double.Parse(destinationNode.SelectSingleNode("pos_long").InnerText)

            Dim startLat As Double = IFETabletLrg.UserPosLat  ' Formula Start latitude in degrees (Current User's A/C Lat)
            Dim startLon As Double = IFETabletLrg.userPosLng    ' Formula Start longitude in degrees (Current User's A/C Long)

            Dim endLat As Double = destinationAirportLat   ' Destination latitude in degrees
            Dim endLon As Double = destinationAirportLon   ' Destination longitude in degrees
            Dim speed As Double = ((gspeed.Value / 33714.6311111111) * 1.852)  ' User's Groundspeed

            ' Calculate the distance using the Haversine formula
            Dim distanceNM As Double = HaversineDistance(startLat, startLon, endLat, endLon)

            ' Convert distance to nautical miles to kilometers
            Dim distanceKm As Double = distanceNM * 1.852

            ' Calculate the ETA in hours
            Dim timeHours As Double = distanceKm / speed

            ' Calculate hours and minutes from the total hours (timeHours)
            Dim hours As Integer = CInt(Math.Floor(timeHours))
            Dim minutes As Integer = CInt(Math.Round((timeHours - hours) * 60))

            ' Format the ETA as "hh'h mm'm"
            Dim formattedETA As String = $"{hours:00}h {minutes:00}m"

            ' Display the formatted ETA in the label
            IFETabletLrg.ETALbl.Text = formattedETA
        End If
    End Sub

    Private Function HaversineDistance(startLat As Double, startLon As Double, endLat As Double, endLon As Double) As Double
        ' Convert degrees to radians
        Dim startLatRad As Double = startLat * Math.PI / 180.0
        Dim startLonRad As Double = startLon * Math.PI / 180.0
        Dim endLatRad As Double = endLat * Math.PI / 180.0
        Dim endLonRad As Double = endLon * Math.PI / 180.0

        ' Earth's radius in nautical miles
        Const EarthRadiusNM As Double = 3443.8445

        ' Haversine formula
        Dim dLat As Double = endLatRad - startLatRad
        Dim dLon As Double = endLonRad - startLonRad
        Dim a As Double = Math.Sin(dLat / 2.0) * Math.Sin(dLat / 2.0) + Math.Cos(startLatRad) * Math.Cos(endLatRad) * Math.Sin(dLon / 2.0) * Math.Sin(dLon / 2.0)
        Dim c As Double = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a))
        Dim distanceNM As Double = EarthRadiusNM * c

        Return distanceNM
    End Function

 

 

This is returning a result however it seems to be inaccurate when compared to other software such as SmartCars. It is slower by a range of 30 minutes to 90 minutes.

 

Any ideas?

Edited by AirFlow
  • Like 1
Posted

Hi,

I can't see anything obviously wrong. The haversine calculation is giving correct values.

The only thing I can't check is the ground speed conversion:

Dim speed As Double = ((gspeed.Value / 33714.6311111111) * 1.852)  ' User's Groundspeed

Where is gspeed coming from? What units is it in?

Paul

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.