Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed YKNCT_Movement SBDBT BNO055 YKNCT_MD YKNCT_I2C
main.cpp@13:c8af1467ba8b, 2020-03-17 (annotated)
- Committer:
- yoshidayuito
- Date:
- Tue Mar 17 13:32:34 2020 +0000
- Revision:
- 13:c8af1467ba8b
- Parent:
- 12:10ce310bbdf4
- Child:
- 14:18ec50f38993
Fix Move X;Ver.yoshida
Who changed what in which revision?
User | Revision | Line number | New 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 | |
yoshidayuito | 7:8bea84f72e64 | 31 | /* 壁あて */ |
yoshidayuito | 7:8bea84f72e64 | 32 | void ToWall(void); |
TakushimaYukimasa | 0:9e851dc42cde | 33 | |
yoshidayuito | 13:c8af1467ba8b | 34 | /* x座標移動 */ |
yoshidayuito | 13:c8af1467ba8b | 35 | int XCooMove(int Tar,int Err); |
yoshidayuito | 13:c8af1467ba8b | 36 | |
TakushimaYukimasa | 0:9e851dc42cde | 37 | /* 変数定義 ------------------------------------------------------------------*/ |
TakushimaYukimasa | 0:9e851dc42cde | 38 | |
TakushimaYukimasa | 0:9e851dc42cde | 39 | /* 操作権 0…なし 1…手動 2…自動 */ |
TakushimaYukimasa | 0:9e851dc42cde | 40 | int operate=0; |
TakushimaYukimasa | 0:9e851dc42cde | 41 | |
TakushimaYukimasa | 0:9e851dc42cde | 42 | /* 自動シーケンス */ |
TakushimaYukimasa | 0:9e851dc42cde | 43 | int auto_mode=0; |
TakushimaYukimasa | 0:9e851dc42cde | 44 | |
TakushimaYukimasa | 0:9e851dc42cde | 45 | /* 直読みエンコーダ角度保存(degree) */ |
TakushimaYukimasa | 0:9e851dc42cde | 46 | double EncoderDeg[EncoderMAX] = {0}; |
TakushimaYukimasa | 0:9e851dc42cde | 47 | |
TakushimaYukimasa | 0:9e851dc42cde | 48 | /* 足回り値保存変数 */ |
yoshidayuito | 7:8bea84f72e64 | 49 | int MovMotor[4]= {0}; |
TakushimaYukimasa | 0:9e851dc42cde | 50 | |
TakushimaYukimasa | 0:9e851dc42cde | 51 | /* 自動yaw補整目標角度 */ |
TakushimaYukimasa | 0:9e851dc42cde | 52 | double TarTheta=0; |
TakushimaYukimasa | 0:9e851dc42cde | 53 | |
yoshidayuito | 13:c8af1467ba8b | 54 | /* x座標移動タイマーリセットフラグ */ |
yoshidayuito | 13:c8af1467ba8b | 55 | bool flag=0; |
yoshidayuito | 13:c8af1467ba8b | 56 | |
TakushimaYukimasa | 0:9e851dc42cde | 57 | /* クラス定義 ----------------------------------------------------------------*/ |
TakushimaYukimasa | 0:9e851dc42cde | 58 | |
TakushimaYukimasa | 0:9e851dc42cde | 59 | /* 割り込み用クラス */ |
TakushimaYukimasa | 0:9e851dc42cde | 60 | Ticker flipper; |
TakushimaYukimasa | 0:9e851dc42cde | 61 | |
TakushimaYukimasa | 0:9e851dc42cde | 62 | /* UART (Tx,Rx) */ |
TakushimaYukimasa | 0:9e851dc42cde | 63 | Serial telemetry(USBTX, USBRX, 115200); |
TakushimaYukimasa | 0:9e851dc42cde | 64 | |
TakushimaYukimasa | 0:9e851dc42cde | 65 | /* コントローラー */ |
TakushimaYukimasa | 0:9e851dc42cde | 66 | SBDBT DS3(PA_0, PA_1, 9600); |
TakushimaYukimasa | 0:9e851dc42cde | 67 | |
TakushimaYukimasa | 0:9e851dc42cde | 68 | /* オンボードLED */ |
TakushimaYukimasa | 0:9e851dc42cde | 69 | DigitalOut led(LED2); |
TakushimaYukimasa | 0:9e851dc42cde | 70 | |
TakushimaYukimasa | 0:9e851dc42cde | 71 | /* USERボタン */ |
TakushimaYukimasa | 0:9e851dc42cde | 72 | DigitalIn UB(PC_13,PullDown); |
TakushimaYukimasa | 0:9e851dc42cde | 73 | |
TakushimaYukimasa | 0:9e851dc42cde | 74 | /* エンコーダーピン CS */ |
TakushimaYukimasa | 0:9e851dc42cde | 75 | DigitalOut CS[] = {PA_2,PA_3}; |
TakushimaYukimasa | 0:9e851dc42cde | 76 | DigitalOut CL[] = {PA_4,PA_5}; |
TakushimaYukimasa | 0:9e851dc42cde | 77 | DigitalIn DO[] = {PA_6,PA_7}; |
TakushimaYukimasa | 0:9e851dc42cde | 78 | |
yoshidayuito | 1:520233a969e2 | 79 | /* ジャイロ用タイマー */ |
yoshidayuito | 1:520233a969e2 | 80 | Timer yawCnt; |
yoshidayuito | 1:520233a969e2 | 81 | |
yoshidayuito | 10:9ee22b22e583 | 82 | /* x座標移動タイマー */ |
yoshidayuito | 10:9ee22b22e583 | 83 | Timer CooCnt; |
yoshidayuito | 10:9ee22b22e583 | 84 | |
TakushimaYukimasa | 0:9e851dc42cde | 85 | /* 足回り動作クラス定義 */ |
TakushimaYukimasa | 0:9e851dc42cde | 86 | Move omuni(MovMotor,NowLoc.theta); |
TakushimaYukimasa | 0:9e851dc42cde | 87 | |
TakushimaYukimasa | 0:9e851dc42cde | 88 | /* I2C MDのクラス定義 */ |
TakushimaYukimasa | 0:9e851dc42cde | 89 | YKNCT_MD_I2C MD(PB_9,PB_8); |
TakushimaYukimasa | 0:9e851dc42cde | 90 | |
yoshidayuito | 8:367c7d6ef5a3 | 91 | /* I2C Inputのクラス定義 */ |
yoshidayuito | 8:367c7d6ef5a3 | 92 | YKNCT_I2C_IN In(PB_9,PB_8); |
yoshidayuito | 8:367c7d6ef5a3 | 93 | |
TakushimaYukimasa | 0:9e851dc42cde | 94 | /* ジャイロのピン設定 */ |
TakushimaYukimasa | 0:9e851dc42cde | 95 | BNO055 bno(PB_9, PB_8); |
TakushimaYukimasa | 0:9e851dc42cde | 96 | |
TakushimaYukimasa | 0:9e851dc42cde | 97 | |
TakushimaYukimasa | 0:9e851dc42cde | 98 | /*----------------------------------- main ----------------------------------*/ |
TakushimaYukimasa | 0:9e851dc42cde | 99 | int main() |
TakushimaYukimasa | 0:9e851dc42cde | 100 | { |
yoshidayuito | 7:8bea84f72e64 | 101 | telemetry.printf("\n\rMainStart"); |
TakushimaYukimasa | 0:9e851dc42cde | 102 | |
yoshidayuito | 7:8bea84f72e64 | 103 | /* 割り込みの設定 |
yoshidayuito | 7:8bea84f72e64 | 104 | * IT_CallBack関数を0.1msで割り込み */ |
yoshidayuito | 7:8bea84f72e64 | 105 | flipper.attach_us(&IT_CallBack, 100); |
TakushimaYukimasa | 0:9e851dc42cde | 106 | |
yoshidayuito | 7:8bea84f72e64 | 107 | /* ジャイロの設定 */ |
yoshidayuito | 7:8bea84f72e64 | 108 | bno.setmode(OPERATION_MODE_IMUPLUS); |
TakushimaYukimasa | 0:9e851dc42cde | 109 | |
yoshidayuito | 7:8bea84f72e64 | 110 | /* I2CMDの設定 */ |
yoshidayuito | 7:8bea84f72e64 | 111 | MD.Init(0,MD_SMB); |
yoshidayuito | 7:8bea84f72e64 | 112 | MD.Init(1,MD_SMB); |
yoshidayuito | 7:8bea84f72e64 | 113 | MD.Init(2,MD_SMB); |
yoshidayuito | 7:8bea84f72e64 | 114 | MD.Init(3,MD_SMB); |
yoshidayuito | 10:9ee22b22e583 | 115 | |
yoshidayuito | 8:367c7d6ef5a3 | 116 | In.Init(0,0); |
yoshidayuito | 8:367c7d6ef5a3 | 117 | In.Init(0,1); |
TakushimaYukimasa | 0:9e851dc42cde | 118 | |
yoshidayuito | 7:8bea84f72e64 | 119 | telemetry.printf("\n\rMainLoopStart"); |
yoshidayuito | 7:8bea84f72e64 | 120 | /* メインループ --------------------------------------------------------------*/ |
yoshidayuito | 11:42310638e241 | 121 | while(1) { |
yoshidayuito | 7:8bea84f72e64 | 122 | /* オンボードLED点滅 */ |
yoshidayuito | 7:8bea84f72e64 | 123 | led=!led; |
yoshidayuito | 7:8bea84f72e64 | 124 | |
yoshidayuito | 7:8bea84f72e64 | 125 | /* 表示改行 */ |
yoshidayuito | 7:8bea84f72e64 | 126 | telemetry.printf("\n\r"); |
TakushimaYukimasa | 0:9e851dc42cde | 127 | |
yoshidayuito | 7:8bea84f72e64 | 128 | /* 自動処理関連テレメトリ */ |
yoshidayuito | 7:8bea84f72e64 | 129 | telemetry.printf("ope:%d ",operate); |
yoshidayuito | 7:8bea84f72e64 | 130 | /* 座標テレメトリ */ |
yoshidayuito | 7:8bea84f72e64 | 131 | telemetry.printf("X:%4.0f Y:%4.0f T:%4.0f ",NowLoc.X,NowLoc.Y,NowLoc.theta); |
TakushimaYukimasa | 0:9e851dc42cde | 132 | |
yoshidayuito | 7:8bea84f72e64 | 133 | /* 自己位置推定更新 */ |
yoshidayuito | 7:8bea84f72e64 | 134 | LocEstimate(); |
TakushimaYukimasa | 0:9e851dc42cde | 135 | |
yoshidayuito | 7:8bea84f72e64 | 136 | /* I2CMD実行 */ |
yoshidayuito | 7:8bea84f72e64 | 137 | MD.Exe(); |
TakushimaYukimasa | 0:9e851dc42cde | 138 | |
yoshidayuito | 11:42310638e241 | 139 | /* ジャイロ用タイマースタート */ |
yoshidayuito | 7:8bea84f72e64 | 140 | yawCnt.start(); |
yoshidayuito | 11:42310638e241 | 141 | /* ジャイロ用タイマーリセット */ |
yoshidayuito | 7:8bea84f72e64 | 142 | yawCnt.reset(); |
yoshidayuito | 7:8bea84f72e64 | 143 | |
yoshidayuito | 7:8bea84f72e64 | 144 | /* 操縦権変更 ×停止 △手動 〇自動 */ |
yoshidayuito | 7:8bea84f72e64 | 145 | if(DS3.CROSS) operate=0; |
yoshidayuito | 7:8bea84f72e64 | 146 | if(DS3.TRIANGLE) operate=1; |
yoshidayuito | 7:8bea84f72e64 | 147 | if(DS3.CIRCLE) operate=2; |
TakushimaYukimasa | 0:9e851dc42cde | 148 | |
yoshidayuito | 7:8bea84f72e64 | 149 | /* 操縦権:なし 停止動作 */ |
yoshidayuito | 7:8bea84f72e64 | 150 | if(operate==0) { |
yoshidayuito | 7:8bea84f72e64 | 151 | /* 足回り停止 */ |
yoshidayuito | 7:8bea84f72e64 | 152 | omuni.XmarkOmni_Move(0,0,0); |
yoshidayuito | 10:9ee22b22e583 | 153 | |
yoshidayuito | 7:8bea84f72e64 | 154 | for(int i=0; i<4; i++) |
yoshidayuito | 7:8bea84f72e64 | 155 | MD.Set(i,MovMotor[i]); |
yoshidayuito | 7:8bea84f72e64 | 156 | } |
yoshidayuito | 7:8bea84f72e64 | 157 | /* 操縦権:手動 */ |
yoshidayuito | 7:8bea84f72e64 | 158 | else if(operate==1) { |
yoshidayuito | 7:8bea84f72e64 | 159 | /* 足回り手動動作 */ |
yoshidayuito | 7:8bea84f72e64 | 160 | int x_val = (double)(DS3.LX-64)*100/64; |
yoshidayuito | 7:8bea84f72e64 | 161 | int y_val = (double)(64-DS3.LY)*100/64; |
yoshidayuito | 7:8bea84f72e64 | 162 | int r_val = (double)(DS3.RX-64)*100/64; |
yoshidayuito | 7:8bea84f72e64 | 163 | |
yoshidayuito | 7:8bea84f72e64 | 164 | /* タイマーをリセット */ |
yoshidayuito | 7:8bea84f72e64 | 165 | if(DS3.RX!=64) yawCnt.reset(); |
yoshidayuito | 7:8bea84f72e64 | 166 | /* 目標角度再設定 */ |
yoshidayuito | 7:8bea84f72e64 | 167 | if(yawCnt.read_ms()<1000) TarTheta=NowLoc.theta; |
yoshidayuito | 7:8bea84f72e64 | 168 | /* r_val補正 */ |
yoshidayuito | 7:8bea84f72e64 | 169 | r_val+=(TarTheta-NowLoc.theta)*CONST_CORRECTION_YAW; |
yoshidayuito | 7:8bea84f72e64 | 170 | |
yoshidayuito | 7:8bea84f72e64 | 171 | omuni.XmarkOmni_Move(x_val,y_val,r_val); |
yoshidayuito | 10:9ee22b22e583 | 172 | |
yoshidayuito | 9:94112b5df540 | 173 | /* 壁あて実行 */ |
yoshidayuito | 9:94112b5df540 | 174 | if(DS3.R1) ToWall(); |
yoshidayuito | 10:9ee22b22e583 | 175 | |
yoshidayuito | 7:8bea84f72e64 | 176 | for(int i=0; i<4; i++) |
yoshidayuito | 7:8bea84f72e64 | 177 | MD.Set(i,MovMotor[i]); |
yoshidayuito | 7:8bea84f72e64 | 178 | } |
yoshidayuito | 7:8bea84f72e64 | 179 | /* 操縦権:自動 */ |
yoshidayuito | 7:8bea84f72e64 | 180 | else if(operate==2) { |
yoshidayuito | 7:8bea84f72e64 | 181 | switch(auto_mode) { |
yoshidayuito | 7:8bea84f72e64 | 182 | /* スタート待機処理 */ |
yoshidayuito | 7:8bea84f72e64 | 183 | case 0: |
yoshidayuito | 7:8bea84f72e64 | 184 | /* オンボードSWで次のステップに */ |
yoshidayuito | 7:8bea84f72e64 | 185 | if(UB) auto_mode++; |
yoshidayuito | 7:8bea84f72e64 | 186 | break; |
yoshidayuito | 7:8bea84f72e64 | 187 | |
yoshidayuito | 7:8bea84f72e64 | 188 | /* 〇〇の処理 */ |
yoshidayuito | 7:8bea84f72e64 | 189 | case 1: |
yoshidayuito | 13:c8af1467ba8b | 190 | XCooMove(1000,50); |
yoshidayuito | 13:c8af1467ba8b | 191 | /* 動作終了時次のステップに */ |
yoshidayuito | 13:c8af1467ba8b | 192 | if(XCooMove(1000,50)==0) |
yoshidayuito | 13:c8af1467ba8b | 193 | auto_mode++; |
yoshidayuito | 7:8bea84f72e64 | 194 | break; |
yoshidayuito | 7:8bea84f72e64 | 195 | |
yoshidayuito | 7:8bea84f72e64 | 196 | /* 終了処理 */ |
yoshidayuito | 7:8bea84f72e64 | 197 | default: |
yoshidayuito | 7:8bea84f72e64 | 198 | auto_mode=0; |
yoshidayuito | 7:8bea84f72e64 | 199 | operate=0; |
yoshidayuito | 7:8bea84f72e64 | 200 | break; |
yoshidayuito | 7:8bea84f72e64 | 201 | } |
yoshidayuito | 7:8bea84f72e64 | 202 | } |
yoshidayuito | 7:8bea84f72e64 | 203 | |
TakushimaYukimasa | 0:9e851dc42cde | 204 | } |
TakushimaYukimasa | 0:9e851dc42cde | 205 | } |
TakushimaYukimasa | 0:9e851dc42cde | 206 | |
TakushimaYukimasa | 0:9e851dc42cde | 207 | |
TakushimaYukimasa | 0:9e851dc42cde | 208 | /******************************************************************************* |
TakushimaYukimasa | 0:9e851dc42cde | 209 | * @概要 自己位置推定関数 |
TakushimaYukimasa | 0:9e851dc42cde | 210 | * @引数 なし |
TakushimaYukimasa | 0:9e851dc42cde | 211 | * @返り値 なし |
TakushimaYukimasa | 0:9e851dc42cde | 212 | *******************************************************************************/ |
yoshidayuito | 7:8bea84f72e64 | 213 | void LocEstimate(void) |
yoshidayuito | 7:8bea84f72e64 | 214 | { |
yoshidayuito | 7:8bea84f72e64 | 215 | static double GyroDeg[2]= {0}; |
yoshidayuito | 7:8bea84f72e64 | 216 | static double EncDeg[2][2]= {0}; |
yoshidayuito | 7:8bea84f72e64 | 217 | static double disp[3]= {0}; |
TakushimaYukimasa | 0:9e851dc42cde | 218 | |
yoshidayuito | 7:8bea84f72e64 | 219 | /* ジャイロの値取得 */ |
yoshidayuito | 7:8bea84f72e64 | 220 | bno.get_angles(); |
yoshidayuito | 7:8bea84f72e64 | 221 | GyroDeg[1]=GyroDeg[0]; |
yoshidayuito | 7:8bea84f72e64 | 222 | GyroDeg[0]=bno.euler.yaw; |
yoshidayuito | 7:8bea84f72e64 | 223 | if(GyroDeg[0]!=0) { |
yoshidayuito | 7:8bea84f72e64 | 224 | /* 359→0を跨いだ時,前回の値を0から逆回転で負の値で表記 */ |
yoshidayuito | 7:8bea84f72e64 | 225 | if(GyroDeg[1]<90 && GyroDeg[0]>270) GyroDeg[1]+=360; |
yoshidayuito | 7:8bea84f72e64 | 226 | /* 0→359を跨いだ時,前回の値を360以上の値で表記 */ |
yoshidayuito | 7:8bea84f72e64 | 227 | else if(GyroDeg[1]>270 && GyroDeg[0]<90) GyroDeg[1]-=360; |
yoshidayuito | 7:8bea84f72e64 | 228 | /* 差を求める*/ |
yoshidayuito | 7:8bea84f72e64 | 229 | disp[2]=GyroDeg[1]-GyroDeg[0]; |
yoshidayuito | 7:8bea84f72e64 | 230 | } |
yoshidayuito | 7:8bea84f72e64 | 231 | /* Enc2つの差分求める */ |
yoshidayuito | 7:8bea84f72e64 | 232 | for(int i=0; i<2; i++) { |
yoshidayuito | 7:8bea84f72e64 | 233 | EncDeg[i][1]=EncDeg[i][0]; |
yoshidayuito | 7:8bea84f72e64 | 234 | EncDeg[i][0]=EncoderDeg[i]; |
yoshidayuito | 7:8bea84f72e64 | 235 | disp[i]=EncDeg[i][1]-EncDeg[i][0]; |
yoshidayuito | 7:8bea84f72e64 | 236 | } |
yoshidayuito | 7:8bea84f72e64 | 237 | /* 差分を加速度として保存 */ |
yoshidayuito | 7:8bea84f72e64 | 238 | NowAcc.theta = disp[2]; |
yoshidayuito | 7:8bea84f72e64 | 239 | NowAcc.X = disp[0] * cos(NowLoc.theta) + disp[1] * sin(NowLoc.theta)+200*disp[2]; |
yoshidayuito | 7:8bea84f72e64 | 240 | NowAcc.Y = -disp[0] * sin(NowLoc.theta) + disp[1] * cos(NowLoc.theta)+200*disp[2]; |
yoshidayuito | 7:8bea84f72e64 | 241 | /* 差分を累積して現在位置を保存 */ |
yoshidayuito | 7:8bea84f72e64 | 242 | NowLoc.X += NowAcc.X; |
yoshidayuito | 7:8bea84f72e64 | 243 | NowLoc.Y += NowAcc.Y; |
yoshidayuito | 7:8bea84f72e64 | 244 | NowLoc.theta += NowAcc.theta; |
TakushimaYukimasa | 0:9e851dc42cde | 245 | } |
TakushimaYukimasa | 0:9e851dc42cde | 246 | |
TakushimaYukimasa | 0:9e851dc42cde | 247 | |
TakushimaYukimasa | 0:9e851dc42cde | 248 | |
TakushimaYukimasa | 0:9e851dc42cde | 249 | |
TakushimaYukimasa | 0:9e851dc42cde | 250 | /* 割り込み(100us) *************************************************************/ |
TakushimaYukimasa | 0:9e851dc42cde | 251 | void IT_CallBack(void) |
TakushimaYukimasa | 0:9e851dc42cde | 252 | { |
yoshidayuito | 7:8bea84f72e64 | 253 | static int cnt = 0; |
yoshidayuito | 7:8bea84f72e64 | 254 | static int data[EncoderMAX] = {0}; |
yoshidayuito | 7:8bea84f72e64 | 255 | static double EncDeg[EncoderMAX][2] = {0}; |
TakushimaYukimasa | 0:9e851dc42cde | 256 | |
yoshidayuito | 7:8bea84f72e64 | 257 | for(int i=0; i<EncoderMAX; i++) |
yoshidayuito | 7:8bea84f72e64 | 258 | switch(cnt) { |
yoshidayuito | 7:8bea84f72e64 | 259 | /* 最初の処理 */ |
yoshidayuito | 7:8bea84f72e64 | 260 | case 0: |
yoshidayuito | 7:8bea84f72e64 | 261 | data[i] = 0; |
yoshidayuito | 7:8bea84f72e64 | 262 | CS[i] = 0; |
yoshidayuito | 7:8bea84f72e64 | 263 | CL[i] = 1; |
yoshidayuito | 7:8bea84f72e64 | 264 | break; |
yoshidayuito | 7:8bea84f72e64 | 265 | /* 最後の処理 */ |
yoshidayuito | 7:8bea84f72e64 | 266 | case 25: |
yoshidayuito | 7:8bea84f72e64 | 267 | CS[i]=1; |
yoshidayuito | 7:8bea84f72e64 | 268 | /* 前回の値更新 今回の値更新(エンコーダの値(0~4096)を角度(0~360)に) */ |
yoshidayuito | 7:8bea84f72e64 | 269 | EncDeg[i][1] = EncDeg[i][0]; |
yoshidayuito | 7:8bea84f72e64 | 270 | EncDeg[i][0] = (double)data[i] * 360.0 / 4096; |
yoshidayuito | 7:8bea84f72e64 | 271 | /* 359→0を跨いだ時,前回の値を0から逆回転で負の値で表記 */ |
yoshidayuito | 7:8bea84f72e64 | 272 | if ((270 <= EncDeg[i][1]) && (EncDeg[i][0] < 90)) |
yoshidayuito | 7:8bea84f72e64 | 273 | EncDeg[i][1] -= 360; |
yoshidayuito | 7:8bea84f72e64 | 274 | /* 0→359を跨いだ時,前回の値を360以上の値で表記 */ |
yoshidayuito | 7:8bea84f72e64 | 275 | else if ((EncDeg[i][1] < 90) && (270 <= EncDeg[i][0])) |
yoshidayuito | 7:8bea84f72e64 | 276 | EncDeg[i][1] += 360; |
yoshidayuito | 7:8bea84f72e64 | 277 | /* 差を求める*/ |
yoshidayuito | 7:8bea84f72e64 | 278 | EncoderDeg[i] += EncDeg[i][0] - EncDeg[i][1]; |
yoshidayuito | 7:8bea84f72e64 | 279 | break; |
yoshidayuito | 7:8bea84f72e64 | 280 | /* 通常の処理 */ |
yoshidayuito | 7:8bea84f72e64 | 281 | default: |
yoshidayuito | 7:8bea84f72e64 | 282 | CL[i]=!CL[i]; |
yoshidayuito | 7:8bea84f72e64 | 283 | /* 最初でも最後でもなく奇数回で最初以外の時読み取り処理 */ |
yoshidayuito | 7:8bea84f72e64 | 284 | if(cnt != 1 && cnt % 2) { |
yoshidayuito | 7:8bea84f72e64 | 285 | data[i] |= (DO[i]==1); |
yoshidayuito | 7:8bea84f72e64 | 286 | data[i] = data[i] << 1; |
yoshidayuito | 7:8bea84f72e64 | 287 | } |
yoshidayuito | 7:8bea84f72e64 | 288 | break; |
yoshidayuito | 7:8bea84f72e64 | 289 | } |
yoshidayuito | 7:8bea84f72e64 | 290 | cnt++; |
yoshidayuito | 7:8bea84f72e64 | 291 | cnt%=26; |
yoshidayuito | 7:8bea84f72e64 | 292 | } |
yoshidayuito | 7:8bea84f72e64 | 293 | |
yoshidayuito | 7:8bea84f72e64 | 294 | |
yoshidayuito | 7:8bea84f72e64 | 295 | |
yoshidayuito | 7:8bea84f72e64 | 296 | |
yoshidayuito | 7:8bea84f72e64 | 297 | /******************************************************************************* |
yoshidayuito | 7:8bea84f72e64 | 298 | * @概要 壁あて関数 |
yoshidayuito | 7:8bea84f72e64 | 299 | * @引数 なし |
yoshidayuito | 7:8bea84f72e64 | 300 | * @返り値 なし |
yoshidayuito | 7:8bea84f72e64 | 301 | *******************************************************************************/ |
yoshidayuito | 7:8bea84f72e64 | 302 | void ToWall(void) |
yoshidayuito | 7:8bea84f72e64 | 303 | { |
yoshidayuito | 9:94112b5df540 | 304 | if(In.Get(0)==0) |
yoshidayuito | 7:8bea84f72e64 | 305 | for(int i=0; i<2; i++) MovMotor[i]-=10; |
yoshidayuito | 7:8bea84f72e64 | 306 | else for(int i=0; i<2; i++) MovMotor[i]+=0; |
yoshidayuito | 9:94112b5df540 | 307 | if(In.Get(1)==0) |
yoshidayuito | 7:8bea84f72e64 | 308 | for(int i=2; i<4; i++) MovMotor[i]+=10; |
yoshidayuito | 7:8bea84f72e64 | 309 | else for(int i=2; i<4; i++) MovMotor[i]+=0; |
yoshidayuito | 10:9ee22b22e583 | 310 | } |
yoshidayuito | 10:9ee22b22e583 | 311 | |
yoshidayuito | 10:9ee22b22e583 | 312 | |
yoshidayuito | 10:9ee22b22e583 | 313 | |
yoshidayuito | 10:9ee22b22e583 | 314 | /******************************************************************************* |
yoshidayuito | 10:9ee22b22e583 | 315 | * @概要 x座標移動関数 |
yoshidayuito | 11:42310638e241 | 316 | * @引数 Tar:目標値 |
yoshidayuito | 10:9ee22b22e583 | 317 | * @引数 Err :許容誤差 |
yoshidayuito | 10:9ee22b22e583 | 318 | * @返り値 val_p:残り距離 |
yoshidayuito | 10:9ee22b22e583 | 319 | *******************************************************************************/ |
yoshidayuito | 11:42310638e241 | 320 | int XCooMove(int Tar,int Err) |
yoshidayuito | 10:9ee22b22e583 | 321 | { |
yoshidayuito | 11:42310638e241 | 322 | int val_p=0,val_t=0,val=0; |
yoshidayuito | 10:9ee22b22e583 | 323 | |
yoshidayuito | 13:c8af1467ba8b | 324 | if(flag==0) { |
yoshidayuito | 13:c8af1467ba8b | 325 | /* タイマースタート */ |
yoshidayuito | 13:c8af1467ba8b | 326 | CooCnt.start(); |
yoshidayuito | 13:c8af1467ba8b | 327 | /* X座標移動用タイマーリセット */ |
yoshidayuito | 13:c8af1467ba8b | 328 | CooCnt.reset(); |
yoshidayuito | 13:c8af1467ba8b | 329 | flag=1; |
yoshidayuito | 13:c8af1467ba8b | 330 | } |
yoshidayuito | 10:9ee22b22e583 | 331 | |
yoshidayuito | 13:c8af1467ba8b | 332 | val_p=(Tar-NowLoc.X)*CONST_MOVE_X; |
yoshidayuito | 13:c8af1467ba8b | 333 | val_t=CooCnt.read_ms()*CONST_MOVE_X; |
yoshidayuito | 11:42310638e241 | 334 | |
yoshidayuito | 11:42310638e241 | 335 | val=MIN(val_p,val_t); |
yoshidayuito | 10:9ee22b22e583 | 336 | |
yoshidayuito | 11:42310638e241 | 337 | /* -100~100に調整 */ |
yoshidayuito | 10:9ee22b22e583 | 338 | if(val>100) val=100; |
yoshidayuito | 11:42310638e241 | 339 | else if(val<-100) val=-100; |
yoshidayuito | 11:42310638e241 | 340 | |
yoshidayuito | 10:9ee22b22e583 | 341 | omuni.XmarkOmni_Move(val,0,0); |
yoshidayuito | 11:42310638e241 | 342 | |
yoshidayuito | 13:c8af1467ba8b | 343 | int RemDis=Tar-NowLoc.X; |
yoshidayuito | 11:42310638e241 | 344 | |
yoshidayuito | 11:42310638e241 | 345 | if(RemDis<=Err) return 0; |
yoshidayuito | 11:42310638e241 | 346 | else return RemDis; |
TakushimaYukimasa | 0:9e851dc42cde | 347 | } |