course1

Dependencies:   WS2812 PixelArray Adafruit_GFX HC_SR04_Ultrasonic_Library

Committer:
kmsmile2
Date:
Mon Jun 17 02:36:43 2019 +0000
Revision:
99:43e306bf6f13
Parent:
98:d8ead5e04047
Child:
101:18e7ac07b620
group2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kmsmile2 97:8f3abd5cf5ce 1 #include "mbed.h"
kmsmile2 97:8f3abd5cf5ce 2 #include "RemoteIR.h"
kmsmile2 97:8f3abd5cf5ce 3 #include "ReceiverIR.h"
kmsmile2 97:8f3abd5cf5ce 4 #include "TB6612FNG.h"
kmsmile2 97:8f3abd5cf5ce 5 #include "TRSensors.h"
kmsmile2 97:8f3abd5cf5ce 6 #include "ultrasonic.h"
kmsmile2 97:8f3abd5cf5ce 7 #include "Adafruit_SSD1306.h"
kmsmile2 98:d8ead5e04047 8 #include "WS2812.h"
kmsmile2 98:d8ead5e04047 9 #include "PixelArray.h"
kmsmile2 97:8f3abd5cf5ce 10
kmsmile2 97:8f3abd5cf5ce 11 #define button_SENSORS 5
kmsmile2 97:8f3abd5cf5ce 12 #define ADT7420_TEMP_REG (0x00)
kmsmile2 97:8f3abd5cf5ce 13 #define ADT7420_CONF_REG (0x03)
kmsmile2 97:8f3abd5cf5ce 14 #define EVAL_ADT7420_ADDR (0x48)
kmsmile2 97:8f3abd5cf5ce 15 #define PCF8574_ADDR (0x20)
kmsmile2 98:d8ead5e04047 16 #define WS2812_BUF 100
kmsmile2 98:d8ead5e04047 17 #define WS2812_BUF2 4
kmsmile2 98:d8ead5e04047 18 #define NUM_COLORS 3
kmsmile2 98:d8ead5e04047 19 #define NUM_LEDS_PER_COLOR 4
mbed_official 82:abf1b1785bd7 20
kmsmile2 97:8f3abd5cf5ce 21 // create object
kmsmile2 97:8f3abd5cf5ce 22 DigitalOut dc(D8,1);
kmsmile2 97:8f3abd5cf5ce 23 DigitalOut rst(D9,1);
kmsmile2 97:8f3abd5cf5ce 24 I2C i2c(I2C_SDA, I2C_SCL);
kmsmile2 97:8f3abd5cf5ce 25 TB6612FNG motorDriver(D6, A1, A0, D5, A2, A3);
kmsmile2 97:8f3abd5cf5ce 26 Ultrasonic ultra(D3, D2, .1, false);
kmsmile2 97:8f3abd5cf5ce 27 ReceiverIR IR(D4);
kmsmile2 97:8f3abd5cf5ce 28 TRSensors trs;
kmsmile2 97:8f3abd5cf5ce 29 RawSerial pc(USBTX, USBRX, 115200);
kmsmile2 97:8f3abd5cf5ce 30 Adafruit_SSD1306_I2c gOled2(i2c,D9,0x7A,64,128);
kmsmile2 97:8f3abd5cf5ce 31 Timer timer;
kmsmile2 98:d8ead5e04047 32 PixelArray px(WS2812_BUF);
kmsmile2 98:d8ead5e04047 33 WS2812 ws(D7, WS2812_BUF, 7, 15, 10, 15);
kmsmile2 99:43e306bf6f13 34 // Thread thread1;
kmsmile2 99:43e306bf6f13 35 // Thread thread2;
mbed_official 88:bea4f2daa48c 36
kmsmile2 99:43e306bf6f13 37
kmsmile2 97:8f3abd5cf5ce 38 unsigned int sensorValues[button_SENSORS];
kmsmile2 99:43e306bf6f13 39
kmsmile2 99:43e306bf6f13 40 // Error variables
kmsmile2 97:8f3abd5cf5ce 41 static int prev_err = 0;
kmsmile2 97:8f3abd5cf5ce 42 static int int_err = 0;
kmsmile2 99:43e306bf6f13 43 int err = 0;
kmsmile2 99:43e306bf6f13 44
kmsmile2 99:43e306bf6f13 45 // PID & velocity value
kmsmile2 98:d8ead5e04047 46 static float pval = 0.15;
kmsmile2 98:d8ead5e04047 47 static float ival = 0.00003;
kmsmile2 98:d8ead5e04047 48 static float dval = 2.3;
kmsmile2 98:d8ead5e04047 49 static float vel = 120.0;
kmsmile2 99:43e306bf6f13 50
kmsmile2 97:8f3abd5cf5ce 51 volatile int button = 0;
kmsmile2 97:8f3abd5cf5ce 52 int position = 0;
kmsmile2 97:8f3abd5cf5ce 53 int dist = 0;
kmsmile2 97:8f3abd5cf5ce 54 int cnt = 0;
kmsmile2 99:43e306bf6f13 55 float t = 0;
mbed_official 82:abf1b1785bd7 56
kmsmile2 97:8f3abd5cf5ce 57 // Reflective sensor
kmsmile2 97:8f3abd5cf5ce 58 bool rightObs= false;
kmsmile2 97:8f3abd5cf5ce 59 bool leftObs = false;
kmsmile2 97:8f3abd5cf5ce 60
kmsmile2 97:8f3abd5cf5ce 61 // motor Driver
kmsmile2 97:8f3abd5cf5ce 62 float fPwmAPeriod;
kmsmile2 97:8f3abd5cf5ce 63 float fPwmAPulsewidth;
kmsmile2 97:8f3abd5cf5ce 64 float fPwmBPeriod;
kmsmile2 97:8f3abd5cf5ce 65 float fPwmBPulsewidth;
mbed_official 82:abf1b1785bd7 66
kmsmile2 97:8f3abd5cf5ce 67 // opearte ultrasonic
kmsmile2 98:d8ead5e04047 68 //void ultrasonic_thread()
kmsmile2 98:d8ead5e04047 69 //{
kmsmile2 98:d8ead5e04047 70 // while (1)
kmsmile2 98:d8ead5e04047 71 // {
kmsmile2 98:d8ead5e04047 72 // ultra.setMode(false); // have updates every .1 seconds and try only once.
kmsmile2 98:d8ead5e04047 73 // ultra.trig();
kmsmile2 98:d8ead5e04047 74 // dist = ultra.getDistance();
kmsmile2 98:d8ead5e04047 75 // pc.printf("Detected value: %d\r\n", dist);
kmsmile2 98:d8ead5e04047 76 // }
kmsmile2 98:d8ead5e04047 77 //}
kmsmile2 98:d8ead5e04047 78
kmsmile2 97:8f3abd5cf5ce 79
kmsmile2 97:8f3abd5cf5ce 80 // opearte reflective sensors
kmsmile2 97:8f3abd5cf5ce 81 /*
kmsmile2 97:8f3abd5cf5ce 82 void reflective_thread()
kmsmile2 97:8f3abd5cf5ce 83 {
kmsmile2 97:8f3abd5cf5ce 84 while(1){
kmsmile2 97:8f3abd5cf5ce 85 char data_write[2];
kmsmile2 97:8f3abd5cf5ce 86 char data_read[2];
kmsmile2 97:8f3abd5cf5ce 87 i2c.frequency(100000);
kmsmile2 97:8f3abd5cf5ce 88 i2c.read((PCF8574_ADDR<<1|0x01), data_read, 2, 0);
kmsmile2 97:8f3abd5cf5ce 89 pc.printf("hexa: %x, %x\r\n", data_read[0], data_read[1]);
kmsmile2 97:8f3abd5cf5ce 90 if(data_read[0]>>6 == 0b10)
kmsmile2 97:8f3abd5cf5ce 91 {
kmsmile2 97:8f3abd5cf5ce 92 pc.printf("Obstacle on left!\r\n");
kmsmile2 97:8f3abd5cf5ce 93 data_write[0] = 0xDF;
kmsmile2 97:8f3abd5cf5ce 94 leftObs = true;
kmsmile2 97:8f3abd5cf5ce 95 }
kmsmile2 97:8f3abd5cf5ce 96 else if(data_read[0]>>6 == 0b01)
kmsmile2 97:8f3abd5cf5ce 97 {
kmsmile2 97:8f3abd5cf5ce 98 pc.printf("Obstacle on right!\r\n");
kmsmile2 97:8f3abd5cf5ce 99 data_write[0] = 0xDF;
kmsmile2 97:8f3abd5cf5ce 100 rightObs = true;
kmsmile2 97:8f3abd5cf5ce 101 }
kmsmile2 97:8f3abd5cf5ce 102 else if(data_read[0]>>6 == 0b00)
kmsmile2 97:8f3abd5cf5ce 103 {
kmsmile2 97:8f3abd5cf5ce 104 pc.printf("Obstacle on front!\r\n");
kmsmile2 97:8f3abd5cf5ce 105 data_write[0] = 0xDF;
kmsmile2 97:8f3abd5cf5ce 106 leftObs = true;
kmsmile2 97:8f3abd5cf5ce 107 rightObs = true;
kmsmile2 97:8f3abd5cf5ce 108 wait(3);
kmsmile2 97:8f3abd5cf5ce 109 }
kmsmile2 97:8f3abd5cf5ce 110
kmsmile2 97:8f3abd5cf5ce 111 else
kmsmile2 97:8f3abd5cf5ce 112 {
kmsmile2 97:8f3abd5cf5ce 113 data_write[0]=0xFF;
kmsmile2 97:8f3abd5cf5ce 114 }
kmsmile2 97:8f3abd5cf5ce 115 i2c.write((PCF8574_ADDR<<1),data_write,1,0);
kmsmile2 97:8f3abd5cf5ce 116 }
kmsmile2 97:8f3abd5cf5ce 117 }*/
kmsmile2 97:8f3abd5cf5ce 118
kmsmile2 97:8f3abd5cf5ce 119 inline void update_display(){
kmsmile2 97:8f3abd5cf5ce 120 gOled2.clearDisplay();
kmsmile2 97:8f3abd5cf5ce 121 gOled2.setTextCursor(0,0);
kmsmile2 97:8f3abd5cf5ce 122 gOled2.printf("E-RON alphabot\r\n");
kmsmile2 97:8f3abd5cf5ce 123 gOled2.printf("Timer: %.2f\r\n",t);
kmsmile2 97:8f3abd5cf5ce 124 gOled2.printf("V: %.0f\r\n",vel);
kmsmile2 97:8f3abd5cf5ce 125 gOled2.printf("P: %.2f\r\n",pval);
kmsmile2 97:8f3abd5cf5ce 126 gOled2.printf("I: %.5f\r\n",ival);
kmsmile2 97:8f3abd5cf5ce 127 gOled2.printf("D: %.2f\r\n",dval);
kmsmile2 97:8f3abd5cf5ce 128 gOled2.display();
kmsmile2 97:8f3abd5cf5ce 129 }
kmsmile2 98:d8ead5e04047 130 int colorbuf[NUM_COLORS] = {0xff0000,0x00ff00,0x0000ff};
kmsmile2 98:d8ead5e04047 131 int colorbuf3 =0x000000;
kmsmile2 99:43e306bf6f13 132
kmsmile2 97:8f3abd5cf5ce 133 int main()
kmsmile2 98:d8ead5e04047 134 {
kmsmile2 98:d8ead5e04047 135 for (int i = 0; i < WS2812_BUF; i++) {
kmsmile2 98:d8ead5e04047 136 px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
kmsmile2 98:d8ead5e04047 137 }
kmsmile2 98:d8ead5e04047 138 for (int j=0; j<WS2812_BUF; j++) {
kmsmile2 98:d8ead5e04047 139 // px.SetI(pixel position, II value)
kmsmile2 98:d8ead5e04047 140 px.SetI(j%WS2812_BUF, 0xf+(0xf*4));
kmsmile2 98:d8ead5e04047 141 }
kmsmile2 97:8f3abd5cf5ce 142
kmsmile2 97:8f3abd5cf5ce 143 // thread1.start(ultrasonic_thread);
kmsmile2 97:8f3abd5cf5ce 144 // thread2.start(reflective_thread);
kmsmile2 97:8f3abd5cf5ce 145 RemoteIR::Format format;
kmsmile2 97:8f3abd5cf5ce 146 uint8_t buf[32];
kmsmile2 97:8f3abd5cf5ce 147 int bitcount;
kmsmile2 97:8f3abd5cf5ce 148 update_display();
kmsmile2 97:8f3abd5cf5ce 149 while(1)
kmsmile2 97:8f3abd5cf5ce 150 { // read the value of the code
kmsmile2 97:8f3abd5cf5ce 151 if (IR.getState() == ReceiverIR::Received)
kmsmile2 97:8f3abd5cf5ce 152 {
kmsmile2 97:8f3abd5cf5ce 153 bitcount = IR.getData(&format, buf, sizeof(buf) * 8);
kmsmile2 97:8f3abd5cf5ce 154 pc.printf("%d\r\n", bitcount);
kmsmile2 97:8f3abd5cf5ce 155 pc.printf("buf[2]: %d\r\n", buf[2]);
kmsmile2 97:8f3abd5cf5ce 156 button = buf[2];
kmsmile2 97:8f3abd5cf5ce 157 }
kmsmile2 97:8f3abd5cf5ce 158
kmsmile2 97:8f3abd5cf5ce 159 switch(button)
kmsmile2 97:8f3abd5cf5ce 160 {
kmsmile2 97:8f3abd5cf5ce 161 case 0x45:
kmsmile2 97:8f3abd5cf5ce 162 // ch- button (P value up)
kmsmile2 97:8f3abd5cf5ce 163 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 164 pval += 0.01;
kmsmile2 97:8f3abd5cf5ce 165 pval = fabs(pval);
kmsmile2 97:8f3abd5cf5ce 166 update_display();
kmsmile2 97:8f3abd5cf5ce 167 // pc.printf("pval:%f\r\n", pval);
kmsmile2 97:8f3abd5cf5ce 168 wait(0.3);
kmsmile2 97:8f3abd5cf5ce 169 break;
kmsmile2 97:8f3abd5cf5ce 170
kmsmile2 97:8f3abd5cf5ce 171 case 0x46:
kmsmile2 97:8f3abd5cf5ce 172 // ch common button (I value up)
kmsmile2 97:8f3abd5cf5ce 173 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 174 ival += 0.00001;
kmsmile2 97:8f3abd5cf5ce 175 ival = fabs(ival);
kmsmile2 97:8f3abd5cf5ce 176 update_display() ;
kmsmile2 97:8f3abd5cf5ce 177 // pc.printf("ival:%f\r\n", ival);
kmsmile2 97:8f3abd5cf5ce 178 wait(0.3);
kmsmile2 97:8f3abd5cf5ce 179 break;
kmsmile2 97:8f3abd5cf5ce 180
kmsmile2 97:8f3abd5cf5ce 181 case 0x47:
kmsmile2 97:8f3abd5cf5ce 182 // ch+ button (D value up)
kmsmile2 97:8f3abd5cf5ce 183 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 184 dval += 0.05;
kmsmile2 97:8f3abd5cf5ce 185 dval = fabs(dval);
kmsmile2 97:8f3abd5cf5ce 186 update_display();
kmsmile2 97:8f3abd5cf5ce 187 wait(0.3);
kmsmile2 97:8f3abd5cf5ce 188 break;
kmsmile2 97:8f3abd5cf5ce 189
kmsmile2 97:8f3abd5cf5ce 190 case 0x44:
kmsmile2 97:8f3abd5cf5ce 191 // prev button (P value down)
kmsmile2 97:8f3abd5cf5ce 192 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 193 pval -= 0.01;
kmsmile2 97:8f3abd5cf5ce 194 pval = fabs(pval);
kmsmile2 97:8f3abd5cf5ce 195 update_display();
kmsmile2 97:8f3abd5cf5ce 196 wait(0.3);
kmsmile2 97:8f3abd5cf5ce 197 break;
kmsmile2 97:8f3abd5cf5ce 198
kmsmile2 97:8f3abd5cf5ce 199 case 0x40:
kmsmile2 97:8f3abd5cf5ce 200 // next button (I value down)
kmsmile2 97:8f3abd5cf5ce 201 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 202 ival -= 0.00001;
kmsmile2 97:8f3abd5cf5ce 203 ival = fabs(ival);
kmsmile2 97:8f3abd5cf5ce 204 update_display();
kmsmile2 97:8f3abd5cf5ce 205 wait(0.3);
kmsmile2 97:8f3abd5cf5ce 206 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 207 break;
kmsmile2 97:8f3abd5cf5ce 208
kmsmile2 97:8f3abd5cf5ce 209 case 0x43:
kmsmile2 97:8f3abd5cf5ce 210 // play/pause button (D value down)
kmsmile2 97:8f3abd5cf5ce 211 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 212 dval -= 0.05;
kmsmile2 97:8f3abd5cf5ce 213 dval = fabs(dval);
kmsmile2 97:8f3abd5cf5ce 214 update_display();
kmsmile2 97:8f3abd5cf5ce 215 wait(0.3);
kmsmile2 97:8f3abd5cf5ce 216 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 217 break;
kmsmile2 97:8f3abd5cf5ce 218
kmsmile2 97:8f3abd5cf5ce 219 case 0x07:
kmsmile2 97:8f3abd5cf5ce 220 // vol- button (velocity down)
kmsmile2 97:8f3abd5cf5ce 221 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 222 vel -= 5;
kmsmile2 97:8f3abd5cf5ce 223 update_display();
kmsmile2 97:8f3abd5cf5ce 224 wait(0.3);
kmsmile2 97:8f3abd5cf5ce 225 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 226 break;
kmsmile2 97:8f3abd5cf5ce 227
kmsmile2 97:8f3abd5cf5ce 228 case 0x15:
kmsmile2 97:8f3abd5cf5ce 229 // vol+ button (velocity up)
kmsmile2 97:8f3abd5cf5ce 230 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 231 vel += 5;
kmsmile2 97:8f3abd5cf5ce 232 update_display();
kmsmile2 97:8f3abd5cf5ce 233 wait(0.3);
kmsmile2 97:8f3abd5cf5ce 234 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 235 break;
kmsmile2 97:8f3abd5cf5ce 236
kmsmile2 97:8f3abd5cf5ce 237 case 0x16:
kmsmile2 97:8f3abd5cf5ce 238 // 0 button (Reset the error variables)
kmsmile2 97:8f3abd5cf5ce 239 int_err = 0;
kmsmile2 97:8f3abd5cf5ce 240 err = 0;
kmsmile2 97:8f3abd5cf5ce 241 prev_err = 0;
kmsmile2 97:8f3abd5cf5ce 242 wait(1);
kmsmile2 97:8f3abd5cf5ce 243 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 244 break;
kmsmile2 97:8f3abd5cf5ce 245
kmsmile2 97:8f3abd5cf5ce 246 case 0x19:
kmsmile2 97:8f3abd5cf5ce 247 // 100+ button (Calibrate)
kmsmile2 97:8f3abd5cf5ce 248 pc.printf("calibrate!\r\n");
kmsmile2 97:8f3abd5cf5ce 249 trs.calibrate();
kmsmile2 97:8f3abd5cf5ce 250 pc.printf("calibrate done\r\n");
kmsmile2 97:8f3abd5cf5ce 251 wait(0.2);
kmsmile2 97:8f3abd5cf5ce 252 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 253 break;
kmsmile2 97:8f3abd5cf5ce 254
kmsmile2 97:8f3abd5cf5ce 255 case 0x18:
kmsmile2 97:8f3abd5cf5ce 256 // 2 button (move forward)
kmsmile2 97:8f3abd5cf5ce 257 motorDriver.user_forward(0.3,0.3);
kmsmile2 98:d8ead5e04047 258 trs.calibrate();
kmsmile2 97:8f3abd5cf5ce 259 wait(0.1);
kmsmile2 97:8f3abd5cf5ce 260 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 261 break;
kmsmile2 97:8f3abd5cf5ce 262
kmsmile2 97:8f3abd5cf5ce 263 case 0x52:
kmsmile2 97:8f3abd5cf5ce 264 // 8 button (move backward)
kmsmile2 97:8f3abd5cf5ce 265 motorDriver.user_backward(0.3,0.3);
kmsmile2 98:d8ead5e04047 266 trs.calibrate();
kmsmile2 97:8f3abd5cf5ce 267 wait(0.1);
kmsmile2 97:8f3abd5cf5ce 268 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 269 break;
kmsmile2 97:8f3abd5cf5ce 270
kmsmile2 97:8f3abd5cf5ce 271 case 0x5A:
kmsmile2 97:8f3abd5cf5ce 272 // 6 button (clockwise turn)
kmsmile2 97:8f3abd5cf5ce 273 motorDriver.user_right(0.3,0.1);
kmsmile2 97:8f3abd5cf5ce 274 wait(0.1);
kmsmile2 97:8f3abd5cf5ce 275 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 276 break;
kmsmile2 97:8f3abd5cf5ce 277
kmsmile2 97:8f3abd5cf5ce 278 case 0x08:
kmsmile2 97:8f3abd5cf5ce 279 // 4 button (counter clockwise turn)
kmsmile2 97:8f3abd5cf5ce 280 motorDriver.user_left(0.1,0.3);
kmsmile2 97:8f3abd5cf5ce 281 wait(0.1);
kmsmile2 97:8f3abd5cf5ce 282 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 283 break;
kmsmile2 97:8f3abd5cf5ce 284
kmsmile2 97:8f3abd5cf5ce 285 case 0x1C:
kmsmile2 97:8f3abd5cf5ce 286 // 5 button (motor stop)
kmsmile2 97:8f3abd5cf5ce 287 motorDriver.stop();
kmsmile2 97:8f3abd5cf5ce 288 break;
kmsmile2 97:8f3abd5cf5ce 289
kmsmile2 97:8f3abd5cf5ce 290 case 0x0D:
kmsmile2 97:8f3abd5cf5ce 291 // 200+ button (line tracer)
kmsmile2 97:8f3abd5cf5ce 292 t=0;
kmsmile2 97:8f3abd5cf5ce 293 timer.reset();
kmsmile2 97:8f3abd5cf5ce 294 timer.start();
kmsmile2 98:d8ead5e04047 295 ultra.setMode(false); // have updates every .1 seconds and try only once.
kmsmile2 98:d8ead5e04047 296
kmsmile2 97:8f3abd5cf5ce 297 while(1)
kmsmile2 97:8f3abd5cf5ce 298 {
kmsmile2 97:8f3abd5cf5ce 299 t=timer.read();
kmsmile2 97:8f3abd5cf5ce 300 position=trs.readLine(sensorValues,0);
kmsmile2 98:d8ead5e04047 301 ultra.trig();
kmsmile2 98:d8ead5e04047 302 dist = ultra.getDistance();
kmsmile2 98:d8ead5e04047 303 pc.printf("dist:%d\r\n", dist);
kmsmile2 98:d8ead5e04047 304 if(dist<=21){
kmsmile2 98:d8ead5e04047 305 motorDriver.user_left(0.2,0.2);
kmsmile2 98:d8ead5e04047 306 wait(0.1);
kmsmile2 98:d8ead5e04047 307 while(1){
kmsmile2 98:d8ead5e04047 308
kmsmile2 98:d8ead5e04047 309 // pc.printf("distance: %d\r\n", dist);
kmsmile2 98:d8ead5e04047 310 // pc.printf("position: %d\r\n", position);
kmsmile2 97:8f3abd5cf5ce 311 position=trs.readLine(sensorValues,0);
kmsmile2 97:8f3abd5cf5ce 312 if(position > 2000)
kmsmile2 97:8f3abd5cf5ce 313 {
kmsmile2 97:8f3abd5cf5ce 314 int_err = 0;
kmsmile2 97:8f3abd5cf5ce 315 err = 0;
kmsmile2 97:8f3abd5cf5ce 316 prev_err = 0;
kmsmile2 97:8f3abd5cf5ce 317 break;
kmsmile2 97:8f3abd5cf5ce 318 }
kmsmile2 97:8f3abd5cf5ce 319 }
kmsmile2 98:d8ead5e04047 320 }
kmsmile2 97:8f3abd5cf5ce 321
kmsmile2 97:8f3abd5cf5ce 322 err=(int)position-2000; // error>0 --> right, error<0 --> left
kmsmile2 97:8f3abd5cf5ce 323 int_err+=err;
kmsmile2 97:8f3abd5cf5ce 324
kmsmile2 97:8f3abd5cf5ce 325 const float kp = pval;
kmsmile2 97:8f3abd5cf5ce 326 const float kd = dval;
kmsmile2 97:8f3abd5cf5ce 327 const float ki = ival;
kmsmile2 97:8f3abd5cf5ce 328
kmsmile2 97:8f3abd5cf5ce 329 float power_difference = kp*err + kd*(err-prev_err) + ki*int_err; //+derivative; //error를 누적하는 것
kmsmile2 97:8f3abd5cf5ce 330 prev_err = err;
kmsmile2 97:8f3abd5cf5ce 331
kmsmile2 97:8f3abd5cf5ce 332 const float maximum=vel;
kmsmile2 97:8f3abd5cf5ce 333 if(power_difference > maximum)
kmsmile2 97:8f3abd5cf5ce 334 power_difference = maximum;
kmsmile2 97:8f3abd5cf5ce 335 if(power_difference < -maximum)
kmsmile2 97:8f3abd5cf5ce 336 power_difference = -maximum;
kmsmile2 97:8f3abd5cf5ce 337
kmsmile2 97:8f3abd5cf5ce 338 if(power_difference<0)
kmsmile2 99:43e306bf6f13 339 motorDriver.user_forward((maximum)/255,(maximum+power_difference)/255);
kmsmile2 97:8f3abd5cf5ce 340 else
kmsmile2 99:43e306bf6f13 341 motorDriver.user_forward((maximum-power_difference)/255,(maximum)/255);
kmsmile2 97:8f3abd5cf5ce 342
kmsmile2 97:8f3abd5cf5ce 343 pc.printf("position value: %d\r\n", position);
kmsmile2 97:8f3abd5cf5ce 344
kmsmile2 97:8f3abd5cf5ce 345 //pc.printf("cnt: %d\r\n", cnt);
kmsmile2 97:8f3abd5cf5ce 346 for(int i=0;i<5;i++){
kmsmile2 97:8f3abd5cf5ce 347 pc.printf("%d\r\n",sensorValues[i]);
kmsmile2 97:8f3abd5cf5ce 348 }
kmsmile2 97:8f3abd5cf5ce 349 if((sensorValues[0] > 650) && (sensorValues[1]>650) && (sensorValues[2]>650) && (sensorValues[3]>650) && (sensorValues[4]>650))
kmsmile2 97:8f3abd5cf5ce 350 { // 5 IR sensor are on black
kmsmile2 97:8f3abd5cf5ce 351 timer.stop();
kmsmile2 97:8f3abd5cf5ce 352 t=timer.read();
kmsmile2 97:8f3abd5cf5ce 353 update_display();
kmsmile2 98:d8ead5e04047 354 motorDriver.stop();
kmsmile2 98:d8ead5e04047 355 for(int z=48;z>=0;z=z-4){
kmsmile2 98:d8ead5e04047 356 ws.write_offsets(px.getBuf(),z,z,z);
kmsmile2 98:d8ead5e04047 357 wait(0.1);
kmsmile2 98:d8ead5e04047 358 }
kmsmile2 98:d8ead5e04047 359
kmsmile2 97:8f3abd5cf5ce 360 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 361 break;
kmsmile2 97:8f3abd5cf5ce 362 }
kmsmile2 97:8f3abd5cf5ce 363 }
kmsmile2 97:8f3abd5cf5ce 364 break;
kmsmile2 97:8f3abd5cf5ce 365
kmsmile2 97:8f3abd5cf5ce 366 case 0x42:
kmsmile2 97:8f3abd5cf5ce 367 // 7 button (read sensor values)
kmsmile2 98:d8ead5e04047 368 position=trs.readLine(sensorValues,0);
kmsmile2 98:d8ead5e04047 369 for(int i=0; i<5; i++) {
kmsmile2 98:d8ead5e04047 370 pc.printf("%d\r\n",sensorValues[i]);
kmsmile2 97:8f3abd5cf5ce 371 }
kmsmile2 98:d8ead5e04047 372 pc.printf("done!\r\n");
kmsmile2 98:d8ead5e04047 373 button = 0x1C;
kmsmile2 98:d8ead5e04047 374 break;
kmsmile2 98:d8ead5e04047 375
kmsmile2 97:8f3abd5cf5ce 376
kmsmile2 97:8f3abd5cf5ce 377 case 0x4A:
kmsmile2 97:8f3abd5cf5ce 378 // 9 button (read position)
kmsmile2 97:8f3abd5cf5ce 379 int j = 0;
kmsmile2 97:8f3abd5cf5ce 380 while(j!=100)
kmsmile2 97:8f3abd5cf5ce 381 {
kmsmile2 97:8f3abd5cf5ce 382 j++;
kmsmile2 97:8f3abd5cf5ce 383 position=trs.readLine(sensorValues,0);
kmsmile2 97:8f3abd5cf5ce 384 pc.printf("%d\r\n",position);
kmsmile2 97:8f3abd5cf5ce 385 }
kmsmile2 97:8f3abd5cf5ce 386 button = 0x1C;
kmsmile2 97:8f3abd5cf5ce 387 break;
kmsmile2 98:d8ead5e04047 388 default:
kmsmile2 97:8f3abd5cf5ce 389 // wrong button
kmsmile2 97:8f3abd5cf5ce 390 pc.printf("wrong button!\r\n");
kmsmile2 97:8f3abd5cf5ce 391 break;
kmsmile2 97:8f3abd5cf5ce 392 }
kmsmile2 97:8f3abd5cf5ce 393 }
kmsmile2 97:8f3abd5cf5ce 394 }