Exemplo n.º 1
0
def test_gravitation_ecef():
    g = earth.gravity(90, 100)
    g0_e = earth.gravitation_ecef(90, 0, 100)
    assert_allclose(g0_e, [0, 0, -g], atol=1e-12)

    g = earth.gravity(0)
    g0_e = earth.gravitation_ecef(0, 90)
    assert_allclose(g0_e, [0, -g - earth.RATE**2 * earth.R0, 0],
                    atol=1e-12)

    g0_true = [[0, 0, -earth.gravity(90, 100)],
               [0, -earth.gravity(0) - earth.RATE**2 * earth.R0, 0]]
    assert_allclose(earth.gravitation_ecef([90, 0], [0, 90], [100, 0]),
                    g0_true, atol=1e-12)
Exemplo n.º 2
0
def test_gravity():
    # Another smoke test.
    g = earth.gravity(0)
    assert_allclose(g, 9.7803253359, rtol=1e-10)

    g = earth.gravity(90)
    assert_allclose(g, 9.8321849378, rtol=1e-10)

    g = earth.gravity(0, 0.5)
    assert_allclose(g, 9.7803253359 * (1 - 1 / earth.R0), rtol=1e-10)

    g = earth.gravity([0, 0], [0, 0.5])
    assert_allclose(g, [9.7803253359, 9.7803253359 * (1 - 1 / earth.R0)],
                    rtol=1e-10)
Exemplo n.º 3
0
def test_gravity():
    # Another smoke test.
    earth.set_model('WGS84')
    g = earth.gravity(0)
    assert_allclose(g, 9.7803253359, rtol=1e-10)

    g = earth.gravity(1)
    assert_allclose(g, 9.8321849378, rtol=1e-10)

    g = earth.gravity(0, 0.5)
    assert_allclose(g, 9.7803253359 * (1 - 1 / earth.R0), rtol=1e-10)

    g = earth.gravity([0, 0], [0, 0.5])
    assert_allclose(g, [9.7803253359, 9.7803253359 * (1 - 1 / earth.R0)],
                    rtol=1e-10)

    earth.set_model('PZ90')
    g = earth.gravity(0)
    assert_allclose(g, 9.7803284, rtol=1e-10)

    g = earth.gravity(1)
    assert_allclose(g, 9.8321880, rtol=1e-10)

    g = earth.gravity(0, 0.5)
    assert_allclose(g, 9.7803284 * (1 - 1 / earth.R0), rtol=1e-10)

    g = earth.gravity([0, 0], [0, 0.5])
    assert_allclose(g, [9.7803284, 9.7803284 * (1 - 1 / earth.R0)],
                    rtol=1e-10)
Exemplo n.º 4
0
def test_gravitation_ecef():
    for model in ['WGS84', 'PZ90']:
        earth.set_model(model)
        g = earth.gravity(1, 100)
        g0_e = earth.gravitation_ecef(90, 0, 100)
        assert_allclose(g0_e, [0, 0, -g], atol=1e-12)

        g = earth.gravity(0)
        g0_e = earth.gravitation_ecef(0, 90)
        assert_allclose(g0_e, [0, -g - earth.RATE**2 * earth.R0, 0],
                        atol=1e-12)

        g0_true = [[0, 0, -earth.gravity(1, 100)],
                   [0, -earth.gravity(0) - earth.RATE**2 * earth.R0, 0]]
        assert_allclose(earth.gravitation_ecef([90, 0], [0, 90], [100, 0]),
                        g0_true, atol=1e-12)
Exemplo n.º 5
0
def test_from_position():
    dt = 1e-1
    n_points = 1000

    lat = np.full(n_points, 50.0)
    lon = np.full(n_points, 45.0)
    alt = np.zeros(n_points)
    h = np.zeros(n_points)
    p = np.zeros(n_points)
    r = np.zeros(n_points)
    VE = np.zeros(n_points)
    VN = np.zeros(n_points)
    VU = np.zeros(n_points)

    slat = np.sin(np.deg2rad(50))
    clat = (1 - slat**2)**0.5

    gyro = earth.RATE * np.array([0, clat, slat]) * dt
    accel = np.array([0, 0, earth.gravity(slat)]) * dt

    traj, gyro_g, accel_g = sim.from_position(dt, lat, lon, alt, h, p, r)
    assert_allclose(traj.lat, 50, rtol=1e-12)
    assert_allclose(traj.lon, 45, rtol=1e-12)
    assert_allclose(traj.VE, 0, atol=1e-7)
    assert_allclose(traj.VN, 0, atol=1e-7)
    assert_allclose(traj.h, 0, atol=1e-8)
    assert_allclose(traj.p, 0, atol=1e-8)
    assert_allclose(traj.r, 0, atol=1e-8)

    for i in range(3):
        assert_allclose(gyro_g[:, i], gyro[i], atol=1e-16)
        assert_allclose(accel_g[:, i], accel[i], atol=1e-7)

    traj, gyro_g, accel_g = sim.from_velocity(dt, 50, 45, 0, VE, VN, VU, h, p,
                                              r)
    assert_allclose(traj.lat, 50, rtol=1e-12)
    assert_allclose(traj.lon, 45, rtol=1e-12)
    assert_allclose(traj.VE, 0, atol=1e-7)
    assert_allclose(traj.VN, 0, atol=1e-7)
    assert_allclose(traj.h, 0, atol=1e-8)
    assert_allclose(traj.p, 0, atol=1e-8)
    assert_allclose(traj.r, 0, atol=1e-8)

    for i in range(3):
        assert_allclose(gyro_g[:, i], gyro[i], atol=1e-16)
        assert_allclose(accel_g[:, i], accel[i], atol=1e-7)
Exemplo n.º 6
0
def test_stationary():
    dt = 0.1
    n_points = 100
    t = dt * np.arange(n_points)
    lat = 58
    alt = -10
    h = np.full(n_points, 45)
    p = np.full(n_points, -10)
    r = np.full(n_points, 5)
    Cnb = dcm.from_hpr(h, p, r)

    slat = np.sin(np.deg2rad(lat))
    clat = (1 - slat**2)**0.5
    omega_n = earth.RATE * np.array([0, clat, slat])
    g_n = np.array([0, 0, -earth.gravity(lat, alt)])
    Omega_b = Cnb[0].T.dot(omega_n)
    g_b = Cnb[0].T.dot(g_n)

    gyro, accel = sim.stationary_rotation(0.1, lat, alt, Cnb)
    gyro_true = np.tile(Omega_b * dt, (n_points - 1, 1))
    accel_true = np.tile(-g_b * dt, (n_points - 1, 1))
    assert_allclose(gyro, gyro_true)
    assert_allclose(accel, accel_true, rtol=1e-5, atol=1e-8)

    # Rotate around Earth's axis with additional rate.
    rate = 6
    rate_n = rate * np.array([0, clat, slat])
    rate_s = Cnb[0].T.dot(rate_n)
    Cbs = dcm.from_rv(rate_s * t[:, None])

    gyro, accel = sim.stationary_rotation(0.1, lat, alt, Cnb, Cbs)
    gyro_true = np.tile((Omega_b + rate_s) * dt, (n_points - 1, 1))
    assert_allclose(gyro, gyro_true)

    # Place IMU horizontally and rotate around vertical axis.
    # Gravity components should be identically 0.
    p = np.full(n_points, 0)
    r = np.full(n_points, 0)
    Cnb = dcm.from_hpr(h, p, r)
    Cbs = dcm.from_hpr(rate * t, 0, 0)
    gyro, accel = sim.stationary_rotation(0.1, lat, alt, Cnb, Cbs)
    accel_true = np.tile(-g_n * dt, (n_points - 1, 1))
    assert_allclose(accel, accel_true, rtol=1e-5, atol=1e-7)
Exemplo n.º 7
0
def test_integrate():
    # Test on the static bench.
    dt = 1e-1
    n = 100

    Cnb = dcm.from_hpr(45, -30, 60)
    gyro = np.array([0, 1 / np.sqrt(2), 1 / np.sqrt(2)]) * earth.RATE * dt
    gyro = Cnb.T.dot(gyro)
    gyro = np.resize(gyro, (n, 3))

    accel = np.array([0, 0, earth.gravity(1 / np.sqrt(2))]) * dt
    accel = Cnb.T.dot(accel)
    accel = np.resize(accel, (n, 3))

    theta, dv = coning_sculling(gyro, accel)

    traj = integrate(dt, 45, 50, 0, 0, 45, -30, 60, theta, dv)

    assert_allclose(traj.lat, 45, rtol=1e-12)
    assert_allclose(traj.lon, 50, rtol=1e-12)
    assert_allclose(traj.VE, 0, atol=1e-8)
    assert_allclose(traj.VN, 0, atol=1e-8)
    assert_allclose(traj.h, 45, rtol=1e-12)
    assert_allclose(traj.p, -30, rtol=1e-12)
    assert_allclose(traj.r, 60, rtol=1e-12)

    Integrator.INITIAL_SIZE = 50
    I = Integrator(dt, 45, 50, 0, 0, 45, -30, 60)
    I.integrate(theta[:n // 2], dv[:n // 2])
    I.integrate(theta[n // 2:], dv[n // 2:])

    assert_allclose(I.traj.lat, 45, rtol=1e-12)
    assert_allclose(I.traj.lon, 50, rtol=1e-12)
    assert_allclose(I.traj.VE, 0, atol=1e-8)
    assert_allclose(I.traj.VN, 0, atol=1e-8)
    assert_allclose(I.traj.h, 45, rtol=1e-12)
    assert_allclose(I.traj.p, -30, rtol=1e-12)
    assert_allclose(I.traj.r, 60, rtol=1e-12)
Exemplo n.º 8
0
def test_wahba():
    lat = 45
    Cnb = dcm.from_hpr(45, -30, 60)

    dt = 1e-1
    n = 1000

    gyro = np.array([0, 1 / np.sqrt(2), 1 / np.sqrt(2)]) * earth.RATE * dt
    gyro = Cnb.T.dot(gyro)
    gyro = np.resize(gyro, (n, 3))

    accel = np.array([0, 0, earth.gravity(1 / np.sqrt(2))]) * dt
    accel = Cnb.T.dot(accel)
    accel = np.resize(accel, (n, 3))

    np.random.seed(0)
    gyro += 1e-6 * np.random.randn(*gyro.shape) * dt
    accel += 1e-4 * np.random.randn(*accel.shape) * dt

    phi, dv = coning_sculling(gyro, accel)
    hpr, P = align.align_wahba(dt, phi, dv, lat)

    assert_allclose(hpr, [45, -30, 60], rtol=1e-3)
Exemplo n.º 9
0
def align_wahba(dt, theta, dv, lat, VE=None, VN=None):
    """Estimate attitude matrix by solving Wahba's problem.

    This method is based on solving a least-squares problem for a direction
    cosine matrix A (originally formulated in [1]_)::

        L = sum(||A r_i - b_i||^2, i=1, ..., m) -> min A,
        s. t. A being a right orthogonal matrix.

    Here ``(r_i, b_i)`` are measurements of the same unit vectors in two
    frames.

    The application of this method to self alignment of INS is explained in
    [2]_. In this problem the vectors ``(r_i, b_i)`` are normalized velocity
    increments due to gravity. It is applicable to dynamic conditions as well,
    but in this case a full accuracy can be achieved only if velocity is
    provided.

    The optimization problem is solved using the most straightforward method
    based on SVD [3]_.

    Parameters
    ----------
    dt : double
        Sensors sampling period.
    theta, dv : array_like, shape (n_samples, 3)
        Rotation vectors and velocity increments computed from gyro and
        accelerometer readings after applying coning and sculling
        corrections.
    lat : float
        Latitude of the place.
    VE, VN : array_like with shape (n_samples + 1, 3) or None
        East and North velocity of the target. If None (default), it is
        assumed to be 0. See Notes for further details.

    Returns
    -------
    hpr : tuple of 3 floats
        Estimated heading, pitch and roll at the end of the alignment.
    P_align : ndarray, shape (3, 3)
        Covariance matrix of misalignment angles, commonly known as
        "phi-angle" in INS literature. Its values are measured in degrees
        squared. This matrix is estimated in a rather ad-hoc fashion, see
        Notes.

    Notes
    -----
    If the alignment takes place in dynamic conditions but velocities `VE`
    and `VN` are not provided, the alignment accuracy will be decreased (to
    some extent it will be reflected in `P_align`). Note that `VE` and `VN` are
    required with the same rate as inertial readings (and contain 1 more
    sample). It means that you usually have to do some sort of interpolation.
    In on-board implementation you just provide the last available velocity
    data from GPS and it will work fine.

    The paper [3]_ contains a recipe of computing the covariance matrix given
    that errors in measurements are independent, small and follow a statistical
    distribution with zero mean and known variance. In our case we estimate
    measurement error variance from the optimal value of the optimized function
    (see above). But as our errors are not independent and necessary small
    (nor they follow any reasonable distribution) we don't scale their
    variance by the number of observations (which is commonly done for the
    variance of an average value). Some experiments show that this approach
    gives reasonable values of `P_align`.

    Also note, that `P_align` accounts only for misalignment errors due
    to non-perfect alignment conditions. In addition to that, azimuth accuracy
    is always limited by gyro drifts and level accuracy is limited by the
    accelerometer biases. You should add these systematic uncertainties to the
    diagonal of `P_align`.

    References
    ----------
    .. [1] G. Wahba, "Problem 65–1: A Least Squares Estimate of Spacecraft
           Attitude", SIAM Review, 1965, 7(3), 409.
    .. [2] P. M. G. Silson, "Coarse Alignment of a Ship’s Strapdown Inertial
          Attitude Reference System Using Velocity Loci", IEEE Trans. Instrum.
          Meas., vol. 60, pp. 1930-1941, Jun. 2011.
    .. [3] F. L. Markley, "Attitude Determination using Vector Observations
           and the Singular Value Decomposition", The Journal of the
           Astronautical Sciences, Vol. 36, No. 3, pp. 245-258, Jul.-Sept.
           1988.
    """
    n_samples = theta.shape[0]
    Vg = np.zeros((n_samples + 1, 3))
    if VE is not None:
        Vg[:, 0] = VE
    if VN is not None:
        Vg[:, 1] = VN

    lat = np.deg2rad(lat)

    slat, clat = np.sin(lat), np.cos(lat)
    tlat = slat / clat
    re, rn = earth.principal_radii(lat)
    u = earth.RATE * np.array([0, clat, slat])
    g = np.array([0, 0, -earth.gravity(slat)])

    Cb0b = np.empty((n_samples + 1, 3, 3))
    Cg0g = np.empty((n_samples + 1, 3, 3))
    Cb0b[0] = np.identity(3)
    Cg0g[0] = np.identity(3)

    Vg_m = 0.5 * (Vg[1:] + Vg[:-1])

    rho = np.empty_like(Vg_m)
    rho[:, 0] = -Vg_m[:, 1] / rn
    rho[:, 1] = Vg_m[:, 0] / re
    rho[:, 2] = Vg_m[:, 0] / re * tlat

    for i in range(n_samples):
        Cg0g[i + 1] = Cg0g[i].dot(dcm.from_rv((rho[i] + u) * dt))
        Cb0b[i + 1] = Cb0b[i].dot(dcm.from_rv(theta[i]))

    f_g = np.cross(u, Vg) - g
    f_g0 = util.mv_prod(Cg0g, f_g)
    f_g0 = 0.5 * (f_g0[1:] + f_g0[:-1])
    f_g0 = np.vstack((np.zeros(3), f_g0))
    V_g0 = util.mv_prod(Cg0g, Vg) + dt * np.cumsum(f_g0, axis=0)

    V_b0 = np.cumsum(util.mv_prod(Cb0b[:-1], dv), axis=0)
    V_b0 = np.vstack((np.zeros(3), V_b0))

    k = n_samples // 2
    b = V_g0[k:2 * k] - V_g0[:k]
    b /= np.linalg.norm(b, axis=1)[:, None]

    r = V_b0[k:2 * k] - V_b0[:k]
    r /= np.linalg.norm(r, axis=1)[:, None]

    B = np.zeros((3, 3))
    for bi, ri in zip(b, r):
        B += np.outer(bi, ri)
    n_obs = b.shape[0]
    B /= n_obs

    U, s, VT = svd(B, overwrite_a=True)
    d = det(U) * det(VT)
    Cg0b0 = U.dot(np.diag([1, 1, d])).dot(VT)

    Cgb = Cg0g[-1].T.dot(Cg0b0).dot(Cb0b[-1])

    s[-1] *= d
    trace_s = np.sum(s)
    L = 1 - trace_s
    D = trace_s - s
    M = np.identity(3) - np.diag(s)
    if L < 0 or np.any(M < 0):
        L = max(L, 0)
        M[M < 0] = 0
        warn("Negative values encountered when estimating the covariance, "
             "they were set to zeros.")

    R = (L * M / n_obs)**0.5 / D
    R = U.dot(R)
    R = Cg0g[-1].T.dot(R)
    R = np.rad2deg(R)

    return dcm.to_hpr(Cgb), R.dot(R.T)