前提・実現したいこと
スマートフォンから取得した加速度センサーとジャイロセンサーの値(別々のcsvファイル)を使って,madgwick filter を実装して出てきた値をcsvファイルとして保存し,スマートフォンの姿勢推定をしたいと思っています.
またmainでそれぞれのcsvファイルから読みだして用意した配列に格納しファイルを閉じ,filterUpdate関数を通して出てきた値をcsvファイルに書き込んで,推定された姿勢をpythonのほうでアニメ―ションにしたいと思っています.
質問
・用意した配列(例えばa[2][1000],w[2][1000])をどのようにしてfilterUpdate関数に通せばうまくいくのか ・filterUpdate関数から出てくる値はどのような値なのか(例えば関数のどこの値が出てくるのか). 出てくる値の数はいくつなのか. ・関数から出てきた値をどのようにcsvファイルに書き込めばよいのか
該当のソースコード
c
1#include <stdio.h> 2#pragma warning(disable : 4996) 3// Math library required for ‘sqrt’ 4#include <math.h> 5// System constants 6#define deltat 0.001f // sampling period in seconds (shown as 1 ms) 7#define gyroMeasError 3.14159265358979f * (5.0f / 180.0f) // gyroscope measurement error in rad/s (shown as 5 deg/s) 8#define beta sqrt(3.0f / 4.0f) * gyroMeasError // compute beta 9// Global system variables 10float a_x, a_y, a_z; // accelerometer measurements 11float w_x, w_y, w_z; // gyroscope measurements in rad/s 12float SEq_1 = 1.0f, SEq_2 = 0.0f, SEq_3 = 0.0f, SEq_4 = 0.0f; // estimated orientation quaternion elements with initial conditions 13void filterUpdate(float w_x, float w_y, float w_z, float a_x, float a_y, float a_z) 14{ 15// Local system variables 16float norm; // vector norm 17float SEqDot_omega_1, SEqDot_omega_2, SEqDot_omega_3, SEqDot_omega_4; // quaternion derrivative from gyroscopes elements 18float f_1, f_2, f_3; // objective function elements 19float J_11or24, J_12or23, J_13or22, J_14or21, J_32, J_33; // objective function Jacobian elements 20float SEqHatDot_1, SEqHatDot_2, SEqHatDot_3, SEqHatDot_4; // estimated direction of the gyroscope error 21// Axulirary variables to avoid reapeated calcualtions 22float halfSEq_1 = 0.5f * SEq_1; 23float halfSEq_2 = 0.5f * SEq_2; 24float halfSEq_3 = 0.5f * SEq_3; 25float halfSEq_4 = 0.5f * SEq_4; 26float twoSEq_1 = 2.0f * SEq_1; 27float twoSEq_2 = 2.0f * SEq_2; 28float twoSEq_3 = 2.0f * SEq_3; 29// Normalise the accelerometer measurement 30norm = sqrt(a_x * a_x + a_y * a_y + a_z * a_z); 31a_x /= norm; 32a_y /= norm; 33a_z /= norm; 34// Compute the objective function and Jacobian 35f_1 = twoSEq_2 * SEq_4 - twoSEq_1 * SEq_3 - a_x; 36f_2 = twoSEq_1 * SEq_2 + twoSEq_3 * SEq_4 - a_y; 37f_3 = 1.0f - twoSEq_2 * SEq_2 - twoSEq_3 * SEq_3 - a_z; 38J_11or24 = twoSEq_3; // J_11 negated in matrix multiplication 39J_12or23 = 2.0f * SEq_4; 40J_13or22 = twoSEq_1; // J_12 negated in matrix multiplication 41J_14or21 = twoSEq_2; 42J_32 = 2.0f * J_14or21; // negated in matrix multiplication 43J_33 = 2.0f * J_11or24; // negated in matrix multiplication 44// Compute the gradient (matrix multiplication) 45SEqHatDot_1 = J_14or21 * f_2 - J_11or24 * f_1; 46SEqHatDot_2 = J_12or23 * f_1 + J_13or22 * f_2 - J_32 * f_3; 47SEqHatDot_3 = J_12or23 * f_2 - J_33 * f_3 - J_13or22 * f_1; 48SEqHatDot_4 = J_14or21 * f_1 + J_11or24 * f_2; 49// Normalise the gradient 50norm = sqrt(SEqHatDot_1 * SEqHatDot_1 + SEqHatDot_2 * SEqHatDot_2 + SEqHatDot_3 * SEqHatDot_3 + SEqHatDot_4 * SEqHatDot_4); 51SEqHatDot_1 /= norm; 52SEqHatDot_2 /= norm; 53SEqHatDot_3 /= norm; 54SEqHatDot_4 /= norm; 55// Compute the quaternion derrivative measured by gyroscopes 56SEqDot_omega_1 = -halfSEq_2 * w_x - halfSEq_3 * w_y - halfSEq_4 * w_z; 57SEqDot_omega_2 = halfSEq_1 * w_x + halfSEq_3 * w_z - halfSEq_4 * w_y; 58SEqDot_omega_3 = halfSEq_1 * w_y - halfSEq_2 * w_z + halfSEq_4 * w_x; 59SEqDot_omega_4 = halfSEq_1 * w_z + halfSEq_2 * w_y - halfSEq_3 * w_x; 60// Compute then integrate the estimated quaternion derrivative 61SEq_1 += (SEqDot_omega_1 - (beta * SEqHatDot_1)) * deltat; 62SEq_2 += (SEqDot_omega_2 - (beta * SEqHatDot_2)) * deltat; 63SEq_3 += (SEqDot_omega_3 - (beta * SEqHatDot_3)) * deltat; 64SEq_4 += (SEqDot_omega_4 - (beta * SEqHatDot_4)) * deltat; 65// Normalise quaternion 66norm = sqrt(SEq_1 * SEq_1 + SEq_2 * SEq_2 + SEq_3 * SEq_3 + SEq_4 * SEq_4); 67SEq_1 /= norm; 68SEq_2 /= norm; 69SEq_3 /= norm; 70SEq_4 /= norm; 71} 72 73int main() { 74 75 float a[2][1000],w[2][1000]; 76 77 78 FILE *fp1,*fp2; 79 80 fp1 = fopen("加速度.csv", "r"); 81 if (fp1 == NULL) { 82 printf("ファイルを開くことが出来ませんでした.¥n"); 83 return; 84 } 85 printf("\n"); 86 fscanf(fp1, "%lf,%lf,%lf", a[0], a[1], a[2]); 87 fclose(fp1); 88 89 90 fp2 = fopen("角速度.csv", "r"); 91 if (fp2 == NULL) { 92 printf("ファイルを開くことが出来ませんでした.¥n"); 93 return; 94 } 95 printf("\n"); 96 fscanf(fp2, "%lf,%lf,%lf", w[0], w[1], w[2]); 97 fclose(fp2); 98 99 100 //////////////////////////////// 101 /*ここからの実装がわかりません*/ 102 //////////////////////////////// 103 104 105 106 return 0; 107 108 }
補足情報(FW/ツールのバージョンなど)
visual studio2017を使っています
またmain()のファイル操作が間違っていたらご指摘お願いします.
用意した加速度のcsvファイルは下の画像の様になっています.
角速度のほうも同じように用意してます
あなたの回答
tips
プレビュー