// One update that can be checked by hand. The rod is held in a stiff clutch, so its angle
// barely changes, and an encoder on the shaft reads that angle directly. This small filter
// works in degrees to keep the arithmetic easy to follow.
#include <cmath>
#include <cstdio>
#include <cstdlib>

#include <Eigen/Core>
#include <Eigen/Geometry>
#include <UKF/Core.h>
#include <UKF/MeasurementVector.h>

#include "arguments.h"
#include "trace.h"

enum StateKey { ShaftAngle };
enum SensorKey { Encoder = 10 };
using Shaft = UKF::StateVector<UKF::Field<ShaftAngle, real_t>>;                  // degrees
using Reading = UKF::FixedMeasurementVector<UKF::Field<Encoder, real_t>>;         // degrees
using Filter = UKF::Core<Shaft, Reading>;  // no integrator: the model gives the next state directly

namespace UKF {
// The clutch holds the rod, so the best prediction of the angle is the last estimate.
template <>
template <>
Shaft Shaft::process_model<>(real_t) const { return *this; }

template <>
template <>
real_t Reading::expected_measurement<Shaft, Encoder>(const Shaft& state) {
    return state.get_field<ShaftAngle>();
}
}  // namespace UKF

int main(int argc, char** argv) {
    Trace trace(argc, argv);
    const real_t encoder_reading = argument(argc, argv, 1, 21.0);  // degrees
    const real_t encoder_sigma = argument(argc, argv, 2, 0.5);     // degrees
    const real_t prior_sigma = argument(argc, argv, 3, 1.0);       // degrees
    const real_t creep = argument(argc, argv, 4, 0.04);            // degrees^2 per second

    Filter filter;
    filter.state.set_field<ShaftAngle>(20.0);                 // the last estimate
    filter.covariance(0, 0) = prior_sigma * prior_sigma;      // its variance
    const real_t dt = 1.0;                                    // s since that estimate
    filter.process_noise_covariance(0, 0) = creep * dt;       // the clutch may have crept
    filter.measurement_covariance.set_field<Encoder>(encoder_sigma * encoder_sigma);
    Reading reading;
    reading.set_field<Encoder>(encoder_reading);

    if (!filter.a_priori_step(dt)) return 1;       // 1. predict
    const real_t predicted = filter.state.get_field<ShaftAngle>();
    const real_t predicted_variance = filter.covariance(0, 0);

    if (!filter.innovation_step(reading)) return 1;  // 2. compare with the reading
    const real_t innovation = filter.innovation.get_field<Encoder>();
    const real_t innovation_variance = filter.innovation_covariance(0, 0);

    if (!filter.a_posteriori_step()) return 1;     // 3. correct
    const real_t corrected = filter.state.get_field<ShaftAngle>();
    const real_t corrected_sigma = std::sqrt(filter.covariance(0, 0));

    std::printf("1. predicted  %.3f deg, variance %.3f deg^2\n", predicted, predicted_variance);
    std::printf("2. innovation %.3f deg, variance %.3f deg^2\n", innovation, innovation_variance);
    std::printf("3. corrected  %.3f deg, standard deviation %.3f deg\n", corrected, corrected_sigma);
    std::printf("   the estimate moved %.1f%% of the way to the reading\n",
                100.0 * predicted_variance / innovation_variance);

    // Three seconds with no reading: predict only. The estimate stays; its uncertainty grows.
    const real_t gap = 3.0;
    filter.process_noise_covariance(0, 0) = creep * gap;
    if (!filter.a_priori_step(gap)) return 1;
    std::printf("after %.0f s without a reading: %.3f deg, standard deviation %.3f deg\n", gap,
                filter.state.get_field<ShaftAngle>(), std::sqrt(filter.covariance(0, 0)));

    trace.row("update", {predicted, predicted_variance, innovation, innovation_variance, corrected,
                         corrected_sigma, std::sqrt(filter.covariance(0, 0))});
    // By hand: gain = predicted variance / innovation variance.
    const real_t by_hand = predicted + predicted_variance / innovation_variance * innovation;
    return std::abs(corrected - by_hand) < 1e-9 ? 0 : 1;
}
