DERPS

Dependencies:   BMP280 BNO055_fusion PowerControl mbed

Fork of STRAIGHT_DRIVE_NO_SEP by Antoine Laurens

Committer:
12104404
Date:
Sun Apr 24 22:37:07 2016 +0000
Revision:
35:68917796c30a
Parent:
34:083073e54dbd
Child:
36:dc69442c4c47
angle bias;

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