Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

AttitudeObserver.cpp

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////////////////////////////
00002 /*! \file AttitudeObserver.cpp
00003  *  \brief The Attitude observer class inherits the functionality of the observer class and provides
00004  *         the space for the the actual filter code.
00005  *  \author     $Author: jayhawk_hokie $
00006  *  \version $Revision: 1.4 $
00007  *  \date    $Date: 2007/08/31 16:08:01 $
00008 *//////////////////////////////////////////////////////////////////////////////////////////////////
00009 /*  
00010 */
00011 //////////////////////////////////////////////////////////////////////////////////////////////////
00012 
00013 #include "AttitudeObserver.h"
00014 
00015 Matrix Att_Fmatrix(KalmanFilter* ekf);
00016 Matrix Att_Hmatrix(KalmanFilter* kf);
00017 Matrix Att_Qmatrix(KalmanFilter* kf);
00018 Matrix Att_Rmatrix(KalmanFilter* kf);
00019 Vector Att_stateRHSFunc(double t, Vector initialState, Vector controls, Vector params);
00020 Vector Att_measurementRHSFunc(ExtendedKalmanFilter* ekf);
00021 Vector qdot(double time, Vector state, Vector controls, Vector params);
00022 
00023 /*! Initialize the filter */
00024 int AttitudeObserver::Initialize() {
00025         
00026         /*! Set up intial kalman filter data */
00027         Matrix P(3,3);
00028         P(1,1) = 0.001; P(1,2) = 0.0;   P(1,3) = 0.0;
00029         P(2,1) = 0.0;   P(2,2) = 0.001; P(2,3) = 0.0;
00030         P(3,1) = 0.0;   P(3,2) = 0.0;   P(3,3) = 0.001;
00031         Matrix K(3,3);
00032         K(1,1) = 0.001; K(1,2) = 0.0;   K(1,3) = 0.0;
00033         K(2,1) = 0.0;   K(2,2) = 0.001; K(2,3) = 0.0;
00034         K(3,1) = 0.0;   K(3,2) = 0.0;   K(3,3) = 0.001;
00035         
00036         ekf.SetCovarianceMatrix(P);
00037         ekf.SetKalmanGainMatrix(K);
00038         
00039         ekf.SetSystemProcessNoiseMatrix(&Att_Qmatrix);
00040         ekf.SetMeasurementCovarianceMatrix(&Att_Rmatrix);
00041         ekf.SetStateJacobianMatrix(&Att_Fmatrix);
00042         ekf.SetMeasurementJacobianMatrix(&Att_Hmatrix);
00043         
00044         ekf.SetStateRHS(&Att_stateRHSFunc);
00045         ekf.SetMeasurementRHS(&Att_measurementRHSFunc);
00046         
00047         Vector b(3);
00048         for ( int ii = 1; ii <= 100; ii++ ) {
00049                 b(1) = b(1) + m_whorl->GetRateGyro(string("dmu_rg1"))->GetMeasurement().GetAsDouble();
00050                 b(2) = b(2) + m_whorl->GetRateGyro(string("dmu_rg2"))->GetMeasurement().GetAsDouble();
00051                 b(3) = b(3) + m_whorl->GetRateGyro(string("dmu_rg3"))->GetMeasurement().GetAsDouble();
00052         };
00053         
00054         Vector zeroVector(3);
00055         m_b = zeroVector;               
00056         m_b = b / 100;
00057         
00058         return 0;
00059         
00060 };
00061 
00062 /*! Run is the funtion that implements the estimator in the derived class Filter */
00063 int AttitudeObserver::Run() {
00064         
00065         /** Get rate gyro measurements */
00066         m_measurements[0] = m_whorl->GetRateGyro(string("dmu_rg1"))->GetMeasurement();
00067         m_measurements[1] = m_whorl->GetRateGyro(string("dmu_rg2"))->GetMeasurement();
00068         m_measurements[2] = m_whorl->GetRateGyro(string("dmu_rg3"))->GetMeasurement();
00069         Vector z(3);
00070         z(1) = m_measurements[0].GetAsDouble() - m_b(1);
00071         z(2) = m_measurements[1].GetAsDouble() - m_b(2);
00072         z(3) = m_measurements[2].GetAsDouble() - m_b(3);
00073         
00074         /** Perform time calculations */
00075         timeval measurementTime, currentTime;
00076         m_measurements[0].GetTime(measurementTime);
00077         currentTime = m_whorl->GetTimeOfEstimate();
00078         double relTimeOfMeasurement = static_cast<double>(measurementTime.tv_sec - currentTime.tv_sec) + (static_cast<double>(measurementTime.tv_usec - currentTime.tv_usec)) * 1e-6;
00079         
00080         /* Begin filter code --------------------------------- */
00081         
00082         /** Initialize filter */
00083         Vector state   = m_whorl->GetOmegaBL();
00084         Vector control = m_whorl->GetControl();
00085         Vector params  = m_whorl->GetParameter();
00086         double zero = 0.0;
00087         ekf.SetTimeOfEstimate(zero);             // set time of the last estimate
00088         ekf.SetTimeOfMeasurements(relTimeOfMeasurement); // set time of the measurements
00089         ekf.SetStateVector(state);                  // set the state vector
00090         ekf.SetControlVector(control);
00091         ekf.SetParameterVector(params);
00092         ekf.SetMeasurementVector(z);
00093         
00094         /*! Estimate state using measurements */
00095         ekf.EstimateState();
00096         
00097         /* End filter code --------------------------------- */
00098         // Update the global Whorl class
00099         state = ekf.GetStateVector();
00100         Vector fullState(7);
00101         int numberOfSteps = 5;
00102         Vector q = m_whorl->GetQuaternion();
00103         // FIX
00104         // Unknown symbol: RungeKutta
00105         // fullState(_(1,4)) = RungeKutta(q, state, control, 0.0, relTimeOfMeasurement, numberOfSteps, &qdot);
00106         fullState(_(5,7)) = state;
00107 
00108         cout << "RATE? : " << fullState << endl;
00109 
00110         m_whorl->SetState(fullState);
00111         m_whorl->SetTimeOfEstimate(measurementTime);
00112         
00113         return 0;
00114         
00115 };
00116 
00117 Vector qdot(double time, Vector q, Vector controls, Vector w) {
00118         
00119         Matrix Q(4,3);
00120         Q(_(1,3),_(1,3)) = skew(q(_(1,3))) + q(4) * eye(3);
00121         Q(4,1) = -q(1);
00122         Q(4,2) = -q(2);
00123         Q(4,3) = -q(3);
00124         
00125         return 0.5 * Q * w;
00126         
00127 };
00128 
00129 Matrix Att_Fmatrix(KalmanFilter* ekf) {
00130         
00131         Vector state  = ekf->GetStateVector();
00132         Vector params = ekf->GetParameterVector();
00133         
00134         Matrix MOI(3,3);
00135 
00136         MOI(1,1) = params(1); MOI(1,2) = params(2); MOI(1,3) = params(3);
00137         MOI(2,1) = params(2); MOI(2,2) = params(4); MOI(2,3) = params(5);
00138         MOI(3,1) = params(3); MOI(3,2) = params(5); MOI(3,3) = params(6);
00139         
00140         Vector w = state;
00141         
00142         Matrix temp = MOI/( -skew(w)*MOI +  skew(MOI * w) );
00143         
00144         return temp;
00145         
00146 };
00147 
00148 Matrix Att_Hmatrix(KalmanFilter* kf) {
00149         
00150         Matrix temp(3,3);
00151         
00152         temp(1,1) = 1.0; temp(1,2) = 0.0; temp(1,3) = 0.0;
00153         temp(2,1) = 0.0; temp(2,2) = 1.0; temp(2,3) = 0.0;
00154         temp(3,1) = 0.0; temp(3,2) = 0.0; temp(3,3) = 1.0;
00155         
00156         return temp;
00157         
00158 };
00159 
00160 Matrix Att_Qmatrix(KalmanFilter* kf) {
00161         
00162         Matrix temp(3,3);
00163         
00164         temp(1,1) = 0.0001; temp(1,2) = 0.0;   temp(1,3) = 0.0;
00165         temp(2,1) = 0.0;   temp(2,2) = 0.001; temp(2,3) = 0.0;
00166         temp(3,1) = 0.0;   temp(3,2) = 0.0;   temp(3,3) = 0.001;
00167 
00168         return temp;
00169 
00170 };
00171 
00172 Matrix Att_Rmatrix(KalmanFilter* ekf) {
00173         
00174         Matrix temp(3,3);
00175         double gyro_err = 0.005, gyro_std = gyro_err / pow(2.0, 0.5);
00176         
00177         temp(1,1) = pow(gyro_std, 2.0); temp(1,2) = 0.0;                temp(1,3) = 0.0;
00178         temp(2,1) = 0.0;                temp(2,2) = pow(gyro_std, 2.0); temp(2,3) = 0.0;
00179         temp(3,1) = 0.0;                temp(3,2) = 0.0;                temp(3,3) = pow(gyro_std, 2.0);
00180         
00181         return temp;
00182         
00183 };
00184 
00185 Vector Att_stateRHSFunc(double t, Vector initialState, Vector controls, Vector params) {
00186 
00187         Vector w = initialState;
00188 
00189         Matrix MOI(3,3);
00190 
00191         MOI(1,1) = params(1); MOI(1,2) = params(2); MOI(1,3) = params(3);
00192         MOI(2,1) = params(2); MOI(2,2) = params(4); MOI(2,3) = params(5);
00193         MOI(3,1) = params(3); MOI(3,2) = params(5); MOI(3,3) = params(6);
00194         
00195         return (MOI / ( (-skew(w) * MOI * w) + controls) );
00196         
00197 };
00198 
00199 Vector Att_measurementRHSFunc(ExtendedKalmanFilter* ekf) { return ekf->GetStateVector(); };
00200 
00201 // Do not change the comments below - they will be added automatically by CVS
00202 /*****************************************************************************
00203 *       $Log: AttitudeObserver.cpp,v $
00204 *       Revision 1.4  2007/08/31 16:08:01  jayhawk_hokie
00205 *       Modified.
00206 *       
00207 *       Revision 1.3  2005/03/16 18:08:14  cakinli
00208 *       *** empty log message ***
00209 *       
00210 *       Revision 1.2  2003/08/27 21:55:43  simpliciter
00211 *       Fixed bugs in RHS and F functions.
00212 *       
00213 *       Revision 1.1  2003/08/18 19:38:37  mavandyk
00214 *       *** empty log message ***
00215 *       
00216 *       Revision 1.10  2003/08/13 23:18:07  mavandyk
00217 *       Altered structure as outline in meeting.
00218 *       
00219 *       Revision 1.9  2003/08/06 21:49:11  mavandyk
00220 *       Added comments fixed class structure and runs with config parsig.
00221 *       
00222 *       Revision 1.8  2003/08/01 11:21:30  mavandyk
00223 *       Changed class structure.
00224 *       
00225 *       Revision 1.7  2003/07/22 21:22:52  mavandyk
00226 *       Fixed bugs.
00227 *       
00228 *       Revision 1.6  2003/07/21 21:53:48  mavandyk
00229 *       Fixed some syntax errors.
00230 *       
00231 *       Revision 1.5  2003/07/14 20:06:48  mavandyk
00232 *       Fixed calls to Whorl class, and handling of time and measurements.
00233 *       
00234 *       Revision 1.4  2003/07/10 19:51:02  mavandyk
00235 *       Fixed time handling.
00236 *       
00237 *       Revision 1.3  2003/07/07 20:47:44  mavandyk
00238 *       Changed the state RHS function to use the skew utility function.
00239 *       
00240 *       Revision 1.2  2003/07/07 20:43:34  mavandyk
00241 *       Fixed the Fmatrix function with JS's derivation of the Jacobian matrix for omegadot.
00242 *       
00243 *       Revision 1.1  2003/07/04 14:12:29  simpliciter
00244 *       Moved from src directory.
00245 *       
00246 *       Revision 1.1  2003/07/02 21:33:42  mavandyk
00247 *       The observer for the demo.
00248 *       
00249 *       Revision 1.1  2003/06/30 16:06:00  nicmcp
00250 *       replaced the controller files with the Controller files
00251 *       
00252 *       Revision 1.3  2003/06/30 15:51:57  nicmcp
00253 *       made some changes to the controller files
00254 *       
00255 *       Revision 1.2  2003/06/30 14:13:35  simpliciter
00256 *       Modified comments, added log blocks.
00257 *       
00258 *       
00259 *
00260 ******************************************************************************/

Generated on Wed Sep 5 12:54:18 2007 for DSACSS Operational Code by  doxygen 1.3.9.1