MPUとHMCでうごくかもver

Dependencies:   ConfigFile SDFileSystem mbed

Fork of LAURUS_program by hiroya taura

Committer:
ojan
Date:
Mon Jun 22 15:16:00 2015 +0000
Revision:
18:9dd72e417c60
Parent:
16:174daf81eea0
Child:
19:ad8ff2de68f5
LAURUS_program_v2.1; ; + some refactoring; + GPS bug fixed (by Onaka

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ojan 0:bc6f14fc60c7 1 #include "mbed.h"
ojan 0:bc6f14fc60c7 2 #include "MPU6050.h"
ojan 0:bc6f14fc60c7 3 #include "HMC5883L.h"
ojan 0:bc6f14fc60c7 4 #include "LPS25H.h"
ojan 1:6cd6d2760856 5 #include "GMS6_CR6.h"
ojan 0:bc6f14fc60c7 6 #include "ErrorLogger.h"
ojan 0:bc6f14fc60c7 7 #include "Vector.h"
ojan 3:5358a691a100 8 #include "Matrix.h"
ojan 3:5358a691a100 9 #include "Vector_Matrix_operator.h"
ojan 0:bc6f14fc60c7 10 #include "myConstants.h"
onaka 7:0ec343d29641 11 #include "SDFileSystem.h"
onaka 7:0ec343d29641 12 #include "BufferedSerial.h"
onaka 7:0ec343d29641 13 #include "ConfigFile.h"
ojan 0:bc6f14fc60c7 14
ojan 14:f85cb5340cb8 15 /****************************** private define ******************************/
ojan 0:bc6f14fc60c7 16 #define TRUE 1
ojan 0:bc6f14fc60c7 17 #define FALSE 0
ojan 8:602865d8fca3 18
ojan 15:d14d385d37e2 19 #define RULE1
ojan 15:d14d385d37e2 20 //#define RULE2
ojan 15:d14d385d37e2 21 //#define RULE3
ojan 15:d14d385d37e2 22
ojan 14:f85cb5340cb8 23 const float dt = 0.01f; // 割り込み周期(s)
ojan 14:f85cb5340cb8 24 const float ServoMax = 0.0023f; // サーボの最大パルス長(s)
ojan 14:f85cb5340cb8 25 const float ServoMin = 0.0006f; // サーボの最小パルス長(s)
ojan 14:f85cb5340cb8 26 const float PullMax = 25.0f; // 引っ張れる紐の最大量(mm)
ojan 14:f85cb5340cb8 27 const float BorderSpiral = 40.0f; // スパイラル検知角度
ojan 14:f85cb5340cb8 28 const short BorderOpt = 30000; // 光センサーの閾値
ojan 14:f85cb5340cb8 29 const float BorderGravity = 0.3f; // 無重力状態の閾値
ojan 14:f85cb5340cb8 30 const int BorderParafoil = 0; // 物理スイッチのOFF出力
ojan 14:f85cb5340cb8 31 const int MaxCount = 3; // 投下シグナルを何回連続で検知したら投下と判断するか(×0.2[s])
ojan 14:f85cb5340cb8 32 const int WaitTime = 1; // 投下後、安定するまで何秒滑空するか
ojan 15:d14d385d37e2 33 const float Alpha = 30.0f; // 目標方向と自分の進行方向との差の閾値(deg)(制御則1&2&3の定数
ojan 15:d14d385d37e2 34 const float Beta = 60.0f; // 目標方向と自分の進行方向との間に取るべき角度差(deg)(制御則3の定数
ojan 15:d14d385d37e2 35 const float BorderDistance = 10.0f; // 落下制御に入るための目標値との距離の閾値(m)(制御則2の定数
ojan 1:6cd6d2760856 36
ojan 14:f85cb5340cb8 37 /****************************** private macro ******************************/
ojan 14:f85cb5340cb8 38 /****************************** private typedef ******************************/
ojan 14:f85cb5340cb8 39 /****************************** private variables ******************************/
ojan 10:8ee11e412ad7 40 DigitalOut myled(LED1); // デバッグ用LEDのためのデジタル出力
ojan 10:8ee11e412ad7 41 I2C i2c(PB_9, PB_8); // I2Cポート
ojan 10:8ee11e412ad7 42 MPU6050 mpu(&i2c); // 加速度・角速度センサ
ojan 10:8ee11e412ad7 43 HMC5883L hmc(&i2c); // 地磁気センサ
ojan 10:8ee11e412ad7 44 LPS25H lps(&i2c); // 気圧センサ
ojan 10:8ee11e412ad7 45 Serial gps(PA_11, PA_12); // GPS通信用シリアルポート
ojan 10:8ee11e412ad7 46 Serial pc(SERIAL_TX, SERIAL_RX); // PC通信用シリアルポート
ojan 10:8ee11e412ad7 47 GMS6_CR6 gms(&gps, &pc); // GPS
onaka 7:0ec343d29641 48 SDFileSystem sd(PB_5, PB_4, PB_3, PB_10, "sd"); // microSD
onaka 7:0ec343d29641 49 BufferedSerial xbee(PA_9, PA_10, PC_1); // Xbee
ojan 14:f85cb5340cb8 50 ConfigFile cfg; // ConfigFile
ojan 10:8ee11e412ad7 51 PwmOut servoL(PB_6), servoR(PC_7); // サーボ用PWM出力
ojan 14:f85cb5340cb8 52 AnalogIn optSensor(PC_0); // 照度センサ用アナログ入力
onaka 16:174daf81eea0 53 AnalogIn servoVcc(PA_1); // バッテリー電圧監視用アナログ入力(サーボ用)
onaka 16:174daf81eea0 54 AnalogIn logicVcc(PA_0); // バッテリー電圧監視用アナログ入力(ロジック用)
ojan 14:f85cb5340cb8 55 DigitalIn paraSensor(PB_0); // パラフォイルに繋がる(予定)の物理スイッチ
ojan 10:8ee11e412ad7 56 Ticker INT_timer; // 割り込みタイマー
ojan 14:f85cb5340cb8 57 Timer timer; // 時間計測用タイマー
ojan 0:bc6f14fc60c7 58
ojan 14:f85cb5340cb8 59 int lps_cnt = 0; // 気圧センサ読み取りカウント
ojan 8:602865d8fca3 60 uint8_t INT_flag = TRUE; // 割り込み可否フラグ
ojan 14:f85cb5340cb8 61 Vector raw_acc(3); // 加速度(m/s^2) 生
ojan 14:f85cb5340cb8 62 Vector raw_gyro(3); // 角速度(deg/s) 生
ojan 14:f85cb5340cb8 63 Vector raw_geomag(3); // 地磁気(?) 生
ojan 14:f85cb5340cb8 64 float raw_press; // 気圧(hPa) 生
ojan 14:f85cb5340cb8 65 Vector acc(3); // 加速度(m/s^2)
ojan 14:f85cb5340cb8 66 Vector gyro(3); // 角速度(deg/s)
ojan 14:f85cb5340cb8 67 Vector geomag(3); // 地磁気(?)
ojan 14:f85cb5340cb8 68 float press; // 気圧(hPa)
ojan 1:6cd6d2760856 69
ojan 14:f85cb5340cb8 70 Vector raw_g(3); // 重力ベクトル 生
ojan 14:f85cb5340cb8 71 Vector g(3); // 重力ベクトル
ojan 14:f85cb5340cb8 72 Vector target_p(2); // 目標情報(経度、緯度)(rad)
ojan 14:f85cb5340cb8 73 Vector p(2); // 位置情報(経度, 緯度)(rad)
ojan 14:f85cb5340cb8 74 Vector pre_p(2); // 過去の位置情報(経度, 緯度)(rad)
ojan 14:f85cb5340cb8 75 int UTC_t = 0; // UTC時刻
ojan 14:f85cb5340cb8 76 int pre_UTC_t = 0; // 前のUTC時刻
ojan 8:602865d8fca3 77
ojan 14:f85cb5340cb8 78 Vector b_f(3); // 機体座標に固定された、機体前方向きのベクトル(x軸)
ojan 14:f85cb5340cb8 79 Vector b_u(3); // 機体座標に固定された、機体上方向きのベクトル(z軸)
ojan 14:f85cb5340cb8 80 Vector b_l(3); // 機体座標に固定された、機体左方向きのベクトル(y軸)
ojan 9:6d4578dcc1ed 81
ojan 14:f85cb5340cb8 82 Vector r_f(3); // 世界座標に固定された、北向きのベクトル(X軸)
ojan 14:f85cb5340cb8 83 Vector r_u(3); // 世界座標に固定された、上向きのベクトル(Z軸)
ojan 14:f85cb5340cb8 84 Vector r_l(3); // 世界座標に固定された、西向きのベクトル(Y軸)
ojan 14:f85cb5340cb8 85
ojan 14:f85cb5340cb8 86 int pull_L = 0; // 左サーボの引っ張り量(mm:0~PullMax)
ojan 14:f85cb5340cb8 87 int pull_R = 0; // 右サーボの引っ張り量(mm:0~PullMax)
ojan 9:6d4578dcc1ed 88
ojan 14:f85cb5340cb8 89 float yaw = 0.0f; // 本体のヨー角(deg)z軸周り
ojan 14:f85cb5340cb8 90 float pitch = 0.0f; // 本体のピッチ角(deg)y軸周り
ojan 14:f85cb5340cb8 91 float roll = 0.0f; // 本体のロール角(deg)x軸周り
ojan 9:6d4578dcc1ed 92
ojan 14:f85cb5340cb8 93 float vrt_acc = 0.0f; // 鉛直方向の加速度成分(落下検知に使用)
ojan 13:df1e8a650185 94
ojan 14:f85cb5340cb8 95 int step = 0; // シーケンス制御のステップ
ojan 14:f85cb5340cb8 96 int count = 0; // 時間測定用カウンター
ojan 14:f85cb5340cb8 97 int dir = 0; // 旋回方向(0:左 1:右)
ojan 14:f85cb5340cb8 98 char data[512] = {}; // 送信データ用配列
ojan 3:5358a691a100 99
onaka 7:0ec343d29641 100 /** config.txt **
ojan 15:d14d385d37e2 101 * #から始めるのはコメント行
onaka 7:0ec343d29641 102 * #イコールの前後に空白を入れない
onaka 7:0ec343d29641 103 * target_x=111.222
onaka 7:0ec343d29641 104 * target_y=33.444
onaka 7:0ec343d29641 105 */
onaka 7:0ec343d29641 106 float target_x, target_y;
onaka 7:0ec343d29641 107
ojan 14:f85cb5340cb8 108 /* ---------- Kalman Filter ---------- */
ojan 11:083c8c9a5b84 109 // 地磁気ベクトル用
ojan 14:f85cb5340cb8 110 // ジャイロのz軸周りのバイアスも推定
ojan 13:df1e8a650185 111 Vector pri_x1(7);
ojan 13:df1e8a650185 112 Matrix pri_P1(7, 7);
ojan 13:df1e8a650185 113 Vector post_x1(7);
ojan 13:df1e8a650185 114 Matrix post_P1(7, 7);
ojan 13:df1e8a650185 115 Matrix F1(7, 7), H1(3, 7);
ojan 13:df1e8a650185 116 Matrix R1(7, 7), Q1(3, 3);
ojan 13:df1e8a650185 117 Matrix I1(7, 7);
ojan 13:df1e8a650185 118 Matrix K1(7, 3);
ojan 11:083c8c9a5b84 119 Matrix S1(3, 3), S_inv1(3, 3);
ojan 11:083c8c9a5b84 120
ojan 11:083c8c9a5b84 121 // 重力ベクトル用
ojan 14:f85cb5340cb8 122 // ジャイロのx軸、y軸周りのバイアスも同時に推定
ojan 13:df1e8a650185 123 Vector pri_x2(5);
ojan 13:df1e8a650185 124 Matrix pri_P2(5, 5);
ojan 13:df1e8a650185 125 Vector post_x2(5);
ojan 13:df1e8a650185 126 Matrix post_P2(5, 5);
ojan 13:df1e8a650185 127 Matrix F2(5, 5), H2(3, 5);
ojan 13:df1e8a650185 128 Matrix R2(5, 5), Q2(3, 3);
ojan 13:df1e8a650185 129 Matrix I2(5, 5);
ojan 13:df1e8a650185 130 Matrix K2(5, 3);
ojan 13:df1e8a650185 131 Matrix S2(3, 3), S_inv2(3, 3);
ojan 14:f85cb5340cb8 132 /* ---------- ------------- ---------- */
ojan 3:5358a691a100 133
ojan 1:6cd6d2760856 134
ojan 14:f85cb5340cb8 135 /****************************** private functions ******************************/
ojan 14:f85cb5340cb8 136 void LoadConfig(); // config読み取り
ojan 14:f85cb5340cb8 137 int Find_last(); // SDカード初期化用関数
ojan 14:f85cb5340cb8 138 void KalmanInit(); // カルマンフィルタ初期化
ojan 14:f85cb5340cb8 139 void KalmanUpdate(); // カルマンフィルタ更新
ojan 14:f85cb5340cb8 140 float Distance(Vector p1, Vector p2); // 緯度・経度の差から2点間の距離を算出(m)
ojan 14:f85cb5340cb8 141 bool thrown(); // 投下されたかどうかを判断する
ojan 14:f85cb5340cb8 142 void INT_func(); // 割り込み用関数
ojan 14:f85cb5340cb8 143 void toString(Matrix& m); // デバッグ用
ojan 14:f85cb5340cb8 144 void toString(Vector& v); // デバッグ用
ojan 1:6cd6d2760856 145
ojan 15:d14d385d37e2 146 inline float min(float a, float b)
ojan 15:d14d385d37e2 147 {
ojan 14:f85cb5340cb8 148 return ((a > b) ? b : a);
ojan 14:f85cb5340cb8 149 }
ojan 14:f85cb5340cb8 150
ojan 14:f85cb5340cb8 151 /****************************** main function ******************************/
ojan 0:bc6f14fc60c7 152
ojan 15:d14d385d37e2 153 int main()
ojan 15:d14d385d37e2 154 {
ojan 15:d14d385d37e2 155
ojan 14:f85cb5340cb8 156 i2c.frequency(400000); // I2Cの通信速度を400kHzに設定
ojan 15:d14d385d37e2 157
ojan 0:bc6f14fc60c7 158 if(!mpu.init()) AbortWithMsg("mpu6050 Initialize Error !!"); // mpu6050初期化
ojan 0:bc6f14fc60c7 159 if(!hmc.init()) AbortWithMsg("hmc5883l Initialize Error !!"); // hmc5883l初期化
ojan 15:d14d385d37e2 160
onaka 7:0ec343d29641 161 //Config読み取り
onaka 7:0ec343d29641 162 LoadConfig();
ojan 15:d14d385d37e2 163
onaka 7:0ec343d29641 164 //SDカード初期化
onaka 7:0ec343d29641 165 FILE *fp;
onaka 7:0ec343d29641 166 char filename[15];
ojan 14:f85cb5340cb8 167 int n = Find_last();
ojan 15:d14d385d37e2 168 if(n < 0) {
onaka 7:0ec343d29641 169 pc.printf("Could not read a SD Card.\n");
onaka 7:0ec343d29641 170 return 0;
onaka 7:0ec343d29641 171 }
onaka 7:0ec343d29641 172 sprintf(filename, "/sd/log%03d.csv", n+1);
onaka 7:0ec343d29641 173 fp = fopen(filename, "w");
onaka 7:0ec343d29641 174 fprintf(fp, "log data\r\n");
onaka 7:0ec343d29641 175 xbee.printf("log data\r\n");
ojan 15:d14d385d37e2 176
ojan 14:f85cb5340cb8 177 servoL.period(0.020f); // サーボの信号の周期は20ms
ojan 8:602865d8fca3 178 servoR.period(0.020f);
ojan 15:d14d385d37e2 179
onaka 7:0ec343d29641 180 //カルマンフィルタ初期化
ojan 3:5358a691a100 181 KalmanInit();
ojan 15:d14d385d37e2 182
ojan 14:f85cb5340cb8 183 INT_timer.attach(&INT_func, dt); // 割り込み有効化(Freq = 0.01fなので、10msおきの割り込み)
ojan 15:d14d385d37e2 184
ojan 1:6cd6d2760856 185 //重力ベクトルの初期化
ojan 4:45dc5590abc0 186 raw_g.SetComp(1, 0.0f);
ojan 4:45dc5590abc0 187 raw_g.SetComp(2, 0.0f);
ojan 4:45dc5590abc0 188 raw_g.SetComp(3, 1.0f);
ojan 15:d14d385d37e2 189
ojan 9:6d4578dcc1ed 190 // 機体に固定されたベクトルの初期化
ojan 13:df1e8a650185 191 b_f.SetComp(1, 0.0f);
ojan 9:6d4578dcc1ed 192 b_f.SetComp(2, 0.0f);
ojan 13:df1e8a650185 193 b_f.SetComp(3, -1.0f);
ojan 13:df1e8a650185 194 b_u.SetComp(1, 1.0f);
ojan 9:6d4578dcc1ed 195 b_u.SetComp(2, 0.0f);
ojan 13:df1e8a650185 196 b_u.SetComp(3, 0.0f);
ojan 9:6d4578dcc1ed 197 b_l = Cross(b_u, b_f);
ojan 15:d14d385d37e2 198
ojan 14:f85cb5340cb8 199 // 目標座標をベクトルに代入
ojan 14:f85cb5340cb8 200 target_p.SetComp(1, target_x * DEG_TO_RAD);
ojan 14:f85cb5340cb8 201 target_p.SetComp(2, target_y * DEG_TO_RAD);
ojan 15:d14d385d37e2 202
ojan 14:f85cb5340cb8 203 /* ------------------------------ ↓↓↓ ここからメインループ ↓↓↓ ------------------------------ */
ojan 0:bc6f14fc60c7 204 while(1) {
ojan 4:45dc5590abc0 205 timer.stop();
ojan 4:45dc5590abc0 206 timer.reset();
ojan 4:45dc5590abc0 207 timer.start();
ojan 14:f85cb5340cb8 208 myled = 1; // LED is ON
ojan 15:d14d385d37e2 209
ojan 15:d14d385d37e2 210
ojan 14:f85cb5340cb8 211 /******************************* データ処理 *******************************/
ojan 9:6d4578dcc1ed 212 {
ojan 14:f85cb5340cb8 213 INT_flag = FALSE; // 割り込みによる変数書き換えを阻止
ojan 9:6d4578dcc1ed 214 gms.read(); // GPSデータ取得
ojan 9:6d4578dcc1ed 215 UTC_t = (int)gms.time;
ojan 15:d14d385d37e2 216
ojan 15:d14d385d37e2 217
ojan 15:d14d385d37e2 218
ojan 9:6d4578dcc1ed 219 // 参照座標系の基底を求める
ojan 9:6d4578dcc1ed 220 r_u = g;
ojan 9:6d4578dcc1ed 221 r_f = geomag.GetPerpCompTo(g).Normalize();
ojan 9:6d4578dcc1ed 222 r_l = Cross(r_u, r_f);
ojan 15:d14d385d37e2 223
ojan 9:6d4578dcc1ed 224 // 回転行列を経由してオイラー角を求める
ojan 9:6d4578dcc1ed 225 // オイラー角はヨー・ピッチ・ロールの順に回転したものとする
ojan 9:6d4578dcc1ed 226 // 各オイラー角を求めるのに回転行列の全成分は要らないので、一部だけ計算する。
ojan 15:d14d385d37e2 227
ojan 14:f85cb5340cb8 228 float R_11 = r_f * b_f; // 回転行列の(1,1)成分
ojan 14:f85cb5340cb8 229 float R_12 = r_f * b_l; // 回転行列の(1,2)成分
ojan 14:f85cb5340cb8 230 float R_13 = r_f * b_u; // 回転行列の(1,3)成分
ojan 14:f85cb5340cb8 231 float R_23 = r_l * b_u; // 回転行列の(2,3)成分
ojan 14:f85cb5340cb8 232 float R_33 = r_u * b_u; // 回転行列の(3,3)成分
ojan 15:d14d385d37e2 233
ojan 15:d14d385d37e2 234 #ifdef RULE3
ojan 15:d14d385d37e2 235 yaw = atan2(-R_12, R_11) * RAD_TO_DEG + MAG_DECLINATION - Beta;
ojan 15:d14d385d37e2 236 #else
ojan 15:d14d385d37e2 237 yaw = atan2(-R_12, R_11) * RAD_TO_DEG + MAG_DECLINATION;
ojan 15:d14d385d37e2 238 #endif
ojan 11:083c8c9a5b84 239 roll = atan2(-R_23, R_33) * RAD_TO_DEG;
ojan 11:083c8c9a5b84 240 pitch = asin(R_13) * RAD_TO_DEG;
ojan 15:d14d385d37e2 241
ojan 14:f85cb5340cb8 242 if(yaw < -180.0f) yaw += 360.0f; // ヨー角を[-π, π]に補正
ojan 15:d14d385d37e2 243 if(yaw > 180.0f) yaw -= 360.0f; // ヨー角を[-π, π]に補正
ojan 15:d14d385d37e2 244
ojan 15:d14d385d37e2 245
ojan 15:d14d385d37e2 246
ojan 9:6d4578dcc1ed 247 if(UTC_t - pre_UTC_t >= 1) { // GPSデータが更新されていたら
ojan 9:6d4578dcc1ed 248 p.SetComp(1, gms.longitude * DEG_TO_RAD);
ojan 14:f85cb5340cb8 249 p.SetComp(2, gms.latitude * DEG_TO_RAD);
ojan 15:d14d385d37e2 250
ojan 9:6d4578dcc1ed 251 } else { // 更新されていなかったら
ojan 14:f85cb5340cb8 252 // GPSの補完処理を追加予定
ojan 9:6d4578dcc1ed 253 }
ojan 15:d14d385d37e2 254
ojan 9:6d4578dcc1ed 255 pre_p = p;
ojan 9:6d4578dcc1ed 256 pre_UTC_t = UTC_t;
ojan 15:d14d385d37e2 257
ojan 14:f85cb5340cb8 258 float sv = (float)servoVcc.read_u16() * ADC_LSB_TO_V * 2.0f; // サーボ電源電圧
ojan 14:f85cb5340cb8 259 float lv = (float)logicVcc.read_u16() * ADC_LSB_TO_V * 2.0f; // ロジック電源電圧
ojan 15:d14d385d37e2 260
ojan 13:df1e8a650185 261 // データをmicroSDに保存し、XBeeでPCへ送信する
ojan 18:9dd72e417c60 262 sprintf(data, "%d, %.1f,%.1f,%.1f, %.3f,%.5f,%.5f",
ojan 18:9dd72e417c60 263 UTC_t, yaw, pitch, roll,
ojan 18:9dd72e417c60 264 press, gms.longitude, gms.latitude);
ojan 14:f85cb5340cb8 265 fprintf(fp, data);
ojan 14:f85cb5340cb8 266 fflush(fp);
ojan 14:f85cb5340cb8 267 xbee.printf(data);
ojan 18:9dd72e417c60 268
ojan 18:9dd72e417c60 269 sprintf(data, ", %.3f,%.3f,%.3f, %.3f,%d\r\n",
ojan 18:9dd72e417c60 270 sv, lv, vrt_acc,
ojan 18:9dd72e417c60 271 Distance(target_p, p), optSensor.read_u16());
ojan 18:9dd72e417c60 272
ojan 18:9dd72e417c60 273 fprintf(fp, data);
ojan 18:9dd72e417c60 274 fflush(fp);
ojan 18:9dd72e417c60 275 xbee.printf(data);
ojan 15:d14d385d37e2 276
ojan 13:df1e8a650185 277 INT_flag = TRUE; // 割り込み許可
ojan 9:6d4578dcc1ed 278 }
ojan 15:d14d385d37e2 279
ojan 15:d14d385d37e2 280
ojan 14:f85cb5340cb8 281 /******************************* 制御ルーチン *******************************/
ojan 9:6d4578dcc1ed 282 {
ojan 15:d14d385d37e2 283
ojan 15:d14d385d37e2 284 switch(step) {
ojan 15:d14d385d37e2 285
ojan 15:d14d385d37e2 286 // 投下&安定滑空シーケンス
ojan 15:d14d385d37e2 287 case 0:
ojan 15:d14d385d37e2 288 if(thrown() || count != 0) {
ojan 14:f85cb5340cb8 289 count++;
ojan 14:f85cb5340cb8 290 // 投下直後に紐を引く場合はコメントアウトをはずす
ojan 14:f85cb5340cb8 291 // pull_L = 15;
ojan 14:f85cb5340cb8 292 // pull_R = 15;
ojan 14:f85cb5340cb8 293 if(count >= WaitTime*5) {
ojan 14:f85cb5340cb8 294 pull_L = 0;
ojan 14:f85cb5340cb8 295 pull_R = 0;
ojan 14:f85cb5340cb8 296 step = 1;
ojan 14:f85cb5340cb8 297 }
ojan 14:f85cb5340cb8 298 }
ojan 15:d14d385d37e2 299 break;
ojan 15:d14d385d37e2 300
ojan 15:d14d385d37e2 301 // 制御シーケンス
ojan 15:d14d385d37e2 302 case 1:
ojan 15:d14d385d37e2 303 if(fabs(roll) > BorderSpiral) {
ojan 15:d14d385d37e2 304 // スパイラル回避行動
ojan 15:d14d385d37e2 305 if(roll > 0) {
ojan 15:d14d385d37e2 306 pull_L = PullMax;
ojan 15:d14d385d37e2 307 pull_R = 0;
ojan 14:f85cb5340cb8 308 } else {
ojan 15:d14d385d37e2 309 pull_L = 0;
ojan 15:d14d385d37e2 310 pull_R = PullMax;
ojan 14:f85cb5340cb8 311 }
ojan 14:f85cb5340cb8 312 } else {
ojan 15:d14d385d37e2 313
ojan 15:d14d385d37e2 314 /* いずれも地球を完全球体と仮定 */
ojan 15:d14d385d37e2 315 float target_lng = target_x * DEG_TO_RAD;
ojan 15:d14d385d37e2 316 float target_lat = target_y * DEG_TO_RAD;
ojan 15:d14d385d37e2 317 /* 北から西回りで目標方向の角度を出力 */
ojan 15:d14d385d37e2 318 float targetY = cos( target_lat ) * sin( target_lng - p.GetComp(1) );
ojan 15:d14d385d37e2 319 float targetX = cos( p.GetComp(2) ) * sin( target_lat ) - sin( p.GetComp(2) ) * cos( target_lat ) * cos( target_lng - p.GetComp(1) );
ojan 15:d14d385d37e2 320 float theta = -atan2f( targetY, targetX );
ojan 15:d14d385d37e2 321 float delta_theta = 0.0f;
ojan 15:d14d385d37e2 322
ojan 15:d14d385d37e2 323 if(yaw >= 0.0f) { // ヨー角が正
ojan 15:d14d385d37e2 324 if(theta >= 0.0f) { // 目標角も正ならば、
ojan 15:d14d385d37e2 325 if(theta - yaw > Alpha) dir = 0; // 単純に差を取って閾値αと比べる
ojan 15:d14d385d37e2 326 else if(theta - yaw < -Alpha) dir = 1;
ojan 15:d14d385d37e2 327 } else { // 目標角が負であれば
ojan 15:d14d385d37e2 328 theta += 360.0f; // 360°足して正の値に変換してから
ojan 15:d14d385d37e2 329 delta_theta = theta - yaw; // 差を取る(yawから左回りに見たthetaとの差分)
ojan 15:d14d385d37e2 330 if(delta_theta < 180.0f) { // 差が180°より小さければ左旋回
ojan 15:d14d385d37e2 331 if(delta_theta > Alpha) dir = 0;
ojan 15:d14d385d37e2 332 } else { // 180°より大きければ右旋回
ojan 15:d14d385d37e2 333 if(360.0f - delta_theta > Alpha) dir = 1;
ojan 15:d14d385d37e2 334 }
ojan 14:f85cb5340cb8 335 }
ojan 15:d14d385d37e2 336 } else { // ヨー角が負
ojan 15:d14d385d37e2 337 if(theta <= 0.0f) { // 目標角も負ならば、
ojan 15:d14d385d37e2 338 if(theta - yaw > Alpha) dir = 0; // 単純に差を取って閾値αと比べる
ojan 15:d14d385d37e2 339 else if(theta - yaw < -Alpha) dir = 1;
ojan 15:d14d385d37e2 340 } else { // 目標角が正であれば、
ojan 15:d14d385d37e2 341 delta_theta = theta - yaw; // 差を取る(yawから左回りに見たthetaとの差分)
ojan 15:d14d385d37e2 342 if(delta_theta < 180.0f) { // 差が180°より小さければ左旋回
ojan 15:d14d385d37e2 343 if(delta_theta > Alpha) dir = 0;
ojan 15:d14d385d37e2 344 } else { // 180°より大きければ右旋回
ojan 15:d14d385d37e2 345 if(360.0f - delta_theta > Alpha) dir = 1;
ojan 15:d14d385d37e2 346 }
ojan 15:d14d385d37e2 347 }
ojan 15:d14d385d37e2 348 }
ojan 15:d14d385d37e2 349
ojan 15:d14d385d37e2 350 if(dir == 0) { //目標は左方向
ojan 15:d14d385d37e2 351
ojan 15:d14d385d37e2 352 pull_L = 20;
ojan 15:d14d385d37e2 353 pull_R = 0;
ojan 15:d14d385d37e2 354
ojan 15:d14d385d37e2 355 } else if (dir == 1) { //目標は右方向
ojan 15:d14d385d37e2 356
ojan 15:d14d385d37e2 357 pull_L = 0;
ojan 15:d14d385d37e2 358 pull_R = 20;
ojan 15:d14d385d37e2 359
ojan 14:f85cb5340cb8 360 }
ojan 14:f85cb5340cb8 361 }
ojan 15:d14d385d37e2 362
ojan 15:d14d385d37e2 363 #ifdef RULE2
ojan 15:d14d385d37e2 364 // 目標地点との距離が閾値以下だった場合、落下シーケンスへと移行する
ojan 15:d14d385d37e2 365 if(Distance(target_p, p) < BorderDistance) step = 2;
ojan 15:d14d385d37e2 366 #endif
ojan 15:d14d385d37e2 367
ojan 15:d14d385d37e2 368 break;
ojan 15:d14d385d37e2 369
ojan 15:d14d385d37e2 370 #ifdef RULE2
ojan 15:d14d385d37e2 371 // 落下シーケンス
ojan 15:d14d385d37e2 372 case 2:
ojan 15:d14d385d37e2 373 pull_L = 25;
ojan 15:d14d385d37e2 374 pull_R = 0;
ojan 15:d14d385d37e2 375
ojan 15:d14d385d37e2 376 // もし落下中に目標値から離れてしまった場合は、体勢を立て直して再び滑空
ojan 15:d14d385d37e2 377 // 境界で制御が不安定にならないよう閾値にマージンを取る
ojan 15:d14d385d37e2 378 if(Distance(target_p, p) > BorderDistance+5.0f) step = 1;
ojan 15:d14d385d37e2 379 break;
ojan 15:d14d385d37e2 380 #endif
ojan 14:f85cb5340cb8 381 }
ojan 15:d14d385d37e2 382
ojan 13:df1e8a650185 383 // 指定された引っ張り量だけ紐を引っ張る
ojan 13:df1e8a650185 384 if(pull_L < 0) pull_L = 0;
ojan 13:df1e8a650185 385 else if(pull_L > PullMax) pull_L = PullMax;
ojan 13:df1e8a650185 386 if(pull_R < 0) pull_R = 0;
ojan 13:df1e8a650185 387 else if(pull_R > PullMax) pull_R = PullMax;
ojan 15:d14d385d37e2 388
ojan 9:6d4578dcc1ed 389 servoL.pulsewidth((ServoMax - ServoMin) * pull_L / PullMax + ServoMin);
ojan 9:6d4578dcc1ed 390 servoR.pulsewidth((ServoMax - ServoMin) * pull_R / PullMax + ServoMin);
ojan 15:d14d385d37e2 391
ojan 15:d14d385d37e2 392
ojan 15:d14d385d37e2 393
ojan 9:6d4578dcc1ed 394 }
ojan 15:d14d385d37e2 395
ojan 9:6d4578dcc1ed 396 myled = 0; // LED is OFF
ojan 15:d14d385d37e2 397
ojan 9:6d4578dcc1ed 398 // ループはきっかり0.2秒ごと
ojan 9:6d4578dcc1ed 399 while(timer.read_ms() < 200);
ojan 15:d14d385d37e2 400
ojan 15:d14d385d37e2 401
ojan 0:bc6f14fc60c7 402 }
ojan 15:d14d385d37e2 403
ojan 14:f85cb5340cb8 404 /* ------------------------------ ↑↑↑ ここまでメインループ ↑↑↑ ------------------------------ */
onaka 7:0ec343d29641 405 }
onaka 7:0ec343d29641 406
ojan 15:d14d385d37e2 407 void LoadConfig()
ojan 15:d14d385d37e2 408 {
onaka 7:0ec343d29641 409 char value[20];
onaka 7:0ec343d29641 410 //Read a configuration file from a mbed.
ojan 15:d14d385d37e2 411 if (!cfg.read("/sd/config.txt")) {
onaka 7:0ec343d29641 412 pc.printf("Config file does not exist\n");
ojan 15:d14d385d37e2 413 } else {
onaka 7:0ec343d29641 414 //Get values
onaka 7:0ec343d29641 415 if (cfg.getValue("target_x", &value[0], sizeof(value))) target_x = atof(value);
ojan 15:d14d385d37e2 416 else {
onaka 7:0ec343d29641 417 pc.printf("Failed to get value for target_x\n");
onaka 7:0ec343d29641 418 }
onaka 7:0ec343d29641 419 if (cfg.getValue("target_y", &value[0], sizeof(value))) target_y = atof(value);
ojan 15:d14d385d37e2 420 else {
onaka 7:0ec343d29641 421 pc.printf("Failed to get value for target_y\n");
onaka 7:0ec343d29641 422 }
onaka 7:0ec343d29641 423 }
onaka 7:0ec343d29641 424 }
onaka 7:0ec343d29641 425
ojan 15:d14d385d37e2 426 int Find_last()
ojan 15:d14d385d37e2 427 {
onaka 7:0ec343d29641 428 int i, n = 0;
onaka 7:0ec343d29641 429 char c;
onaka 7:0ec343d29641 430 DIR *dp;
onaka 7:0ec343d29641 431 struct dirent *dirst;
onaka 7:0ec343d29641 432 dp = opendir("/sd/");
ojan 15:d14d385d37e2 433 if (!dp) {
onaka 7:0ec343d29641 434 pc.printf("Could not open directry.\n");
onaka 7:0ec343d29641 435 return -1;
onaka 7:0ec343d29641 436 }
onaka 7:0ec343d29641 437 while((dirst = readdir(dp)) != NULL) {
onaka 7:0ec343d29641 438 if(sscanf(dirst->d_name, "log%03d.csv%c", &i, &c) == 1 && i>n) {
onaka 7:0ec343d29641 439 n = i;
onaka 7:0ec343d29641 440 }
onaka 7:0ec343d29641 441 }
onaka 7:0ec343d29641 442 closedir(dp);
onaka 7:0ec343d29641 443 return n;
ojan 0:bc6f14fc60c7 444 }
ojan 0:bc6f14fc60c7 445
ojan 15:d14d385d37e2 446 void KalmanInit()
ojan 15:d14d385d37e2 447 {
ojan 11:083c8c9a5b84 448 // 重力
ojan 11:083c8c9a5b84 449 {
ojan 11:083c8c9a5b84 450 // 誤差共分散行列の値を決める(対角成分のみ)
ojan 13:df1e8a650185 451 float alpha_R2 = 0.002f;
ojan 12:0d978eb4d639 452 float alpha_Q2 = 0.5f;
ojan 11:083c8c9a5b84 453 R2 *= alpha_R2;
ojan 11:083c8c9a5b84 454 Q2 *= alpha_Q2;
ojan 15:d14d385d37e2 455
ojan 13:df1e8a650185 456 // 観測方程式のヤコビアンの値を設定(時間変化無し)
ojan 13:df1e8a650185 457 float h2[15] = {
ojan 13:df1e8a650185 458 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
ojan 15:d14d385d37e2 459 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
ojan 13:df1e8a650185 460 0.0f, 0.0f, 1.0f, 0.0f, 0.0f
ojan 11:083c8c9a5b84 461 };
ojan 15:d14d385d37e2 462
ojan 13:df1e8a650185 463 H2.SetComps(h2);
ojan 11:083c8c9a5b84 464 }
ojan 15:d14d385d37e2 465
ojan 11:083c8c9a5b84 466 // 地磁気
ojan 11:083c8c9a5b84 467 {
ojan 11:083c8c9a5b84 468 // 誤差共分散行列の値を決める(対角成分のみ)
ojan 13:df1e8a650185 469 float alpha_R1 = 0.002f;
ojan 13:df1e8a650185 470 float alpha_Q1 = 0.5f;
ojan 11:083c8c9a5b84 471 R1 *= alpha_R1;
ojan 11:083c8c9a5b84 472 Q1 *= alpha_Q1;
ojan 15:d14d385d37e2 473
ojan 13:df1e8a650185 474 // 観測方程式のヤコビアンの値を設定(時間変化無し)
ojan 13:df1e8a650185 475 float h1[21] = {
ojan 13:df1e8a650185 476 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
ojan 13:df1e8a650185 477 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
ojan 13:df1e8a650185 478 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f
ojan 11:083c8c9a5b84 479 };
ojan 15:d14d385d37e2 480
ojan 13:df1e8a650185 481 H1.SetComps(h1);
ojan 11:083c8c9a5b84 482 }
ojan 3:5358a691a100 483 }
ojan 3:5358a691a100 484
ojan 15:d14d385d37e2 485 void KalmanUpdate()
ojan 15:d14d385d37e2 486 {
ojan 13:df1e8a650185 487 // 重力
ojan 15:d14d385d37e2 488
ojan 11:083c8c9a5b84 489 {
ojan 13:df1e8a650185 490 // ヤコビアンの更新
ojan 13:df1e8a650185 491 float f2[25] = {
ojan 15:d14d385d37e2 492 1.0f, (raw_gyro.GetComp(3) - post_x1.GetComp(7))*dt, -(raw_gyro.GetComp(2) - post_x2.GetComp(5))*dt, 0.0f, post_x2.GetComp(3)*dt,
ojan 15:d14d385d37e2 493 -(raw_gyro.GetComp(3) - post_x1.GetComp(7))*dt, 1.0f, (raw_gyro.GetComp(1) - post_x2.GetComp(4))*dt, -post_x2.GetComp(3)*dt, 0.0f,
ojan 15:d14d385d37e2 494 (raw_gyro.GetComp(2) - post_x2.GetComp(5))*dt, -(raw_gyro.GetComp(1) - post_x2.GetComp(4))*dt, 1.0f, post_x2.GetComp(2)*dt, -post_x2.GetComp(1)*dt,
ojan 15:d14d385d37e2 495 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
ojan 13:df1e8a650185 496 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
ojan 13:df1e8a650185 497 };
ojan 15:d14d385d37e2 498
ojan 13:df1e8a650185 499 F2.SetComps(f2);
ojan 15:d14d385d37e2 500
ojan 11:083c8c9a5b84 501 // 事前推定値の更新
ojan 13:df1e8a650185 502 //pri_x2 = F2 * post_x2;
ojan 15:d14d385d37e2 503
ojan 13:df1e8a650185 504 float pri_x2_vals[5] = {
ojan 15:d14d385d37e2 505 post_x2.GetComp(1) + post_x2.GetComp(2) * (raw_gyro.GetComp(3) - post_x1.GetComp(7)) * dt - post_x2.GetComp(3) * (raw_gyro.GetComp(2) - post_x2.GetComp(5)) * dt,
ojan 15:d14d385d37e2 506 post_x2.GetComp(2) + post_x2.GetComp(3) * (raw_gyro.GetComp(1) - post_x2.GetComp(4)) * dt - post_x2.GetComp(1) * (raw_gyro.GetComp(3) - post_x1.GetComp(7)) * dt,
ojan 15:d14d385d37e2 507 post_x2.GetComp(3) + post_x2.GetComp(1) * (raw_gyro.GetComp(2) - post_x2.GetComp(5)) * dt - post_x2.GetComp(2) * (raw_gyro.GetComp(1) - post_x2.GetComp(4)) * dt,
ojan 15:d14d385d37e2 508 post_x2.GetComp(4),
ojan 13:df1e8a650185 509 post_x2.GetComp(5)
ojan 13:df1e8a650185 510 };
ojan 15:d14d385d37e2 511
ojan 13:df1e8a650185 512 pri_x2.SetComps(pri_x2_vals);
ojan 15:d14d385d37e2 513
ojan 11:083c8c9a5b84 514 // 事前誤差分散行列の更新
ojan 11:083c8c9a5b84 515 pri_P2 = F2 * post_P2 * F2.Transpose() + R2;
ojan 15:d14d385d37e2 516
ojan 11:083c8c9a5b84 517 // カルマンゲインの計算
ojan 11:083c8c9a5b84 518 S2 = Q2 + H2 * pri_P2 * H2.Transpose();
ojan 13:df1e8a650185 519 static float det;
ojan 11:083c8c9a5b84 520 if((det = S2.Inverse(S_inv2)) >= 0.0f) {
ojan 11:083c8c9a5b84 521 pc.printf("E:%.3f\r\n", det);
ojan 11:083c8c9a5b84 522 return; // 万が一、逆行列が見つからなかった場合は前回の推定値を保持して終了
ojan 11:083c8c9a5b84 523 }
ojan 15:d14d385d37e2 524 K2 = pri_P2 * H2.Transpose() * S_inv2;
ojan 15:d14d385d37e2 525
ojan 11:083c8c9a5b84 526 // 事後推定値の更新
ojan 13:df1e8a650185 527 post_x2 = pri_x2 + K2 * (raw_acc - H2 * pri_x2);
ojan 11:083c8c9a5b84 528 // 事後誤差分散行列の更新
ojan 11:083c8c9a5b84 529 post_P2 = (I2 - K2 * H2) * pri_P2;
ojan 11:083c8c9a5b84 530 }
ojan 15:d14d385d37e2 531
ojan 15:d14d385d37e2 532
ojan 11:083c8c9a5b84 533 // 地磁気
ojan 11:083c8c9a5b84 534 {
ojan 11:083c8c9a5b84 535 // ヤコビアンの更新
ojan 13:df1e8a650185 536 float f1[49] = {
ojan 15:d14d385d37e2 537 1.0f, (raw_gyro.GetComp(3) - post_x1.GetComp(7))*dt, -(raw_gyro.GetComp(2) - post_x2.GetComp(5))*dt, 0.0f, 0.0f, 0.0f, -post_x1.GetComp(2) * dt,
ojan 15:d14d385d37e2 538 -(raw_gyro.GetComp(3) - post_x1.GetComp(7))*dt, 1.0f, (raw_gyro.GetComp(1) - post_x2.GetComp(4))*dt, 0.0f, 0.0f, 0.0f, post_x1.GetComp(1) * dt,
ojan 15:d14d385d37e2 539 (raw_gyro.GetComp(2) - post_x2.GetComp(5))*dt, -(raw_gyro.GetComp(1) - post_x2.GetComp(4))*dt, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
ojan 15:d14d385d37e2 540 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
ojan 15:d14d385d37e2 541 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
ojan 15:d14d385d37e2 542 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
ojan 13:df1e8a650185 543 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
ojan 11:083c8c9a5b84 544 };
ojan 15:d14d385d37e2 545
ojan 13:df1e8a650185 546 F1.SetComps(f1);
ojan 15:d14d385d37e2 547
ojan 11:083c8c9a5b84 548 // 事前推定値の更新
ojan 13:df1e8a650185 549 //pri_x1 = F1 * post_x1;
ojan 13:df1e8a650185 550 float pri_x1_vals[7] = {
ojan 15:d14d385d37e2 551 post_x1.GetComp(1) + post_x1.GetComp(2) * (raw_gyro.GetComp(3) - post_x1.GetComp(7)) * dt - post_x1.GetComp(3) * (raw_gyro.GetComp(2) - post_x2.GetComp(5)) * dt,
ojan 15:d14d385d37e2 552 post_x1.GetComp(2) + post_x1.GetComp(3) * (raw_gyro.GetComp(1) - post_x2.GetComp(4)) * dt - post_x1.GetComp(1) * (raw_gyro.GetComp(3) - post_x1.GetComp(7)) * dt,
ojan 15:d14d385d37e2 553 post_x1.GetComp(3) + post_x1.GetComp(1) * (raw_gyro.GetComp(2) - post_x2.GetComp(5)) * dt - post_x1.GetComp(2) * (raw_gyro.GetComp(1) - post_x2.GetComp(4)) * dt,
ojan 15:d14d385d37e2 554 post_x1.GetComp(4),
ojan 15:d14d385d37e2 555 post_x1.GetComp(5),
ojan 15:d14d385d37e2 556 post_x1.GetComp(6),
ojan 13:df1e8a650185 557 post_x1.GetComp(7)
ojan 13:df1e8a650185 558 };
ojan 15:d14d385d37e2 559
ojan 13:df1e8a650185 560 pri_x1.SetComps(pri_x1_vals);
ojan 15:d14d385d37e2 561
ojan 11:083c8c9a5b84 562 // 事前誤差分散行列の更新
ojan 11:083c8c9a5b84 563 pri_P1 = F1 * post_P1 * F1.Transpose() + R1;
ojan 15:d14d385d37e2 564
ojan 11:083c8c9a5b84 565 // カルマンゲインの計算
ojan 11:083c8c9a5b84 566 S1 = Q1 + H1 * pri_P1 * H1.Transpose();
ojan 13:df1e8a650185 567 static float det;
ojan 11:083c8c9a5b84 568 if((det = S1.Inverse(S_inv1)) >= 0.0f) {
ojan 11:083c8c9a5b84 569 pc.printf("E:%.3f\r\n", det);
ojan 11:083c8c9a5b84 570 return; // 万が一、逆行列が見つからなかった場合は前回の推定値を保持して終了
ojan 11:083c8c9a5b84 571 }
ojan 15:d14d385d37e2 572 K1 = pri_P1 * H1.Transpose() * S_inv1;
ojan 15:d14d385d37e2 573
ojan 11:083c8c9a5b84 574 // 事後推定値の更新
ojan 11:083c8c9a5b84 575 post_x1 = pri_x1 + K1 * (raw_geomag - H1 * pri_x1);
ojan 13:df1e8a650185 576 // 地磁気ベクトルの大きさに拘束条件を与える
ojan 13:df1e8a650185 577 /*{
ojan 13:df1e8a650185 578 Vector gm(3);
ojan 13:df1e8a650185 579 gm.SetComp(1, post_x1.GetComp(1));
ojan 13:df1e8a650185 580 gm.SetComp(2, post_x1.GetComp(2));
ojan 13:df1e8a650185 581 gm.SetComp(3, post_x1.GetComp(3));
ojan 15:d14d385d37e2 582
ojan 13:df1e8a650185 583 gm = MAG_MAGNITUDE * gm.Normalize();
ojan 15:d14d385d37e2 584
ojan 13:df1e8a650185 585 post_x1.SetComp(1, gm.GetComp(1));
ojan 13:df1e8a650185 586 post_x1.SetComp(2, gm.GetComp(2));
ojan 13:df1e8a650185 587 post_x1.SetComp(3, gm.GetComp(3));
ojan 13:df1e8a650185 588 }*/
ojan 11:083c8c9a5b84 589 // 事後誤差分散行列の更新
ojan 11:083c8c9a5b84 590 post_P1 = (I1 - K1 * H1) * pri_P1;
ojan 3:5358a691a100 591 }
ojan 3:5358a691a100 592 }
ojan 3:5358a691a100 593
ojan 15:d14d385d37e2 594 float Distance(Vector p1, Vector p2)
ojan 15:d14d385d37e2 595 {
ojan 9:6d4578dcc1ed 596 if(p1.GetDim() != p2.GetDim()) return 0.0f;
ojan 15:d14d385d37e2 597
ojan 13:df1e8a650185 598 static float mu_y = (p1.GetComp(2) + p2.GetComp(2)) * 0.5f;
ojan 13:df1e8a650185 599 static float s_mu_y = sin(mu_y);
ojan 13:df1e8a650185 600 static float w = sqrt(1 - GPS_SQ_E * s_mu_y * s_mu_y);
ojan 13:df1e8a650185 601 static float m = GPS_A * (1 - GPS_SQ_E) / (w * w * w);
ojan 13:df1e8a650185 602 static float n = GPS_A / w;
ojan 13:df1e8a650185 603 static float d1 = m * (p1.GetComp(2) - p2.GetComp(2));
ojan 13:df1e8a650185 604 static float d2 = n * cos(mu_y) * (p1.GetComp(1) - p2.GetComp(1));
ojan 15:d14d385d37e2 605
ojan 9:6d4578dcc1ed 606 return sqrt(d1 * d1 + d2 * d2);
ojan 9:6d4578dcc1ed 607 }
ojan 9:6d4578dcc1ed 608
ojan 14:f85cb5340cb8 609 /** 投下を検知する関数
ojan 15:d14d385d37e2 610 *
ojan 14:f85cb5340cb8 611 * @return 投下されたかどうか(true=投下 false=未投下
ojan 15:d14d385d37e2 612 *
ojan 14:f85cb5340cb8 613 */
ojan 15:d14d385d37e2 614 bool thrown()
ojan 15:d14d385d37e2 615 {
ojan 14:f85cb5340cb8 616 static int opt_count = 0;
ojan 14:f85cb5340cb8 617 static int g_count = 0;
ojan 14:f85cb5340cb8 618 static int para_count = 0;
ojan 15:d14d385d37e2 619
ojan 14:f85cb5340cb8 620 if(optSensor.read_u16() > BorderOpt) opt_count++;
ojan 14:f85cb5340cb8 621 else opt_count = 0;
ojan 14:f85cb5340cb8 622 if(vrt_acc < BorderGravity) g_count++;
ojan 14:f85cb5340cb8 623 else g_count = 0;
ojan 14:f85cb5340cb8 624 if((int)paraSensor == BorderParafoil) para_count++;
ojan 14:f85cb5340cb8 625 else para_count = 0;
ojan 15:d14d385d37e2 626
ojan 14:f85cb5340cb8 627 if(opt_count >= MaxCount || g_count >= MaxCount || para_count >= MaxCount) {
ojan 14:f85cb5340cb8 628 return true;
ojan 14:f85cb5340cb8 629 }
ojan 15:d14d385d37e2 630
ojan 14:f85cb5340cb8 631 return false;
ojan 15:d14d385d37e2 632
ojan 14:f85cb5340cb8 633 }
ojan 14:f85cb5340cb8 634
ojan 14:f85cb5340cb8 635 /* ------------------------------ 割り込み関数 ------------------------------ */
ojan 9:6d4578dcc1ed 636
ojan 15:d14d385d37e2 637 void INT_func()
ojan 15:d14d385d37e2 638 {
ojan 4:45dc5590abc0 639 // センサーの値を更新
ojan 4:45dc5590abc0 640 mpu.read();
ojan 4:45dc5590abc0 641 hmc.read();
ojan 15:d14d385d37e2 642
ojan 4:45dc5590abc0 643 for(int i=0; i<3; i++) {
ojan 4:45dc5590abc0 644 raw_acc.SetComp(i+1, (float)mpu.data.value.acc[i] * ACC_LSB_TO_G);
ojan 4:45dc5590abc0 645 raw_gyro.SetComp(i+1, (float)mpu.data.value.gyro[i] * GYRO_LSB_TO_DEG * DEG_TO_RAD);
ojan 4:45dc5590abc0 646 raw_geomag.SetComp(i+1, (float)hmc.data.value[i] * MAG_LSB_TO_GAUSS);
ojan 4:45dc5590abc0 647 }
ojan 15:d14d385d37e2 648
ojan 14:f85cb5340cb8 649 Vector delta_g = Cross(raw_gyro, raw_g); // Δg = ω × g
ojan 4:45dc5590abc0 650 raw_g = 0.9f * (raw_g - delta_g * dt) + 0.1f * raw_acc.Normalize(); // 相補フィルタ
ojan 4:45dc5590abc0 651 raw_g = raw_g.Normalize();
ojan 15:d14d385d37e2 652
ojan 4:45dc5590abc0 653 KalmanUpdate();
ojan 15:d14d385d37e2 654
ojan 4:45dc5590abc0 655 // LPS25Hによる気圧の取得は10Hz
ojan 4:45dc5590abc0 656 lps_cnt = (lps_cnt+1)%10;
ojan 4:45dc5590abc0 657 if(lps_cnt == 0) {
ojan 4:45dc5590abc0 658 raw_press = (float)lps.pressure() * PRES_LSB_TO_HPA;
ojan 4:45dc5590abc0 659 }
ojan 15:d14d385d37e2 660
ojan 4:45dc5590abc0 661 if(INT_flag != FALSE) {
ojan 4:45dc5590abc0 662 g = raw_g;
ojan 4:45dc5590abc0 663 for(int i=0; i<3; i++) {
ojan 11:083c8c9a5b84 664 geomag.SetComp(i+1, post_x1.GetComp(i+1));
ojan 4:45dc5590abc0 665 }
ojan 4:45dc5590abc0 666 acc = raw_acc;
ojan 4:45dc5590abc0 667 gyro = raw_gyro;
ojan 4:45dc5590abc0 668 press = raw_press;
ojan 15:d14d385d37e2 669
ojan 13:df1e8a650185 670 vrt_acc = raw_acc * raw_g;
ojan 15:d14d385d37e2 671
ojan 0:bc6f14fc60c7 672 }
ojan 3:5358a691a100 673 }
ojan 3:5358a691a100 674
ojan 14:f85cb5340cb8 675 /* ------------------------------ デバッグ用関数 ------------------------------ */
ojan 9:6d4578dcc1ed 676
ojan 15:d14d385d37e2 677 void toString(Matrix& m)
ojan 15:d14d385d37e2 678 {
ojan 15:d14d385d37e2 679
ojan 3:5358a691a100 680 for(int i=0; i<m.GetRow(); i++) {
ojan 3:5358a691a100 681 for(int j=0; j<m.GetCol(); j++) {
ojan 3:5358a691a100 682 pc.printf("%.6f\t", m.GetComp(i+1, j+1));
ojan 3:5358a691a100 683 }
ojan 3:5358a691a100 684 pc.printf("\r\n");
ojan 3:5358a691a100 685 }
ojan 15:d14d385d37e2 686
ojan 3:5358a691a100 687 }
ojan 3:5358a691a100 688
ojan 15:d14d385d37e2 689 void toString(Vector& v)
ojan 15:d14d385d37e2 690 {
ojan 15:d14d385d37e2 691
ojan 3:5358a691a100 692 for(int i=0; i<v.GetDim(); i++) {
ojan 3:5358a691a100 693 pc.printf("%.6f\t", v.GetComp(i+1));
ojan 3:5358a691a100 694 }
ojan 3:5358a691a100 695 pc.printf("\r\n");
ojan 15:d14d385d37e2 696
ojan 0:bc6f14fc60c7 697 }