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