Final Project로 실제 점검에 사용된 코드
Dependencies: mbed Adafruit_GFX
main.cpp@0:22391cd705e2, 2019-06-15 (annotated)
- Committer:
- 21400688
- Date:
- Sat Jun 15 20:52:15 2019 +0000
- Revision:
- 0:22391cd705e2
vb
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
21400688 | 0:22391cd705e2 | 1 | #include "mbed.h" |
21400688 | 0:22391cd705e2 | 2 | #include "ReceiverIR.h" |
21400688 | 0:22391cd705e2 | 3 | #include "TB6612FNG.h" |
21400688 | 0:22391cd705e2 | 4 | #include "Ultrasonic.h" |
21400688 | 0:22391cd705e2 | 5 | |
21400688 | 0:22391cd705e2 | 6 | #include "Adafruit_SSD1306.h" |
21400688 | 0:22391cd705e2 | 7 | #include "Adafruit_GFX.h" |
21400688 | 0:22391cd705e2 | 8 | #include "glcdfont.h" |
21400688 | 0:22391cd705e2 | 9 | #define SSD1306_DISPLAYON 0xAF |
21400688 | 0:22391cd705e2 | 10 | #include "WS2812.h" |
21400688 | 0:22391cd705e2 | 11 | #include "PixelArray.h" |
21400688 | 0:22391cd705e2 | 12 | |
21400688 | 0:22391cd705e2 | 13 | #define WS2812_BUF 150 |
21400688 | 0:22391cd705e2 | 14 | #define NUM_COLORS 6 |
21400688 | 0:22391cd705e2 | 15 | #define NUM_LEDS_PER_COLOR 10 |
21400688 | 0:22391cd705e2 | 16 | #define button2 18 |
21400688 | 0:22391cd705e2 | 17 | |
21400688 | 0:22391cd705e2 | 18 | // PWMA, AIN1, AIN2, PWMBm, BIN1, BIN2, STBY |
21400688 | 0:22391cd705e2 | 19 | TB6612FNG motorDriver(D6, A0, A1, D5, A3, A2, A5); |
21400688 | 0:22391cd705e2 | 20 | ReceiverIR ir_rx(D4); |
21400688 | 0:22391cd705e2 | 21 | Serial pc(USBTX, USBRX); |
21400688 | 0:22391cd705e2 | 22 | SPI spi(D11,D12,D13); |
21400688 | 0:22391cd705e2 | 23 | DigitalOut spi_cs(D10,1); |
21400688 | 0:22391cd705e2 | 24 | Ultrasonic ultra(D3, D2); |
21400688 | 0:22391cd705e2 | 25 | PixelArray px(WS2812_BUF); |
21400688 | 0:22391cd705e2 | 26 | WS2812 ws(D7, WS2812_BUF, 7, 15, 10, 15); |
21400688 | 0:22391cd705e2 | 27 | |
21400688 | 0:22391cd705e2 | 28 | I2C i2c(D14, D15); |
21400688 | 0:22391cd705e2 | 29 | Adafruit_SSD1306_I2c oled(i2c, D9, 0x78, 64, 128); |
21400688 | 0:22391cd705e2 | 30 | |
21400688 | 0:22391cd705e2 | 31 | Timer timer; |
21400688 | 0:22391cd705e2 | 32 | Timer btimer; |
21400688 | 0:22391cd705e2 | 33 | int cal[5]; //센서 입력 모아놓은 배열 |
21400688 | 0:22391cd705e2 | 34 | int maxv = 0; //칼리브레이션 최대값 |
21400688 | 0:22391cd705e2 | 35 | int minv = 999; //칼리브레이션 최소값 |
21400688 | 0:22391cd705e2 | 36 | float fPwmPeriod; //모터 주기 |
21400688 | 0:22391cd705e2 | 37 | float fPwmPulsewidth; //모터 속도 |
21400688 | 0:22391cd705e2 | 38 | RemoteIR::Format format; |
21400688 | 0:22391cd705e2 | 39 | uint8_t buf[32]; //리모컨 입력받는 버퍼 |
21400688 | 0:22391cd705e2 | 40 | int num; //리모컨 입력 숫자 |
21400688 | 0:22391cd705e2 | 41 | int bitcount, decoded; //리모컨 입력 필요변수 |
21400688 | 0:22391cd705e2 | 42 | |
21400688 | 0:22391cd705e2 | 43 | float aspeed=0.19; |
21400688 | 0:22391cd705e2 | 44 | float speed=0.19; //모터 속도 |
21400688 | 0:22391cd705e2 | 45 | float lspd=0.17; //최소 속도 |
21400688 | 0:22391cd705e2 | 46 | float mspd=0.25; //최대 속도 |
21400688 | 0:22391cd705e2 | 47 | float tspeed; //임시 속도 변 |
21400688 | 0:22391cd705e2 | 48 | |
21400688 | 0:22391cd705e2 | 49 | void setspeed(float a){ |
21400688 | 0:22391cd705e2 | 50 | speed=a; |
21400688 | 0:22391cd705e2 | 51 | motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 52 | motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 53 | } |
21400688 | 0:22391cd705e2 | 54 | |
21400688 | 0:22391cd705e2 | 55 | int going=0; //주행 중인지 아닌지 판별 |
21400688 | 0:22391cd705e2 | 56 | |
21400688 | 0:22391cd705e2 | 57 | int maxcal(int a[], int k){ |
21400688 | 0:22391cd705e2 | 58 | int q; |
21400688 | 0:22391cd705e2 | 59 | int maxone = 2; |
21400688 | 0:22391cd705e2 | 60 | int range = 150; |
21400688 | 0:22391cd705e2 | 61 | if(a[2]- a[0]>range) maxone = 0; |
21400688 | 0:22391cd705e2 | 62 | else if(a[2]- a[1]>range) maxone = 1; |
21400688 | 0:22391cd705e2 | 63 | else if(a[2]- a[3]>range) maxone = 3; |
21400688 | 0:22391cd705e2 | 64 | else if(a[2]- a[4]>range) maxone = 4; |
21400688 | 0:22391cd705e2 | 65 | int maxa2 = 250; |
21400688 | 0:22391cd705e2 | 66 | int mina2 = 100; |
21400688 | 0:22391cd705e2 | 67 | //if(timer.read() > 15 && maxa2 > a[0] && maxa2 > a[1] && mina2 < a[0] && mina2 < a[1]){ |
21400688 | 0:22391cd705e2 | 68 | // if(maxa2 > a[3] && mina2 < a[3] && maxa2 >a[4] && mina2 < a[4]){ |
21400688 | 0:22391cd705e2 | 69 | // maxone = 6; |
21400688 | 0:22391cd705e2 | 70 | // } |
21400688 | 0:22391cd705e2 | 71 | // maxone = 5; |
21400688 | 0:22391cd705e2 | 72 | // } |
21400688 | 0:22391cd705e2 | 73 | |
21400688 | 0:22391cd705e2 | 74 | return maxone; |
21400688 | 0:22391cd705e2 | 75 | } |
21400688 | 0:22391cd705e2 | 76 | |
21400688 | 0:22391cd705e2 | 77 | void moveStop(void){ |
21400688 | 0:22391cd705e2 | 78 | motorDriver.motorB_stop(); |
21400688 | 0:22391cd705e2 | 79 | motorDriver.motorA_stop(); |
21400688 | 0:22391cd705e2 | 80 | } |
21400688 | 0:22391cd705e2 | 81 | |
21400688 | 0:22391cd705e2 | 82 | void moveBackward(void){ |
21400688 | 0:22391cd705e2 | 83 | setspeed(speed); |
21400688 | 0:22391cd705e2 | 84 | motorDriver.motorA_ccw(); |
21400688 | 0:22391cd705e2 | 85 | motorDriver.motorB_ccw(); |
21400688 | 0:22391cd705e2 | 86 | //wait(0.3); |
21400688 | 0:22391cd705e2 | 87 | //moveStop(); |
21400688 | 0:22391cd705e2 | 88 | } |
21400688 | 0:22391cd705e2 | 89 | |
21400688 | 0:22391cd705e2 | 90 | void moveForward(void){ |
21400688 | 0:22391cd705e2 | 91 | setspeed(speed); |
21400688 | 0:22391cd705e2 | 92 | motorDriver.motorA_cw(); |
21400688 | 0:22391cd705e2 | 93 | motorDriver.motorB_cw(); |
21400688 | 0:22391cd705e2 | 94 | //wait(0.3); |
21400688 | 0:22391cd705e2 | 95 | //moveStop(); |
21400688 | 0:22391cd705e2 | 96 | } |
21400688 | 0:22391cd705e2 | 97 | |
21400688 | 0:22391cd705e2 | 98 | void moveLeft(void){ |
21400688 | 0:22391cd705e2 | 99 | setspeed(speed); |
21400688 | 0:22391cd705e2 | 100 | motorDriver.motorA_ccw(); |
21400688 | 0:22391cd705e2 | 101 | motorDriver.motorB_cw(); |
21400688 | 0:22391cd705e2 | 102 | wait(0.08); |
21400688 | 0:22391cd705e2 | 103 | moveStop(); |
21400688 | 0:22391cd705e2 | 104 | } |
21400688 | 0:22391cd705e2 | 105 | |
21400688 | 0:22391cd705e2 | 106 | void moveRight(void){ |
21400688 | 0:22391cd705e2 | 107 | setspeed(speed); |
21400688 | 0:22391cd705e2 | 108 | motorDriver.motorA_cw(); |
21400688 | 0:22391cd705e2 | 109 | motorDriver.motorB_ccw(); |
21400688 | 0:22391cd705e2 | 110 | wait(0.08); |
21400688 | 0:22391cd705e2 | 111 | moveStop(); |
21400688 | 0:22391cd705e2 | 112 | } |
21400688 | 0:22391cd705e2 | 113 | void finishlight(void) |
21400688 | 0:22391cd705e2 | 114 | { |
21400688 | 0:22391cd705e2 | 115 | ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling |
21400688 | 0:22391cd705e2 | 116 | |
21400688 | 0:22391cd705e2 | 117 | int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f}; |
21400688 | 0:22391cd705e2 | 118 | |
21400688 | 0:22391cd705e2 | 119 | for (int i = 0; i < WS2812_BUF; i++) { |
21400688 | 0:22391cd705e2 | 120 | px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]); |
21400688 | 0:22391cd705e2 | 121 | } |
21400688 | 0:22391cd705e2 | 122 | |
21400688 | 0:22391cd705e2 | 123 | for (int j=0; j<WS2812_BUF; j++) { |
21400688 | 0:22391cd705e2 | 124 | px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS_PER_COLOR))); |
21400688 | 0:22391cd705e2 | 125 | } |
21400688 | 0:22391cd705e2 | 126 | |
21400688 | 0:22391cd705e2 | 127 | while (1) { |
21400688 | 0:22391cd705e2 | 128 | for (int z=WS2812_BUF; z >= 0 ; z--) { |
21400688 | 0:22391cd705e2 | 129 | ws.write_offsets(px.getBuf(),z,z,z); |
21400688 | 0:22391cd705e2 | 130 | wait(0.075); |
21400688 | 0:22391cd705e2 | 131 | } |
21400688 | 0:22391cd705e2 | 132 | } |
21400688 | 0:22391cd705e2 | 133 | } |
21400688 | 0:22391cd705e2 | 134 | int value; |
21400688 | 0:22391cd705e2 | 135 | int ch = 0; |
21400688 | 0:22391cd705e2 | 136 | int status; |
21400688 | 0:22391cd705e2 | 137 | void drive(void){ |
21400688 | 0:22391cd705e2 | 138 | spi_cs = 0; |
21400688 | 0:22391cd705e2 | 139 | ultra.trig(); |
21400688 | 0:22391cd705e2 | 140 | ultra.setMode(true); |
21400688 | 0:22391cd705e2 | 141 | value = spi.write(ch<<12); |
21400688 | 0:22391cd705e2 | 142 | pc.printf("calibration..\r\n"); |
21400688 | 0:22391cd705e2 | 143 | timer.start(); |
21400688 | 0:22391cd705e2 | 144 | |
21400688 | 0:22391cd705e2 | 145 | //주행시작 |
21400688 | 0:22391cd705e2 | 146 | motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 147 | motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 148 | moveForward(); |
21400688 | 0:22391cd705e2 | 149 | while(1){ |
21400688 | 0:22391cd705e2 | 150 | oled.clearDisplay(); |
21400688 | 0:22391cd705e2 | 151 | oled.setTextCursor(0,0); |
21400688 | 0:22391cd705e2 | 152 | oled.printf("speed: %f\r\n",speed); |
21400688 | 0:22391cd705e2 | 153 | oled.printf("0: %d, 1: %d\r\n2: %d, 3: %d\r\n4: %d st: %d\r\n",cal[0],cal[1],cal[2],cal[3],cal[4],status); |
21400688 | 0:22391cd705e2 | 154 | oled.printf("time: %.3f\r\n",timer.read_ms()/1000); |
21400688 | 0:22391cd705e2 | 155 | oled.display(); |
21400688 | 0:22391cd705e2 | 156 | |
21400688 | 0:22391cd705e2 | 157 | status = maxcal(cal,5); |
21400688 | 0:22391cd705e2 | 158 | pc.printf("status:%d\r\n"); |
21400688 | 0:22391cd705e2 | 159 | switch(status){ |
21400688 | 0:22391cd705e2 | 160 | //●○○○○, 오른쪽으로 가야함 |
21400688 | 0:22391cd705e2 | 161 | case 4 : |
21400688 | 0:22391cd705e2 | 162 | setspeed(aspeed); |
21400688 | 0:22391cd705e2 | 163 | motorDriver.motorA_cw(); |
21400688 | 0:22391cd705e2 | 164 | motorDriver.motorB_ccw(); |
21400688 | 0:22391cd705e2 | 165 | wait(0.08); |
21400688 | 0:22391cd705e2 | 166 | moveStop(); |
21400688 | 0:22391cd705e2 | 167 | wait(0.08); |
21400688 | 0:22391cd705e2 | 168 | break; |
21400688 | 0:22391cd705e2 | 169 | |
21400688 | 0:22391cd705e2 | 170 | //○●○○○, 오른쪽으로 가야함 |
21400688 | 0:22391cd705e2 | 171 | case 3 : |
21400688 | 0:22391cd705e2 | 172 | setspeed(aspeed); |
21400688 | 0:22391cd705e2 | 173 | motorDriver.motorA_cw(); |
21400688 | 0:22391cd705e2 | 174 | motorDriver.motorB_ccw(); |
21400688 | 0:22391cd705e2 | 175 | wait(0.08); |
21400688 | 0:22391cd705e2 | 176 | moveStop(); |
21400688 | 0:22391cd705e2 | 177 | wait(0.08); |
21400688 | 0:22391cd705e2 | 178 | break; |
21400688 | 0:22391cd705e2 | 179 | |
21400688 | 0:22391cd705e2 | 180 | //○○●○○, 정주행 |
21400688 | 0:22391cd705e2 | 181 | // 중앙 값이 가장 낮지만 검은 줄이 아니면 (<350) 뒤로 0.5초간 가본다. 그 뒤에 다시 직진. |
21400688 | 0:22391cd705e2 | 182 | case 2 : |
21400688 | 0:22391cd705e2 | 183 | speed=aspeed; |
21400688 | 0:22391cd705e2 | 184 | if(speed<mspd){speed+=0.02;} |
21400688 | 0:22391cd705e2 | 185 | motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 186 | motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 187 | if(cal[2]<350 || btimer.read_ms()>500){moveForward();btimer.reset();btimer.start();} |
21400688 | 0:22391cd705e2 | 188 | else{ |
21400688 | 0:22391cd705e2 | 189 | if(btimer.read_ms()<500){ |
21400688 | 0:22391cd705e2 | 190 | moveBackward(); |
21400688 | 0:22391cd705e2 | 191 | btimer.reset(); |
21400688 | 0:22391cd705e2 | 192 | btimer.start(); |
21400688 | 0:22391cd705e2 | 193 | } |
21400688 | 0:22391cd705e2 | 194 | |
21400688 | 0:22391cd705e2 | 195 | } |
21400688 | 0:22391cd705e2 | 196 | |
21400688 | 0:22391cd705e2 | 197 | break; |
21400688 | 0:22391cd705e2 | 198 | |
21400688 | 0:22391cd705e2 | 199 | //○○○●○, 왼쪽으로 가야함 |
21400688 | 0:22391cd705e2 | 200 | case 1 : |
21400688 | 0:22391cd705e2 | 201 | setspeed(aspeed); |
21400688 | 0:22391cd705e2 | 202 | motorDriver.motorA_ccw(); |
21400688 | 0:22391cd705e2 | 203 | motorDriver.motorB_cw(); |
21400688 | 0:22391cd705e2 | 204 | wait(0.08); |
21400688 | 0:22391cd705e2 | 205 | moveStop(); |
21400688 | 0:22391cd705e2 | 206 | wait(0.08); |
21400688 | 0:22391cd705e2 | 207 | break; |
21400688 | 0:22391cd705e2 | 208 | |
21400688 | 0:22391cd705e2 | 209 | //○○○○●, 왼쪽으로 가야함 |
21400688 | 0:22391cd705e2 | 210 | case 0 : |
21400688 | 0:22391cd705e2 | 211 | setspeed(aspeed); |
21400688 | 0:22391cd705e2 | 212 | motorDriver.motorA_ccw(); |
21400688 | 0:22391cd705e2 | 213 | motorDriver.motorB_cw(); |
21400688 | 0:22391cd705e2 | 214 | wait(0.08); |
21400688 | 0:22391cd705e2 | 215 | moveStop(); |
21400688 | 0:22391cd705e2 | 216 | wait(0.08); |
21400688 | 0:22391cd705e2 | 217 | break; |
21400688 | 0:22391cd705e2 | 218 | |
21400688 | 0:22391cd705e2 | 219 | // case 5 : |
21400688 | 0:22391cd705e2 | 220 | // speed=1.5; |
21400688 | 0:22391cd705e2 | 221 | // motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 222 | // motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 223 | // moveLeft(); |
21400688 | 0:22391cd705e2 | 224 | // moveForward(); |
21400688 | 0:22391cd705e2 | 225 | // break; |
21400688 | 0:22391cd705e2 | 226 | |
21400688 | 0:22391cd705e2 | 227 | case 6 : |
21400688 | 0:22391cd705e2 | 228 | moveStop(); |
21400688 | 0:22391cd705e2 | 229 | finishlight(); |
21400688 | 0:22391cd705e2 | 230 | |
21400688 | 0:22391cd705e2 | 231 | default : |
21400688 | 0:22391cd705e2 | 232 | motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 233 | motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 234 | } |
21400688 | 0:22391cd705e2 | 235 | |
21400688 | 0:22391cd705e2 | 236 | //센서 입력 받기 |
21400688 | 0:22391cd705e2 | 237 | for(ch=0;ch<6;ch++){ |
21400688 | 0:22391cd705e2 | 238 | spi_cs = 0; |
21400688 | 0:22391cd705e2 | 239 | spi.format(16, 0); |
21400688 | 0:22391cd705e2 | 240 | spi.frequency(2000000); |
21400688 | 0:22391cd705e2 | 241 | wait_us(2); |
21400688 | 0:22391cd705e2 | 242 | value = spi.write(ch<<12); |
21400688 | 0:22391cd705e2 | 243 | value >>= 6; |
21400688 | 0:22391cd705e2 | 244 | spi_cs = 1; |
21400688 | 0:22391cd705e2 | 245 | wait_us(21); |
21400688 | 0:22391cd705e2 | 246 | switch(ch) |
21400688 | 0:22391cd705e2 | 247 | { |
21400688 | 0:22391cd705e2 | 248 | case 1 : cal[0] = value;break; |
21400688 | 0:22391cd705e2 | 249 | case 2 : cal[1] = value;break; |
21400688 | 0:22391cd705e2 | 250 | case 3 : cal[2] = value;break; |
21400688 | 0:22391cd705e2 | 251 | case 4 : cal[3] = value;break; |
21400688 | 0:22391cd705e2 | 252 | case 5 : cal[4] = value;break; |
21400688 | 0:22391cd705e2 | 253 | } |
21400688 | 0:22391cd705e2 | 254 | } |
21400688 | 0:22391cd705e2 | 255 | |
21400688 | 0:22391cd705e2 | 256 | //각종 프린트 |
21400688 | 0:22391cd705e2 | 257 | //if(timer.read()>1){ |
21400688 | 0:22391cd705e2 | 258 | for(ch=0;ch<5;ch++){ |
21400688 | 0:22391cd705e2 | 259 | pc.printf("cal[%d]: %d, ",ch, cal[ch]); |
21400688 | 0:22391cd705e2 | 260 | if(cal[ch] < minv) minv = cal[ch]; |
21400688 | 0:22391cd705e2 | 261 | if(cal[ch] > maxv) maxv = cal[ch]; |
21400688 | 0:22391cd705e2 | 262 | } |
21400688 | 0:22391cd705e2 | 263 | pc.printf("\r\n"); |
21400688 | 0:22391cd705e2 | 264 | pc.printf("maxv: %d, minv: %d,\r\n",maxv, minv); |
21400688 | 0:22391cd705e2 | 265 | pc.printf("maxcal : %d\r\n",status); |
21400688 | 0:22391cd705e2 | 266 | pc.printf("\r\n"); |
21400688 | 0:22391cd705e2 | 267 | //} |
21400688 | 0:22391cd705e2 | 268 | |
21400688 | 0:22391cd705e2 | 269 | //장애물 만나면 스탑!! |
21400688 | 0:22391cd705e2 | 270 | if(ultra.getDistance()<8) |
21400688 | 0:22391cd705e2 | 271 | { |
21400688 | 0:22391cd705e2 | 272 | pc.printf("WARNING!\r\n"); |
21400688 | 0:22391cd705e2 | 273 | tspeed = speed; |
21400688 | 0:22391cd705e2 | 274 | speed=0.2; |
21400688 | 0:22391cd705e2 | 275 | motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 276 | motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 277 | moveBackward(); |
21400688 | 0:22391cd705e2 | 278 | wait(0.2); |
21400688 | 0:22391cd705e2 | 279 | moveStop(); |
21400688 | 0:22391cd705e2 | 280 | speed=tspeed; |
21400688 | 0:22391cd705e2 | 281 | motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 282 | motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 283 | } |
21400688 | 0:22391cd705e2 | 284 | |
21400688 | 0:22391cd705e2 | 285 | //리모컨 입력받기 |
21400688 | 0:22391cd705e2 | 286 | if (ir_rx.getState() == ReceiverIR::Received) { |
21400688 | 0:22391cd705e2 | 287 | bitcount = ir_rx.getData(&format, buf, sizeof(buf) * 8); |
21400688 | 0:22391cd705e2 | 288 | if (bitcount>0) { |
21400688 | 0:22391cd705e2 | 289 | decoded=buf[3]; |
21400688 | 0:22391cd705e2 | 290 | num = buf[2]; |
21400688 | 0:22391cd705e2 | 291 | //pc.printf("\r\nDecoded: %X %X %X %X ", buf[0],buf[1],num,buf[3]); |
21400688 | 0:22391cd705e2 | 292 | pc.printf("\r\nnum: %X %d", num, num); |
21400688 | 0:22391cd705e2 | 293 | switch(num){ |
21400688 | 0:22391cd705e2 | 294 | case 24: |
21400688 | 0:22391cd705e2 | 295 | moveForward(); |
21400688 | 0:22391cd705e2 | 296 | break; |
21400688 | 0:22391cd705e2 | 297 | case 90: |
21400688 | 0:22391cd705e2 | 298 | moveRight(); |
21400688 | 0:22391cd705e2 | 299 | break; |
21400688 | 0:22391cd705e2 | 300 | case 82: |
21400688 | 0:22391cd705e2 | 301 | moveBackward(); |
21400688 | 0:22391cd705e2 | 302 | break; |
21400688 | 0:22391cd705e2 | 303 | case 8: |
21400688 | 0:22391cd705e2 | 304 | moveLeft(); |
21400688 | 0:22391cd705e2 | 305 | break; |
21400688 | 0:22391cd705e2 | 306 | case 21: //속도상승 |
21400688 | 0:22391cd705e2 | 307 | speed+=0.05; |
21400688 | 0:22391cd705e2 | 308 | motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 309 | motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 310 | break; |
21400688 | 0:22391cd705e2 | 311 | case 7: //속도저하 |
21400688 | 0:22391cd705e2 | 312 | speed-=0.05; |
21400688 | 0:22391cd705e2 | 313 | motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 314 | motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 315 | break; |
21400688 | 0:22391cd705e2 | 316 | case 28: |
21400688 | 0:22391cd705e2 | 317 | pc.printf("stop"); |
21400688 | 0:22391cd705e2 | 318 | moveStop(); |
21400688 | 0:22391cd705e2 | 319 | going=0; |
21400688 | 0:22391cd705e2 | 320 | break; |
21400688 | 0:22391cd705e2 | 321 | case 68: |
21400688 | 0:22391cd705e2 | 322 | pc.printf("stop drive\n\r"); |
21400688 | 0:22391cd705e2 | 323 | going=0; |
21400688 | 0:22391cd705e2 | 324 | break; |
21400688 | 0:22391cd705e2 | 325 | } |
21400688 | 0:22391cd705e2 | 326 | } |
21400688 | 0:22391cd705e2 | 327 | } |
21400688 | 0:22391cd705e2 | 328 | } |
21400688 | 0:22391cd705e2 | 329 | } |
21400688 | 0:22391cd705e2 | 330 | |
21400688 | 0:22391cd705e2 | 331 | /* |
21400688 | 0:22391cd705e2 | 332 | 1 = 12 |
21400688 | 0:22391cd705e2 | 333 | 2 = 24 |
21400688 | 0:22391cd705e2 | 334 | 3 = 94 |
21400688 | 0:22391cd705e2 | 335 | 4 = 8 |
21400688 | 0:22391cd705e2 | 336 | 5 = 28 |
21400688 | 0:22391cd705e2 | 337 | 6 = 90 |
21400688 | 0:22391cd705e2 | 338 | 7 = 66 |
21400688 | 0:22391cd705e2 | 339 | 8 = 82 |
21400688 | 0:22391cd705e2 | 340 | 9 = 74 |
21400688 | 0:22391cd705e2 | 341 | 0 = 22 |
21400688 | 0:22391cd705e2 | 342 | +100 = 25 |
21400688 | 0:22391cd705e2 | 343 | +200 = 13 |
21400688 | 0:22391cd705e2 | 344 | next = 64 |
21400688 | 0:22391cd705e2 | 345 | prev = 68 |
21400688 | 0:22391cd705e2 | 346 | vol+ = 21 |
21400688 | 0:22391cd705e2 | 347 | vol- = 7 |
21400688 | 0:22391cd705e2 | 348 | */ |
21400688 | 0:22391cd705e2 | 349 | int main() |
21400688 | 0:22391cd705e2 | 350 | { |
21400688 | 0:22391cd705e2 | 351 | pc.printf("HEllO\r\n\r\n"); |
21400688 | 0:22391cd705e2 | 352 | pc.printf("maxv: %d, minv: %d,\r\n",maxv, minv); |
21400688 | 0:22391cd705e2 | 353 | |
21400688 | 0:22391cd705e2 | 354 | while(1) { |
21400688 | 0:22391cd705e2 | 355 | if (ir_rx.getState() == ReceiverIR::Received) { |
21400688 | 0:22391cd705e2 | 356 | bitcount = ir_rx.getData(&format, buf, sizeof(buf) * 8); |
21400688 | 0:22391cd705e2 | 357 | if (bitcount>0) { |
21400688 | 0:22391cd705e2 | 358 | decoded=buf[3]; |
21400688 | 0:22391cd705e2 | 359 | num = buf[2]; |
21400688 | 0:22391cd705e2 | 360 | //pc.printf("\r\nDecoded: %X %X %X %X ", buf[0],buf[1],num,buf[3]); |
21400688 | 0:22391cd705e2 | 361 | pc.printf("\r\nnum: %X %d", num, num); |
21400688 | 0:22391cd705e2 | 362 | switch(num){ |
21400688 | 0:22391cd705e2 | 363 | case 24: |
21400688 | 0:22391cd705e2 | 364 | moveForward(); |
21400688 | 0:22391cd705e2 | 365 | break; |
21400688 | 0:22391cd705e2 | 366 | case 90: |
21400688 | 0:22391cd705e2 | 367 | moveRight(); |
21400688 | 0:22391cd705e2 | 368 | break; |
21400688 | 0:22391cd705e2 | 369 | case 82: |
21400688 | 0:22391cd705e2 | 370 | moveBackward(); |
21400688 | 0:22391cd705e2 | 371 | break; |
21400688 | 0:22391cd705e2 | 372 | case 8: |
21400688 | 0:22391cd705e2 | 373 | moveLeft(); |
21400688 | 0:22391cd705e2 | 374 | break; |
21400688 | 0:22391cd705e2 | 375 | case 21: //속도상승 |
21400688 | 0:22391cd705e2 | 376 | speed+=0.1; |
21400688 | 0:22391cd705e2 | 377 | motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 378 | motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 379 | break; |
21400688 | 0:22391cd705e2 | 380 | case 7: //속도저하 |
21400688 | 0:22391cd705e2 | 381 | speed-=0.1; |
21400688 | 0:22391cd705e2 | 382 | motorDriver.setPwmApulsewidth(speed); |
21400688 | 0:22391cd705e2 | 383 | motorDriver.setPwmBpulsewidth(speed); |
21400688 | 0:22391cd705e2 | 384 | break; |
21400688 | 0:22391cd705e2 | 385 | case 28: |
21400688 | 0:22391cd705e2 | 386 | pc.printf("stop"); |
21400688 | 0:22391cd705e2 | 387 | moveStop(); |
21400688 | 0:22391cd705e2 | 388 | going=0; |
21400688 | 0:22391cd705e2 | 389 | break; |
21400688 | 0:22391cd705e2 | 390 | case 64: |
21400688 | 0:22391cd705e2 | 391 | pc.printf("drive\n\r"); |
21400688 | 0:22391cd705e2 | 392 | going=1; |
21400688 | 0:22391cd705e2 | 393 | timer.reset(); |
21400688 | 0:22391cd705e2 | 394 | drive(); |
21400688 | 0:22391cd705e2 | 395 | break; |
21400688 | 0:22391cd705e2 | 396 | case 68: |
21400688 | 0:22391cd705e2 | 397 | pc.printf("stop drive\n\r"); |
21400688 | 0:22391cd705e2 | 398 | going=0; |
21400688 | 0:22391cd705e2 | 399 | break; |
21400688 | 0:22391cd705e2 | 400 | } |
21400688 | 0:22391cd705e2 | 401 | } |
21400688 | 0:22391cd705e2 | 402 | } |
21400688 | 0:22391cd705e2 | 403 | } |
21400688 | 0:22391cd705e2 | 404 | } |