DERPS

Dependencies:   BMP280 BNO055_fusion PowerControl mbed

Fork of STRAIGHT_DRIVE_NO_SEP by Antoine Laurens

Committer:
12104404
Date:
Wed Apr 27 04:24:11 2016 +0000
Revision:
38:16208e003dc9
Parent:
37:e8a6ea255c09
Child:
39:ecc9defe3dc0
Child:
40:9a97c4403c0a
good good;

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 38:16208e003dc9 27 LOCALIZE loc(i2c1, i2c2, p29, p5, p6, p7, p8, 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 6:0602a9e8118b 30
12104404 20:10a305298e27 31 void BrownOut();
12104404 6:0602a9e8118b 32
12104404 38:16208e003dc9 33 int xTarget=FRAME_W;
12104404 36:dc69442c4c47 34 int angle_error=1;
12104404 25:f3bf8351bbd4 35 bool xGood=false;
12104404 25:f3bf8351bbd4 36 bool yGood=false;
12104404 25:f3bf8351bbd4 37 bool angleGood=false;
12104404 35:68917796c30a 38 bool flip=false;
alaurens 33:baf24c59affc 39 int xState=X_INCREASE;
12104404 37:e8a6ea255c09 40 int angleTarget=355;
12104404 35:68917796c30a 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);
12104404 37:e8a6ea255c09 89 suction.pulsewidth_us(1200);
12104404 37:e8a6ea255c09 90 wait(0.5);
alaurens 33:baf24c59affc 91 suction.pulsewidth_us(1250);
alaurens 33:baf24c59affc 92 wait(0.5);
12104404 0:96d6eb224379 93 while(1) {
12104404 37:e8a6ea255c09 94 suction.pulsewidth_us(1285);
alaurens 33:baf24c59affc 95 //uncomment this part if you want the robot to just drive down the window with no separtor
12104404 38:16208e003dc9 96 if (xya.y>FRAME_H*0.65) {
12104404 38:16208e003dc9 97 while(1)
12104404 38:16208e003dc9 98 {
12104404 38:16208e003dc9 99 suction.pulsewidth_us(1285);
12104404 38:16208e003dc9 100 motion.mStop();
12104404 38:16208e003dc9 101 safe.kick();
12104404 38:16208e003dc9 102 }
12104404 38:16208e003dc9 103 //continue;
12104404 26:0ea6a550a99d 104 }
12104404 25:f3bf8351bbd4 105 loc.get_xy(&xya);
12104404 36:dc69442c4c47 106 xGood=motion.setXPos(xTarget,xya.x,2,0);
12104404 36:dc69442c4c47 107 if(!xGood)
12104404 37:e8a6ea255c09 108 motion.setAngle(angleTarget,xya.a,angle_error,ANGLE_BIAS);
12104404 36:dc69442c4c47 109 else {
12104404 38:16208e003dc9 110 xTarget=(xTarget==FRAME_W)?0:FRAME_W;
12104404 37:e8a6ea255c09 111 angleTarget=(angleTarget==5)?-5:5;
12104404 36:dc69442c4c47 112 }
12104404 35:68917796c30a 113 //motion.setYBias(0,xya.y,2,angleTarget);
12104404 38:16208e003dc9 114 //loc.get_xy(&xya);5
12104404 19:2dd81b864e14 115 #if defined(PC_MODE)
12104404 36:dc69442c4c47 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 38:16208e003dc9 126 pc.printf("%c%c%c%c\n",(char)xya.x,(char)xya.y,xya.a/10,xya.a%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
alaurens 33:baf24c59affc 134 suction.pulsewidth_us(1250);
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 }