提问者:小点点

如何利用Gps和罗盘开发到点移动算法


我正在研制一种控制船舵的算法。 我迷失在地理算法中。。。 函数不能正常工作。

direction WhereToMove(double CurrentLatitude, double CurrentLongitude, double TargetLatitude, double TargetLongitude, double azimuth) {
        double azimuthHysteresis = 5; //straight if the deviation is less than 5 degrees
        double pi = 2 * asin(1.0);
        double target = atan2(TargetLatitude - CurrentLatitude, TargetLongitude - CurrentLongitude) * 180 / pi;
        double delta = azimuth - target;
        if (delta > 180) delta -= 360;
        if (delta < -180) delta += 360;
        if (delta < -2) { 
            return right;
        }
        if (delta > 2) {
            return left;
        }
        return straight; // go straight
    }

共1个答案

匿名用户

以下几点:

可以使用常数M_PI来表示pi

我想你希望你的角度是从北面顺时针方向测量的。 atan2给出了从x轴逆时针方向的角度。 这很容易修复,使用

atan2( dLon, dLat) 

而不是

atan2( dLat, dLon) 

经度表示的距离大致是纬度表示的距离的cos(lat)乘以。 所以您应该按cos(M_PI/180.0*lat)在上面缩放您的dlon。 (cos就像所有处理角度的数学函数一样,以弧度作为参数)。

您可以通过使用数学库函数余数来简化方位角和目标差的计算,如

delta = remainder( azimuth-target, 360.0)

这将给出一个介于-180和180之间的delta

我不知道你的代码是否会在180E附近使用。 我认为你应该把经度差计算出来

remainder( TargetLongitude - CurrentLongitude, 360.0)

而不是

TargetLongitude - CurrentLongitude

这可能看起来很OTT,但我发现(很难)养成总是用这种方式计算经度差的习惯,要比在代码中到处追踪这种差,当您的代码使用180E时要容易得多。