最終調整
Dependencies: mbed AQM0802 CRotaryEncoder TB6612FNG
main.cpp@22:b40f7d0c0f12, 2019-11-22 (annotated)
- Committer:
- GGU
- Date:
- Fri Nov 22 04:20:20 2019 +0000
- Revision:
- 22:b40f7d0c0f12
- Parent:
- 21:97cc65e61580
- Child:
- 23:def04f2e894f
test;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GGU | 16:017874772ea7 | 1 | ////ライントレースサンプル |
yusaku0125 | 3:e455433c8cae | 2 | #include "mbed.h" |
yusaku0125 | 3:e455433c8cae | 3 | #include "CRotaryEncoder.h" |
yusaku0125 | 4:ac9e6772ddb3 | 4 | #include "TB6612.h" |
yusaku0125 | 8:15b4e1d7a2c5 | 5 | #include "AQM0802.h" |
yusaku0125 | 4:ac9e6772ddb3 | 6 | |
yusaku0125 | 4:ac9e6772ddb3 | 7 | |
yusaku0125 | 4:ac9e6772ddb3 | 8 | //☆★☆★各種パラメータ調整箇所☆★☆★☆★ |
GGU | 22:b40f7d0c0f12 | 9 | #define DEFAULT_SPEED 450 //1走目の基本速度[mm/sec] |
GGU | 22:b40f7d0c0f12 | 10 | #define DEFAULT_SPEED1 700 |
GGU | 22:b40f7d0c0f12 | 11 | #define DEFAULT_SPEED2 950 |
GGU | 22:b40f7d0c0f12 | 12 | #define DEFAULT_SPEED3 800 |
GGU | 16:017874772ea7 | 13 | #define STOP_DISTANCE 200000 //停止距離200000[um]⇒20[cm] |
poritekutama | 14:7ed78f52f40e | 14 | #define TURN_POWER 0.6 //コースアウト時の旋回力 |
poritekutama | 14:7ed78f52f40e | 15 | #define PULSE_TO_UM 28 //エンコーダ1パルス当たりのタイヤ移動距離[um] |
GGU | 16:017874772ea7 | 16 | #define INTERRUPT_TIME 3000 //割りこみ周期[us] |
poritekutama | 14:7ed78f52f40e | 17 | #define DEFAULT_GRAY 0.2f //フォトリフレクタデジタル入力の閾値 |
yusaku0125 | 7:cfbf8d4a4d36 | 18 | //シリアル通信でSensor_Digital値を確認し調整する。 |
poritekutama | 14:7ed78f52f40e | 19 | #define MARKER_WIDTH 10000 //マーカ幅[um](ビニルテープ幅19000[um]以内) |
yusaku0125 | 7:cfbf8d4a4d36 | 20 | //コースの傷によってマーカ誤検知する場合は値を大きくする。 |
yusaku0125 | 7:cfbf8d4a4d36 | 21 | #define CROSS_JUDGE 4 //ラインセンサいくつ以上白線検知で交差点認識するか設定。 |
yusaku0125 | 5:f635f1f01d2d | 22 | //モータ速度のゲイン関連(むやみに調整しない) |
yusaku0125 | 6:afd8f0d02c8d | 23 | #define M_KP 0.002f //P(比例)制御成分 |
yusaku0125 | 6:afd8f0d02c8d | 24 | #define M_KD 0.001f //D(微分)制御成分 |
yusaku0125 | 5:f635f1f01d2d | 25 | |
yusaku0125 | 5:f635f1f01d2d | 26 | //フォトリフレクタのゲイン(外側に行くにつれ値を何倍させたいか調整する。) |
yusaku0125 | 6:afd8f0d02c8d | 27 | #define S_K1 1.0f //float演算させる値には必ずfを付ける |
poritekutama | 14:7ed78f52f40e | 28 | #define S_K2 2.4f //2倍 |
poritekutama | 14:7ed78f52f40e | 29 | #define S_K3 4.7f //4倍 |
yusaku0125 | 5:f635f1f01d2d | 30 | |
yusaku0125 | 5:f635f1f01d2d | 31 | //ラインセンサ各種制御成分 |
poritekutama | 14:7ed78f52f40e | 32 | #define S_KP 1.0f //ラインセンサ比例成分。大きいほど曲がりやすい |
poritekutama | 14:7ed78f52f40e | 33 | #define S_KD 0.5f //ラインセンサ微分成分。大きいほど急なラインずれに強くなる。 |
yusaku0125 | 4:ac9e6772ddb3 | 34 | //////////☆★☆★☆★☆★☆★////////////// |
yusaku0125 | 4:ac9e6772ddb3 | 35 | |
yusaku0125 | 8:15b4e1d7a2c5 | 36 | |
yusaku0125 | 8:15b4e1d7a2c5 | 37 | |
yusaku0125 | 8:15b4e1d7a2c5 | 38 | //スイッチ状態の定義 |
yusaku0125 | 8:15b4e1d7a2c5 | 39 | #define PUSH 0 //スイッチ押したときの状態 |
yusaku0125 | 8:15b4e1d7a2c5 | 40 | #define PULL 1 //スイッチ離したときの状態 |
yusaku0125 | 6:afd8f0d02c8d | 41 | //機体状態の定義 |
yusaku0125 | 6:afd8f0d02c8d | 42 | #define STOP 0x80 //機体停止状態 |
yusaku0125 | 6:afd8f0d02c8d | 43 | #define RUN_START 0x40 //スタートマーカ通過 |
yusaku0125 | 6:afd8f0d02c8d | 44 | #define RUN_COURSE_LOUT 0x20 //左コースアウト状態 |
yusaku0125 | 6:afd8f0d02c8d | 45 | #define RUN_COURSE_CENTER 0x18 //ライン中央走行状態 |
yusaku0125 | 6:afd8f0d02c8d | 46 | #define RUN_COURSE_ROUT 0x04 //右コースアウト状態 |
yusaku0125 | 6:afd8f0d02c8d | 47 | #define SECOND_RUN 0x02 //機体停止状態 |
yusaku0125 | 6:afd8f0d02c8d | 48 | #define TUARD_RUN 0x01 //機体設定モード |
yusaku0125 | 6:afd8f0d02c8d | 49 | |
yusaku0125 | 10:e1eb10665472 | 50 | |
yusaku0125 | 8:15b4e1d7a2c5 | 51 | //デジタル入力オブジェクト定義 |
yusaku0125 | 8:15b4e1d7a2c5 | 52 | DigitalIn push_sw(D13); |
yusaku0125 | 5:f635f1f01d2d | 53 | /////アナログ入力オブジェクト定義////////// |
GGU | 22:b40f7d0c0f12 | 54 | |
poritekutama | 14:7ed78f52f40e | 55 | AnalogIn s1(D3); |
poritekutama | 14:7ed78f52f40e | 56 | AnalogIn s2(A6); |
poritekutama | 14:7ed78f52f40e | 57 | AnalogIn s3(A5); |
poritekutama | 14:7ed78f52f40e | 58 | AnalogIn s4(A4); |
poritekutama | 14:7ed78f52f40e | 59 | AnalogIn s5(A3); |
poritekutama | 14:7ed78f52f40e | 60 | AnalogIn s6(A2); |
poritekutama | 14:7ed78f52f40e | 61 | AnalogIn s7(A1); |
yusaku0125 | 6:afd8f0d02c8d | 62 | AnalogIn s8(A0); |
GGU | 16:017874772ea7 | 63 | |
GGU | 22:b40f7d0c0f12 | 64 | /* |
GGU | 16:017874772ea7 | 65 | AnalogIn s1(A1); |
GGU | 16:017874772ea7 | 66 | AnalogIn s2(D3); |
GGU | 16:017874772ea7 | 67 | AnalogIn s3(A6); |
GGU | 16:017874772ea7 | 68 | AnalogIn s4(A5); |
GGU | 16:017874772ea7 | 69 | AnalogIn s5(A4); |
GGU | 16:017874772ea7 | 70 | AnalogIn s6(A3); |
GGU | 16:017874772ea7 | 71 | AnalogIn s7(A2); |
GGU | 16:017874772ea7 | 72 | AnalogIn s8(A0); |
GGU | 22:b40f7d0c0f12 | 73 | */ |
GGU | 20:3b35311f4576 | 74 | |
yusaku0125 | 5:f635f1f01d2d | 75 | /////////////////////////////////////// |
yusaku0125 | 6:afd8f0d02c8d | 76 | Serial PC(USBTX,USBRX); |
yusaku0125 | 6:afd8f0d02c8d | 77 | CRotaryEncoder encoder_a(D1,D0); //モータAのエンコーダ |
yusaku0125 | 6:afd8f0d02c8d | 78 | CRotaryEncoder encoder_b(D11,D12); //モータBのエンコーダ |
yusaku0125 | 6:afd8f0d02c8d | 79 | Ticker timer; //タイマ割込み用 |
yusaku0125 | 4:ac9e6772ddb3 | 80 | TB6612 motor_a(D2,D7,D6); //モータA制御用(pwma,ain1,ain2) |
yusaku0125 | 6:afd8f0d02c8d | 81 | TB6612 motor_b(D10,D8,D9); //モータB制御用(pwmb,bin1,bin2) |
yusaku0125 | 8:15b4e1d7a2c5 | 82 | AQM0802 lcd(I2C_SDA,I2C_SCL); //液晶制御用 |
yusaku0125 | 4:ac9e6772ddb3 | 83 | |
yusaku0125 | 5:f635f1f01d2d | 84 | //使用変数の定義 |
yusaku0125 | 8:15b4e1d7a2c5 | 85 | int Sw_Ptn; |
yusaku0125 | 8:15b4e1d7a2c5 | 86 | int Old_Sw_Ptn; |
poritekutama | 14:7ed78f52f40e | 87 | int Sw=0; |
GGU | 16:017874772ea7 | 88 | int Coner_c=0; //カウントを格納 |
poritekutama | 14:7ed78f52f40e | 89 | char Coner_str[3]; |
yusaku0125 | 8:15b4e1d7a2c5 | 90 | double S1_Data,S2_Data,S3_Data,S4_Data,S5_Data,S6_Data,S7_Data,S8_Data; |
yusaku0125 | 8:15b4e1d7a2c5 | 91 | double All_Sensor_Data; //ラインセンサ総データ量 |
yusaku0125 | 8:15b4e1d7a2c5 | 92 | double Sensor_Diff[2]={0,0}; //ラインセンサ偏差 |
yusaku0125 | 8:15b4e1d7a2c5 | 93 | double Sensor_P =0.0f; //ラインセンサP(比例成分)制御量 |
yusaku0125 | 8:15b4e1d7a2c5 | 94 | double Sensor_D =0.0f; //ラインセンサD(微分成分)制御量 |
yusaku0125 | 8:15b4e1d7a2c5 | 95 | double Sensor_PD=0.0f; //ラインセンサP,D成分の合計 |
yusaku0125 | 12:dc4c569248d7 | 96 | char Gray_Str[5]; //LCD閾値表示用文字列 |
yusaku0125 | 12:dc4c569248d7 | 97 | float Gray=DEFAULT_GRAY; |
yusaku0125 | 5:f635f1f01d2d | 98 | long int Enc_Count_A=0,Enc_Count_B=0; //エンコーダパルス数を格納 |
yusaku0125 | 12:dc4c569248d7 | 99 | long int Enc_A_Rotate=0,Enc_B_Rotate=0; |
GGU | 16:017874772ea7 | 100 | long int Stop_Distance=STOP_DISTANCE; |
yusaku0125 | 12:dc4c569248d7 | 101 | |
GGU | 16:017874772ea7 | 102 | long int memory_A=0; //移動距離格納 |
GGU | 16:017874772ea7 | 103 | long int memory_B=0; |
GGU | 16:017874772ea7 | 104 | char MemoryA_Str[5]; //LCD閾値表示用文字列 |
GGU | 15:cfe79ebfcb25 | 105 | char MemoryB_Str[5]; |
GGU | 15:cfe79ebfcb25 | 106 | long int Distance_A=0,Distance_B=0; //タイヤ移動距離を格納[mm] |
GGU | 15:cfe79ebfcb25 | 107 | long int Distance_memory_A=0, Distance_memory_B=0; |
poritekutama | 14:7ed78f52f40e | 108 | |
yusaku0125 | 7:cfbf8d4a4d36 | 109 | long int Marker_Run_Distance=0; |
yusaku0125 | 5:f635f1f01d2d | 110 | long int Speed_A=0, Speed_B=0; //現在速度 |
GGU | 16:017874772ea7 | 111 | |
GGU | 16:017874772ea7 | 112 | long int Low_Speed=DEFAULT_SPEED; |
GGU | 16:017874772ea7 | 113 | long int Medium_Speed=DEFAULT_SPEED1; |
GGU | 16:017874772ea7 | 114 | long int High_Speed=DEFAULT_SPEED2; |
GGU | 22:b40f7d0c0f12 | 115 | long int High1_Speed=DEFAULT_SPEED3; |
yusaku0125 | 11:2cd6f8be124e | 116 | char Speed_Str[5]; //LCD速度表示用文字列 |
yusaku0125 | 5:f635f1f01d2d | 117 | long int Target_Speed_A=0,Target_Speed_B=0; //目標速度 |
yusaku0125 | 8:15b4e1d7a2c5 | 118 | long int Motor_A_Diff[2]={0,0}; //過去の速度偏差と現在の速度偏差を格納 |
yusaku0125 | 8:15b4e1d7a2c5 | 119 | long int Motor_B_Diff[2]={0,0}; // |
yusaku0125 | 8:15b4e1d7a2c5 | 120 | float Motor_A_P,Motor_B_P; //モータ速度制御P成分 |
yusaku0125 | 8:15b4e1d7a2c5 | 121 | float Motor_A_D,Motor_B_D; //モータ速度制御D成分 |
yusaku0125 | 8:15b4e1d7a2c5 | 122 | float Motor_A_PD,Motor_B_PD; //モータ速度制御PD合成 |
yusaku0125 | 4:ac9e6772ddb3 | 123 | float Motor_A_Pwm,Motor_B_Pwm; //モータへの出力 |
yusaku0125 | 6:afd8f0d02c8d | 124 | unsigned char Sensor_Digital =0x00; |
yusaku0125 | 6:afd8f0d02c8d | 125 | unsigned char Old_Sensor_Digital=0x00; |
yusaku0125 | 7:cfbf8d4a4d36 | 126 | int Sensor_Cnt=0; |
yusaku0125 | 8:15b4e1d7a2c5 | 127 | unsigned char Machine_Status =STOP; //機体状態 |
yusaku0125 | 6:afd8f0d02c8d | 128 | unsigned char Old_Machine_Status=0x00; //過去の機体状態 |
yusaku0125 | 7:cfbf8d4a4d36 | 129 | int Marker_Pass_Flag = 0; |
yusaku0125 | 7:cfbf8d4a4d36 | 130 | int Old_Marker_Pass_Flag=0; |
yusaku0125 | 7:cfbf8d4a4d36 | 131 | int Corner_Flag=0; |
yusaku0125 | 7:cfbf8d4a4d36 | 132 | int SG_Flag=0; |
yusaku0125 | 7:cfbf8d4a4d36 | 133 | int SG_Cnt=0; |
yusaku0125 | 7:cfbf8d4a4d36 | 134 | int Cross_Flag=0; |
poritekutama | 19:edf765724d2d | 135 | long int Imaginary_Speed=0; |
poritekutama | 19:edf765724d2d | 136 | |
GGU | 16:017874772ea7 | 137 | |
GGU | 16:017874772ea7 | 138 | int Row=0; //行変数 |
GGU | 16:017874772ea7 | 139 | float course_data[100][3]; //記憶走行用配列 |
poritekutama | 19:edf765724d2d | 140 | float curvature=0; |
GGU | 16:017874772ea7 | 141 | |
yusaku0125 | 6:afd8f0d02c8d | 142 | void sensor_analog_read(){ |
yusaku0125 | 5:f635f1f01d2d | 143 | S1_Data=s1.read(); |
yusaku0125 | 5:f635f1f01d2d | 144 | S2_Data=s2.read(); |
yusaku0125 | 5:f635f1f01d2d | 145 | S3_Data=s3.read(); |
yusaku0125 | 5:f635f1f01d2d | 146 | S4_Data=s4.read(); |
yusaku0125 | 5:f635f1f01d2d | 147 | S5_Data=s5.read(); |
yusaku0125 | 5:f635f1f01d2d | 148 | S6_Data=s6.read(); |
yusaku0125 | 5:f635f1f01d2d | 149 | S7_Data=s7.read(); |
yusaku0125 | 6:afd8f0d02c8d | 150 | S8_Data=s8.read(); |
yusaku0125 | 6:afd8f0d02c8d | 151 | } |
yusaku0125 | 7:cfbf8d4a4d36 | 152 | |
yusaku0125 | 7:cfbf8d4a4d36 | 153 | |
yusaku0125 | 6:afd8f0d02c8d | 154 | void sensor_digital_read(){//8つのフォトリフレクタの入力を8ビットのデジタルパターンに変換 |
yusaku0125 | 7:cfbf8d4a4d36 | 155 | Sensor_Cnt=0; |
yusaku0125 | 6:afd8f0d02c8d | 156 | Old_Sensor_Digital=Sensor_Digital; |
yusaku0125 | 12:dc4c569248d7 | 157 | if(S1_Data>Gray){ |
yusaku0125 | 7:cfbf8d4a4d36 | 158 | Sensor_Digital |= 0x80; //7ビット目のみセット (1にする。) |
yusaku0125 | 7:cfbf8d4a4d36 | 159 | }else Sensor_Digital &= 0x7F; //7ビット目のみマスク(0にする。) |
yusaku0125 | 12:dc4c569248d7 | 160 | if(S2_Data>Gray){ |
yusaku0125 | 7:cfbf8d4a4d36 | 161 | Sensor_Digital |= 0x40; //6ビット目のみセット (1にする。) |
yusaku0125 | 7:cfbf8d4a4d36 | 162 | Sensor_Cnt++; |
yusaku0125 | 7:cfbf8d4a4d36 | 163 | }else Sensor_Digital &= 0xBF; //6ビット目のみマスク(0にする。) |
yusaku0125 | 12:dc4c569248d7 | 164 | if(S3_Data>Gray){ |
yusaku0125 | 7:cfbf8d4a4d36 | 165 | Sensor_Digital |= 0x20; //5ビット目のみセット (1にする。) |
yusaku0125 | 7:cfbf8d4a4d36 | 166 | Sensor_Cnt++; |
yusaku0125 | 7:cfbf8d4a4d36 | 167 | }else Sensor_Digital &= 0xDF; //5ビット目のみマスク(0にする。) |
yusaku0125 | 12:dc4c569248d7 | 168 | if(S4_Data>Gray){ |
yusaku0125 | 7:cfbf8d4a4d36 | 169 | Sensor_Digital |= 0x10; //4ビット目のみセット (1にする。) |
yusaku0125 | 7:cfbf8d4a4d36 | 170 | Sensor_Cnt++; |
yusaku0125 | 7:cfbf8d4a4d36 | 171 | }else Sensor_Digital &= 0xEF; //4ビット目のみマスク(0にする。) |
yusaku0125 | 12:dc4c569248d7 | 172 | if(S5_Data>Gray){ |
yusaku0125 | 7:cfbf8d4a4d36 | 173 | Sensor_Digital |= 0x08; //3ビット目のみセット (1にする。) |
yusaku0125 | 7:cfbf8d4a4d36 | 174 | Sensor_Cnt++; |
yusaku0125 | 7:cfbf8d4a4d36 | 175 | }else Sensor_Digital &= 0xF7; //3ビット目のみマスク(0にする。) |
yusaku0125 | 12:dc4c569248d7 | 176 | if(S6_Data>Gray){ |
yusaku0125 | 7:cfbf8d4a4d36 | 177 | Sensor_Digital |= 0x04; //2ビット目のみセット (1にする。) |
yusaku0125 | 7:cfbf8d4a4d36 | 178 | Sensor_Cnt++; |
yusaku0125 | 7:cfbf8d4a4d36 | 179 | }else Sensor_Digital &= 0xFB; //2ビット目のみマスク(0にする。) |
yusaku0125 | 12:dc4c569248d7 | 180 | if(S7_Data>Gray){ |
yusaku0125 | 7:cfbf8d4a4d36 | 181 | Sensor_Digital |= 0x02; //1ビット目のみセット (1にする。) |
yusaku0125 | 7:cfbf8d4a4d36 | 182 | Sensor_Cnt++; |
yusaku0125 | 7:cfbf8d4a4d36 | 183 | }else Sensor_Digital &= 0xFD; //1ビット目のみマスク(0にする。) |
yusaku0125 | 12:dc4c569248d7 | 184 | if(S8_Data>Gray){ |
yusaku0125 | 7:cfbf8d4a4d36 | 185 | Sensor_Digital |= 0x01; //0ビット目のみセット (1にする。) |
yusaku0125 | 7:cfbf8d4a4d36 | 186 | }else Sensor_Digital &= 0xFE; //0ビット目のみマスク(0にする。) |
yusaku0125 | 6:afd8f0d02c8d | 187 | } |
yusaku0125 | 6:afd8f0d02c8d | 188 | |
yusaku0125 | 6:afd8f0d02c8d | 189 | void Machine_Status_Set(){ |
yusaku0125 | 7:cfbf8d4a4d36 | 190 | Old_Machine_Status=Machine_Status; |
yusaku0125 | 7:cfbf8d4a4d36 | 191 | |
yusaku0125 | 6:afd8f0d02c8d | 192 | //機体がライン中央に位置するとき |
yusaku0125 | 6:afd8f0d02c8d | 193 | if(Sensor_Digital&RUN_COURSE_CENTER )Machine_Status|=RUN_COURSE_CENTER; |
yusaku0125 | 6:afd8f0d02c8d | 194 | else Machine_Status &= 0xE7;//ライン中央情報のマスク |
yusaku0125 | 6:afd8f0d02c8d | 195 | if((Sensor_Digital==0x00)&&(Old_Sensor_Digital==0x40)){//左センサコースアウト時 |
yusaku0125 | 6:afd8f0d02c8d | 196 | Machine_Status|=RUN_COURSE_LOUT;//左コースアウト状態のビットをセット |
yusaku0125 | 6:afd8f0d02c8d | 197 | }else if((Machine_Status&RUN_COURSE_LOUT)&&(Sensor_Digital&RUN_COURSE_CENTER)){ |
yusaku0125 | 6:afd8f0d02c8d | 198 | //左コースアウト状態かつ機体がライン中央に復帰したとき |
yusaku0125 | 6:afd8f0d02c8d | 199 | Machine_Status &= 0xDF;//左コースアウト情報のみマスク |
yusaku0125 | 6:afd8f0d02c8d | 200 | } |
yusaku0125 | 6:afd8f0d02c8d | 201 | if((Sensor_Digital==0x00)&&(Old_Sensor_Digital==0x02)){//右センサコースアウト時 |
yusaku0125 | 6:afd8f0d02c8d | 202 | Machine_Status|=RUN_COURSE_ROUT;//右コースアウト状態のビットをセット |
yusaku0125 | 6:afd8f0d02c8d | 203 | }else if((Machine_Status&RUN_COURSE_ROUT)&&(Sensor_Digital&RUN_COURSE_CENTER)){ |
yusaku0125 | 6:afd8f0d02c8d | 204 | //右コースアウト状態かつ機体がライン中央に復帰したとき |
yusaku0125 | 6:afd8f0d02c8d | 205 | Machine_Status &= 0xFB;//右コースアウト情報のみマスク |
yusaku0125 | 6:afd8f0d02c8d | 206 | } |
yusaku0125 | 6:afd8f0d02c8d | 207 | } |
poritekutama | 19:edf765724d2d | 208 | void coner_curvature(){ |
GGU | 22:b40f7d0c0f12 | 209 | if(Sw==0){ |
GGU | 22:b40f7d0c0f12 | 210 | Enc_Count_A=encoder_a.Get(); //エンコーダパルス数を取得 |
GGU | 22:b40f7d0c0f12 | 211 | Enc_Count_B=-encoder_b.Get(); |
GGU | 22:b40f7d0c0f12 | 212 | Distance_memory_A=(Enc_Count_A*PULSE_TO_UM); |
GGU | 22:b40f7d0c0f12 | 213 | Distance_memory_B=(Enc_Count_B*PULSE_TO_UM); |
GGU | 22:b40f7d0c0f12 | 214 | course_data[Row][0]=(float)Distance_memory_A; |
GGU | 22:b40f7d0c0f12 | 215 | course_data[Row][1]=(float)Distance_memory_B; |
GGU | 22:b40f7d0c0f12 | 216 | //キョクリツ演算 |
GGU | 22:b40f7d0c0f12 | 217 | if((Distance_memory_A>(Distance_memory_B*1.8)) || (Distance_memory_A<(Distance_memory_B*0.2))){//左が右より80%以上早いか20%以上遅いとき |
GGU | 22:b40f7d0c0f12 | 218 | Imaginary_Speed=Low_Speed; |
GGU | 22:b40f7d0c0f12 | 219 | }else if((Distance_memory_A>(Distance_memory_B*1.5)) || (Distance_memory_A<(Distance_memory_B*0.5))){//左が右より50%以上早いか50%以上遅いとき |
GGU | 22:b40f7d0c0f12 | 220 | Imaginary_Speed=Medium_Speed; |
GGU | 22:b40f7d0c0f12 | 221 | }else if((Distance_memory_A>(Distance_memory_B*1.2)) || (Distance_memory_A<(Distance_memory_A*0.8))){//左が右より20%以上早いか20%以上遅いとき⇒直線 |
GGU | 22:b40f7d0c0f12 | 222 | Imaginary_Speed=High_Speed; |
GGU | 22:b40f7d0c0f12 | 223 | } |
GGU | 22:b40f7d0c0f12 | 224 | else{ |
GGU | 22:b40f7d0c0f12 | 225 | Imaginary_Speed=High_Speed; |
GGU | 22:b40f7d0c0f12 | 226 | } |
GGU | 22:b40f7d0c0f12 | 227 | course_data[Row][2]=Imaginary_Speed; //仮想の演算速度ヲ格納 |
GGU | 22:b40f7d0c0f12 | 228 | PC.printf("left:%.2f\t",course_data[Row][0]); |
GGU | 22:b40f7d0c0f12 | 229 | PC.printf("right:%.2f\t",course_data[Row][1]); |
GGU | 22:b40f7d0c0f12 | 230 | PC.printf("speed:%.2f\n\r",course_data[Row][2]); |
GGU | 22:b40f7d0c0f12 | 231 | } |
GGU | 22:b40f7d0c0f12 | 232 | else if(Sw==1){ |
GGU | 22:b40f7d0c0f12 | 233 | Target_Speed_A=course_data[Row][2];//記憶走行で演算した仮想速度を使う |
GGU | 22:b40f7d0c0f12 | 234 | Target_Speed_B=course_data[Row][2]; |
GGU | 22:b40f7d0c0f12 | 235 | PC.printf("left:%.2f\t",course_data[Row][0]); |
GGU | 22:b40f7d0c0f12 | 236 | PC.printf("right:%.2f\t",course_data[Row][1]); |
GGU | 22:b40f7d0c0f12 | 237 | PC.printf("speed:%.2f\n\r",course_data[Row][2]); |
GGU | 22:b40f7d0c0f12 | 238 | } |
GGU | 22:b40f7d0c0f12 | 239 | else{ |
GGU | 22:b40f7d0c0f12 | 240 | Distance_memory_A=(Enc_Count_A*PULSE_TO_UM); |
GGU | 22:b40f7d0c0f12 | 241 | Distance_memory_B=(Enc_Count_B*PULSE_TO_UM); |
GGU | 22:b40f7d0c0f12 | 242 | } |
GGU | 22:b40f7d0c0f12 | 243 | |
GGU | 22:b40f7d0c0f12 | 244 | /* |
GGU | 22:b40f7d0c0f12 | 245 | if((motor_a>(motor_b*1.8)) || (motor_a<(motor_b*0.2))){//左が右より80%以上早いか20%以上遅いとき |
poritekutama | 19:edf765724d2d | 246 | Imaginary_Speed=Low_Speed; |
poritekutama | 19:edf765724d2d | 247 | Target_Speed_A=Imaginary_Speed; |
poritekutama | 19:edf765724d2d | 248 | Target_Speed_B=Imaginary_Speed; |
poritekutama | 19:edf765724d2d | 249 | |
poritekutama | 19:edf765724d2d | 250 | }else if((motor_a>(motor_b*1.5)) || (motor_a<(motor_b*0.5))){//左が右より50%以上早いか50%以上遅いとき |
poritekutama | 19:edf765724d2d | 251 | Imaginary_Speed=Medium_Speed; |
poritekutama | 19:edf765724d2d | 252 | Target_Speed_A=Imaginary_Speed; |
poritekutama | 19:edf765724d2d | 253 | Target_Speed_B=Imaginary_Speed; |
poritekutama | 19:edf765724d2d | 254 | }else if((motor_a>(motor_b*1.2)) || (motor_a<(motor_b*0.8))){//左が右より20%以上早いか20%以上遅いとき⇒直線 |
poritekutama | 19:edf765724d2d | 255 | Imaginary_Speed=High_Speed; |
poritekutama | 19:edf765724d2d | 256 | Target_Speed_A=Imaginary_Speed; |
poritekutama | 19:edf765724d2d | 257 | Target_Speed_B=Imaginary_Speed; |
poritekutama | 19:edf765724d2d | 258 | } |
poritekutama | 19:edf765724d2d | 259 | else{ |
poritekutama | 19:edf765724d2d | 260 | Imaginary_Speed=High_Speed; |
GGU | 22:b40f7d0c0f12 | 261 | Target_Speed_A=Imaginary_Speed; |
poritekutama | 19:edf765724d2d | 262 | Target_Speed_B=Imaginary_Speed; |
poritekutama | 19:edf765724d2d | 263 | } |
GGU | 22:b40f7d0c0f12 | 264 | */ |
GGU | 22:b40f7d0c0f12 | 265 | /*course_data[Row][0]=(float)Distance_memory_A; |
poritekutama | 19:edf765724d2d | 266 | course_data[Row][1]=(float)Distance_memory_B; |
poritekutama | 19:edf765724d2d | 267 | course_data[Row][2]=Imaginary_Speed; |
poritekutama | 19:edf765724d2d | 268 | PC.printf("left:%.2f\t",course_data[Row][0]); |
poritekutama | 19:edf765724d2d | 269 | PC.printf("right:%.2f\t",course_data[Row][1]); |
GGU | 22:b40f7d0c0f12 | 270 | PC.printf("speed:%.2f\n\r",course_data[Row][2]);*/ |
poritekutama | 19:edf765724d2d | 271 | |
poritekutama | 19:edf765724d2d | 272 | } |
yusaku0125 | 8:15b4e1d7a2c5 | 273 | |
yusaku0125 | 8:15b4e1d7a2c5 | 274 | //タイマ割り込み1[ms]周期 |
yusaku0125 | 6:afd8f0d02c8d | 275 | void timer_interrupt(){ |
yusaku0125 | 7:cfbf8d4a4d36 | 276 | |
yusaku0125 | 6:afd8f0d02c8d | 277 | //ラインセンサ情報取得 |
yusaku0125 | 6:afd8f0d02c8d | 278 | sensor_analog_read(); |
yusaku0125 | 6:afd8f0d02c8d | 279 | sensor_digital_read(); |
yusaku0125 | 7:cfbf8d4a4d36 | 280 | |
yusaku0125 | 7:cfbf8d4a4d36 | 281 | //機体状態の取得 |
yusaku0125 | 6:afd8f0d02c8d | 282 | Machine_Status_Set(); |
yusaku0125 | 7:cfbf8d4a4d36 | 283 | |
yusaku0125 | 7:cfbf8d4a4d36 | 284 | //交差点の認識 |
yusaku0125 | 7:cfbf8d4a4d36 | 285 | if(Sensor_Cnt>=CROSS_JUDGE )Cross_Flag=1;//ラインセンサ4つ以上検知状態の時は交差点を示す。 |
yusaku0125 | 7:cfbf8d4a4d36 | 286 | |
yusaku0125 | 7:cfbf8d4a4d36 | 287 | //各種マーカの検知 |
yusaku0125 | 7:cfbf8d4a4d36 | 288 | Old_Marker_Pass_Flag=Marker_Pass_Flag;//過去のフラグを退避 |
yusaku0125 | 7:cfbf8d4a4d36 | 289 | if(Sensor_Digital&0x81){ //マーカセンサ検知時 |
yusaku0125 | 7:cfbf8d4a4d36 | 290 | Marker_Pass_Flag=1; //マーカ通過中フラグをON |
yusaku0125 | 7:cfbf8d4a4d36 | 291 | if(Sensor_Digital&0x80)Corner_Flag=1; //コーナセンサの検知 |
yusaku0125 | 7:cfbf8d4a4d36 | 292 | if(Sensor_Digital&0x01)SG_Flag=1; //スタートゴールセンサの検知 |
yusaku0125 | 7:cfbf8d4a4d36 | 293 | if((Corner_Flag==1)&&(SG_Flag==1));//交差点通過中。何もしない |
yusaku0125 | 7:cfbf8d4a4d36 | 294 | }else Marker_Pass_Flag=0;//マーカ通過終了 |
yusaku0125 | 7:cfbf8d4a4d36 | 295 | |
yusaku0125 | 7:cfbf8d4a4d36 | 296 | //マーカ通過後、マーカ種類判別 |
yusaku0125 | 7:cfbf8d4a4d36 | 297 | if((Old_Marker_Pass_Flag==1)&&(Marker_Pass_Flag==0)){//マーカ通過後 |
yusaku0125 | 7:cfbf8d4a4d36 | 298 | if(Marker_Run_Distance>MARKER_WIDTH){//マーカ幅がもっともらしいとき |
yusaku0125 | 7:cfbf8d4a4d36 | 299 | if(Cross_Flag==1);//交差点の時は何もしない |
yusaku0125 | 7:cfbf8d4a4d36 | 300 | else if((SG_Flag==1)&&(SG_Cnt==0)){//ゴールスタートマーカの時⇒1回目 |
yusaku0125 | 7:cfbf8d4a4d36 | 301 | SG_Cnt=1; |
yusaku0125 | 7:cfbf8d4a4d36 | 302 | }else if((SG_Flag==1)&&(SG_Cnt==1)){//ゴールスタートマーカの時⇒2回目 |
GGU | 22:b40f7d0c0f12 | 303 | Machine_Status|=STOP; //機体停止状態へ |
GGU | 22:b40f7d0c0f12 | 304 | Row=0; |
GGU | 22:b40f7d0c0f12 | 305 | SG_Cnt=0; |
poritekutama | 19:edf765724d2d | 306 | }else if(Corner_Flag==1){//コーナマーカの時 |
GGU | 16:017874772ea7 | 307 | Distance_memory_A=0; |
GGU | 16:017874772ea7 | 308 | Distance_memory_B=0; |
GGU | 22:b40f7d0c0f12 | 309 | Coner_c++; |
poritekutama | 19:edf765724d2d | 310 | coner_curvature(); |
GGU | 22:b40f7d0c0f12 | 311 | Row++; |
yusaku0125 | 7:cfbf8d4a4d36 | 312 | } |
yusaku0125 | 7:cfbf8d4a4d36 | 313 | }else{//マーカではなく、誤検知だった場合。 |
yusaku0125 | 7:cfbf8d4a4d36 | 314 | //何もしない |
yusaku0125 | 7:cfbf8d4a4d36 | 315 | } |
yusaku0125 | 7:cfbf8d4a4d36 | 316 | Corner_Flag=0; |
yusaku0125 | 7:cfbf8d4a4d36 | 317 | SG_Flag=0; |
yusaku0125 | 7:cfbf8d4a4d36 | 318 | Cross_Flag=0; |
yusaku0125 | 7:cfbf8d4a4d36 | 319 | Marker_Run_Distance=0;//マーカ通過距離情報リセット |
yusaku0125 | 7:cfbf8d4a4d36 | 320 | } |
yusaku0125 | 6:afd8f0d02c8d | 321 | |
yusaku0125 | 5:f635f1f01d2d | 322 | //センサ取得値の重ね合わせ(端のセンサほどモータ制御量を大きくする) |
yusaku0125 | 5:f635f1f01d2d | 323 | All_Sensor_Data=-(S2_Data*S_K3+S3_Data*S_K2+S4_Data*S_K1)+(S5_Data*S_K1+S6_Data*S_K2+S7_Data*S_K3); |
yusaku0125 | 6:afd8f0d02c8d | 324 | Sensor_Diff[1]=Sensor_Diff[0];//過去のラインセンサ偏差を退避 |
yusaku0125 | 5:f635f1f01d2d | 325 | Sensor_Diff[0]=All_Sensor_Data; |
yusaku0125 | 5:f635f1f01d2d | 326 | Sensor_P=All_Sensor_Data*S_KP; //ラインセンサ比例成分の演算 |
yusaku0125 | 5:f635f1f01d2d | 327 | Sensor_D=(Sensor_Diff[0]-Sensor_Diff[1])*S_KD; //ラインセンサ微分成分の演算 |
yusaku0125 | 6:afd8f0d02c8d | 328 | Sensor_PD=Sensor_P+Sensor_D; |
yusaku0125 | 6:afd8f0d02c8d | 329 | |
yusaku0125 | 4:ac9e6772ddb3 | 330 | ////モータ現在速度の取得 |
yusaku0125 | 4:ac9e6772ddb3 | 331 | Enc_Count_A=encoder_a.Get(); //エンコーダパルス数を取得 |
yusaku0125 | 4:ac9e6772ddb3 | 332 | Enc_Count_B=-encoder_b.Get(); |
GGU | 17:b29e2c88b3c5 | 333 | Distance_A=(Enc_Count_A*PULSE_TO_UM); //移動距離をmm単位で格納 |
GGU | 17:b29e2c88b3c5 | 334 | Distance_B=(Enc_Count_B*PULSE_TO_UM); |
GGU | 15:cfe79ebfcb25 | 335 | |
GGU | 15:cfe79ebfcb25 | 336 | Distance_memory_A=(Enc_Count_A*PULSE_TO_UM); |
GGU | 15:cfe79ebfcb25 | 337 | Distance_memory_B=(Enc_Count_B*PULSE_TO_UM); |
GGU | 15:cfe79ebfcb25 | 338 | |
GGU | 16:017874772ea7 | 339 | Speed_A=(Distance_A*1000)/INTERRUPT_TIME;//走行速度演算[mm/s] |
GGU | 16:017874772ea7 | 340 | Speed_B=(Distance_B*1000)/INTERRUPT_TIME; |
yusaku0125 | 10:e1eb10665472 | 341 | |
yusaku0125 | 10:e1eb10665472 | 342 | if(Machine_Status&STOP){//機体停止状態の時 |
yusaku0125 | 12:dc4c569248d7 | 343 | Enc_A_Rotate+=Enc_Count_A;//閾値用に左エンコーダ値の蓄積 |
yusaku0125 | 12:dc4c569248d7 | 344 | if(Enc_A_Rotate<-6400)Enc_A_Rotate=-6400; |
yusaku0125 | 12:dc4c569248d7 | 345 | if(Enc_A_Rotate>6400)Enc_A_Rotate=6400; |
yusaku0125 | 12:dc4c569248d7 | 346 | Enc_B_Rotate+=Enc_Count_B;//速度用に右エンコーダ値の蓄積 |
yusaku0125 | 11:2cd6f8be124e | 347 | if(Enc_B_Rotate<-6400)Enc_B_Rotate=-6400; |
yusaku0125 | 10:e1eb10665472 | 348 | if(Enc_B_Rotate>6400)Enc_B_Rotate=6400; |
GGU | 16:017874772ea7 | 349 | if(Stop_Distance<0)Stop_Distance=0; |
GGU | 16:017874772ea7 | 350 | if(Stop_Distance>STOP_DISTANCE)Stop_Distance=STOP_DISTANCE; |
yusaku0125 | 10:e1eb10665472 | 351 | } |
yusaku0125 | 7:cfbf8d4a4d36 | 352 | if(Marker_Pass_Flag==1){//マーカ通過中は通過距離情報を蓄積する。 |
yusaku0125 | 7:cfbf8d4a4d36 | 353 | Marker_Run_Distance+=(Distance_A+Distance_B)/2; |
yusaku0125 | 7:cfbf8d4a4d36 | 354 | } |
yusaku0125 | 8:15b4e1d7a2c5 | 355 | //エンコーダ関連情報のリセット |
GGU | 17:b29e2c88b3c5 | 356 | Distance_A=0; |
GGU | 17:b29e2c88b3c5 | 357 | Distance_B=0; |
GGU | 16:017874772ea7 | 358 | |
yusaku0125 | 3:e455433c8cae | 359 | encoder_a.Set(0); |
yusaku0125 | 4:ac9e6772ddb3 | 360 | encoder_b.Set(0); |
yusaku0125 | 4:ac9e6772ddb3 | 361 | |
GGU | 15:cfe79ebfcb25 | 362 | memory_A=Distance_memory_A; |
GGU | 15:cfe79ebfcb25 | 363 | memory_B=Distance_memory_B; |
GGU | 15:cfe79ebfcb25 | 364 | |
yusaku0125 | 4:ac9e6772ddb3 | 365 | /////各モータの目標速度の設定 |
GGU | 16:017874772ea7 | 366 | if(Sw==0){ |
GGU | 21:97cc65e61580 | 367 | Target_Speed_A=Medium_Speed; |
GGU | 21:97cc65e61580 | 368 | Target_Speed_B=Medium_Speed; |
GGU | 21:97cc65e61580 | 369 | |
poritekutama | 14:7ed78f52f40e | 370 | Motor_A_Diff[1]=Motor_A_Diff[0]; |
poritekutama | 14:7ed78f52f40e | 371 | Motor_B_Diff[1]=Motor_B_Diff[0]; |
GGU | 21:97cc65e61580 | 372 | |
poritekutama | 14:7ed78f52f40e | 373 | Motor_A_Diff[0]=(Target_Speed_A-Speed_A); |
poritekutama | 14:7ed78f52f40e | 374 | Motor_B_Diff[0]=(Target_Speed_B-Speed_B); |
poritekutama | 14:7ed78f52f40e | 375 | } |
GGU | 22:b40f7d0c0f12 | 376 | else if(Sw==1) { |
GGU | 22:b40f7d0c0f12 | 377 | Target_Speed_A=High_Speed; |
GGU | 22:b40f7d0c0f12 | 378 | Target_Speed_B=High_Speed; |
poritekutama | 14:7ed78f52f40e | 379 | |
poritekutama | 14:7ed78f52f40e | 380 | Motor_A_Diff[1]=Motor_A_Diff[0]; |
poritekutama | 14:7ed78f52f40e | 381 | Motor_B_Diff[1]=Motor_B_Diff[0]; |
GGU | 21:97cc65e61580 | 382 | |
GGU | 22:b40f7d0c0f12 | 383 | Motor_A_Diff[0]=(Target_Speed_A-Speed_A); |
GGU | 22:b40f7d0c0f12 | 384 | Motor_B_Diff[0]=(Target_Speed_B-Speed_B); |
poritekutama | 14:7ed78f52f40e | 385 | } |
GGU | 22:b40f7d0c0f12 | 386 | |
poritekutama | 19:edf765724d2d | 387 | |
poritekutama | 19:edf765724d2d | 388 | /* |
yusaku0125 | 4:ac9e6772ddb3 | 389 | /////モータの速度制御 |
yusaku0125 | 4:ac9e6772ddb3 | 390 | //過去の速度偏差を退避 |
GGU | 16:017874772ea7 | 391 | Motor_A_Diff[1]=Motor_A_Diff[0]; |
yusaku0125 | 6:afd8f0d02c8d | 392 | Motor_B_Diff[1]=Motor_B_Diff[0]; |
yusaku0125 | 4:ac9e6772ddb3 | 393 | //現在の速度偏差を取得。 |
yusaku0125 | 4:ac9e6772ddb3 | 394 | Motor_A_Diff[0]=(Target_Speed_A-Speed_A); |
yusaku0125 | 4:ac9e6772ddb3 | 395 | Motor_B_Diff[0]=(Target_Speed_B-Speed_B); |
poritekutama | 14:7ed78f52f40e | 396 | |
poritekutama | 14:7ed78f52f40e | 397 | Motor_A_Diff[0]=(Target_Speed_A1-Speed_A); |
poritekutama | 14:7ed78f52f40e | 398 | Motor_B_Diff[0]=(Target_Speed_B1-Speed_B); |
GGU | 20:3b35311f4576 | 399 | */ |
poritekutama | 14:7ed78f52f40e | 400 | |
poritekutama | 19:edf765724d2d | 401 | |
GGU | 16:017874772ea7 | 402 | //P成分演算 |
yusaku0125 | 4:ac9e6772ddb3 | 403 | Motor_A_P=Motor_A_Diff[0]*M_KP; |
yusaku0125 | 4:ac9e6772ddb3 | 404 | Motor_B_P=Motor_B_Diff[0]*M_KP; |
yusaku0125 | 4:ac9e6772ddb3 | 405 | //D成分演算 |
yusaku0125 | 4:ac9e6772ddb3 | 406 | Motor_A_D=(Motor_A_Diff[0]-Motor_A_Diff[1])*M_KD; |
yusaku0125 | 4:ac9e6772ddb3 | 407 | Motor_B_D=(Motor_B_Diff[0]-Motor_B_Diff[1])*M_KD; |
yusaku0125 | 8:15b4e1d7a2c5 | 408 | //モータ速度制御のPD合成 |
yusaku0125 | 8:15b4e1d7a2c5 | 409 | Motor_A_PD=Motor_A_P+Motor_A_D; |
yusaku0125 | 8:15b4e1d7a2c5 | 410 | Motor_B_PD=Motor_B_P+Motor_B_D; |
yusaku0125 | 8:15b4e1d7a2c5 | 411 | //最終的なモータ制御量の合成 |
yusaku0125 | 8:15b4e1d7a2c5 | 412 | Motor_A_Pwm=Motor_A_PD+Sensor_PD; |
yusaku0125 | 8:15b4e1d7a2c5 | 413 | Motor_B_Pwm=Motor_B_PD-Sensor_PD; |
yusaku0125 | 8:15b4e1d7a2c5 | 414 | |
GGU | 16:017874772ea7 | 415 | //モータ制御量の上限下限設定 |
yusaku0125 | 6:afd8f0d02c8d | 416 | if(Motor_A_Pwm>0.95f)Motor_A_Pwm=0.95f; |
yusaku0125 | 6:afd8f0d02c8d | 417 | else if(Motor_A_Pwm<-0.95)Motor_A_Pwm=-0.95f; |
yusaku0125 | 6:afd8f0d02c8d | 418 | if(Motor_B_Pwm>0.95f)Motor_B_Pwm=0.95f; |
yusaku0125 | 6:afd8f0d02c8d | 419 | else if(Motor_B_Pwm<-0.95f)Motor_B_Pwm=-0.95f; |
GGU | 20:3b35311f4576 | 420 | |
yusaku0125 | 8:15b4e1d7a2c5 | 421 | //モータへの出力 |
yusaku0125 | 6:afd8f0d02c8d | 422 | if(!(Machine_Status&STOP)){//マシンが停止状態でなければ |
yusaku0125 | 8:15b4e1d7a2c5 | 423 | if(Machine_Status&RUN_COURSE_LOUT){ //左端センサ振り切れた時 |
yusaku0125 | 8:15b4e1d7a2c5 | 424 | motor_a=-(-TURN_POWER); //左旋回 |
yusaku0125 | 6:afd8f0d02c8d | 425 | motor_b=-(TURN_POWER); |
yusaku0125 | 8:15b4e1d7a2c5 | 426 | }else if(Machine_Status&RUN_COURSE_ROUT){ //右端センサ振り切れた時 |
yusaku0125 | 8:15b4e1d7a2c5 | 427 | motor_a=-(TURN_POWER); //右旋回 |
yusaku0125 | 6:afd8f0d02c8d | 428 | motor_b=-(-TURN_POWER); |
yusaku0125 | 7:cfbf8d4a4d36 | 429 | }else if(Cross_Flag==1){//交差点通過中 |
yusaku0125 | 7:cfbf8d4a4d36 | 430 | motor_a=-0.3;//交差点なので控えめの速度で直進 |
yusaku0125 | 7:cfbf8d4a4d36 | 431 | motor_b=-0.3; |
yusaku0125 | 7:cfbf8d4a4d36 | 432 | } |
yusaku0125 | 7:cfbf8d4a4d36 | 433 | else{ |
yusaku0125 | 6:afd8f0d02c8d | 434 | motor_a=-Motor_A_Pwm; |
yusaku0125 | 6:afd8f0d02c8d | 435 | motor_b=-Motor_B_Pwm; |
yusaku0125 | 6:afd8f0d02c8d | 436 | } |
yusaku0125 | 6:afd8f0d02c8d | 437 | }else{//停止状態の時はモータへの出力は無効 |
yusaku0125 | 6:afd8f0d02c8d | 438 | motor_a=0; |
yusaku0125 | 6:afd8f0d02c8d | 439 | motor_b=0; |
GGU | 20:3b35311f4576 | 440 | } |
yusaku0125 | 3:e455433c8cae | 441 | } |
yusaku0125 | 3:e455433c8cae | 442 | |
yusaku0125 | 3:e455433c8cae | 443 | int main() { |
yusaku0125 | 8:15b4e1d7a2c5 | 444 | timer.attach_us(&timer_interrupt,INTERRUPT_TIME);//タイマ割り込みスタート |
yusaku0125 | 8:15b4e1d7a2c5 | 445 | lcd.cls();//表示クリア |
yusaku0125 | 8:15b4e1d7a2c5 | 446 | lcd.locate(0,0); |
yusaku0125 | 10:e1eb10665472 | 447 | lcd.print("STOP"); |
yusaku0125 | 10:e1eb10665472 | 448 | lcd.locate(0,1); |
GGU | 16:017874772ea7 | 449 | sprintf(Speed_Str,"%04d",Medium_Speed); |
GGU | 16:017874772ea7 | 450 | lcd.print(Speed_Str); |
GGU | 15:cfe79ebfcb25 | 451 | |
yusaku0125 | 3:e455433c8cae | 452 | while(1){ |
yusaku0125 | 8:15b4e1d7a2c5 | 453 | Old_Sw_Ptn=Sw_Ptn; //過去のスイッチ入力情報を退避 |
yusaku0125 | 8:15b4e1d7a2c5 | 454 | Sw_Ptn=push_sw; //現在のスイッチ入力情報の取得 |
poritekutama | 14:7ed78f52f40e | 455 | if((!(Old_Machine_Status&STOP))&&(Machine_Status&STOP)){//走行終了時 |
GGU | 16:017874772ea7 | 456 | //sprintf(MemoryA_Str,"%d",memory_A); |
GGU | 16:017874772ea7 | 457 | //sprintf(MemoryB_Str,"%d",memory_B); |
yusaku0125 | 10:e1eb10665472 | 458 | lcd.locate(0,0); |
yusaku0125 | 10:e1eb10665472 | 459 | lcd.print(" "); |
yusaku0125 | 10:e1eb10665472 | 460 | lcd.locate(0,0); |
GGU | 16:017874772ea7 | 461 | lcd.print("STOP"); |
GGU | 15:cfe79ebfcb25 | 462 | |
GGU | 16:017874772ea7 | 463 | //lcd.locate(0,0); |
GGU | 16:017874772ea7 | 464 | //lcd.print(MemoryA_Str); |
GGU | 16:017874772ea7 | 465 | //lcd.locate(0,1); |
GGU | 16:017874772ea7 | 466 | //lcd.print(MemoryB_Str); |
GGU | 16:017874772ea7 | 467 | wait(5); |
GGU | 16:017874772ea7 | 468 | Gray=DEFAULT_GRAY; |
GGU | 22:b40f7d0c0f12 | 469 | Medium_Speed=DEFAULT_SPEED1; |
GGU | 16:017874772ea7 | 470 | Sw++; |
GGU | 22:b40f7d0c0f12 | 471 | Coner_c=0; |
yusaku0125 | 10:e1eb10665472 | 472 | } |
poritekutama | 14:7ed78f52f40e | 473 | |
yusaku0125 | 10:e1eb10665472 | 474 | if(Machine_Status&STOP){//機体停止状態の時 |
yusaku0125 | 12:dc4c569248d7 | 475 | |
yusaku0125 | 12:dc4c569248d7 | 476 | Gray=DEFAULT_GRAY+((float)Enc_A_Rotate/16000);//センサ閾値調整 |
GGU | 16:017874772ea7 | 477 | sprintf(Gray_Str,"%3.2f",Gray);//速度情報文字列変換 |
GGU | 16:017874772ea7 | 478 | sprintf(Coner_str,"%d",Coner_c); |
GGU | 20:3b35311f4576 | 479 | |
GGU | 20:3b35311f4576 | 480 | lcd.locate(0,0); |
GGU | 16:017874772ea7 | 481 | lcd.print(Gray_Str); |
GGU | 16:017874772ea7 | 482 | lcd.print(" "); |
GGU | 20:3b35311f4576 | 483 | lcd.print(Coner_str); |
GGU | 20:3b35311f4576 | 484 | |
GGU | 20:3b35311f4576 | 485 | lcd.locate(0,1); |
GGU | 20:3b35311f4576 | 486 | lcd.print(" "); |
GGU | 20:3b35311f4576 | 487 | lcd.locate(0,1); |
GGU | 20:3b35311f4576 | 488 | lcd.print(Speed_Str); |
poritekutama | 14:7ed78f52f40e | 489 | if(Sw==0){ |
GGU | 16:017874772ea7 | 490 | Medium_Speed=DEFAULT_SPEED1+(Enc_B_Rotate/16);//標準速度調整 |
GGU | 16:017874772ea7 | 491 | sprintf(Speed_Str,"%04d",Medium_Speed);//速度情報文字列変換 |
GGU | 22:b40f7d0c0f12 | 492 | } |
GGU | 22:b40f7d0c0f12 | 493 | else if(Sw==1) { |
GGU | 16:017874772ea7 | 494 | High_Speed=DEFAULT_SPEED2+(Enc_B_Rotate/16); |
GGU | 16:017874772ea7 | 495 | sprintf(Speed_Str,"%04d",High_Speed);//速度情報文字列変換 |
GGU | 22:b40f7d0c0f12 | 496 | } |
GGU | 22:b40f7d0c0f12 | 497 | else if(Sw==2) |
GGU | 22:b40f7d0c0f12 | 498 | High1_Speed=DEFAULT_SPEED3+(Enc_B_Rotate/16); |
GGU | 22:b40f7d0c0f12 | 499 | sprintf(Speed_Str,"%04d",High1_Speed);//速度情報文字列変換 |
yusaku0125 | 10:e1eb10665472 | 500 | } |
yusaku0125 | 10:e1eb10665472 | 501 | |
yusaku0125 | 8:15b4e1d7a2c5 | 502 | |
yusaku0125 | 8:15b4e1d7a2c5 | 503 | if((Old_Sw_Ptn==PULL)&&(Sw_Ptn==PUSH)){//スイッチが押された瞬間 |
yusaku0125 | 10:e1eb10665472 | 504 | if(Machine_Status&STOP){//機体停止状態の時 |
poritekutama | 14:7ed78f52f40e | 505 | Coner_c=0; |
yusaku0125 | 10:e1eb10665472 | 506 | lcd.locate(0,0); |
yusaku0125 | 10:e1eb10665472 | 507 | lcd.print(" "); |
yusaku0125 | 8:15b4e1d7a2c5 | 508 | lcd.locate(0,0); |
yusaku0125 | 10:e1eb10665472 | 509 | lcd.print("GO!!"); |
yusaku0125 | 10:e1eb10665472 | 510 | wait(2); |
GGU | 16:017874772ea7 | 511 | Stop_Distance=0; |
GGU | 20:3b35311f4576 | 512 | Machine_Status&=0x7F;//ストップ状態解除 |
GGU | 21:97cc65e61580 | 513 | //Target_Speed_A=Medium_Speed; |
GGU | 21:97cc65e61580 | 514 | //Target_Speed_B=Medium_Speed; |
GGU | 20:3b35311f4576 | 515 | |
GGU | 20:3b35311f4576 | 516 | |
yusaku0125 | 10:e1eb10665472 | 517 | }else{//機体走行中であったとき |
yusaku0125 | 10:e1eb10665472 | 518 | //各種フラグのクリア |
yusaku0125 | 10:e1eb10665472 | 519 | Corner_Flag=0; |
yusaku0125 | 10:e1eb10665472 | 520 | SG_Flag=0; |
yusaku0125 | 10:e1eb10665472 | 521 | Cross_Flag=0; |
yusaku0125 | 10:e1eb10665472 | 522 | Marker_Run_Distance=0;//マーカ通過距離情報リセット |
yusaku0125 | 10:e1eb10665472 | 523 | |
yusaku0125 | 10:e1eb10665472 | 524 | Machine_Status |= STOP;//機体停止状態にする。 |
yusaku0125 | 8:15b4e1d7a2c5 | 525 | lcd.locate(0,0); |
yusaku0125 | 10:e1eb10665472 | 526 | lcd.print(" "); |
yusaku0125 | 8:15b4e1d7a2c5 | 527 | lcd.locate(0,0); |
yusaku0125 | 10:e1eb10665472 | 528 | lcd.print("STOP"); |
yusaku0125 | 10:e1eb10665472 | 529 | } |
yusaku0125 | 9:1c28fcc1e9b8 | 530 | } |
yusaku0125 | 0:df99e50ed3fd | 531 | } |
yusaku0125 | 3:e455433c8cae | 532 | } |