DERPS
Dependencies: BMP280 BNO055_fusion PowerControl mbed
Fork of STRAIGHT_DRIVE_NO_SEP by
main.cpp@37:e8a6ea255c09, 2016-04-26 (annotated)
- Committer:
- 12104404
- Date:
- Tue Apr 26 20:09:25 2016 +0000
- Revision:
- 37:e8a6ea255c09
- Parent:
- 36:dc69442c4c47
- Child:
- 38:16208e003dc9
WORKING REALLY WORKING
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
12104404 | 0:96d6eb224379 | 1 | #include "LOCALIZE.h" |
12104404 | 6:0602a9e8118b | 2 | #include "LOCOMOTION.h" |
12104404 | 23:455f7da3dd7a | 3 | #include "SAFETY.h" |
12104404 | 30:116cd143fdf7 | 4 | #include "BMP280.h" |
12104404 | 0:96d6eb224379 | 5 | |
12104404 | 19:2dd81b864e14 | 6 | #define WATCHDOG_TIME 10 |
12104404 | 27:fb1298d35bd1 | 7 | //#define PC_MODE 1 |
12104404 | 6:0602a9e8118b | 8 | |
12104404 | 19:2dd81b864e14 | 9 | #if defined (PC_MODE) |
12104404 | 19:2dd81b864e14 | 10 | Serial pc(USBTX, USBRX); |
12104404 | 19:2dd81b864e14 | 11 | #else |
12104404 | 8:b36be08c44f8 | 12 | Serial pc(p13, p14); |
12104404 | 19:2dd81b864e14 | 13 | Ticker t; |
12104404 | 19:2dd81b864e14 | 14 | void send(); |
12104404 | 19:2dd81b864e14 | 15 | #endif |
12104404 | 19:2dd81b864e14 | 16 | |
12104404 | 23:455f7da3dd7a | 17 | DigitalOut led1(LED1); |
12104404 | 23:455f7da3dd7a | 18 | DigitalOut led2(LED2); |
12104404 | 23:455f7da3dd7a | 19 | DigitalOut led3(LED3); |
12104404 | 23:455f7da3dd7a | 20 | DigitalOut led4(LED4); |
12104404 | 6:0602a9e8118b | 21 | |
12104404 | 23:455f7da3dd7a | 22 | SAFETY safe; |
12104404 | 19:2dd81b864e14 | 23 | PwmOut suction(p26); |
12104404 | 27:fb1298d35bd1 | 24 | I2C i2c2(p28, p27); |
12104404 | 27:fb1298d35bd1 | 25 | I2C i2c1(p9, p10); |
12104404 | 30:116cd143fdf7 | 26 | BMP280 pres(i2c2); |
12104404 | 23:455f7da3dd7a | 27 | LOCALIZE loc(i2c1, i2c2, p29, p8, p7, p6, p5, led1, led2, led3, led4); |
12104404 | 0:96d6eb224379 | 28 | LOCALIZE_xya xya; |
12104404 | 27:fb1298d35bd1 | 29 | LOCOMOTION motion(p11, p23, p24, p21, p22, p16, p15, led1, led2, led3, led4); |
12104404 | 27:fb1298d35bd1 | 30 | //LOCOMOTION motion(p11, p21, p22, p23, p24, p15, p16, led1, led2, led3, led4); |
12104404 | 6:0602a9e8118b | 31 | |
12104404 | 20:10a305298e27 | 32 | void BrownOut(); |
12104404 | 6:0602a9e8118b | 33 | |
12104404 | 30:116cd143fdf7 | 34 | int xTarget=120; |
12104404 | 36:dc69442c4c47 | 35 | int angle_error=1; |
12104404 | 25:f3bf8351bbd4 | 36 | bool xGood=false; |
12104404 | 25:f3bf8351bbd4 | 37 | bool yGood=false; |
12104404 | 25:f3bf8351bbd4 | 38 | bool angleGood=false; |
12104404 | 35:68917796c30a | 39 | bool flip=false; |
alaurens | 33:baf24c59affc | 40 | int xState=X_INCREASE; |
12104404 | 37:e8a6ea255c09 | 41 | int angleTarget=355; |
12104404 | 35:68917796c30a | 42 | int yTarget=ROB_SIZE/2; |
12104404 | 30:116cd143fdf7 | 43 | int pressure; |
12104404 | 25:f3bf8351bbd4 | 44 | |
12104404 | 20:10a305298e27 | 45 | //bool flag=false; |
12104404 | 20:10a305298e27 | 46 | //int target=20; |
12104404 | 20:10a305298e27 | 47 | //int angle_error=2; |
12104404 | 20:10a305298e27 | 48 | //bool xGood=false; |
12104404 | 20:10a305298e27 | 49 | //int angleTarget=0; |
12104404 | 6:0602a9e8118b | 50 | |
12104404 | 0:96d6eb224379 | 51 | int main() |
12104404 | 0:96d6eb224379 | 52 | { |
12104404 | 19:2dd81b864e14 | 53 | //Set ESC Period |
12104404 | 19:2dd81b864e14 | 54 | suction.period_ms(2); |
12104404 | 19:2dd81b864e14 | 55 | //Initialize to 0 |
12104404 | 19:2dd81b864e14 | 56 | suction.pulsewidth_us(1000); |
12104404 | 27:fb1298d35bd1 | 57 | wait(2); |
12104404 | 27:fb1298d35bd1 | 58 | /*suction.pulsewidth_us(1000); |
12104404 | 27:fb1298d35bd1 | 59 | wait(2);*/ |
12104404 | 24:fb1f083ebd62 | 60 | //Watchdog Reset Indicator |
12104404 | 24:fb1f083ebd62 | 61 | if ((LPC_WDT->WDMOD>>2)&1) |
12104404 | 24:fb1f083ebd62 | 62 | led4=1; |
12104404 | 24:fb1f083ebd62 | 63 | else |
12104404 | 24:fb1f083ebd62 | 64 | led3=1; |
12104404 | 19:2dd81b864e14 | 65 | //Start Watchdog |
12104404 | 23:455f7da3dd7a | 66 | safe.kick(WATCHDOG_TIME); |
12104404 | 23:455f7da3dd7a | 67 | //Power Down Ethernet and USB and Enable Brown Out Interrupt |
12104404 | 23:455f7da3dd7a | 68 | safe.init((unsigned)BrownOut); |
12104404 | 19:2dd81b864e14 | 69 | //Serial Baudrate |
12104404 | 0:96d6eb224379 | 70 | pc.baud(9600); |
12104404 | 26:0ea6a550a99d | 71 | //Initialize Locomotion |
12104404 | 26:0ea6a550a99d | 72 | loc.init(); |
12104404 | 30:116cd143fdf7 | 73 | //pres.initialize(); |
12104404 | 19:2dd81b864e14 | 74 | //Attach Periodic Wireless Printing |
12104404 | 19:2dd81b864e14 | 75 | #if not defined(PC_MODE) |
12104404 | 8:b36be08c44f8 | 76 | t.attach(&send,1); |
12104404 | 19:2dd81b864e14 | 77 | #endif |
12104404 | 24:fb1f083ebd62 | 78 | led1=0; |
12104404 | 24:fb1f083ebd62 | 79 | led2=0; |
12104404 | 24:fb1f083ebd62 | 80 | led3=0; |
12104404 | 24:fb1f083ebd62 | 81 | led4=0; |
alaurens | 31:69caabc10879 | 82 | suction.pulsewidth_us(1000); |
12104404 | 30:116cd143fdf7 | 83 | wait(0.5); |
alaurens | 31:69caabc10879 | 84 | suction.pulsewidth_us(1050); |
12104404 | 30:116cd143fdf7 | 85 | wait(0.5); |
alaurens | 31:69caabc10879 | 86 | suction.pulsewidth_us(1100); |
12104404 | 30:116cd143fdf7 | 87 | wait(0.5); |
alaurens | 31:69caabc10879 | 88 | suction.pulsewidth_us(1150); |
12104404 | 30:116cd143fdf7 | 89 | wait(0.5); |
12104404 | 37:e8a6ea255c09 | 90 | suction.pulsewidth_us(1200); |
12104404 | 37:e8a6ea255c09 | 91 | wait(0.5); |
alaurens | 33:baf24c59affc | 92 | suction.pulsewidth_us(1250); |
alaurens | 33:baf24c59affc | 93 | wait(0.5); |
12104404 | 0:96d6eb224379 | 94 | while(1) { |
12104404 | 37:e8a6ea255c09 | 95 | suction.pulsewidth_us(1285); |
12104404 | 36:dc69442c4c47 | 96 | /*pressure=(int)read[0];//(int)pres.getTemperature(); |
alaurens | 31:69caabc10879 | 97 | if(pressure==88) |
12104404 | 36:dc69442c4c47 | 98 | led1=1; |
12104404 | 27:fb1298d35bd1 | 99 | else |
12104404 | 36:dc69442c4c47 | 100 | led1=0;*/ |
alaurens | 33:baf24c59affc | 101 | //uncomment this part if you want the robot to just drive down the window with no separtor |
alaurens | 31:69caabc10879 | 102 | if (xState==0) { |
12104404 | 26:0ea6a550a99d | 103 | motion.mStop(); |
12104404 | 26:0ea6a550a99d | 104 | safe.kick(); |
12104404 | 26:0ea6a550a99d | 105 | continue; |
12104404 | 26:0ea6a550a99d | 106 | } |
12104404 | 25:f3bf8351bbd4 | 107 | loc.get_xy(&xya); |
12104404 | 36:dc69442c4c47 | 108 | //motion.check_xya(&xGood,&yGood,&angleGood,xTarget,angleTarget,yTarget,xya,3,3,angle_error); |
12104404 | 36:dc69442c4c47 | 109 | |
12104404 | 36:dc69442c4c47 | 110 | //motion.setXstate(&xState,&xTarget,xGood,angleGood,&angleTarget,&angle_error,yTarget); |
alaurens | 34:083073e54dbd | 111 | //motion.setAngleTol(&angle_error,yGood,xGood); |
12104404 | 36:dc69442c4c47 | 112 | // motion.setYgoal(xGood,angleGood,yGood,&yTarget); |
12104404 | 36:dc69442c4c47 | 113 | //motion.setXPos(xTarget,xya.x,5,angleTarget,xya.a); |
12104404 | 36:dc69442c4c47 | 114 | xGood=motion.setXPos(xTarget,xya.x,2,0); |
12104404 | 36:dc69442c4c47 | 115 | if(!xGood) |
12104404 | 37:e8a6ea255c09 | 116 | motion.setAngle(angleTarget,xya.a,angle_error,ANGLE_BIAS); |
12104404 | 36:dc69442c4c47 | 117 | else { |
12104404 | 36:dc69442c4c47 | 118 | xTarget=(xTarget==120)?30:120; |
12104404 | 37:e8a6ea255c09 | 119 | angleTarget=(angleTarget==5)?-5:5; |
12104404 | 36:dc69442c4c47 | 120 | } |
12104404 | 35:68917796c30a | 121 | //motion.setYBias(0,xya.y,2,angleTarget); |
12104404 | 36:dc69442c4c47 | 122 | //loc.get_xy(&xya); |
12104404 | 19:2dd81b864e14 | 123 | #if defined(PC_MODE) |
12104404 | 36:dc69442c4c47 | 124 | pc.printf("X: %3d\tY: %3d\tP: %3d\txGood: %d\tAngleGood: %d\tState: %d \n",xya.y,yTarget,xya.a,xGood,angleGood,xState); |
12104404 | 19:2dd81b864e14 | 125 | #endif |
12104404 | 19:2dd81b864e14 | 126 | //Feed the dog |
12104404 | 23:455f7da3dd7a | 127 | safe.kick(); |
12104404 | 0:96d6eb224379 | 128 | } |
12104404 | 0:96d6eb224379 | 129 | } |
12104404 | 6:0602a9e8118b | 130 | |
12104404 | 6:0602a9e8118b | 131 | void send() |
12104404 | 6:0602a9e8118b | 132 | { |
12104404 | 19:2dd81b864e14 | 133 | //Print over serial port to WiFi MCU |
12104404 | 36:dc69442c4c47 | 134 | pc.printf("%c%c%c%c\n",(char)xya.x,(char)xTarget,xya.a/10,xya.a%10); |
12104404 | 6:0602a9e8118b | 135 | } |
12104404 | 17:2f89826b5679 | 136 | |
12104404 | 19:2dd81b864e14 | 137 | void BrownOut() |
12104404 | 6:0602a9e8118b | 138 | { |
12104404 | 24:fb1f083ebd62 | 139 | //Stop Motors and Driver |
12104404 | 23:455f7da3dd7a | 140 | motion.eStop(); |
12104404 | 24:fb1f083ebd62 | 141 | //Ensure suction on |
alaurens | 33:baf24c59affc | 142 | suction.pulsewidth_us(1250); |
12104404 | 24:fb1f083ebd62 | 143 | //Light Up Error Light Pattern |
12104404 | 23:455f7da3dd7a | 144 | led1=1; |
12104404 | 23:455f7da3dd7a | 145 | led2=0; |
12104404 | 23:455f7da3dd7a | 146 | led3=1; |
12104404 | 23:455f7da3dd7a | 147 | led4=0; |
12104404 | 19:2dd81b864e14 | 148 | //Power Down Non Essential |
12104404 | 24:fb1f083ebd62 | 149 | Peripheral_PowerDown(~(LPC1768_PCONP_PCPWM1 | LPC1768_PCONP_PCGPIO)); |
12104404 | 19:2dd81b864e14 | 150 | //Sleep wake by interrupt |
12104404 | 23:455f7da3dd7a | 151 | //Sleep(); |
12104404 | 17:2f89826b5679 | 152 | } |