drive down

Dependencies:   BMP280 BNO055_fusion PowerControl mbed

Fork of TEAM_G_FLOW_RIDA by Edwin Cho

Committer:
alaurens
Date:
Wed Apr 20 18:25:43 2016 +0000
Revision:
34:083073e54dbd
Parent:
33:baf24c59affc
df

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 33:baf24c59affc 39 int xState=X_INCREASE;
alaurens 33:baf24c59affc 40 int angleTarget=0;
alaurens 33:baf24c59affc 41 int yTarget=+10+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 33:baf24c59affc 89 suction.pulsewidth_us(1250);
alaurens 33:baf24c59affc 90 wait(0.5);
alaurens 34:083073e54dbd 91 suction.pulsewidth_us(1300);
alaurens 34:083073e54dbd 92 wait(0.5);
12104404 0:96d6eb224379 93 while(1) {
alaurens 34:083073e54dbd 94 suction.pulsewidth_us(1350);
alaurens 31:69caabc10879 95 /*pressure=(int)read[0];//(int)pres.getTemperature();
alaurens 31:69caabc10879 96 if(pressure==88)
alaurens 31:69caabc10879 97 led1=1;
12104404 27:fb1298d35bd1 98 else
alaurens 31:69caabc10879 99 led1=0;*/
alaurens 33:baf24c59affc 100 //uncomment this part if you want the robot to just drive down the window with no separtor
alaurens 31:69caabc10879 101 if (xState==0) {
12104404 26:0ea6a550a99d 102 motion.mStop();
12104404 26:0ea6a550a99d 103 safe.kick();
12104404 26:0ea6a550a99d 104 continue;
12104404 26:0ea6a550a99d 105 }
12104404 25:f3bf8351bbd4 106 loc.get_xy(&xya);
12104404 25:f3bf8351bbd4 107 motion.check_xya(&xGood,&yGood,&angleGood,xTarget,angleTarget,yTarget,xya,3,3,angle_error);
12104404 26:0ea6a550a99d 108
alaurens 31:69caabc10879 109 motion.setXstate(&xState,&xTarget,xGood,angleGood,&angleTarget,&angle_error,yTarget);
alaurens 34:083073e54dbd 110 //motion.setAngleTol(&angle_error,yGood,xGood);
alaurens 34:083073e54dbd 111 // motion.setYgoal(xGood,angleGood,yGood,&yTarget);
12104404 25:f3bf8351bbd4 112 if(motion.setAngle(angleTarget,xya.a,angle_error,ANGLE_TURN)) {
12104404 25:f3bf8351bbd4 113 motion.setXPos(xTarget,xya.x,2,angleTarget);
alaurens 34:083073e54dbd 114 // motion.setYBias(yTarget,xya.y,2,angleTarget);
12104404 25:f3bf8351bbd4 115
alaurens 33:baf24c59affc 116 }
alaurens 33:baf24c59affc 117 //loc.get_xy(&xya);
12104404 19:2dd81b864e14 118 #if defined(PC_MODE)
12104404 26:0ea6a550a99d 119 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 120 #endif
12104404 19:2dd81b864e14 121 //Feed the dog
12104404 23:455f7da3dd7a 122 safe.kick();
12104404 0:96d6eb224379 123 }
12104404 0:96d6eb224379 124 }
12104404 6:0602a9e8118b 125
12104404 6:0602a9e8118b 126 void send()
12104404 6:0602a9e8118b 127 {
12104404 19:2dd81b864e14 128 //Print over serial port to WiFi MCU
alaurens 33:baf24c59affc 129 pc.printf("%c%c%c%c\n",(char)xya.x,(char)xGood,xya.a/10,xya.a%10);
12104404 6:0602a9e8118b 130 }
12104404 17:2f89826b5679 131
12104404 19:2dd81b864e14 132 void BrownOut()
12104404 6:0602a9e8118b 133 {
12104404 24:fb1f083ebd62 134 //Stop Motors and Driver
12104404 23:455f7da3dd7a 135 motion.eStop();
12104404 24:fb1f083ebd62 136 //Ensure suction on
alaurens 33:baf24c59affc 137 suction.pulsewidth_us(1250);
12104404 24:fb1f083ebd62 138 //Light Up Error Light Pattern
12104404 23:455f7da3dd7a 139 led1=1;
12104404 23:455f7da3dd7a 140 led2=0;
12104404 23:455f7da3dd7a 141 led3=1;
12104404 23:455f7da3dd7a 142 led4=0;
12104404 19:2dd81b864e14 143 //Power Down Non Essential
12104404 24:fb1f083ebd62 144 Peripheral_PowerDown(~(LPC1768_PCONP_PCPWM1 | LPC1768_PCONP_PCGPIO));
12104404 19:2dd81b864e14 145 //Sleep wake by interrupt
12104404 23:455f7da3dd7a 146 //Sleep();
12104404 17:2f89826b5679 147 }