Testing ekf implementation for Quadro_1.

Dependencies:   mbed Eigen

EKF_RP.cpp

Committer:
pmic
Date:
2019-10-18
Revision:
1:6b803652d032
Parent:
0:a0e9705be9c4
Child:
4:e50e18eac72b

File content as of revision 1:6b803652d032:

#include "EKF_RP.h"

using namespace std;
using namespace Eigen;

EKF_RP::EKF_RP(float Ts)
{
    this->Ts = Ts;
    /* [n_gyro; n_b_g; n_v] */
    var_fx << 1000, 1000, 20, 20, 10, 10;
    /* [n_acc] */
    var_gy << 40, 40;
    rho = 5000;
    kv = 0.5;       /* k1/m */
    g = 9.81;
    reset();
}

EKF_RP::~EKF_RP() {}

void EKF_RP::reset()
{   
    u.setZero();
    y.setZero();
    x.setZero();
    update_F();
    update_H();
    initialize_Q();
    initialize_R();
    P = Q;
    K.setZero();
    I.setIdentity();
}

void EKF_RP::update(float gyro_x, float gyro_y, float accel_x, float accel_y)
{
    // u << gyro_x, gyro_y;
    // y << accel_x, accel_y;
    
    // update_F();
    // update_H(); /* H remains constant */
    // update_Q();
    
    // x = fxd();
    // P = F * P * F.transpose() + Q;
    
    // K = P * H.transpose() * ( H * P * H.transpose() + R ).inverse();
    
    // x = x + K * (y - gy());
    // P = (I - K * H) * P;
}

float EKF_RP::get_est_state(uint8_t i)
{   
    /* x = [ang; b_g; v] */
    return x(i);
}

void EKF_RP::update_angles()
{
    s1 = sin(x(0));
    c1 = cos(x(0));
    s2 = sin(x(1));
    c2 = cos(x(1));   
}

void EKF_RP::update_F()
{
    F << (Ts*c1*s2*(u(1) - x(3)))/c2 + 1, -(Ts*s1*(u(1) - x(3)))/(s2*s2 - 1), -Ts, -(Ts*s1*s2)/c2,         0,         0,
                    -Ts*s1*(u(1) - x(3)),                                  1,   0,         -Ts*c1,         0,         0,
                                       0,                                  0,   1,              0,         0,         0,
                                       0,                                  0,   0,              1,         0,         0,
                                       0,                            Ts*c2*g,   0,              0, 1 - Ts*kv,         0,
                             -Ts*c1*c2*g,                         Ts*g*s1*s2,   0,              0,         0, 1 - Ts*kv;
}

void EKF_RP::update_H()
{
    H << 0, 0, 0, 0, -kv,   0,
         0, 0, 0, 0,   0, -kv;
}

void EKF_RP::initialize_R()
{
    R <<  rho*var_gy(0)/Ts,                0,
                         0, rho*var_gy(1)/Ts;
}

void EKF_RP::initialize_Q()
{   
    Q << Ts*(var_fx(0) + (s1*s1*s2*s2*var_fx(1))/(c2*c2)), (Ts*c1*s1*s2*var_fx(1))/c2,            0,            0,            0,            0,
                               (Ts*c1*s1*s2*var_fx(1))/c2,         Ts*c1*c1*var_fx(1),            0,            0,            0,            0,
                                                        0,                          0, Ts*var_fx(2),            0,            0,            0,
                                                        0,                          0,            0, Ts*var_fx(3),            0,            0,
                                                        0,                          0,            0,            0, Ts*var_fx(4),            0,
                                                        0,                          0,            0,            0,            0, Ts*var_fx(5);
}

void EKF_RP::update_Q()
{   
    Q(0,0) = Ts*(var_fx(0) + (s1*s1*s2*s2*var_fx(1))/(c2*c2));
    Q(0,1) = Ts*c1*s1*s2*var_fx(1)/c2;
    Q(1,0) = Q(0,1);
    Q(1,1) = Ts*c1*c1*var_fx(1);
}

Matrix <float, 6, 1>  EKF_RP::fxd()
{
    Matrix <float, 6, 1> retval;
    retval << x(0) + Ts*(u(0) - x(2) + (s1*s2*(u(1) - x(3)))/c2),
                                      x(1) + Ts*c1*(u(1) - x(3)),
                                                            x(2),
                                                            x(3),
                                      x(4) + Ts*(g*s2 - kv*x(4)),
                                   x(5) - Ts*(kv*x(5) + c2*g*s1);
    return retval;
}

Matrix <float, 2, 1> EKF_RP::gy()
{
    Matrix <float, 2, 1> retval;
    retval << -kv*x(4),
              -kv*x(5);
    return retval;
}