ss
Dependencies: WS2812 PixelArray Adafruit_GFX
Diff: main.cpp
- Revision:
- 2:a3f7435f9475
- Parent:
- 0:27e31cadeb36
- Child:
- 3:700a0cf6beea
--- a/main.cpp Sat Jun 15 13:30:57 2019 +0000 +++ b/main.cpp Sat Jun 15 13:42:33 2019 +0000 @@ -73,8 +73,8 @@ typedef enum{NOK = 0, YOK}flag; // Nessary change to enum!!! +flag flag_cal_Max; flag flag_cal_Min; -flag flag_cal_Max; flag flag_start; flag flag_over; flag flag_out; @@ -114,9 +114,8 @@ Adafruit_SSD1306_I2c oled(i2c, D9, 0x78, 64, 128); int main(){ - neothread.start(&NeopixelOn); oled.clearDisplay(); - oled.printf("Welcome to Alphabot\r\n\r\n"); + oled.printf("Welcome to Alphabot\r\n"); oled.display(); Initialization(); @@ -124,14 +123,16 @@ Driving(); if(flag_end){ + // neothread.start(&NeopixelOn); oled.clearDisplay(); int duration = total.read(); oled.printf("Congratulation!! \r\n"); oled.printf("Time is %.3f", total.read()); oled.display(); - flag_neo = YOK; + NeopixelOn(); + // wait(10); } - + //NeopixelOn(); } //==================================================== @@ -144,6 +145,8 @@ flag_over = NOK; flag_IR = NOK; flag_distance = NOK; + flag_cal_Min = NOK; + flag_cal_Max = NOK; TR.calibrate_init(calMin,calMax); @@ -186,12 +189,13 @@ if ((ir_rx.getState() == ReceiverIR::Received)&&(ir_rx.getData(&format, IR_buf, sizeof(IR_buf) * 8)!=0)) { for(int i =0 ; i<4; i ++){ IR_buf_sum |=(IR_buf[i]<<8*(3-i)); - } + } pc.printf("%X\r\n",IR_buf_sum); if(IR_buf_sum == 0x00FF0CF3){ io.write(0x7F); TR.calibrate(sensor_val, calMin, calMax); wait(0.5); + TR.calibrate(sensor_val, calMin, calMax); io.write(0xFF); for(int i = 0; i<5; i++){ pc.printf("Min_sensor_value[%d] = %d\r\n",i+1,calMin[i]); @@ -216,7 +220,9 @@ io.write(0xBF); TR.calibrate(sensor_val, calMin, calMax); wait(0.5); + TR.calibrate(sensor_val, calMin, calMax); io.write(0xFF); + for(int i = 0; i<5; i++){ pc.printf("Min_sensor_value[%d] = %d\r\n",i+1,calMin[i]); pc.printf("Max_sensor_value[%d] = %d\r\n",i+1,calMax[i]); @@ -228,7 +234,7 @@ } } - + //=================start=============================== while(flag_start == NOK){ @@ -261,13 +267,12 @@ total.start(); - Etime.start(); while(!flag_end){ Etime.reset(); - // Obstacle_check(); + Obstacle_check(); if(flag_distance == NOK){ Actuator(); @@ -278,13 +283,14 @@ do{ - }while(Etime.read()<0.00300001); + }while(Etime.read()<=0.0075); + } } -int End_check(int *sensor_values){ +int End_check(int *sensor_values){ int avg = 0; int sum = 0; @@ -292,7 +298,6 @@ sum += sensor_values[i]; } avg = sum / NUMSENSORS; - //pc.printf("\tavg function: %d\r\n",avg); return avg; } @@ -302,9 +307,9 @@ den = 30; - //int temp = TR.readLine(sensor_val, calMin, calMax,on_line,1) - MIDDLE; + int temp = TR.readLine(sensor_val, calMin, calMax,on_line,1); - //pc.printf("new temp: %d\r\n",temp); + TR.readCalibrated(sensor_for_end, calMin, calMax); int avg = End_check(sensor_for_end); @@ -330,30 +335,27 @@ pc.printf("online = %d\r\n", on_line[0]); } - R_PWM = 140; - L_PWM = 140; + R_PWM = 135; + L_PWM = 133; - - //if(weight > (2000/den)*2/3) weight = (2000/den)*3/4; - if(weight >= (2000/den)*3/4){ if(direction == LEFT){ - PWMA.pulsewidth_us(L_PWM+0.0*weight); + PWMA.pulsewidth_us(L_PWM+3+0.0*weight); PWMB.pulsewidth_us(R_PWM-2.1*weight); } else{ - PWMA.pulsewidth_us(L_PWM-2.1*weight); + PWMA.pulsewidth_us(L_PWM+3-2.1*weight); PWMB.pulsewidth_us(R_PWM+0.0*weight); } }else{ if(direction == LEFT){ - PWMA.pulsewidth_us(L_PWM+0.0*weight); - PWMB.pulsewidth_us(R_PWM-1.4*weight); + PWMA.pulsewidth_us(L_PWM+3+0.0*weight); + PWMB.pulsewidth_us(R_PWM-1.5*weight); } else{ - PWMA.pulsewidth_us(L_PWM-1.4*weight); + PWMA.pulsewidth_us(L_PWM+3-1.5*weight); PWMB.pulsewidth_us(R_PWM+0.0*weight); } } @@ -370,21 +372,21 @@ { if(P_weight >=(2000/den)*2/3 && P_weight<=(2000/den)*4/5){ if(P_direction == LEFT){ - PWMA.pulsewidth_us(L_PWM-P_weight); + PWMA.pulsewidth_us(L_PWM-P_weight+3); PWMB.pulsewidth_us(R_PWM); } else{ - PWMA.pulsewidth_us(L_PWM); + PWMA.pulsewidth_us(L_PWM+3); PWMB.pulsewidth_us(R_PWM-P_weight); } } if(P_weight>=(2000/den)*4/5){ if(P_direction == LEFT){ - PWMA.pulsewidth_us(L_PWM-1.0*P_weight); + PWMA.pulsewidth_us(L_PWM+3-1.0*P_weight); PWMB.pulsewidth_us(R_PWM+1.0*P_weight); } else{ - PWMA.pulsewidth_us(L_PWM+1.0*P_weight); + PWMA.pulsewidth_us(L_PWM+3+1.0*P_weight); PWMB.pulsewidth_us(R_PWM-1.0*P_weight); } } @@ -410,14 +412,12 @@ if(flag_distance == YOK){ while(1){ - // pc.printf("distance check. turn left!!\r\n"); PWMB.pulsewidth_us(100); PWMA.pulsewidth_us(100); AIN1.write(1); AIN2.write(0); BIN1.write(0); BIN2.write(1); - //pc.printf("ABS == ") if((abs(TR.readLine(sensor_val, calMin, calMax,on_line,1) - MIDDLE)<=400 && on_line[0] == 1)){ Motor_init(); flag_distance = NOK; @@ -431,9 +431,6 @@ void Obstacle_check(){ - //io.write 체크 - //flag_IR 변경 YOK - // pc.printf("flag_out = %x\r\n", io.read()); if(io.read() == 0x7F){ //왼쪽읽음 flag_IR = YOK; flag_obstacle = YOK; @@ -448,24 +445,15 @@ if(flag_IR){ Otime.start(); - //flag_IR = NOK; - // pc.printf("\tActuator Obstacel start\r\n"); while(flag_obstacle == YOK){ - //while(io.read() != 0xFF){ - Actuator_Obstacle(Obs_direction); pc.printf("obstacle!\r\n"); - //pc.printf("\t\tActuator Obstacel start\r\n"); - } Otime.stop(); Otime.reset(); flag_IR = NOK; } - - - } @@ -482,7 +470,7 @@ } else if(dir == 1 ){ PWMA.pulsewidth_us(L_PWM-100); - PWMB.pulsewidth_us(R_PWM-40); + PWMB.pulsewidth_us(R_PWM-30); } }else if(Otime.read() <= 1.0){ @@ -500,10 +488,10 @@ if(dir == 0){ PWMA.pulsewidth_us(L_PWM-100); - PWMB.pulsewidth_us(R_PWM-40); + PWMB.pulsewidth_us(R_PWM-30); } else if(dir == 1 ){ - PWMA.pulsewidth_us(L_PWM-20); + PWMA.pulsewidth_us(L_PWM-10); PWMB.pulsewidth_us(R_PWM-100); } @@ -545,8 +533,9 @@ int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f}; // Now the buffer is written, write it to the led array. - while (flag_neo) + while (1) { + //pc.printf("thread\r\n"); //get starting RGB components for interpolation std::size_t c1 = colorbuf[colorFrom]; std::size_t r1 = (c1 & 0xff0000) >> 16; @@ -587,20 +576,4 @@ colorTo = colorIdx; } -} - - -/* -else if(Otime.read() <= 2.0){ - - if(dir == 0){ - PWMA.pulsewidth_us(L_PWM+5); - PWMB.pulsewidth_us(R_PWM); - } - else if(dir == 1 ){ - PWMA.pulsewidth_us(L_PWM+5); - PWMB.pulsewidth_us(R_PWM); - } - -} -*/ \ No newline at end of file +} \ No newline at end of file