Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

mavextra: error in distance estimation #635

Closed
gitdillo opened this issue Feb 14, 2022 · 3 comments · Fixed by #903
Closed

mavextra: error in distance estimation #635

gitdillo opened this issue Feb 14, 2022 · 3 comments · Fixed by #903
Assignees

Comments

@gitdillo
Copy link

I think there is an error in distance estimation in mavextra's distance_lat_lon().

Specifically, latitude and longitude are converted to radians only when taking their difference:

    dLat = radians(lat2) - radians(lat1)
    dLon = radians(lon2) - radians(lon1)

but then the code mixes up radians and degrees when using the difference (in radians) or the actual values (in degrees), i.e.:

    a = sin(0.5*dLat)**2 + sin(0.5*dLon)**2 * cos(lat1) * cos(lat2)
    c = 2.0 * atan2(sqrt(a), sqrt(1.0-a))

In the bit above, dLat and dLon are in radians while lat1, lat2 are in degrees.

Ideally, the conversion should happen to the inputs immediately, as it happens for example in distance_from().

@gitdillo
Copy link
Author

For example, this works:

def distance_lat_lon(lat1, lon1, lat2, lon2):
    '''distance between two points'''
    lat1 = radians(lat1)
    lon1 = radians(lon1)
    lat2 = radians(lat2)
    lon2 = radians(lon2)
    dLat = lat2 - lat1
    dLon = lon2 - lon1

    a = sin(0.5*dLat)**2 + sin(0.5*dLon)**2 * cos(lat1) * cos(lat2)
    c = 2.0 * atan2(sqrt(a), sqrt(1.0-a))
    ground_dist = 6371 * 1000 * c
    return ground_dist

@khancyr
Copy link
Contributor

khancyr commented Feb 25, 2022

@gitdillo yes that an error. The function is unsafe on lat/long wraping too.
Could you make a PR to update it like https://github.com/ArduPilot/MAVProxy/blob/d43ee6396f3b38d2c2049adc86ccd65b9b661250/MAVProxy/modules/lib/mp_util.py#L42 and maybe put a comment to this function on mavproxy that is more maintained ?

@khancyr khancyr self-assigned this Feb 25, 2022
@gitdillo
Copy link
Author

gitdillo commented Feb 25, 2022 via email

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants