read acceleration and angler ratio from mpu6050 and estimate pitch and roll angle

Dependencies:   mbed

Committer:
ojan
Date:
Thu Apr 16 08:51:04 2015 +0000
Revision:
1:2eca9b376580
Child:
2:4a6b46653abf
???????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ojan 1:2eca9b376580 1 #include "mbed.h"
ojan 1:2eca9b376580 2 #include "myConstants.h"
ojan 1:2eca9b376580 3 #include "Matrix.h"
ojan 1:2eca9b376580 4
ojan 1:2eca9b376580 5
ojan 1:2eca9b376580 6
ojan 1:2eca9b376580 7 Matrix::Matrix(int row, int col) : row(row), col(col), components(0) {
ojan 1:2eca9b376580 8 components = new float[row*col];
ojan 1:2eca9b376580 9 if (!components) AbortWithMsg("Memory Allocation Error");
ojan 1:2eca9b376580 10 memset(components, 0, sizeof(float)*row*col);
ojan 1:2eca9b376580 11 if (row == col) {
ojan 1:2eca9b376580 12 for (int i = 0; i < row; i++) {
ojan 1:2eca9b376580 13 components[i * col + i] = 1.0f;
ojan 1:2eca9b376580 14 }
ojan 1:2eca9b376580 15 }
ojan 1:2eca9b376580 16 }
ojan 1:2eca9b376580 17
ojan 1:2eca9b376580 18 Matrix::Matrix(int row, int col, float* comps) : row(row), col(col), components(0) {
ojan 1:2eca9b376580 19 components = new float[row*col];
ojan 1:2eca9b376580 20 if (!components) AbortWithMsg("Memory Allocation Error");
ojan 1:2eca9b376580 21 memcpy(components, comps, sizeof(float)*row*col);
ojan 1:2eca9b376580 22 }
ojan 1:2eca9b376580 23
ojan 1:2eca9b376580 24
ojan 1:2eca9b376580 25 Matrix::~Matrix() {
ojan 1:2eca9b376580 26 delete[] components;
ojan 1:2eca9b376580 27 }
ojan 1:2eca9b376580 28
ojan 1:2eca9b376580 29 Matrix::Matrix(const Matrix& m) : row(m.row), col(m.col), components(0) {
ojan 1:2eca9b376580 30 components = new float[row*col];
ojan 1:2eca9b376580 31 if (!components) AbortWithMsg("Memory Allocation Error");
ojan 1:2eca9b376580 32 memcpy(components, m.GetpComponents(), sizeof(float)*row*col);
ojan 1:2eca9b376580 33 }
ojan 1:2eca9b376580 34
ojan 1:2eca9b376580 35 Matrix& Matrix::operator=(const Matrix& m) {
ojan 1:2eca9b376580 36 if (this == &m) return *this;
ojan 1:2eca9b376580 37 row = m.row;
ojan 1:2eca9b376580 38 col = m.col;
ojan 1:2eca9b376580 39 delete[] components;
ojan 1:2eca9b376580 40 components = new float[row*col];
ojan 1:2eca9b376580 41 if (!components) AbortWithMsg("Memory Allocation Error");
ojan 1:2eca9b376580 42 memcpy(components, m.GetpComponents(), sizeof(float)*row*col);
ojan 1:2eca9b376580 43
ojan 1:2eca9b376580 44 return *this;
ojan 1:2eca9b376580 45 }
ojan 1:2eca9b376580 46
ojan 1:2eca9b376580 47 Matrix& Matrix::operator+=(const Matrix& m) {
ojan 1:2eca9b376580 48 if (row != m.GetRow() || col != m.GetCol()) AbortWithMsg("Irregular Dimention");
ojan 1:2eca9b376580 49
ojan 1:2eca9b376580 50 for (int i = 0; i < row; i++) {
ojan 1:2eca9b376580 51 for (int j = 0; j < col; j++) {
ojan 1:2eca9b376580 52 components[i * col + j] += m.components[i * col + j];
ojan 1:2eca9b376580 53 }
ojan 1:2eca9b376580 54 }
ojan 1:2eca9b376580 55
ojan 1:2eca9b376580 56 this->CleanUp();
ojan 1:2eca9b376580 57
ojan 1:2eca9b376580 58 return *this;
ojan 1:2eca9b376580 59 }
ojan 1:2eca9b376580 60
ojan 1:2eca9b376580 61 Matrix& Matrix::operator-=(const Matrix& m) {
ojan 1:2eca9b376580 62 if (row != m.GetRow() || col != m.GetCol()) AbortWithMsg("Irregular Dimention");
ojan 1:2eca9b376580 63
ojan 1:2eca9b376580 64 for (int i = 0; i < row; i++) {
ojan 1:2eca9b376580 65 for (int j = 0; j < col; j++) {
ojan 1:2eca9b376580 66 components[i * col + j] -= m.components[i * col + j];
ojan 1:2eca9b376580 67 }
ojan 1:2eca9b376580 68 }
ojan 1:2eca9b376580 69
ojan 1:2eca9b376580 70 this->CleanUp();
ojan 1:2eca9b376580 71
ojan 1:2eca9b376580 72 return *this;
ojan 1:2eca9b376580 73 }
ojan 1:2eca9b376580 74
ojan 1:2eca9b376580 75 Matrix& Matrix::operator*=(const Matrix& m) {
ojan 1:2eca9b376580 76 if (col != m.GetRow()) AbortWithMsg("Irregular Dimention");
ojan 1:2eca9b376580 77 Matrix temp = Matrix(*this);
ojan 1:2eca9b376580 78
ojan 1:2eca9b376580 79 col = m.GetCol();
ojan 1:2eca9b376580 80 delete[] components;
ojan 1:2eca9b376580 81 components = new float[row*col];
ojan 1:2eca9b376580 82
ojan 1:2eca9b376580 83 for (int i = 0; i < row; i++) {
ojan 1:2eca9b376580 84 for (int j = 0; j < col; j++) {
ojan 1:2eca9b376580 85 components[i*col + j] = 0.0f;
ojan 1:2eca9b376580 86 for (int k = 0; k < m.GetRow(); k++) {
ojan 1:2eca9b376580 87 components[i * col + j] += temp.components[i * col + k] * m.components[k * col + j];
ojan 1:2eca9b376580 88 }
ojan 1:2eca9b376580 89 }
ojan 1:2eca9b376580 90 }
ojan 1:2eca9b376580 91
ojan 1:2eca9b376580 92 this->CleanUp();
ojan 1:2eca9b376580 93
ojan 1:2eca9b376580 94 return *this;
ojan 1:2eca9b376580 95 }
ojan 1:2eca9b376580 96
ojan 1:2eca9b376580 97 Matrix& Matrix::operator*=(float c) {
ojan 1:2eca9b376580 98 for (int i = 0; i < row; i++) {
ojan 1:2eca9b376580 99 for (int j = 0; j < col; j++) {
ojan 1:2eca9b376580 100 components[i*col + j] *= c;
ojan 1:2eca9b376580 101 }
ojan 1:2eca9b376580 102 }
ojan 1:2eca9b376580 103
ojan 1:2eca9b376580 104 return *this;
ojan 1:2eca9b376580 105 }
ojan 1:2eca9b376580 106
ojan 1:2eca9b376580 107 Matrix& Matrix::operator/=(float c) {
ojan 1:2eca9b376580 108 if (fabs(c) < NEARLY_ZERO) AbortWithMsg("Division by Zero");
ojan 1:2eca9b376580 109 for (int i = 0; i < row; i++) {
ojan 1:2eca9b376580 110 for (int j = 0; j < col; j++) {
ojan 1:2eca9b376580 111 components[i*col + j] /= c;
ojan 1:2eca9b376580 112 }
ojan 1:2eca9b376580 113 }
ojan 1:2eca9b376580 114
ojan 1:2eca9b376580 115 return *this;
ojan 1:2eca9b376580 116 }
ojan 1:2eca9b376580 117
ojan 1:2eca9b376580 118 void Matrix::SetComp(int rowNo, int colNo, float val) {
ojan 1:2eca9b376580 119 if (rowNo > row || colNo > col) AbortWithMsg("Index Out of Bounds Error");
ojan 1:2eca9b376580 120 components[(rowNo-1)*col + (colNo-1)] = val;
ojan 1:2eca9b376580 121 }
ojan 1:2eca9b376580 122
ojan 1:2eca9b376580 123 void Matrix::SetComps(float* pComps) {
ojan 1:2eca9b376580 124 memcpy(components, pComps, sizeof(float) * row * col);
ojan 1:2eca9b376580 125 }
ojan 1:2eca9b376580 126
ojan 1:2eca9b376580 127 float Matrix::Determinant() const{
ojan 1:2eca9b376580 128 if (row != col) AbortWithMsg("failed to calculate det. : matrix is not square");
ojan 1:2eca9b376580 129 int decSign = 0;
ojan 1:2eca9b376580 130 float retVal = 1.0f;
ojan 1:2eca9b376580 131
ojan 1:2eca9b376580 132 // 行列のLU分解
ojan 1:2eca9b376580 133 Matrix LU(this->LU_Decompose(&decSign));
ojan 1:2eca9b376580 134
ojan 1:2eca9b376580 135 for (int i = 0; i < LU.row; i++) {
ojan 1:2eca9b376580 136 retVal *= LU.components[i * LU.col + i];
ojan 1:2eca9b376580 137 }
ojan 1:2eca9b376580 138
ojan 1:2eca9b376580 139 return retVal*decSign;
ojan 1:2eca9b376580 140 }
ojan 1:2eca9b376580 141
ojan 1:2eca9b376580 142 Matrix Matrix::LU_Decompose(int* sign, Matrix* p) const{
ojan 1:2eca9b376580 143 if (row != col) AbortWithMsg("failed to LU decomposition: matrix is not square");
ojan 1:2eca9b376580 144 if (sign != 0) *sign = 1;
ojan 1:2eca9b376580 145 if (p != 0) {
ojan 1:2eca9b376580 146 if (p->row != row || p->row != p->col) AbortWithMsg("failed to LU decomposition: permitation matrix is incorrect");
ojan 1:2eca9b376580 147 // 置換行列は最初に単位行列にしておく
ojan 1:2eca9b376580 148 memset(p->components, 0, sizeof(float) * row * col);
ojan 1:2eca9b376580 149 for (int i = 0; i < row; i++) {
ojan 1:2eca9b376580 150 p->components[i * col + i] = 1.0f;
ojan 1:2eca9b376580 151 }
ojan 1:2eca9b376580 152 }
ojan 1:2eca9b376580 153 Matrix retVal(*this);
ojan 1:2eca9b376580 154
ojan 1:2eca9b376580 155 for (int d = 0; d < row - 1; d++) { // 1行1列ずつ分解を行う
ojan 1:2eca9b376580 156 // d列目の最大の要素を探索し、見つけた要素の行とd行目を交換する
ojan 1:2eca9b376580 157 int maxNo = d;
ojan 1:2eca9b376580 158 for (int i = d; i < row; i++) {
ojan 1:2eca9b376580 159 if (retVal.components[i * col + d] > retVal.components[maxNo * col + d]) maxNo = i;
ojan 1:2eca9b376580 160 }
ojan 1:2eca9b376580 161 if (maxNo != d) {
ojan 1:2eca9b376580 162 retVal.SwapRow(d + 1, maxNo + 1);
ojan 1:2eca9b376580 163 if (sign != 0) *sign *= -1;
ojan 1:2eca9b376580 164 if (p != 0) {
ojan 1:2eca9b376580 165 p->SwapRow(d + 1, maxNo + 1);
ojan 1:2eca9b376580 166 }
ojan 1:2eca9b376580 167 }
ojan 1:2eca9b376580 168 float c = retVal.components[d * col + d];
ojan 1:2eca9b376580 169 if (fabs(c) < NEARLY_ZERO) AbortWithMsg("failed to LU decomposition: Division by Zero");
ojan 1:2eca9b376580 170
ojan 1:2eca9b376580 171 // d行d列目以降の行列について計算
ojan 1:2eca9b376580 172 for (int i = d+1; i < row; i++) {
ojan 1:2eca9b376580 173 retVal.components[i * col + d] /= c;
ojan 1:2eca9b376580 174 for (int j = d+1; j < col; j++) {
ojan 1:2eca9b376580 175 retVal.components[i * col + j] -= retVal.components[d * col + j] * retVal.components[i * col + d];
ojan 1:2eca9b376580 176 }
ojan 1:2eca9b376580 177 }
ojan 1:2eca9b376580 178 }
ojan 1:2eca9b376580 179
ojan 1:2eca9b376580 180 retVal.CleanUp();
ojan 1:2eca9b376580 181
ojan 1:2eca9b376580 182 return retVal;
ojan 1:2eca9b376580 183 }
ojan 1:2eca9b376580 184
ojan 1:2eca9b376580 185 bool Matrix::Inverse(Matrix& invm) const{
ojan 1:2eca9b376580 186 if (row != col) AbortWithMsg("failed to get Inv. : matrix is not square");
ojan 1:2eca9b376580 187
ojan 1:2eca9b376580 188 Matrix P(*this);
ojan 1:2eca9b376580 189 Matrix LU(LU_Decompose(0, &P));
ojan 1:2eca9b376580 190
ojan 1:2eca9b376580 191 // 分解した行列の対角成分の積から行列式を求める
ojan 1:2eca9b376580 192 // det = 0 ならfalse
ojan 1:2eca9b376580 193 float det = 1.0f;
ojan 1:2eca9b376580 194 for (int i = 0; i < row; i++) {
ojan 1:2eca9b376580 195 det *= LU.components[i * col + i];
ojan 1:2eca9b376580 196 }
ojan 1:2eca9b376580 197 if (fabs(det) < NEARLY_ZERO) return false;
ojan 1:2eca9b376580 198
ojan 1:2eca9b376580 199 // U、Lそれぞれの逆行列を計算する
ojan 1:2eca9b376580 200 Matrix U_inv = Matrix(row, col);
ojan 1:2eca9b376580 201 Matrix L_inv = Matrix(row, col);
ojan 1:2eca9b376580 202
ojan 1:2eca9b376580 203 for (int j = 0; j < col; j++) {
ojan 1:2eca9b376580 204 for (int i = 0; i <= j; i++) {
ojan 1:2eca9b376580 205 int i_U = j - i; // U行列の逆行列は対角成分から上へ向かって
ojan 1:2eca9b376580 206 // 左から順番に値を計算する
ojan 1:2eca9b376580 207
ojan 1:2eca9b376580 208 int j_L = col - 1 - j; // L行列の逆行列は右から順番に
ojan 1:2eca9b376580 209 int i_L = j_L + i; // 対角成分から下へ向かって計算する
ojan 1:2eca9b376580 210
ojan 1:2eca9b376580 211 if (i_U != j) { // 非対角成分
ojan 1:2eca9b376580 212 float temp_U = 0.0f;
ojan 1:2eca9b376580 213 float temp_L = 0.0f;
ojan 1:2eca9b376580 214
ojan 1:2eca9b376580 215 for (int k = 0; k < i; k++) {
ojan 1:2eca9b376580 216
ojan 1:2eca9b376580 217 temp_U -= U_inv.components[(j - k) * col + j] * LU.components[i_U * col + (j - k)];
ojan 1:2eca9b376580 218
ojan 1:2eca9b376580 219 if (k == 0) {
ojan 1:2eca9b376580 220 temp_L -= LU.components[i_L * col + j_L];
ojan 1:2eca9b376580 221 } else {
ojan 1:2eca9b376580 222 temp_L -= L_inv.components[(j_L + k) * col + j_L] * LU.components[i_L * col + j_L + k];
ojan 1:2eca9b376580 223 }
ojan 1:2eca9b376580 224
ojan 1:2eca9b376580 225 }
ojan 1:2eca9b376580 226
ojan 1:2eca9b376580 227 U_inv.components[i_U * col + j] = temp_U / LU.components[i_U * col + i_U];
ojan 1:2eca9b376580 228 L_inv.components[i_L * col + j_L] = temp_L;
ojan 1:2eca9b376580 229
ojan 1:2eca9b376580 230 } else { // 対角成分
ojan 1:2eca9b376580 231 if (fabs(LU.components[i_U * col + i_U]) >= NEARLY_ZERO) {
ojan 1:2eca9b376580 232 U_inv.components[i_U * col + i_U] = 1.0f / LU.components[i_U * col + i_U];
ojan 1:2eca9b376580 233 }
ojan 1:2eca9b376580 234 }
ojan 1:2eca9b376580 235 }
ojan 1:2eca9b376580 236 }
ojan 1:2eca9b376580 237
ojan 1:2eca9b376580 238 invm = U_inv * L_inv * P;
ojan 1:2eca9b376580 239
ojan 1:2eca9b376580 240 return true;
ojan 1:2eca9b376580 241 }
ojan 1:2eca9b376580 242
ojan 1:2eca9b376580 243 Matrix Matrix::Transpose() const{
ojan 1:2eca9b376580 244 if (row != col) AbortWithMsg("failed to get Trans. : matrix is not square");
ojan 1:2eca9b376580 245 Matrix retVal(*this);
ojan 1:2eca9b376580 246
ojan 1:2eca9b376580 247 for (int i = 0; i < row; i++) {
ojan 1:2eca9b376580 248 for (int j = i + 1; j < col; j++) {
ojan 1:2eca9b376580 249 float temp = retVal.components[i * col + j];
ojan 1:2eca9b376580 250 retVal.components[i * col + j] = retVal.components[j * col + i];
ojan 1:2eca9b376580 251 retVal.components[j * col + i] = temp;
ojan 1:2eca9b376580 252 }
ojan 1:2eca9b376580 253 }
ojan 1:2eca9b376580 254
ojan 1:2eca9b376580 255 return retVal;
ojan 1:2eca9b376580 256 }
ojan 1:2eca9b376580 257
ojan 1:2eca9b376580 258 Matrix operator+(const Matrix& lhm, const Matrix& rhm) {
ojan 1:2eca9b376580 259 Matrix temp = Matrix(lhm);
ojan 1:2eca9b376580 260 temp += rhm;
ojan 1:2eca9b376580 261 return temp;
ojan 1:2eca9b376580 262 }
ojan 1:2eca9b376580 263
ojan 1:2eca9b376580 264 Matrix operator-(const Matrix& lhm, const Matrix& rhm) {
ojan 1:2eca9b376580 265 Matrix temp = Matrix(lhm);
ojan 1:2eca9b376580 266 temp -= rhm;
ojan 1:2eca9b376580 267 return temp;
ojan 1:2eca9b376580 268 }
ojan 1:2eca9b376580 269
ojan 1:2eca9b376580 270 Matrix operator*(const Matrix& lhm, const Matrix& rhm) {
ojan 1:2eca9b376580 271 Matrix temp = Matrix(lhm);
ojan 1:2eca9b376580 272 temp *= rhm;
ojan 1:2eca9b376580 273 return temp;
ojan 1:2eca9b376580 274 }
ojan 1:2eca9b376580 275
ojan 1:2eca9b376580 276 void Matrix::CleanUp() {
ojan 1:2eca9b376580 277 int num = row*col;
ojan 1:2eca9b376580 278 float maxComp = 0.0f;
ojan 1:2eca9b376580 279 for (int i = 0; i < num; i++) {
ojan 1:2eca9b376580 280 if (maxComp < fabs(components[i])) maxComp = fabs(components[i]);
ojan 1:2eca9b376580 281 }
ojan 1:2eca9b376580 282 if (maxComp > NEARLY_ZERO) {
ojan 1:2eca9b376580 283 for (int i = 0; i < num; i++) {
ojan 1:2eca9b376580 284 if (fabs(components[i]) / maxComp < ZERO_TOLERANCE) components[i] = 0.0f;
ojan 1:2eca9b376580 285 }
ojan 1:2eca9b376580 286 }
ojan 1:2eca9b376580 287 }
ojan 1:2eca9b376580 288
ojan 1:2eca9b376580 289 void Matrix::SwapRow(int rowNo1, int rowNo2) {
ojan 1:2eca9b376580 290 if (rowNo1 > row || rowNo2 > row) AbortWithMsg("Index Out of Bounds Error !!");
ojan 1:2eca9b376580 291 float* temp = new float[col];
ojan 1:2eca9b376580 292
ojan 1:2eca9b376580 293 memcpy(temp, components + (rowNo1 - 1) * col, sizeof(float) * col);
ojan 1:2eca9b376580 294 memcpy(components + (rowNo1 - 1) * col, components + (rowNo2 - 1) * col, sizeof(float) * col);
ojan 1:2eca9b376580 295 memcpy(components + (rowNo2 - 1) * col, temp, sizeof(float) * col);
ojan 1:2eca9b376580 296
ojan 1:2eca9b376580 297 delete[] temp;
ojan 1:2eca9b376580 298 }