ジャイロ補正を追加したプログラム

Dependencies:   mbed YKNCT_Movement SBDBT BNO055 YKNCT_MD YKNCT_I2C

Committer:
TakushimaYukimasa
Date:
Thu Mar 12 02:29:33 2020 +0000
Revision:
0:9e851dc42cde
Child:
1:b9f698be841c
v0.0; ; add TemplateProgram;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TakushimaYukimasa 0:9e851dc42cde 1 /**
TakushimaYukimasa 0:9e851dc42cde 2 ********************************************************************************
TakushimaYukimasa 0:9e851dc42cde 3 * @file main.c
TakushimaYukimasa 0:9e851dc42cde 4 * @author You
TakushimaYukimasa 0:9e851dc42cde 5 * @version V?.?.?
TakushimaYukimasa 0:9e851dc42cde 6 * @date Today
TakushimaYukimasa 0:9e851dc42cde 7 * @brief メインファイル
TakushimaYukimasa 0:9e851dc42cde 8 ******************************************************************************
TakushimaYukimasa 0:9e851dc42cde 9 */
TakushimaYukimasa 0:9e851dc42cde 10
TakushimaYukimasa 0:9e851dc42cde 11 /* Includes ------------------------------------------------------------------*/
TakushimaYukimasa 0:9e851dc42cde 12 #include <main.h>
TakushimaYukimasa 0:9e851dc42cde 13
TakushimaYukimasa 0:9e851dc42cde 14 /* 型定義 --------------------------------------------------------------------*/
TakushimaYukimasa 0:9e851dc42cde 15
TakushimaYukimasa 0:9e851dc42cde 16 /* ロボットの加速度 */
TakushimaYukimasa 0:9e851dc42cde 17 ROCATION NowAcc;
TakushimaYukimasa 0:9e851dc42cde 18 /* ロボットの座標 */
TakushimaYukimasa 0:9e851dc42cde 19 ROCATION NowLoc;
TakushimaYukimasa 0:9e851dc42cde 20
TakushimaYukimasa 0:9e851dc42cde 21 /* 定数定義 ------------------------------------------------------------------*/
TakushimaYukimasa 0:9e851dc42cde 22 /* マクロ定義 ----------------------------------------------------------------*/
TakushimaYukimasa 0:9e851dc42cde 23 /* 関数プロトタイプ宣言 -------------------------------------------------------*/
TakushimaYukimasa 0:9e851dc42cde 24
TakushimaYukimasa 0:9e851dc42cde 25 /* タイマ呼び出し用 */
TakushimaYukimasa 0:9e851dc42cde 26 void IT_CallBack(void);
TakushimaYukimasa 0:9e851dc42cde 27
TakushimaYukimasa 0:9e851dc42cde 28 /* 自己位置推定処理 */
TakushimaYukimasa 0:9e851dc42cde 29 void LocEstimate(void);
TakushimaYukimasa 0:9e851dc42cde 30
TakushimaYukimasa 0:9e851dc42cde 31
TakushimaYukimasa 0:9e851dc42cde 32 /* 変数定義 ------------------------------------------------------------------*/
TakushimaYukimasa 0:9e851dc42cde 33
TakushimaYukimasa 0:9e851dc42cde 34 /* 操作権 0…なし 1…手動 2…自動 */
TakushimaYukimasa 0:9e851dc42cde 35 int operate=0;
TakushimaYukimasa 0:9e851dc42cde 36
TakushimaYukimasa 0:9e851dc42cde 37 /* 自動シーケンス */
TakushimaYukimasa 0:9e851dc42cde 38 int auto_mode=0;
TakushimaYukimasa 0:9e851dc42cde 39
TakushimaYukimasa 0:9e851dc42cde 40 /* 直読みエンコーダ角度保存(degree) */
TakushimaYukimasa 0:9e851dc42cde 41 double EncoderDeg[EncoderMAX] = {0};
TakushimaYukimasa 0:9e851dc42cde 42
TakushimaYukimasa 0:9e851dc42cde 43 /* 足回り値保存変数 */
TakushimaYukimasa 0:9e851dc42cde 44 int MovMotor[4]={0};
TakushimaYukimasa 0:9e851dc42cde 45
TakushimaYukimasa 0:9e851dc42cde 46 /* 自動yaw補整目標角度 */
TakushimaYukimasa 0:9e851dc42cde 47 double TarTheta=0;
TakushimaYukimasa 0:9e851dc42cde 48
TakushimaYukimasa 0:9e851dc42cde 49 /* クラス定義 ----------------------------------------------------------------*/
TakushimaYukimasa 0:9e851dc42cde 50
TakushimaYukimasa 0:9e851dc42cde 51 /* 割り込み用クラス */
TakushimaYukimasa 0:9e851dc42cde 52 Ticker flipper;
TakushimaYukimasa 0:9e851dc42cde 53
TakushimaYukimasa 0:9e851dc42cde 54 /* UART (Tx,Rx) */
TakushimaYukimasa 0:9e851dc42cde 55 Serial telemetry(USBTX, USBRX, 115200);
TakushimaYukimasa 0:9e851dc42cde 56
TakushimaYukimasa 0:9e851dc42cde 57 /* コントローラー */
TakushimaYukimasa 0:9e851dc42cde 58 SBDBT DS3(PA_0, PA_1, 9600);
TakushimaYukimasa 0:9e851dc42cde 59
TakushimaYukimasa 0:9e851dc42cde 60 /* オンボードLED */
TakushimaYukimasa 0:9e851dc42cde 61 DigitalOut led(LED2);
TakushimaYukimasa 0:9e851dc42cde 62
TakushimaYukimasa 0:9e851dc42cde 63 /* USERボタン */
TakushimaYukimasa 0:9e851dc42cde 64 DigitalIn UB(PC_13,PullDown);
TakushimaYukimasa 0:9e851dc42cde 65
TakushimaYukimasa 0:9e851dc42cde 66 /* エンコーダーピン CS */
TakushimaYukimasa 0:9e851dc42cde 67 DigitalOut CS[] = {PA_2,PA_3};
TakushimaYukimasa 0:9e851dc42cde 68 DigitalOut CL[] = {PA_4,PA_5};
TakushimaYukimasa 0:9e851dc42cde 69 DigitalIn DO[] = {PA_6,PA_7};
TakushimaYukimasa 0:9e851dc42cde 70
TakushimaYukimasa 0:9e851dc42cde 71 /* 足回り動作クラス定義 */
TakushimaYukimasa 0:9e851dc42cde 72 Move omuni(MovMotor,NowLoc.theta);
TakushimaYukimasa 0:9e851dc42cde 73
TakushimaYukimasa 0:9e851dc42cde 74 /* I2C MDのクラス定義 */
TakushimaYukimasa 0:9e851dc42cde 75 YKNCT_MD_I2C MD(PB_9,PB_8);
TakushimaYukimasa 0:9e851dc42cde 76
TakushimaYukimasa 0:9e851dc42cde 77 /* ジャイロのピン設定 */
TakushimaYukimasa 0:9e851dc42cde 78 BNO055 bno(PB_9, PB_8);
TakushimaYukimasa 0:9e851dc42cde 79
TakushimaYukimasa 0:9e851dc42cde 80
TakushimaYukimasa 0:9e851dc42cde 81 /*----------------------------------- main ----------------------------------*/
TakushimaYukimasa 0:9e851dc42cde 82 int main()
TakushimaYukimasa 0:9e851dc42cde 83 {
TakushimaYukimasa 0:9e851dc42cde 84 telemetry.printf("\n\rMainStart");
TakushimaYukimasa 0:9e851dc42cde 85
TakushimaYukimasa 0:9e851dc42cde 86 /* 割り込みの設定
TakushimaYukimasa 0:9e851dc42cde 87 * IT_CallBack関数を0.1msで割り込み */
TakushimaYukimasa 0:9e851dc42cde 88 flipper.attach_us(&IT_CallBack, 100);
TakushimaYukimasa 0:9e851dc42cde 89
TakushimaYukimasa 0:9e851dc42cde 90 /* ジャイロの設定 */
TakushimaYukimasa 0:9e851dc42cde 91 bno.setmode(OPERATION_MODE_IMUPLUS);
TakushimaYukimasa 0:9e851dc42cde 92
TakushimaYukimasa 0:9e851dc42cde 93 /* I2CMDの設定 */
TakushimaYukimasa 0:9e851dc42cde 94 MD.Init(0,MD_SMB);
TakushimaYukimasa 0:9e851dc42cde 95 MD.Init(1,MD_SMB);
TakushimaYukimasa 0:9e851dc42cde 96 MD.Init(2,MD_SMB);
TakushimaYukimasa 0:9e851dc42cde 97 MD.Init(3,MD_SMB);
TakushimaYukimasa 0:9e851dc42cde 98
TakushimaYukimasa 0:9e851dc42cde 99 telemetry.printf("\n\rMainLoopStart");
TakushimaYukimasa 0:9e851dc42cde 100 /* メインループ --------------------------------------------------------------*/
TakushimaYukimasa 0:9e851dc42cde 101 while(1) {
TakushimaYukimasa 0:9e851dc42cde 102 /* オンボードLED点滅 */
TakushimaYukimasa 0:9e851dc42cde 103 led=!led;
TakushimaYukimasa 0:9e851dc42cde 104
TakushimaYukimasa 0:9e851dc42cde 105 /* 表示改行 */
TakushimaYukimasa 0:9e851dc42cde 106 telemetry.printf("\n\r");
TakushimaYukimasa 0:9e851dc42cde 107
TakushimaYukimasa 0:9e851dc42cde 108 /* 自動処理関連テレメトリ */
TakushimaYukimasa 0:9e851dc42cde 109 telemetry.printf("ope:%d ",operate);
TakushimaYukimasa 0:9e851dc42cde 110 /* 座標テレメトリ */
TakushimaYukimasa 0:9e851dc42cde 111 telemetry.printf("X:%4.0f Y:%4.0f T:%4.0f ",NowLoc.X,NowLoc.Y,NowLoc.theta);
TakushimaYukimasa 0:9e851dc42cde 112
TakushimaYukimasa 0:9e851dc42cde 113 /* 自己位置推定更新 */
TakushimaYukimasa 0:9e851dc42cde 114 LocEstimate();
TakushimaYukimasa 0:9e851dc42cde 115
TakushimaYukimasa 0:9e851dc42cde 116 /* I2CMD実行 */
TakushimaYukimasa 0:9e851dc42cde 117 MD.Exe();
TakushimaYukimasa 0:9e851dc42cde 118
TakushimaYukimasa 0:9e851dc42cde 119
TakushimaYukimasa 0:9e851dc42cde 120 /* 操縦権変更 ×停止 △手動 〇自動 */
TakushimaYukimasa 0:9e851dc42cde 121 if(DS3.CROSS) operate=0;
TakushimaYukimasa 0:9e851dc42cde 122 if(DS3.TRIANGLE) operate=1;
TakushimaYukimasa 0:9e851dc42cde 123 if(DS3.CIRCLE) operate=2;
TakushimaYukimasa 0:9e851dc42cde 124
TakushimaYukimasa 0:9e851dc42cde 125 /* 操縦権:なし 停止動作 */
TakushimaYukimasa 0:9e851dc42cde 126 if(operate==0){
TakushimaYukimasa 0:9e851dc42cde 127 /* 足回り停止 */
TakushimaYukimasa 0:9e851dc42cde 128 omuni.XmarkOmni_Move(0,0,0);
TakushimaYukimasa 0:9e851dc42cde 129
TakushimaYukimasa 0:9e851dc42cde 130
TakushimaYukimasa 0:9e851dc42cde 131 }
TakushimaYukimasa 0:9e851dc42cde 132 /* 操縦権:手動 */
TakushimaYukimasa 0:9e851dc42cde 133 else if(operate==1){
TakushimaYukimasa 0:9e851dc42cde 134 /* 足回り手動動作 */
TakushimaYukimasa 0:9e851dc42cde 135 int x_val = (double)(DS3.LX-64)*100/64;
TakushimaYukimasa 0:9e851dc42cde 136 int y_val = (double)(64-DS3.LY)*100/64;
TakushimaYukimasa 0:9e851dc42cde 137 int r_val = (double)(DS3.RX-64)*100/64;
TakushimaYukimasa 0:9e851dc42cde 138 /**
TakushimaYukimasa 0:9e851dc42cde 139 * このr_valとNowLoc.thetaとTarThetaをコネコネしてジャイロ補整するのじゃ
TakushimaYukimasa 0:9e851dc42cde 140 * 動けばいいのではなく,見やすく書くのじゃよ
TakushimaYukimasa 0:9e851dc42cde 141 **/
TakushimaYukimasa 0:9e851dc42cde 142 omuni.XmarkOmni_Move(x_val,y_val,r_val);
TakushimaYukimasa 0:9e851dc42cde 143
TakushimaYukimasa 0:9e851dc42cde 144
TakushimaYukimasa 0:9e851dc42cde 145 }
TakushimaYukimasa 0:9e851dc42cde 146 /* 操縦権:自動 */
TakushimaYukimasa 0:9e851dc42cde 147 else if(operate==2){
TakushimaYukimasa 0:9e851dc42cde 148 switch(auto_mode){
TakushimaYukimasa 0:9e851dc42cde 149 /* スタート待機処理 */
TakushimaYukimasa 0:9e851dc42cde 150 case 0:
TakushimaYukimasa 0:9e851dc42cde 151 /* オンボードSWで次のステップに */
TakushimaYukimasa 0:9e851dc42cde 152 if(UB) auto_mode++;
TakushimaYukimasa 0:9e851dc42cde 153 break;
TakushimaYukimasa 0:9e851dc42cde 154
TakushimaYukimasa 0:9e851dc42cde 155 /* 〇〇の処理 */
TakushimaYukimasa 0:9e851dc42cde 156 case 1:
TakushimaYukimasa 0:9e851dc42cde 157 /* 〇〇の時次のステップに */
TakushimaYukimasa 0:9e851dc42cde 158 if(1) auto_mode++;
TakushimaYukimasa 0:9e851dc42cde 159 break;
TakushimaYukimasa 0:9e851dc42cde 160
TakushimaYukimasa 0:9e851dc42cde 161 /* 終了処理 */
TakushimaYukimasa 0:9e851dc42cde 162 default:
TakushimaYukimasa 0:9e851dc42cde 163 auto_mode=0;
TakushimaYukimasa 0:9e851dc42cde 164 operate=0;
TakushimaYukimasa 0:9e851dc42cde 165 break;
TakushimaYukimasa 0:9e851dc42cde 166 }
TakushimaYukimasa 0:9e851dc42cde 167 }
TakushimaYukimasa 0:9e851dc42cde 168
TakushimaYukimasa 0:9e851dc42cde 169 }
TakushimaYukimasa 0:9e851dc42cde 170 }
TakushimaYukimasa 0:9e851dc42cde 171
TakushimaYukimasa 0:9e851dc42cde 172
TakushimaYukimasa 0:9e851dc42cde 173 /*******************************************************************************
TakushimaYukimasa 0:9e851dc42cde 174 * @概要 自己位置推定関数
TakushimaYukimasa 0:9e851dc42cde 175 * @引数 なし
TakushimaYukimasa 0:9e851dc42cde 176 * @返り値 なし
TakushimaYukimasa 0:9e851dc42cde 177 *******************************************************************************/
TakushimaYukimasa 0:9e851dc42cde 178 void LocEstimate(void){
TakushimaYukimasa 0:9e851dc42cde 179 static double GyroDeg[2]={0};
TakushimaYukimasa 0:9e851dc42cde 180 static double EncDeg[2][2]={0};
TakushimaYukimasa 0:9e851dc42cde 181 static double disp[3]={0};
TakushimaYukimasa 0:9e851dc42cde 182
TakushimaYukimasa 0:9e851dc42cde 183 /* ジャイロの値取得 */
TakushimaYukimasa 0:9e851dc42cde 184 bno.get_angles();
TakushimaYukimasa 0:9e851dc42cde 185 GyroDeg[1]=GyroDeg[0];
TakushimaYukimasa 0:9e851dc42cde 186 GyroDeg[0]=bno.euler.yaw;
TakushimaYukimasa 0:9e851dc42cde 187 if(GyroDeg[0]!=0) {
TakushimaYukimasa 0:9e851dc42cde 188 /* 359→0を跨いだ時,前回の値を0から逆回転で負の値で表記 */
TakushimaYukimasa 0:9e851dc42cde 189 if(GyroDeg[1]<90 && GyroDeg[0]>270) GyroDeg[1]+=360;
TakushimaYukimasa 0:9e851dc42cde 190 /* 0→359を跨いだ時,前回の値を360以上の値で表記 */
TakushimaYukimasa 0:9e851dc42cde 191 else if(GyroDeg[1]>270 && GyroDeg[0]<90) GyroDeg[1]-=360;
TakushimaYukimasa 0:9e851dc42cde 192 /* 差を求める*/
TakushimaYukimasa 0:9e851dc42cde 193 disp[2]=GyroDeg[1]-GyroDeg[0];
TakushimaYukimasa 0:9e851dc42cde 194 }
TakushimaYukimasa 0:9e851dc42cde 195 /* Enc2つの差分求める */
TakushimaYukimasa 0:9e851dc42cde 196 for(int i=0;i<2;i++){
TakushimaYukimasa 0:9e851dc42cde 197 EncDeg[i][1]=EncDeg[i][0];
TakushimaYukimasa 0:9e851dc42cde 198 EncDeg[i][0]=EncoderDeg[i];
TakushimaYukimasa 0:9e851dc42cde 199 disp[i]=EncDeg[i][1]-EncDeg[i][0];
TakushimaYukimasa 0:9e851dc42cde 200 }
TakushimaYukimasa 0:9e851dc42cde 201 /* 差分を加速度として保存 */
TakushimaYukimasa 0:9e851dc42cde 202 NowAcc.theta = disp[2];
TakushimaYukimasa 0:9e851dc42cde 203 NowAcc.X = disp[0] * cos(NowLoc.theta) + disp[1] * sin(NowLoc.theta)+200*disp[2];
TakushimaYukimasa 0:9e851dc42cde 204 NowAcc.Y = -disp[0] * sin(NowLoc.theta) + disp[1] * cos(NowLoc.theta)+200*disp[2];
TakushimaYukimasa 0:9e851dc42cde 205 /* 差分を累積して現在位置を保存 */
TakushimaYukimasa 0:9e851dc42cde 206 NowLoc.X += NowAcc.X;
TakushimaYukimasa 0:9e851dc42cde 207 NowLoc.Y += NowAcc.Y;
TakushimaYukimasa 0:9e851dc42cde 208 NowLoc.theta += NowAcc.theta;
TakushimaYukimasa 0:9e851dc42cde 209 }
TakushimaYukimasa 0:9e851dc42cde 210
TakushimaYukimasa 0:9e851dc42cde 211
TakushimaYukimasa 0:9e851dc42cde 212
TakushimaYukimasa 0:9e851dc42cde 213
TakushimaYukimasa 0:9e851dc42cde 214 /* 割り込み(100us) *************************************************************/
TakushimaYukimasa 0:9e851dc42cde 215 void IT_CallBack(void)
TakushimaYukimasa 0:9e851dc42cde 216 {
TakushimaYukimasa 0:9e851dc42cde 217 static int cnt = 0;
TakushimaYukimasa 0:9e851dc42cde 218 static int data[EncoderMAX] = {0};
TakushimaYukimasa 0:9e851dc42cde 219 static double EncDeg[EncoderMAX][2] = {0};
TakushimaYukimasa 0:9e851dc42cde 220
TakushimaYukimasa 0:9e851dc42cde 221 for(int i=0;i<EncoderMAX;i++)
TakushimaYukimasa 0:9e851dc42cde 222 switch(cnt){
TakushimaYukimasa 0:9e851dc42cde 223 /* 最初の処理 */
TakushimaYukimasa 0:9e851dc42cde 224 case 0:
TakushimaYukimasa 0:9e851dc42cde 225 data[i] = 0; CS[i] = 0; CL[i] = 1;
TakushimaYukimasa 0:9e851dc42cde 226 break;
TakushimaYukimasa 0:9e851dc42cde 227 /* 最後の処理 */
TakushimaYukimasa 0:9e851dc42cde 228 case 25:
TakushimaYukimasa 0:9e851dc42cde 229 CS[i]=1;
TakushimaYukimasa 0:9e851dc42cde 230 /* 前回の値更新 今回の値更新(エンコーダの値(0~4096)を角度(0~360)に) */
TakushimaYukimasa 0:9e851dc42cde 231 EncDeg[i][1] = EncDeg[i][0];
TakushimaYukimasa 0:9e851dc42cde 232 EncDeg[i][0] = (double)data[i] * 360.0 / 4096;
TakushimaYukimasa 0:9e851dc42cde 233 /* 359→0を跨いだ時,前回の値を0から逆回転で負の値で表記 */
TakushimaYukimasa 0:9e851dc42cde 234 if ((270 <= EncDeg[i][1]) && (EncDeg[i][0] < 90))
TakushimaYukimasa 0:9e851dc42cde 235 EncDeg[i][1] -= 360;
TakushimaYukimasa 0:9e851dc42cde 236 /* 0→359を跨いだ時,前回の値を360以上の値で表記 */
TakushimaYukimasa 0:9e851dc42cde 237 else if ((EncDeg[i][1] < 90) && (270 <= EncDeg[i][0]))
TakushimaYukimasa 0:9e851dc42cde 238 EncDeg[i][1] += 360;
TakushimaYukimasa 0:9e851dc42cde 239 /* 差を求める*/
TakushimaYukimasa 0:9e851dc42cde 240 EncoderDeg[i] += EncDeg[i][0] - EncDeg[i][1];
TakushimaYukimasa 0:9e851dc42cde 241 break;
TakushimaYukimasa 0:9e851dc42cde 242 /* 通常の処理 */
TakushimaYukimasa 0:9e851dc42cde 243 default:
TakushimaYukimasa 0:9e851dc42cde 244 CL[i]=!CL[i];
TakushimaYukimasa 0:9e851dc42cde 245 /* 最初でも最後でもなく奇数回で最初以外の時読み取り処理 */
TakushimaYukimasa 0:9e851dc42cde 246 if(cnt != 1 && cnt % 2){
TakushimaYukimasa 0:9e851dc42cde 247 data[i] |= (DO[i]==1);
TakushimaYukimasa 0:9e851dc42cde 248 data[i] = data[i] << 1;
TakushimaYukimasa 0:9e851dc42cde 249 }
TakushimaYukimasa 0:9e851dc42cde 250 break;
TakushimaYukimasa 0:9e851dc42cde 251 }
TakushimaYukimasa 0:9e851dc42cde 252 cnt++;
TakushimaYukimasa 0:9e851dc42cde 253 cnt%=26;
TakushimaYukimasa 0:9e851dc42cde 254 }