drive down

Dependencies:   BMP280 BNO055_fusion PowerControl mbed

Fork of TEAM_G_FLOW_RIDA by Edwin Cho

Committer:
alaurens
Date:
Wed Apr 20 08:21:21 2016 +0000
Revision:
31:69caabc10879
Parent:
30:116cd143fdf7
Child:
33:baf24c59affc
drive down window

Who changed what in which revision?

UserRevisionLine numberNew 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;
alaurens 31:69caabc10879 35 int angle_error=2;
12104404 25:f3bf8351bbd4 36 bool xGood=false;
12104404 25:f3bf8351bbd4 37 bool yGood=false;
12104404 25:f3bf8351bbd4 38 bool angleGood=false;
alaurens 31:69caabc10879 39 int xState=X_BEGINING;
alaurens 31:69caabc10879 40 int angleTarget=180;
alaurens 31:69caabc10879 41 int yTarget=ROB_SIZE/2;
12104404 30:116cd143fdf7 42 int pressure;
12104404 25:f3bf8351bbd4 43
12104404 20:10a305298e27 44 //bool flag=false;
12104404 20:10a305298e27 45 //int target=20;
12104404 20:10a305298e27 46 //int angle_error=2;
12104404 20:10a305298e27 47 //bool xGood=false;
12104404 20:10a305298e27 48 //int angleTarget=0;
12104404 6:0602a9e8118b 49
12104404 0:96d6eb224379 50 int main()
12104404 0:96d6eb224379 51 {
12104404 19:2dd81b864e14 52 //Set ESC Period
12104404 19:2dd81b864e14 53 suction.period_ms(2);
12104404 19:2dd81b864e14 54 //Initialize to 0
12104404 19:2dd81b864e14 55 suction.pulsewidth_us(1000);
12104404 27:fb1298d35bd1 56 wait(2);
12104404 27:fb1298d35bd1 57 /*suction.pulsewidth_us(1000);
12104404 27:fb1298d35bd1 58 wait(2);*/
12104404 24:fb1f083ebd62 59 //Watchdog Reset Indicator
12104404 24:fb1f083ebd62 60 if ((LPC_WDT->WDMOD>>2)&1)
12104404 24:fb1f083ebd62 61 led4=1;
12104404 24:fb1f083ebd62 62 else
12104404 24:fb1f083ebd62 63 led3=1;
12104404 19:2dd81b864e14 64 //Start Watchdog
12104404 23:455f7da3dd7a 65 safe.kick(WATCHDOG_TIME);
12104404 23:455f7da3dd7a 66 //Power Down Ethernet and USB and Enable Brown Out Interrupt
12104404 23:455f7da3dd7a 67 safe.init((unsigned)BrownOut);
12104404 19:2dd81b864e14 68 //Serial Baudrate
12104404 0:96d6eb224379 69 pc.baud(9600);
12104404 26:0ea6a550a99d 70 //Initialize Locomotion
12104404 26:0ea6a550a99d 71 loc.init();
12104404 30:116cd143fdf7 72 //pres.initialize();
12104404 19:2dd81b864e14 73 //Attach Periodic Wireless Printing
12104404 19:2dd81b864e14 74 #if not defined(PC_MODE)
12104404 8:b36be08c44f8 75 t.attach(&send,1);
12104404 19:2dd81b864e14 76 #endif
12104404 24:fb1f083ebd62 77 led1=0;
12104404 24:fb1f083ebd62 78 led2=0;
12104404 24:fb1f083ebd62 79 led3=0;
12104404 24:fb1f083ebd62 80 led4=0;
alaurens 31:69caabc10879 81 suction.pulsewidth_us(1000);
12104404 30:116cd143fdf7 82 wait(0.5);
alaurens 31:69caabc10879 83 suction.pulsewidth_us(1050);
12104404 30:116cd143fdf7 84 wait(0.5);
alaurens 31:69caabc10879 85 suction.pulsewidth_us(1100);
12104404 30:116cd143fdf7 86 wait(0.5);
alaurens 31:69caabc10879 87 suction.pulsewidth_us(1150);
12104404 30:116cd143fdf7 88 wait(0.5);
alaurens 31:69caabc10879 89 suction.pulsewidth_us(1200);
12104404 30:116cd143fdf7 90 wait(0.5);
12104404 0:96d6eb224379 91 while(1) {
alaurens 31:69caabc10879 92 suction.pulsewidth_us(1250);
alaurens 31:69caabc10879 93 /*pressure=(int)read[0];//(int)pres.getTemperature();
alaurens 31:69caabc10879 94 if(pressure==88)
alaurens 31:69caabc10879 95 led1=1;
12104404 27:fb1298d35bd1 96 else
alaurens 31:69caabc10879 97 led1=0;*/
alaurens 31:69caabc10879 98 /*uncomment this part if you want the robot to just drive down the window with no separtor
alaurens 31:69caabc10879 99 if (xState==0) {
12104404 26:0ea6a550a99d 100 motion.mStop();
12104404 26:0ea6a550a99d 101 safe.kick();
12104404 26:0ea6a550a99d 102 continue;
12104404 26:0ea6a550a99d 103 }
12104404 25:f3bf8351bbd4 104 loc.get_xy(&xya);
12104404 25:f3bf8351bbd4 105 motion.check_xya(&xGood,&yGood,&angleGood,xTarget,angleTarget,yTarget,xya,3,3,angle_error);
12104404 26:0ea6a550a99d 106
alaurens 31:69caabc10879 107 motion.setXstate(&xState,&xTarget,xGood,angleGood,&angleTarget,&angle_error,yTarget);
12104404 25:f3bf8351bbd4 108 motion.setAngleTol(&angle_error,yGood,xGood);
12104404 25:f3bf8351bbd4 109 motion.setYgoal(xGood,angleGood,yGood,&yTarget);
12104404 25:f3bf8351bbd4 110 if(motion.setAngle(angleTarget,xya.a,angle_error,ANGLE_TURN)) {
12104404 25:f3bf8351bbd4 111 motion.setXPos(xTarget,xya.x,2,angleTarget);
12104404 25:f3bf8351bbd4 112 motion.setYBias(yTarget,xya.y,2,angleTarget);
12104404 25:f3bf8351bbd4 113
12104404 27:fb1298d35bd1 114 }*/
12104404 19:2dd81b864e14 115 #if defined(PC_MODE)
12104404 26:0ea6a550a99d 116 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 117 #endif
12104404 19:2dd81b864e14 118 //Feed the dog
12104404 23:455f7da3dd7a 119 safe.kick();
12104404 0:96d6eb224379 120 }
12104404 0:96d6eb224379 121 }
12104404 6:0602a9e8118b 122
12104404 6:0602a9e8118b 123 void send()
12104404 6:0602a9e8118b 124 {
12104404 19:2dd81b864e14 125 //Print over serial port to WiFi MCU
12104404 30:116cd143fdf7 126 pc.printf("%c%c%c%c\n",(char)xya.x,(char)xya.y,pressure/10,pressure%10);
12104404 6:0602a9e8118b 127 }
12104404 17:2f89826b5679 128
12104404 19:2dd81b864e14 129 void BrownOut()
12104404 6:0602a9e8118b 130 {
12104404 24:fb1f083ebd62 131 //Stop Motors and Driver
12104404 23:455f7da3dd7a 132 motion.eStop();
12104404 24:fb1f083ebd62 133 //Ensure suction on
12104404 19:2dd81b864e14 134 suction.pulsewidth_us(2000);
12104404 24:fb1f083ebd62 135 //Light Up Error Light Pattern
12104404 23:455f7da3dd7a 136 led1=1;
12104404 23:455f7da3dd7a 137 led2=0;
12104404 23:455f7da3dd7a 138 led3=1;
12104404 23:455f7da3dd7a 139 led4=0;
12104404 19:2dd81b864e14 140 //Power Down Non Essential
12104404 24:fb1f083ebd62 141 Peripheral_PowerDown(~(LPC1768_PCONP_PCPWM1 | LPC1768_PCONP_PCGPIO));
12104404 19:2dd81b864e14 142 //Sleep wake by interrupt
12104404 23:455f7da3dd7a 143 //Sleep();
12104404 17:2f89826b5679 144 }