Dependencies:   PID Serialservo TCS3200 XQ_servo encoder encoder2 mbed moter

Fork of nhk_2018_undercarry_test08 by ケンタ ミヤザキ

Committer:
kenken0721
Date:
Wed Mar 28 06:49:41 2018 +0000
Revision:
4:eabb7a738ff8
Parent:
3:aaa2fde4fafd
Child:
5:c5a2a5cf600d

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenken0721 0:f6828b914b1c 1 #include "mbed.h"
kenken0721 0:f6828b914b1c 2 #include "config.h"
kenken0721 0:f6828b914b1c 3 #include "moter.h"
kenken0721 0:f6828b914b1c 4 #include "PID.h"
kenken0721 4:eabb7a738ff8 5 #include "TCS3200.h"
kenken0721 1:f4dbd6b9cc27 6 #include "encoder.h"
kenken0721 4:eabb7a738ff8 7 #include "encoder2.h"
kenken0721 4:eabb7a738ff8 8 #include "Serialservo.h"
kenken0721 4:eabb7a738ff8 9 #include "XQ_servo.h"
kenken0721 0:f6828b914b1c 10
kenken0721 0:f6828b914b1c 11 Serial pc(USBTX, USBRX);
kenken0721 0:f6828b914b1c 12 Serial kbtpc(PC_12 ,PD_2);//serial5
kenken0721 0:f6828b914b1c 13 I2C master(sda, scl);
kenken0721 4:eabb7a738ff8 14 DigitalOut led2(LED2);//チェック用
kenken0721 0:f6828b914b1c 15 PID S_pid(SP, SI, SD, 0.0);
kenken0721 2:c5996dd62e9c 16 PID R_pid(RP, RI, RD, 0.0);
kenken0721 2:c5996dd62e9c 17 PID L_pid(LP, LI, LD, 0.0);
kenken0721 4:eabb7a738ff8 18
kenken0721 4:eabb7a738ff8 19 //DigitalIn zpin(PA_1);
kenken0721 4:eabb7a738ff8 20
kenken0721 2:c5996dd62e9c 21 Encoder Enc_Carry_Y(PA_15,PA_14,PC_1);
kenken0721 2:c5996dd62e9c 22 Encoder Enc_Carry_X(PA_4,PB_0,PB_7);
kenken0721 4:eabb7a738ff8 23 Encoder2 Enc_Arm(PF_1,PA_0,PA_1);
kenken0721 4:eabb7a738ff8 24
kenken0721 3:aaa2fde4fafd 25 DigitalOut buzzer(PA_10);
kenken0721 3:aaa2fde4fafd 26 DigitalOut lamp(PC_3);
kenken0721 3:aaa2fde4fafd 27 Timer timer;
kenken0721 4:eabb7a738ff8 28 Serialservo servo1(PC_10, PC_11);//アームの肘
kenken0721 4:eabb7a738ff8 29 Serialservo servo2(PC_10, PC_11);//回転軸
kenken0721 4:eabb7a738ff8 30 XQ_servo servo_hand(PB_5);
kenken0721 4:eabb7a738ff8 31 //TCS3200 color(PB_5, PB_2, PC_4);
kenken0721 4:eabb7a738ff8 32 DigitalOut dpin(PC_9);
kenken0721 4:eabb7a738ff8 33 DigitalIn distance(PC_2);
kenken0721 4:eabb7a738ff8 34 DigitalIn redIn(PC_4);
kenken0721 4:eabb7a738ff8 35 DigitalIn blueIn(PB_2);
kenken0721 4:eabb7a738ff8 36 DigitalIn micro_switch(PC_0);
kenken0721 4:eabb7a738ff8 37 DigitalOut air(PB_4);
kenken0721 4:eabb7a738ff8 38
kenken0721 4:eabb7a738ff8 39 DigitalOut motor_dir1(PA_8); //旧ピン番号
kenken0721 4:eabb7a738ff8 40 DigitalOut motor_dir2(PA_9);
kenken0721 4:eabb7a738ff8 41 PwmOut motor_pwm(PB_3);
kenken0721 4:eabb7a738ff8 42
kenken0721 4:eabb7a738ff8 43 /*
kenken0721 4:eabb7a738ff8 44 PwmOut motor_pwm(PA_9);
kenken0721 4:eabb7a738ff8 45 DigitalOut motor_dir1(PB_3);
kenken0721 4:eabb7a738ff8 46 DigitalOut motor_dir2(PA_8);
kenken0721 4:eabb7a738ff8 47 */
kenken0721 4:eabb7a738ff8 48 //DigitalOut motor_relay(PB_10);
kenken0721 4:eabb7a738ff8 49
kenken0721 2:c5996dd62e9c 50 void linecheck(char *buff ,int data[2]);
kenken0721 2:c5996dd62e9c 51 void LineCheck(int dmode);
kenken0721 4:eabb7a738ff8 52 void syokikanomai(void);
kenken0721 4:eabb7a738ff8 53 void syokikanomai2(void);
kenken0721 4:eabb7a738ff8 54 void pre_touteki(void);
kenken0721 4:eabb7a738ff8 55 void touteki(float armpower, int pos, bool thro_mode);
kenken0721 0:f6828b914b1c 56
kenken0721 0:f6828b914b1c 57 PwmOut pwm_pins[] = {
kenken0721 0:f6828b914b1c 58 PwmOut( PC_8 ),
kenken0721 0:f6828b914b1c 59 PwmOut( PC_6 ),
kenken0721 0:f6828b914b1c 60 PwmOut( PB_14 ),
kenken0721 0:f6828b914b1c 61 PwmOut( PB_13 )
kenken0721 0:f6828b914b1c 62 };
kenken0721 0:f6828b914b1c 63
kenken0721 0:f6828b914b1c 64 DigitalOut dir_pins_1[] = {
kenken0721 0:f6828b914b1c 65 DigitalOut( PC_5 ),
kenken0721 0:f6828b914b1c 66 DigitalOut( PA_7 ),
kenken0721 2:c5996dd62e9c 67 DigitalOut( PB_6 ),
kenken0721 2:c5996dd62e9c 68 DigitalOut( PB_1)
kenken0721 0:f6828b914b1c 69 };
kenken0721 0:f6828b914b1c 70
kenken0721 0:f6828b914b1c 71 DigitalOut dir_pins_2[] = {
kenken0721 0:f6828b914b1c 72 DigitalOut( PB_12 ),
kenken0721 0:f6828b914b1c 73 DigitalOut( PA_6 ),
kenken0721 2:c5996dd62e9c 74 DigitalOut( PC_7 ),
kenken0721 2:c5996dd62e9c 75 DigitalOut( PB_15)
kenken0721 0:f6828b914b1c 76 };
kenken0721 0:f6828b914b1c 77
kenken0721 2:c5996dd62e9c 78 int crosscount = 0;
kenken0721 0:f6828b914b1c 79 bool mode = true;//trueでラジコン、falseでオート
kenken0721 0:f6828b914b1c 80 bool stopflag = true;//trueで機体停止
kenken0721 0:f6828b914b1c 81 double power = POWER;
kenken0721 2:c5996dd62e9c 82 double rpower = POWER;
kenken0721 2:c5996dd62e9c 83 double lpower = POWER;
kenken0721 0:f6828b914b1c 84 int kbtread = 0;
kenken0721 0:f6828b914b1c 85 int count = 0;
kenken0721 0:f6828b914b1c 86 int counter = 0;
kenken0721 2:c5996dd62e9c 87 int linedata_1[2] = {0};
kenken0721 2:c5996dd62e9c 88 int linedata_2[2] = {0};
kenken0721 2:c5996dd62e9c 89 int linedata_3[2] = {0};
kenken0721 0:f6828b914b1c 90 int prelinedata = 0;
kenken0721 0:f6828b914b1c 91 double output = 0.0;
kenken0721 1:f4dbd6b9cc27 92 char buff1[2];
kenken0721 1:f4dbd6b9cc27 93 char buff2[2];
kenken0721 1:f4dbd6b9cc27 94 char buff3[2];
kenken0721 1:f4dbd6b9cc27 95 float x_dist = 0.0;
kenken0721 1:f4dbd6b9cc27 96 float y_dist = 0.0;
kenken0721 2:c5996dd62e9c 97 float tar_x_dist = 0.0;
kenken0721 2:c5996dd62e9c 98 float tar_y_dist = 0.0;
kenken0721 2:c5996dd62e9c 99 bool ontheline = false;
kenken0721 2:c5996dd62e9c 100 int directmode = STRAIGHT;
kenken0721 3:aaa2fde4fafd 101 int state = START;
kenken0721 3:aaa2fde4fafd 102 int times = 0;
kenken0721 3:aaa2fde4fafd 103 bool throwing = false;
kenken0721 4:eabb7a738ff8 104 int receive = NOT_RECEIVE;
kenken0721 4:eabb7a738ff8 105 int redcount = 0;
kenken0721 4:eabb7a738ff8 106 int bluecount = 0;
kenken0721 4:eabb7a738ff8 107 int yellowcount = 0;
kenken0721 4:eabb7a738ff8 108 bool syokika_flag=false;
kenken0721 4:eabb7a738ff8 109 bool set_flag = false;
kenken0721 4:eabb7a738ff8 110 int encoder_count = 1000;
kenken0721 4:eabb7a738ff8 111
kenken0721 3:aaa2fde4fafd 112 int main() {
kenken0721 3:aaa2fde4fafd 113 timer.reset();
kenken0721 3:aaa2fde4fafd 114 timer.stop();
kenken0721 4:eabb7a738ff8 115 servo1.init(0);//アーム
kenken0721 4:eabb7a738ff8 116 servo2.init(1);//回転軸
kenken0721 4:eabb7a738ff8 117 //servo2.move(50);
kenken0721 4:eabb7a738ff8 118 //servo1.move(152);
kenken0721 4:eabb7a738ff8 119 dpin=0;
kenken0721 4:eabb7a738ff8 120 lamp=1;
kenken0721 4:eabb7a738ff8 121 air=1.0;
kenken0721 4:eabb7a738ff8 122 buzzer = 0.0;
kenken0721 4:eabb7a738ff8 123 //motor_relay = 0.0;
kenken0721 4:eabb7a738ff8 124 micro_switch.mode(PullUp);
kenken0721 4:eabb7a738ff8 125
kenken0721 4:eabb7a738ff8 126 motor_pwm=0;
kenken0721 4:eabb7a738ff8 127 motor_dir1=1; //dir1,dir2はこの組み合わせじゃないと死ぬ
kenken0721 4:eabb7a738ff8 128 motor_dir2=1;
kenken0721 4:eabb7a738ff8 129
kenken0721 1:f4dbd6b9cc27 130 S_pid.init();
kenken0721 3:aaa2fde4fafd 131 R_pid.init();
kenken0721 3:aaa2fde4fafd 132 L_pid.init();
kenken0721 1:f4dbd6b9cc27 133 pwm_pins[0].period(0.00005);
kenken0721 1:f4dbd6b9cc27 134 pwm_pins[1].period(0.00005);
kenken0721 1:f4dbd6b9cc27 135 pwm_pins[2].period(0.00005);
kenken0721 1:f4dbd6b9cc27 136 pwm_pins[3].period(0.00005);
kenken0721 4:eabb7a738ff8 137 motor_pwm.period(0.00005);
kenken0721 3:aaa2fde4fafd 138 pc.baud(115200);
kenken0721 2:c5996dd62e9c 139 kbtpc.baud(9600);
kenken0721 2:c5996dd62e9c 140 master.frequency(100000);
kenken0721 4:eabb7a738ff8 141 lamp = 1.0;
kenken0721 4:eabb7a738ff8 142 //-------初期化----------------------------------------
kenken0721 4:eabb7a738ff8 143 syokikanomai();
kenken0721 4:eabb7a738ff8 144 buzzer = 0;
kenken0721 4:eabb7a738ff8 145 //-----------メインループ----------------------------------------め
kenken0721 1:f4dbd6b9cc27 146 while (true) {
kenken0721 4:eabb7a738ff8 147 master.read(addr1,buff1,2);
kenken0721 4:eabb7a738ff8 148 linecheck(buff1,linedata_1);
kenken0721 4:eabb7a738ff8 149 master.read(addr2,buff2,2);
kenken0721 4:eabb7a738ff8 150 linecheck(buff2,linedata_2);
kenken0721 4:eabb7a738ff8 151 master.read(addr3,buff3,2);
kenken0721 4:eabb7a738ff8 152 linecheck(buff3,linedata_3);
kenken0721 2:c5996dd62e9c 153 x_dist = Enc_Carry_X.read_rotate();
kenken0721 2:c5996dd62e9c 154 y_dist = Enc_Carry_Y.read_rotate();
kenken0721 3:aaa2fde4fafd 155 kbtpc.putc(state);
kenken0721 1:f4dbd6b9cc27 156 if(kbtpc.readable()){
kenken0721 1:f4dbd6b9cc27 157 kbtread = kbtpc.getc();
kenken0721 1:f4dbd6b9cc27 158 }
kenken0721 1:f4dbd6b9cc27 159 if(kbtread == 255){
kenken0721 1:f4dbd6b9cc27 160 mode = true;
kenken0721 1:f4dbd6b9cc27 161 }else if(kbtread == 150){
kenken0721 1:f4dbd6b9cc27 162 mode = false;
kenken0721 1:f4dbd6b9cc27 163 }else if(kbtread == 100){
kenken0721 1:f4dbd6b9cc27 164 stopflag = true;
kenken0721 1:f4dbd6b9cc27 165 }else if(kbtread == 200){
kenken0721 1:f4dbd6b9cc27 166 stopflag = false;
kenken0721 2:c5996dd62e9c 167 }else if(kbtread == STRAIGHT){
kenken0721 2:c5996dd62e9c 168 directmode = STRAIGHT;
kenken0721 2:c5996dd62e9c 169 }else if(kbtread == RIGHT){
kenken0721 2:c5996dd62e9c 170 directmode = RIGHT;
kenken0721 2:c5996dd62e9c 171 }else if(kbtread == LEFT){
kenken0721 2:c5996dd62e9c 172 directmode = LEFT;
kenken0721 3:aaa2fde4fafd 173 }else if(kbtread == MANUAL){
kenken0721 3:aaa2fde4fafd 174 state = MANUAL;
kenken0721 3:aaa2fde4fafd 175 }else if(kbtread == START){
kenken0721 3:aaa2fde4fafd 176 state = START;
kenken0721 4:eabb7a738ff8 177 }else if(kbtread == START_TEST){
kenken0721 4:eabb7a738ff8 178 state = START_TEST;
kenken0721 1:f4dbd6b9cc27 179 }
kenken0721 1:f4dbd6b9cc27 180 //--------------手動--------------------------------------
kenken0721 1:f4dbd6b9cc27 181 if(mode == true){
kenken0721 1:f4dbd6b9cc27 182 led2 = 1.0;
kenken0721 2:c5996dd62e9c 183 if(kbtread == STRAIGHT){
kenken0721 1:f4dbd6b9cc27 184 Straight(power, power, pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 2:c5996dd62e9c 185 }else if(kbtread == BACK){
kenken0721 1:f4dbd6b9cc27 186 Back(power, power, pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 2:c5996dd62e9c 187 }else if(kbtread == RIGHT){
kenken0721 1:f4dbd6b9cc27 188 Right(power, power, pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 2:c5996dd62e9c 189 }else if(kbtread == LEFT){
kenken0721 1:f4dbd6b9cc27 190 Left(power, power, pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 1:f4dbd6b9cc27 191 }else{
kenken0721 1:f4dbd6b9cc27 192 Stop(pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 1:f4dbd6b9cc27 193 }
kenken0721 1:f4dbd6b9cc27 194 //--------------自動---------------------------------
kenken0721 1:f4dbd6b9cc27 195 }else if(mode == false){
kenken0721 1:f4dbd6b9cc27 196 led2 = 0.0;
kenken0721 1:f4dbd6b9cc27 197 if(stopflag == true){
kenken0721 1:f4dbd6b9cc27 198 Stop(pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 1:f4dbd6b9cc27 199 }else{
kenken0721 2:c5996dd62e9c 200 LineCheck(directmode);
kenken0721 3:aaa2fde4fafd 201 switch(state){
kenken0721 4:eabb7a738ff8 202 //------------------任意に進行方向を決めれる----------------------------------------
kenken0721 3:aaa2fde4fafd 203 case MANUAL:
kenken0721 3:aaa2fde4fafd 204 if(crosscount == 1){
kenken0721 3:aaa2fde4fafd 205 directmode = STOP;
kenken0721 3:aaa2fde4fafd 206 Stop(pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 3:aaa2fde4fafd 207 crosscount = 0;
kenken0721 3:aaa2fde4fafd 208 }
kenken0721 3:aaa2fde4fafd 209 break;
kenken0721 4:eabb7a738ff8 210 //------------------テスト用--------------------------------------------------------
kenken0721 4:eabb7a738ff8 211 case START_TEST:
kenken0721 4:eabb7a738ff8 212 if(crosscount == 0){
kenken0721 4:eabb7a738ff8 213 directmode = RIGHT;
kenken0721 4:eabb7a738ff8 214 }else if(crosscount == 1){
kenken0721 4:eabb7a738ff8 215 directmode = STOP;
kenken0721 4:eabb7a738ff8 216 state = STAND_BY_TEST;
kenken0721 4:eabb7a738ff8 217 crosscount = 0;
kenken0721 4:eabb7a738ff8 218 prelinedata = 0;
kenken0721 4:eabb7a738ff8 219 }
kenken0721 4:eabb7a738ff8 220 break;
kenken0721 4:eabb7a738ff8 221 //------------------テスト用待機位置-----------------------------------------------
kenken0721 4:eabb7a738ff8 222 case STAND_BY_TEST:
kenken0721 4:eabb7a738ff8 223 directmode = STOP;
kenken0721 4:eabb7a738ff8 224 if(distance == 1.0){
kenken0721 4:eabb7a738ff8 225 led2 = 1.0;
kenken0721 4:eabb7a738ff8 226 if(redIn == 1.0){
kenken0721 4:eabb7a738ff8 227 buzzer = 1.0;
kenken0721 4:eabb7a738ff8 228 redcount++;
kenken0721 4:eabb7a738ff8 229 }else if(blueIn == 1.0){
kenken0721 4:eabb7a738ff8 230 buzzer = 1.0;
kenken0721 4:eabb7a738ff8 231 bluecount++;
kenken0721 4:eabb7a738ff8 232 }else{
kenken0721 4:eabb7a738ff8 233 buzzer = 0.0;
kenken0721 4:eabb7a738ff8 234 }
kenken0721 4:eabb7a738ff8 235 if(redcount >= 50){
kenken0721 4:eabb7a738ff8 236 servo_hand.move(hand_tsukami_degree);
kenken0721 4:eabb7a738ff8 237 //pre_touteki();
kenken0721 4:eabb7a738ff8 238 }else if(bluecount >= 50){
kenken0721 4:eabb7a738ff8 239 servo_hand.move(hand_tsukami_degree);
kenken0721 4:eabb7a738ff8 240 //pre_touteki();
kenken0721 4:eabb7a738ff8 241 }
kenken0721 4:eabb7a738ff8 242 }else if(distance == 0.0 && redcount >= 50){
kenken0721 4:eabb7a738ff8 243 servo2.move(koshi_nage_degree);
kenken0721 4:eabb7a738ff8 244 state = THROWING_TEST;
kenken0721 4:eabb7a738ff8 245 directmode = STOP;
kenken0721 4:eabb7a738ff8 246 led2 = 0.0;
kenken0721 4:eabb7a738ff8 247 redcount = 0;
kenken0721 4:eabb7a738ff8 248 crosscount = 0;
kenken0721 4:eabb7a738ff8 249 }else if(distance == 0.0 && bluecount >= 50){
kenken0721 4:eabb7a738ff8 250 servo2.move(koshi_nage_degree);
kenken0721 4:eabb7a738ff8 251 state = THROWING_TEST;
kenken0721 4:eabb7a738ff8 252 directmode = STOP;
kenken0721 4:eabb7a738ff8 253 bluecount = 0;
kenken0721 4:eabb7a738ff8 254 led2 = 0.0;
kenken0721 4:eabb7a738ff8 255 crosscount = 0;
kenken0721 4:eabb7a738ff8 256 }
kenken0721 4:eabb7a738ff8 257 break;
kenken0721 4:eabb7a738ff8 258 //------------------------------------------------------------------------------
kenken0721 4:eabb7a738ff8 259 case THROWING_TEST:
kenken0721 4:eabb7a738ff8 260 if(crosscount == 0){
kenken0721 4:eabb7a738ff8 261 directmode = LEFT;
kenken0721 4:eabb7a738ff8 262 }else if(crosscount == 1){
kenken0721 4:eabb7a738ff8 263 directmode = STOP;
kenken0721 4:eabb7a738ff8 264 touteki(0.8, 190, 0);
kenken0721 4:eabb7a738ff8 265 //syokikanomai();
kenken0721 4:eabb7a738ff8 266 buzzer = 0.0;
kenken0721 4:eabb7a738ff8 267 }
kenken0721 4:eabb7a738ff8 268 break;
kenken0721 4:eabb7a738ff8 269 //------------------初期位置から待機位置1------------------------------------------
kenken0721 3:aaa2fde4fafd 270 case START:
kenken0721 3:aaa2fde4fafd 271 if(crosscount == 0){
kenken0721 3:aaa2fde4fafd 272 directmode = STRAIGHT;
kenken0721 3:aaa2fde4fafd 273 }else if(crosscount == 1 && directmode == STRAIGHT){
kenken0721 3:aaa2fde4fafd 274 directmode = LEFT;
kenken0721 3:aaa2fde4fafd 275 prelinedata = 0;
kenken0721 3:aaa2fde4fafd 276 }else if(crosscount == 1 && directmode == LEFT){
kenken0721 3:aaa2fde4fafd 277 directmode = LEFT;
kenken0721 3:aaa2fde4fafd 278 }else if(crosscount == 2){
kenken0721 3:aaa2fde4fafd 279 directmode = STOP;
kenken0721 3:aaa2fde4fafd 280 state = STAND_BY_1;
kenken0721 3:aaa2fde4fafd 281 crosscount = 0;
kenken0721 3:aaa2fde4fafd 282 prelinedata = 0;
kenken0721 3:aaa2fde4fafd 283 }
kenken0721 3:aaa2fde4fafd 284 break;
kenken0721 4:eabb7a738ff8 285 //------------------待機位置1----------------------------------------------------
kenken0721 3:aaa2fde4fafd 286 case STAND_BY_1:
kenken0721 3:aaa2fde4fafd 287 crosscount = 0;
kenken0721 3:aaa2fde4fafd 288 directmode = STOP;
kenken0721 4:eabb7a738ff8 289 if(distance == 1.0){
kenken0721 4:eabb7a738ff8 290 if(redIn == 1.0){
kenken0721 4:eabb7a738ff8 291 buzzer = 1.0;
kenken0721 4:eabb7a738ff8 292 redcount++;
kenken0721 4:eabb7a738ff8 293 }else if(blueIn == 1.0){
kenken0721 4:eabb7a738ff8 294 buzzer = 1.0;
kenken0721 4:eabb7a738ff8 295 bluecount++;
kenken0721 4:eabb7a738ff8 296 }else{
kenken0721 4:eabb7a738ff8 297 buzzer = 0.0;
kenken0721 4:eabb7a738ff8 298 }
kenken0721 4:eabb7a738ff8 299 if(redcount >= 100 ){
kenken0721 4:eabb7a738ff8 300 servo_hand.move(hand_tsukami_degree);
kenken0721 4:eabb7a738ff8 301 servo1.move(hiji_tsukami_degree);
kenken0721 4:eabb7a738ff8 302 //pre_touteki();
kenken0721 4:eabb7a738ff8 303 }else if(bluecount >= 100){
kenken0721 4:eabb7a738ff8 304 servo_hand.move(hand_tsukami_degree);
kenken0721 4:eabb7a738ff8 305 servo1.move(hiji_tsukami_degree);
kenken0721 4:eabb7a738ff8 306 //pre_touteki();
kenken0721 4:eabb7a738ff8 307 }
kenken0721 4:eabb7a738ff8 308 }else if(distance == 0.0 && redcount >= 100){
kenken0721 4:eabb7a738ff8 309 servo2.move(koshi_nage_degree);
kenken0721 3:aaa2fde4fafd 310 state = RED_RECEIVE;
kenken0721 4:eabb7a738ff8 311 directmode = STOP;
kenken0721 4:eabb7a738ff8 312 redcount = 0;
kenken0721 4:eabb7a738ff8 313 crosscount = 0;
kenken0721 4:eabb7a738ff8 314 }else if(distance == 0.0 && bluecount >= 100){
kenken0721 4:eabb7a738ff8 315 state = BLUE_RECEIVE_1;
kenken0721 4:eabb7a738ff8 316 directmode = STOP;
kenken0721 4:eabb7a738ff8 317 bluecount = 0;
kenken0721 4:eabb7a738ff8 318 crosscount = 0;
kenken0721 4:eabb7a738ff8 319 }
kenken0721 4:eabb7a738ff8 320 /*
kenken0721 4:eabb7a738ff8 321 if(receive == RED_RECEIVE){
kenken0721 4:eabb7a738ff8 322 state = RED_RECEIVE;
kenken0721 4:eabb7a738ff8 323 }else if(receive == BLUE_RECEIVE){
kenken0721 3:aaa2fde4fafd 324 state = BLUE_RECEIVE_1;
kenken0721 3:aaa2fde4fafd 325 }
kenken0721 4:eabb7a738ff8 326 */
kenken0721 3:aaa2fde4fafd 327 break;
kenken0721 4:eabb7a738ff8 328 //------------------待機位置1から投てき位置1----------------------------------------
kenken0721 3:aaa2fde4fafd 329 case RED_RECEIVE:
kenken0721 3:aaa2fde4fafd 330 if(crosscount == 0){
kenken0721 3:aaa2fde4fafd 331 directmode = LEFT;
kenken0721 4:eabb7a738ff8 332 }else if(crosscount == 1){
kenken0721 3:aaa2fde4fafd 333 directmode = STOP;
kenken0721 3:aaa2fde4fafd 334 prelinedata = 0;
kenken0721 4:eabb7a738ff8 335 touteki(0.8,190,0);
kenken0721 4:eabb7a738ff8 336 if(set_flag == false){
kenken0721 3:aaa2fde4fafd 337 directmode = RIGHT;
kenken0721 3:aaa2fde4fafd 338 }
kenken0721 3:aaa2fde4fafd 339 }else if(crosscount == 2){
kenken0721 3:aaa2fde4fafd 340 directmode = STOP;
kenken0721 4:eabb7a738ff8 341 syokikanomai2();
kenken0721 4:eabb7a738ff8 342 if(set_flag == true){
kenken0721 4:eabb7a738ff8 343 state = STAND_BY_1;
kenken0721 4:eabb7a738ff8 344 //throwing = false;
kenken0721 4:eabb7a738ff8 345 crosscount = 0;
kenken0721 4:eabb7a738ff8 346 }
kenken0721 3:aaa2fde4fafd 347 }
kenken0721 3:aaa2fde4fafd 348 break;
kenken0721 4:eabb7a738ff8 349 //------------------待機位置1から投てき位置2----------------------------------------
kenken0721 3:aaa2fde4fafd 350 case BLUE_RECEIVE_1:
kenken0721 3:aaa2fde4fafd 351 if(crosscount == 0){
kenken0721 3:aaa2fde4fafd 352 directmode = RIGHT;
kenken0721 3:aaa2fde4fafd 353 }else if(crosscount == 1){
kenken0721 3:aaa2fde4fafd 354 directmode = STRAIGHT;
kenken0721 3:aaa2fde4fafd 355 }else if(crosscount == 2){
kenken0721 3:aaa2fde4fafd 356 directmode = LEFT;
kenken0721 3:aaa2fde4fafd 357 }else if(crosscount == 3){
kenken0721 3:aaa2fde4fafd 358 directmode = STOP;
kenken0721 3:aaa2fde4fafd 359 prelinedata = 0;
kenken0721 4:eabb7a738ff8 360 state = BLUE_RECEIVE_2;
kenken0721 3:aaa2fde4fafd 361 crosscount = 0;
kenken0721 3:aaa2fde4fafd 362 }
kenken0721 3:aaa2fde4fafd 363 break;
kenken0721 4:eabb7a738ff8 364 //------------------待機位置2----------------------------------------------------
kenken0721 3:aaa2fde4fafd 365 case STAND_BY_2:
kenken0721 3:aaa2fde4fafd 366 crosscount = 0;
kenken0721 4:eabb7a738ff8 367 directmode = STOP;
kenken0721 4:eabb7a738ff8 368 if(distance == 1.0){
kenken0721 4:eabb7a738ff8 369 if(blueIn == 1.0){
kenken0721 4:eabb7a738ff8 370 buzzer = 1.0;
kenken0721 4:eabb7a738ff8 371 bluecount++;
kenken0721 4:eabb7a738ff8 372 }else if(micro_switch == 1.0){
kenken0721 4:eabb7a738ff8 373 buzzer = 1.0;
kenken0721 4:eabb7a738ff8 374 yellowcount++;
kenken0721 4:eabb7a738ff8 375 }else{
kenken0721 4:eabb7a738ff8 376 buzzer = 0.0;
kenken0721 4:eabb7a738ff8 377 }
kenken0721 4:eabb7a738ff8 378 if(bluecount >= 50){
kenken0721 4:eabb7a738ff8 379 pre_touteki();
kenken0721 4:eabb7a738ff8 380 }else if(yellowcount >= 500){
kenken0721 4:eabb7a738ff8 381 air = 1.0;
kenken0721 4:eabb7a738ff8 382 }
kenken0721 4:eabb7a738ff8 383 }else if(distance == 0.0 && bluecount >= 50){
kenken0721 4:eabb7a738ff8 384 state = BLUE_RECEIVE_2;
kenken0721 4:eabb7a738ff8 385 directmode = STOP;
kenken0721 4:eabb7a738ff8 386 bluecount = 0;
kenken0721 4:eabb7a738ff8 387 crosscount = 0;
kenken0721 4:eabb7a738ff8 388 }
kenken0721 4:eabb7a738ff8 389 /*
kenken0721 3:aaa2fde4fafd 390 if(kbtread == BLUE_RECEIVE){
kenken0721 3:aaa2fde4fafd 391 state = BLUE_RECEIVE_2;
kenken0721 3:aaa2fde4fafd 392 }else if(kbtread == YELLOW_RECEIVE){
kenken0721 3:aaa2fde4fafd 393 state = YELLOW_RECEIVE;
kenken0721 3:aaa2fde4fafd 394 }
kenken0721 4:eabb7a738ff8 395 */
kenken0721 3:aaa2fde4fafd 396 break;
kenken0721 4:eabb7a738ff8 397 //------------------投てき位置2---------------------------------------------------
kenken0721 3:aaa2fde4fafd 398 case BLUE_RECEIVE_2:
kenken0721 3:aaa2fde4fafd 399 if(crosscount == 0){
kenken0721 3:aaa2fde4fafd 400 directmode = LEFT;
kenken0721 3:aaa2fde4fafd 401 }else if(crosscount == 1){
kenken0721 3:aaa2fde4fafd 402 if(throwing == false){
kenken0721 3:aaa2fde4fafd 403 directmode = STOP;
kenken0721 3:aaa2fde4fafd 404 prelinedata = 0;
kenken0721 3:aaa2fde4fafd 405 if(kbtread == THROWING){
kenken0721 3:aaa2fde4fafd 406 throwing = true;
kenken0721 3:aaa2fde4fafd 407 buzzer = 1.0;
kenken0721 4:eabb7a738ff8 408 //timer.start();
kenken0721 3:aaa2fde4fafd 409 prelinedata = 0;
kenken0721 3:aaa2fde4fafd 410 }
kenken0721 3:aaa2fde4fafd 411 }else{
kenken0721 4:eabb7a738ff8 412 /*
kenken0721 3:aaa2fde4fafd 413 times = timer.read();
kenken0721 3:aaa2fde4fafd 414 if(times > 2){
kenken0721 3:aaa2fde4fafd 415 buzzer = 0.0;
kenken0721 3:aaa2fde4fafd 416 directmode = RIGHT;
kenken0721 3:aaa2fde4fafd 417 timer.reset();
kenken0721 3:aaa2fde4fafd 418 timer.stop();
kenken0721 3:aaa2fde4fafd 419 }
kenken0721 4:eabb7a738ff8 420 */
kenken0721 3:aaa2fde4fafd 421 }
kenken0721 3:aaa2fde4fafd 422 }else if(crosscount == 2){
kenken0721 3:aaa2fde4fafd 423 directmode = STOP;
kenken0721 3:aaa2fde4fafd 424 state = STAND_BY_2;
kenken0721 3:aaa2fde4fafd 425 throwing = false;
kenken0721 3:aaa2fde4fafd 426 crosscount = 0;
kenken0721 3:aaa2fde4fafd 427 }
kenken0721 3:aaa2fde4fafd 428 break;
kenken0721 4:eabb7a738ff8 429 //------------------投てき位置3---------------------------------------------------
kenken0721 3:aaa2fde4fafd 430 case YELLOW_RECEIVE:
kenken0721 3:aaa2fde4fafd 431 if(crosscount != 4){
kenken0721 3:aaa2fde4fafd 432 directmode = LEFT;
kenken0721 3:aaa2fde4fafd 433 }else{
kenken0721 3:aaa2fde4fafd 434 directmode = STOP;
kenken0721 3:aaa2fde4fafd 435 }
kenken0721 3:aaa2fde4fafd 436 break;
kenken0721 3:aaa2fde4fafd 437 default:
kenken0721 3:aaa2fde4fafd 438 break;
kenken0721 3:aaa2fde4fafd 439 }
kenken0721 4:eabb7a738ff8 440 //--------------進行方向---------------------------------------------------------
kenken0721 3:aaa2fde4fafd 441 switch(directmode){
kenken0721 2:c5996dd62e9c 442 case STOP:
kenken0721 2:c5996dd62e9c 443 Stop(pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 2:c5996dd62e9c 444 break;
kenken0721 2:c5996dd62e9c 445 case STRAIGHT:
kenken0721 2:c5996dd62e9c 446 output = S_pid.compute((double)linedata_1[0]);
kenken0721 2:c5996dd62e9c 447 if(output >= 0){
kenken0721 2:c5996dd62e9c 448 rpower = power;
kenken0721 2:c5996dd62e9c 449 lpower = power + output;
kenken0721 2:c5996dd62e9c 450 Straight(rpower, lpower, pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 2:c5996dd62e9c 451 }else {
kenken0721 2:c5996dd62e9c 452 rpower = power + (-1 * output);
kenken0721 2:c5996dd62e9c 453 lpower = power;
kenken0721 2:c5996dd62e9c 454 Straight(rpower, lpower, pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 2:c5996dd62e9c 455 }
kenken0721 2:c5996dd62e9c 456 break;
kenken0721 2:c5996dd62e9c 457 case RIGHT:
kenken0721 2:c5996dd62e9c 458 output = R_pid.compute((double)linedata_2[0]);
kenken0721 1:f4dbd6b9cc27 459 if(output >= 0){
kenken0721 2:c5996dd62e9c 460 rpower = power;
kenken0721 2:c5996dd62e9c 461 lpower = power + output;
kenken0721 2:c5996dd62e9c 462 Right(rpower, lpower, pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 1:f4dbd6b9cc27 463 }else {
kenken0721 2:c5996dd62e9c 464 rpower = power + (-1 * output);
kenken0721 2:c5996dd62e9c 465 lpower = power;
kenken0721 2:c5996dd62e9c 466 Right(rpower, lpower, pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 2:c5996dd62e9c 467 }
kenken0721 2:c5996dd62e9c 468 break;
kenken0721 2:c5996dd62e9c 469 case LEFT:
kenken0721 2:c5996dd62e9c 470 output = L_pid.compute((double)linedata_3[0]);
kenken0721 2:c5996dd62e9c 471 if(output >= 0){
kenken0721 2:c5996dd62e9c 472 rpower = power;
kenken0721 2:c5996dd62e9c 473 lpower = power + output;
kenken0721 3:aaa2fde4fafd 474 Left(rpower, lpower, pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 2:c5996dd62e9c 475 }else {
kenken0721 2:c5996dd62e9c 476 rpower = power + (-1 * output);
kenken0721 2:c5996dd62e9c 477 lpower = power;
kenken0721 2:c5996dd62e9c 478 Left(rpower, lpower, pwm_pins, dir_pins_1, dir_pins_2);
kenken0721 2:c5996dd62e9c 479 }
kenken0721 2:c5996dd62e9c 480 break;
kenken0721 2:c5996dd62e9c 481 default:
kenken0721 2:c5996dd62e9c 482 break;
kenken0721 1:f4dbd6b9cc27 483 }
kenken0721 1:f4dbd6b9cc27 484 }
kenken0721 1:f4dbd6b9cc27 485 }
kenken0721 3:aaa2fde4fafd 486 //pc.printf("kbtread = %d \r" ,kbtread);
kenken0721 2:c5996dd62e9c 487 //pc.printf("buff1[0] = %d \r" ,buff1[0]);
kenken0721 2:c5996dd62e9c 488 //pc.printf("buff2[0] = %d \r" ,buff2[0]);
kenken0721 2:c5996dd62e9c 489 //pc.printf("line1 = %d \r" ,linedata_1[0]);
kenken0721 3:aaa2fde4fafd 490 //pc.printf("line2 = %d \r" ,linedata_3[1]);
kenken0721 4:eabb7a738ff8 491 pc.printf("xdist = %f \r",x_dist);
kenken0721 3:aaa2fde4fafd 492 //pc.printf("tar_xdist = %f \r",tar_x_dist);
kenken0721 3:aaa2fde4fafd 493 //pc.printf("ontheline = %d \r",ontheline);
kenken0721 3:aaa2fde4fafd 494
kenken0721 4:eabb7a738ff8 495 pc.printf("ydist = %f \r",y_dist);
kenken0721 2:c5996dd62e9c 496 //pc.printf("tary = %f \r",tar_y_dist);
kenken0721 2:c5996dd62e9c 497 //pc.printf("stopflag = %d \r",stopflag);
kenken0721 2:c5996dd62e9c 498 //pc.printf("cross = %d \r",crosscount);
kenken0721 1:f4dbd6b9cc27 499 //pc.printf("buff[1] = %d" ,buff1[1]);
kenken0721 2:c5996dd62e9c 500 //pc.printf("line = %d" ,line);
kenken0721 2:c5996dd62e9c 501 //pc.printf("rpower = %lf" ,rpower);
kenken0721 3:aaa2fde4fafd 502 //pc.printf("lpower = %lf" ,lpower);
kenken0721 4:eabb7a738ff8 503 //pc.printf("dicmode = %d \r",directmode);
kenken0721 1:f4dbd6b9cc27 504 pc.printf("output = %lf \n" ,output);
kenken0721 1:f4dbd6b9cc27 505 }
kenken0721 1:f4dbd6b9cc27 506 }
kenken0721 1:f4dbd6b9cc27 507
kenken0721 4:eabb7a738ff8 508 //---------------初期化---------------------------
kenken0721 4:eabb7a738ff8 509
kenken0721 4:eabb7a738ff8 510
kenken0721 4:eabb7a738ff8 511 void syokikanomai(void){
kenken0721 4:eabb7a738ff8 512 //timer.start();
kenken0721 4:eabb7a738ff8 513 //int times = timer.read();
kenken0721 4:eabb7a738ff8 514 syokika_flag = false;
kenken0721 4:eabb7a738ff8 515 while (set_flag==false) {
kenken0721 4:eabb7a738ff8 516 encoder_count= Enc_Arm.read_rotate();
kenken0721 4:eabb7a738ff8 517 servo_hand.move(hand_uke_degree);
kenken0721 4:eabb7a738ff8 518 servo1.move(hiji_nage_degree);//退避
kenken0721 4:eabb7a738ff8 519 servo2.move(koshi_uke_degree);
kenken0721 4:eabb7a738ff8 520 //motor_relay = 0;
kenken0721 4:eabb7a738ff8 521 motor_dir1=0;
kenken0721 4:eabb7a738ff8 522 motor_dir2=1;
kenken0721 4:eabb7a738ff8 523 motor_pwm=0.15;
kenken0721 4:eabb7a738ff8 524
kenken0721 4:eabb7a738ff8 525 if(Enc_Arm.read_z() == 1) {
kenken0721 4:eabb7a738ff8 526 syokika_flag=true;
kenken0721 4:eabb7a738ff8 527 buzzer = 1.0;
kenken0721 4:eabb7a738ff8 528 }
kenken0721 4:eabb7a738ff8 529
kenken0721 4:eabb7a738ff8 530 if (syokika_flag==true) {
kenken0721 4:eabb7a738ff8 531 if(encoder_count == 170) {
kenken0721 4:eabb7a738ff8 532 motor_pwm=0;
kenken0721 4:eabb7a738ff8 533 motor_dir1=1;
kenken0721 4:eabb7a738ff8 534 motor_dir2=1;
kenken0721 4:eabb7a738ff8 535 set_flag=true; //投てき後,falseにする.
kenken0721 4:eabb7a738ff8 536 }
kenken0721 4:eabb7a738ff8 537 }
kenken0721 4:eabb7a738ff8 538 }
kenken0721 4:eabb7a738ff8 539 //if(times >= 1.0){
kenken0721 4:eabb7a738ff8 540 //wait(0.5);
kenken0721 4:eabb7a738ff8 541 //servo_hand.move(hand_uke_degree);
kenken0721 4:eabb7a738ff8 542 servo1.move(hiji_uke_degree);
kenken0721 4:eabb7a738ff8 543 servo1.move(hiji_uke_degree);
kenken0721 4:eabb7a738ff8 544 //servo2.move(koshi_uke_degree);
kenken0721 4:eabb7a738ff8 545 //}
kenken0721 4:eabb7a738ff8 546 }
kenken0721 4:eabb7a738ff8 547
kenken0721 4:eabb7a738ff8 548 void syokikanomai2(void){
kenken0721 4:eabb7a738ff8 549 //timer.start();
kenken0721 4:eabb7a738ff8 550 //int times = timer.read();
kenken0721 4:eabb7a738ff8 551 //syokika_flag = false;
kenken0721 4:eabb7a738ff8 552 if(set_flag==false) {
kenken0721 4:eabb7a738ff8 553 encoder_count= Enc_Arm.read_rotate();
kenken0721 4:eabb7a738ff8 554 servo_hand.move(hand_uke_degree);
kenken0721 4:eabb7a738ff8 555 servo1.move(hiji_nage_degree);//退避
kenken0721 4:eabb7a738ff8 556 servo2.move(koshi_uke_degree);
kenken0721 4:eabb7a738ff8 557 //motor_relay = 0;
kenken0721 4:eabb7a738ff8 558 motor_dir1=0;
kenken0721 4:eabb7a738ff8 559 motor_dir2=1;
kenken0721 4:eabb7a738ff8 560 motor_pwm=0.15;
kenken0721 4:eabb7a738ff8 561
kenken0721 4:eabb7a738ff8 562 if(Enc_Arm.read_z() == 1) {
kenken0721 4:eabb7a738ff8 563 syokika_flag = true;
kenken0721 4:eabb7a738ff8 564 buzzer = 1.0;
kenken0721 4:eabb7a738ff8 565 }
kenken0721 4:eabb7a738ff8 566
kenken0721 4:eabb7a738ff8 567 if (syokika_flag==true) {
kenken0721 4:eabb7a738ff8 568 if(encoder_count == 170) {
kenken0721 4:eabb7a738ff8 569 motor_pwm=0;
kenken0721 4:eabb7a738ff8 570 motor_dir1=1;
kenken0721 4:eabb7a738ff8 571 motor_dir2=1;
kenken0721 4:eabb7a738ff8 572 servo1.move(hiji_uke_degree);
kenken0721 4:eabb7a738ff8 573 servo1.move(hiji_uke_degree);
kenken0721 4:eabb7a738ff8 574 syokika_flag = false;
kenken0721 4:eabb7a738ff8 575 set_flag = true; //投てき後,falseにする.
kenken0721 4:eabb7a738ff8 576 }
kenken0721 4:eabb7a738ff8 577 }
kenken0721 4:eabb7a738ff8 578 }
kenken0721 4:eabb7a738ff8 579 //if(times >= 1.0){
kenken0721 4:eabb7a738ff8 580 //wait(0.5);
kenken0721 4:eabb7a738ff8 581 //servo_hand.move(hand_uke_degree);
kenken0721 4:eabb7a738ff8 582 //servo2.move(koshi_uke_degree);
kenken0721 4:eabb7a738ff8 583 //}
kenken0721 4:eabb7a738ff8 584 }
kenken0721 4:eabb7a738ff8 585
kenken0721 4:eabb7a738ff8 586 void pre_touteki(void){
kenken0721 4:eabb7a738ff8 587 /*
kenken0721 4:eabb7a738ff8 588 while(Enc_Arm.read_rotate()<pos) {
kenken0721 4:eabb7a738ff8 589 motor_dir1 = 0;
kenken0721 4:eabb7a738ff8 590 motor_dir2 = 1;
kenken0721 4:eabb7a738ff8 591 motor_pwm = 0.15;
kenken0721 4:eabb7a738ff8 592 }
kenken0721 4:eabb7a738ff8 593 motor_dir1=1;
kenken0721 4:eabb7a738ff8 594 motor_dir2=1;
kenken0721 4:eabb7a738ff8 595 motor_pwm=0;
kenken0721 4:eabb7a738ff8 596 */
kenken0721 4:eabb7a738ff8 597 //wait(1.0);
kenken0721 4:eabb7a738ff8 598 servo_hand.move(hand_tsukami_degree);
kenken0721 4:eabb7a738ff8 599
kenken0721 4:eabb7a738ff8 600 //wait(1.0);
kenken0721 4:eabb7a738ff8 601 servo2.move(koshi_nage_degree);
kenken0721 4:eabb7a738ff8 602 //servo1.move(hiji_tsukami_degree);
kenken0721 4:eabb7a738ff8 603 }
kenken0721 4:eabb7a738ff8 604
kenken0721 4:eabb7a738ff8 605 void touteki(float armpower ,int pos, bool thro_mode){
kenken0721 4:eabb7a738ff8 606 if(set_flag == true){
kenken0721 4:eabb7a738ff8 607 timer.start();
kenken0721 4:eabb7a738ff8 608 }
kenken0721 4:eabb7a738ff8 609 int times = timer.read();
kenken0721 4:eabb7a738ff8 610 //wait(1.0);
kenken0721 4:eabb7a738ff8 611 if(times >= 1.0 && times < 2.0){
kenken0721 4:eabb7a738ff8 612 servo_hand.move(hand_uke_degree);
kenken0721 4:eabb7a738ff8 613 }
kenken0721 4:eabb7a738ff8 614 //wait(1.0);
kenken0721 4:eabb7a738ff8 615 //lamp = 0.0;
kenken0721 4:eabb7a738ff8 616
kenken0721 4:eabb7a738ff8 617 if(times >= 2.0 && times < 3.0){
kenken0721 4:eabb7a738ff8 618 servo1.move(hiji_nage_degree);
kenken0721 4:eabb7a738ff8 619 }
kenken0721 4:eabb7a738ff8 620
kenken0721 4:eabb7a738ff8 621 //wait(1.0);
kenken0721 4:eabb7a738ff8 622 if(times >= 3.0 && times < 5.0){
kenken0721 4:eabb7a738ff8 623 lamp = 0.0;
kenken0721 4:eabb7a738ff8 624 while(Enc_Arm.read_rotate() < pos) {
kenken0721 4:eabb7a738ff8 625 motor_dir1 = 0;
kenken0721 4:eabb7a738ff8 626 motor_dir2 = 1;
kenken0721 4:eabb7a738ff8 627 motor_pwm = 0.15;
kenken0721 4:eabb7a738ff8 628 }
kenken0721 4:eabb7a738ff8 629 }
kenken0721 4:eabb7a738ff8 630 motor_dir1=1;
kenken0721 4:eabb7a738ff8 631 motor_dir2=1;
kenken0721 4:eabb7a738ff8 632 motor_pwm=0;
kenken0721 4:eabb7a738ff8 633 //wait(2.0);
kenken0721 4:eabb7a738ff8 634 //----投てき-------------------------
kenken0721 4:eabb7a738ff8 635 if(times >= 5.0 && times < 6.0){
kenken0721 4:eabb7a738ff8 636 motor_dir1 = 0;
kenken0721 4:eabb7a738ff8 637 motor_dir2 = 1;
kenken0721 4:eabb7a738ff8 638 motor_pwm = armpower;
kenken0721 4:eabb7a738ff8 639 }
kenken0721 4:eabb7a738ff8 640 //wait(1.0);
kenken0721 4:eabb7a738ff8 641 if(times >= 6.0 && times <7.0){
kenken0721 4:eabb7a738ff8 642 motor_pwm = 0.0;
kenken0721 4:eabb7a738ff8 643 motor_dir1 = 0;
kenken0721 4:eabb7a738ff8 644 motor_dir2 = 0;
kenken0721 4:eabb7a738ff8 645 buzzer = 0;
kenken0721 4:eabb7a738ff8 646 lamp = 1.0;
kenken0721 4:eabb7a738ff8 647 set_flag = false;
kenken0721 4:eabb7a738ff8 648 timer.reset();
kenken0721 4:eabb7a738ff8 649 timer.stop();
kenken0721 4:eabb7a738ff8 650 }
kenken0721 4:eabb7a738ff8 651 }
kenken0721 4:eabb7a738ff8 652
kenken0721 2:c5996dd62e9c 653 void LineCheck(int dmode){
kenken0721 2:c5996dd62e9c 654 if(dmode == STRAIGHT){
kenken0721 2:c5996dd62e9c 655 if(linedata_1[1] == 1){
kenken0721 2:c5996dd62e9c 656 ontheline = true;
kenken0721 2:c5996dd62e9c 657 tar_y_dist = y_dist;
kenken0721 2:c5996dd62e9c 658 }
kenken0721 2:c5996dd62e9c 659 if(ontheline == true){
kenken0721 3:aaa2fde4fafd 660 if((y_dist - tar_y_dist) >= Y_STOP_DIST){
kenken0721 2:c5996dd62e9c 661 ontheline = false;
kenken0721 2:c5996dd62e9c 662 crosscount++;
kenken0721 2:c5996dd62e9c 663 }
kenken0721 2:c5996dd62e9c 664 }
kenken0721 2:c5996dd62e9c 665 }else if(dmode == RIGHT){
kenken0721 3:aaa2fde4fafd 666 if(linedata_2[1] == 1){
kenken0721 3:aaa2fde4fafd 667 ontheline = true;
kenken0721 3:aaa2fde4fafd 668 tar_x_dist = x_dist;
kenken0721 3:aaa2fde4fafd 669 }
kenken0721 2:c5996dd62e9c 670 if(ontheline == true){
kenken0721 3:aaa2fde4fafd 671 if((x_dist - tar_x_dist) >= X_STOP_DIST){
kenken0721 2:c5996dd62e9c 672 ontheline = false;
kenken0721 2:c5996dd62e9c 673 crosscount++;
kenken0721 2:c5996dd62e9c 674 }
kenken0721 2:c5996dd62e9c 675 }
kenken0721 2:c5996dd62e9c 676 }else if(dmode == LEFT){
kenken0721 3:aaa2fde4fafd 677 if(linedata_3[1] == 1){
kenken0721 3:aaa2fde4fafd 678 ontheline = true;
kenken0721 3:aaa2fde4fafd 679 tar_x_dist = x_dist;
kenken0721 3:aaa2fde4fafd 680 }
kenken0721 2:c5996dd62e9c 681 if(ontheline == true){
kenken0721 3:aaa2fde4fafd 682 if((tar_x_dist - x_dist) >= X_STOP_DIST){
kenken0721 2:c5996dd62e9c 683 ontheline = false;
kenken0721 2:c5996dd62e9c 684 crosscount++;
kenken0721 2:c5996dd62e9c 685 }
kenken0721 2:c5996dd62e9c 686 }
kenken0721 2:c5996dd62e9c 687 }
kenken0721 2:c5996dd62e9c 688 }
kenken0721 0:f6828b914b1c 689
kenken0721 2:c5996dd62e9c 690 void linecheck(char *buff, int data[2]){
kenken0721 0:f6828b914b1c 691 if(buff[0] == 24){
kenken0721 2:c5996dd62e9c 692 data[0] = 0;
kenken0721 2:c5996dd62e9c 693 data[1] = 0;
kenken0721 0:f6828b914b1c 694 }else if(buff[0] == 56){
kenken0721 2:c5996dd62e9c 695 data[0] = 1;
kenken0721 2:c5996dd62e9c 696 data[1] = 0;
kenken0721 0:f6828b914b1c 697 }else if(buff[0] == 48){
kenken0721 2:c5996dd62e9c 698 data[0] = 2;
kenken0721 2:c5996dd62e9c 699 data[1] = 0;
kenken0721 0:f6828b914b1c 700 }else if(buff[0] == 112){
kenken0721 2:c5996dd62e9c 701 data[0] = 3;
kenken0721 2:c5996dd62e9c 702 data[1] = 0;
kenken0721 0:f6828b914b1c 703 }else if(buff[0] == 96){
kenken0721 2:c5996dd62e9c 704 data[0] = 4;
kenken0721 2:c5996dd62e9c 705 data[1] = 0;
kenken0721 0:f6828b914b1c 706 }else if(buff[0] == 224){
kenken0721 2:c5996dd62e9c 707 data[0] = 5;
kenken0721 2:c5996dd62e9c 708 data[1] = 0;
kenken0721 0:f6828b914b1c 709 }else if(buff[0] == 192){
kenken0721 2:c5996dd62e9c 710 data[0] = 6;
kenken0721 2:c5996dd62e9c 711 data[1] = 0;
kenken0721 0:f6828b914b1c 712 }else if(buff[0] == 128){
kenken0721 2:c5996dd62e9c 713 data[0] = 7;
kenken0721 2:c5996dd62e9c 714 data[1] = 0;
kenken0721 0:f6828b914b1c 715 }else if(buff[0] == 28){
kenken0721 2:c5996dd62e9c 716 data[0] = -1;
kenken0721 2:c5996dd62e9c 717 data[1] = 0;
kenken0721 0:f6828b914b1c 718 }else if(buff[0] == 12){
kenken0721 2:c5996dd62e9c 719 data[0] = -2;
kenken0721 2:c5996dd62e9c 720 data[1] = 0;
kenken0721 0:f6828b914b1c 721 }else if(buff[0] == 14){
kenken0721 2:c5996dd62e9c 722 data[0] = -3;
kenken0721 2:c5996dd62e9c 723 data[1] = 0;
kenken0721 0:f6828b914b1c 724 }else if(buff[0] == 6){
kenken0721 2:c5996dd62e9c 725 data[0] = -4;
kenken0721 2:c5996dd62e9c 726 data[1] = 0;
kenken0721 0:f6828b914b1c 727 }else if(buff[0] == 7){
kenken0721 2:c5996dd62e9c 728 data[0] = -5;
kenken0721 2:c5996dd62e9c 729 data[1] = 0;
kenken0721 0:f6828b914b1c 730 }else if(buff[0] == 3){
kenken0721 2:c5996dd62e9c 731 data[0] = -6;
kenken0721 2:c5996dd62e9c 732 data[1] = 0;
kenken0721 0:f6828b914b1c 733 }else if(buff[0] == 1){
kenken0721 2:c5996dd62e9c 734 data[0] = -7;
kenken0721 2:c5996dd62e9c 735 data[1] = 0;
kenken0721 2:c5996dd62e9c 736 }else if(buff[0] == 255){
kenken0721 2:c5996dd62e9c 737 data[0] = prelinedata;
kenken0721 2:c5996dd62e9c 738 data[1] = 1;
kenken0721 2:c5996dd62e9c 739 }else if(buff[0] == 0){
kenken0721 2:c5996dd62e9c 740 data[0] = prelinedata;
kenken0721 2:c5996dd62e9c 741 data[1] = 0;
kenken0721 0:f6828b914b1c 742 }else{
kenken0721 2:c5996dd62e9c 743 data[0] = prelinedata;
kenken0721 2:c5996dd62e9c 744 data[1] = 0;
kenken0721 0:f6828b914b1c 745 }
kenken0721 2:c5996dd62e9c 746 prelinedata = data[0];
kenken0721 3:aaa2fde4fafd 747 }