ContentsFoundations to FrontierBezhig

eng-lab/hwe/core/kinematics.py

All lab code. Download this file.

"""EL01 lab: from measured acceleration to velocity and height. YOU WRITE THIS FILE
(EL01 lab, step 8, the phone-accelerometer test).

Your phone records its acceleration many times a second. Velocity is the running
total of acceleration x time, and height is the running total of velocity x time
(EL01, section 2.4: the derivative chain h -> v -> a, walked backwards). Summing
thin trapezoids under a curve is how a computer does that walk, and it is the
same idea as the `velocity += accel * dt` line in a game engine's update loop.

Inputs are pint Quantities wrapping numpy arrays, for example
    t = Q_(numpy.array([0.00, 0.01, 0.02]), "s")
Return Quantity arrays of the same length as t. Indexing t[k] gives one Quantity;
t.m gives the bare numpy array (only for plotting, never inside a calculation).
"""
import numpy as np

from hwe.units import Q_


def cumulative_trapezoid(t, y):
    """The running integral of y over t, by the trapezoid rule, starting at zero.

    out[0] = 0, and for k >= 1:
        out[k] = out[k-1] + (y[k-1] + y[k]) / 2 * (t[k] - t[k-1])
    Samples need not be evenly spaced. The result's unit is y's unit times t's
    unit (integrate m/s^2 over s and you get m/s). Raise ValueError if t and y
    differ in length or t has fewer than 2 samples.
    """
    raise NotImplementedError("Step 8: cumulative_trapezoid")


def remove_bias(t, accel, still_until):
    """Subtract the average reading of the still period from the whole record.

    While the phone lies still before the motion starts (t < still_until), its
    true acceleration is zero, so the average reading over that window is gravity
    plus the sensor's own bias. Subtract that average from every sample. What is
    left is the true acceleration (up positive, for a phone lying screen-up).
    Raise ValueError if no samples fall before still_until.
    """
    raise NotImplementedError("Step 8: remove_bias")


def velocity_and_height(t, accel, still_until):
    """Velocity and height over time from a raw vertical accelerometer record.

    Remove the bias with remove_bias, then integrate once for velocity and again
    for height, both starting from zero. Return the pair (velocity, height) as
    Quantity arrays in m/s and m.
    """
    raise NotImplementedError("Step 8: velocity_and_height")