00001 /* 00002 Copyright (c) 2010 Les Newell. All rights reserved/ 00003 00004 This program is free software: you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation, either version 3 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 00019 PlugF Accel_Vector[MAX_AXES]; 00020 PlugF Gyro_Vector[MAX_AXES]; 00021 PlugF MAG_Heading; 00022 SocketF roll; 00023 SocketF pitch; 00024 SocketF yaw; 00025 ParameterF Kp_ROLLPITCH; 00026 ParameterF Ki_ROLLPITCH; 00027 00028 00029 float G_Dt=0.010; // Integration time (DCM algorithm) We will run the integration loop at 100Hz 00030 00031 #include "Vector.h" 00032 #include "matrix.h" 00033 #include "DCM.h" 00034 00036 00057 class DcmAHRS : public Module 00058 { 00059 DECLARE_MODULE(DcmAHRS,Module); 00060 public: 00061 00062 DcmAHRS() 00063 { 00064 AddSocket("AHRS.Pitch",&pitch); 00065 AddSocket("AHRS.Roll",&roll); 00066 AddSocket("AHRS.Yaw",&yaw); 00067 AddSocket("DCMAHRS.RollPitch.P",&Kp_ROLLPITCH); 00068 AddSocket("DCMAHRS.RollPitch.I",&Ki_ROLLPITCH); 00069 Kp_ROLLPITCH = 0.4; 00070 Ki_ROLLPITCH = 0.0005; 00071 } 00072 00073 virtual void ParamChanged(Socket * param) 00074 { 00075 } 00076 00077 virtual void Init() 00078 { 00079 SetPriority (PRIORITY_NEVER); //don't run unless we initialize correctly 00080 SetInterval(G_Dt * 1000); 00081 Accel_Vector[AXISX].Connect("Accel.X"); 00082 Accel_Vector[AXISY].Connect("Accel.Y"); 00083 Accel_Vector[AXISZ].Connect("Accel.Z"); 00084 Gyro_Vector[1].Connect("Gyro.Pitch"); 00085 Gyro_Vector[0].Connect("Gyro.Roll"); 00086 Gyro_Vector[2].Connect("Gyro.Yaw"); 00087 MAG_Heading.Connect("Compass.Heading"); 00088 for (int ct=0; ct< MAX_AXES; ct++) 00089 { 00090 if (!Accel_Vector[ct].IsConnected()) 00091 { 00092 Errorln("DcmAHRS: I need accelerometers"); 00093 return; 00094 } 00095 if (!Gyro_Vector[ct].IsConnected()) 00096 { 00097 Errorln("DcmAHRS: I need gyros"); 00098 return; 00099 } 00100 } 00101 SetPriority(20); 00102 } 00103 00104 virtual void Reference() 00105 { 00106 DCM_reference(); 00107 } 00108 00109 virtual void Loop(const unsigned long& interval) 00110 { 00111 // Calculations... 00112 Matrix_update(); 00113 Normalize(); 00114 Drift_correction(); 00115 Euler_angles(); 00116 // *** 00117 } 00118 00119 private: 00120 00121 00122 }; 00123 00124 DcmAHRS m_DcmAHRS; 00125
1.7.0