Mekatronics Team G

Dependencies:   BNO055_fusion PowerControl mbed BMP280

Fork of DEMO3 by Edwin Cho

Committer:
12104404
Date:
Thu Mar 17 23:20:22 2016 +0000
Revision:
7:d6dca30f7959
Parent:
6:0602a9e8118b
Child:
8:b36be08c44f8
lol this has some shit

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 6:0602a9e8118b 3 #include "WATCHDOG.h"
12104404 0:96d6eb224379 4
12104404 6:0602a9e8118b 5 #define SPEED_TURN_MIN 0.15
12104404 6:0602a9e8118b 6 #define SPEED_TURN_MAX 0.35
12104404 6:0602a9e8118b 7 #define SPEED_FB_MIN 0.15
12104404 6:0602a9e8118b 8 #define SPEED_FB_MAX 0.50
12104404 6:0602a9e8118b 9
12104404 7:d6dca30f7959 10 //Serial pc(p13, p14);
12104404 7:d6dca30f7959 11 Serial pc(USBTX, USBRX);
12104404 6:0602a9e8118b 12
12104404 6:0602a9e8118b 13 Watchdog wdt;
12104404 0:96d6eb224379 14
12104404 0:96d6eb224379 15 I2C i2c1(p28, p27);
12104404 0:96d6eb224379 16 I2C i2c2(p9, p10);
12104404 0:96d6eb224379 17 LOCALIZE loc(i2c1, i2c2, p26);
12104404 0:96d6eb224379 18 LOCALIZE_xya xya;
12104404 0:96d6eb224379 19
12104404 6:0602a9e8118b 20 DigitalIn sw1(p20);
12104404 6:0602a9e8118b 21 DigitalIn sw2(p19);
12104404 6:0602a9e8118b 22 DigitalIn sw3(p18);
12104404 6:0602a9e8118b 23 DigitalIn sw4(p17);
12104404 6:0602a9e8118b 24 DigitalOut dir1(p15);
12104404 6:0602a9e8118b 25 DigitalOut dir2(p16);
12104404 6:0602a9e8118b 26
12104404 6:0602a9e8118b 27 PwmOut motor1F(p21);
12104404 6:0602a9e8118b 28 PwmOut motor1B(p22);
12104404 6:0602a9e8118b 29 PwmOut motor2F(p23);
12104404 6:0602a9e8118b 30 PwmOut motor2B(p24);
12104404 6:0602a9e8118b 31
12104404 0:96d6eb224379 32 DigitalOut led1(LED1);
12104404 0:96d6eb224379 33 DigitalOut led2(LED2);
12104404 0:96d6eb224379 34 DigitalOut led3(LED3);
12104404 0:96d6eb224379 35 DigitalOut led4(LED4);
12104404 0:96d6eb224379 36
12104404 6:0602a9e8118b 37 Ticker t;
12104404 6:0602a9e8118b 38 bool flag=false;
12104404 6:0602a9e8118b 39
12104404 6:0602a9e8118b 40 void send();
12104404 6:0602a9e8118b 41 void turn(int angle);
12104404 6:0602a9e8118b 42
12104404 0:96d6eb224379 43 int main()
12104404 0:96d6eb224379 44 {
12104404 6:0602a9e8118b 45 wdt.kick(5);
12104404 0:96d6eb224379 46 pc.baud(9600);
12104404 6:0602a9e8118b 47 dir1=0;
12104404 6:0602a9e8118b 48 dir2=0;
12104404 6:0602a9e8118b 49 motor1F=0;
12104404 6:0602a9e8118b 50 motor1B=0;
12104404 6:0602a9e8118b 51 motor2F=0;
12104404 6:0602a9e8118b 52 motor2B=0;
12104404 6:0602a9e8118b 53 //pc.printf("Initialized Localization: %d\n",loc.init());
12104404 7:d6dca30f7959 54 //t.attach(&send,1);
12104404 6:0602a9e8118b 55 sw1.mode(PullUp);
12104404 6:0602a9e8118b 56 sw2.mode(PullUp);
12104404 6:0602a9e8118b 57 sw3.mode(PullUp);
12104404 6:0602a9e8118b 58 sw4.mode(PullUp);
12104404 7:d6dca30f7959 59 /*while(abs(xya.a-180)>5) {
12104404 6:0602a9e8118b 60 loc.get_angle(&xya);
12104404 6:0602a9e8118b 61 turn(180);
12104404 7:d6dca30f7959 62 }*/
12104404 0:96d6eb224379 63 while(1) {
12104404 6:0602a9e8118b 64 led1=!sw1;
12104404 6:0602a9e8118b 65 led2=!sw2;
12104404 6:0602a9e8118b 66 led3=!sw3;
12104404 6:0602a9e8118b 67 led4=!sw4;
12104404 7:d6dca30f7959 68 //loc.get_angle(&xya);
12104404 6:0602a9e8118b 69 //loc.get_xy(&xya);
12104404 6:0602a9e8118b 70 loc.get_raw_xy();
12104404 7:d6dca30f7959 71 pc.printf("X: %3d\tY: %3d\tP: %3d\n",loc._rx_n,xya.y,xya.a);
12104404 6:0602a9e8118b 72 /*if(loc._rx_n>20) {
12104404 6:0602a9e8118b 73 dir1=1;
12104404 6:0602a9e8118b 74 dir2=1;
12104404 6:0602a9e8118b 75 } else if(loc._rx_n<10) {
12104404 6:0602a9e8118b 76 dir1=0;
12104404 6:0602a9e8118b 77 dir2=0;
12104404 6:0602a9e8118b 78 }*/
12104404 7:d6dca30f7959 79 /*
12104404 6:0602a9e8118b 80 if((!sw1==1 && !sw2==1) && flag) {
12104404 6:0602a9e8118b 81 motor1F=0.3;
12104404 6:0602a9e8118b 82 motor1B=0.3;
12104404 6:0602a9e8118b 83 motor2F=0.3;
12104404 6:0602a9e8118b 84 motor2B=0.3;
12104404 6:0602a9e8118b 85 dir1=0;
12104404 6:0602a9e8118b 86 dir2=0;
12104404 6:0602a9e8118b 87 wait(0.5);
12104404 6:0602a9e8118b 88 while((xya.a>5 && xya.a<355)) {
12104404 6:0602a9e8118b 89 loc.get_angle(&xya);
12104404 6:0602a9e8118b 90 turn(0);
12104404 6:0602a9e8118b 91 }
12104404 6:0602a9e8118b 92 flag=false;
12104404 6:0602a9e8118b 93 } else if((!sw1==1 && !sw2==1) && !flag) {
12104404 6:0602a9e8118b 94 motor1F=0.3;
12104404 6:0602a9e8118b 95 motor1B=0.3;
12104404 6:0602a9e8118b 96 motor2F=0.3;
12104404 6:0602a9e8118b 97 motor2B=0.3;
12104404 6:0602a9e8118b 98 dir1=0;
12104404 6:0602a9e8118b 99 dir2=0;
12104404 6:0602a9e8118b 100 wait(0.5);
12104404 6:0602a9e8118b 101 while(abs(xya.a-180)>=5) {
12104404 6:0602a9e8118b 102 loc.get_angle(&xya);
12104404 6:0602a9e8118b 103 turn(180);
12104404 6:0602a9e8118b 104 }
12104404 6:0602a9e8118b 105 flag=true;
12104404 6:0602a9e8118b 106 } else {
12104404 6:0602a9e8118b 107 float s=0;
12104404 6:0602a9e8118b 108 if(loc._rx_n<=10)
12104404 6:0602a9e8118b 109 s=0.1;
12104404 6:0602a9e8118b 110 else if(loc._rx_n<=30)
12104404 6:0602a9e8118b 111 s=0.4*abs(loc._rx_n-30)/30+0.1;
12104404 6:0602a9e8118b 112 else
12104404 6:0602a9e8118b 113 s=0.5;
12104404 6:0602a9e8118b 114 motor1F=s;
12104404 6:0602a9e8118b 115 motor1B=s;
12104404 6:0602a9e8118b 116 motor2F=s;
12104404 6:0602a9e8118b 117 motor2B=s;
12104404 6:0602a9e8118b 118 dir1=1;
12104404 6:0602a9e8118b 119 dir2=1;
12104404 6:0602a9e8118b 120 }
12104404 7:d6dca30f7959 121 */
12104404 6:0602a9e8118b 122 wdt.kick();
12104404 0:96d6eb224379 123 }
12104404 0:96d6eb224379 124 }
12104404 6:0602a9e8118b 125
12104404 6:0602a9e8118b 126 void send()
12104404 6:0602a9e8118b 127 {
12104404 6:0602a9e8118b 128 pc.printf("%c%c%c%c\n",(char)loc._rx_n,(char)loc._ry_n,xya.a/10,xya.a%10);
12104404 6:0602a9e8118b 129 }
12104404 6:0602a9e8118b 130
12104404 6:0602a9e8118b 131 void turn(int angle)
12104404 6:0602a9e8118b 132 {
12104404 6:0602a9e8118b 133 float s = 0;
12104404 6:0602a9e8118b 134 if(angle==180) {
12104404 6:0602a9e8118b 135 if(abs(xya.a-angle)<=5)
12104404 6:0602a9e8118b 136 s=SPEED_TURN_MIN;
12104404 6:0602a9e8118b 137 else
12104404 6:0602a9e8118b 138 s=(SPEED_TURN_MAX*abs(xya.a-angle)/180)+SPEED_TURN_MIN;
12104404 6:0602a9e8118b 139 motor1F=s;
12104404 6:0602a9e8118b 140 motor1B=s;
12104404 6:0602a9e8118b 141 motor2F=s;
12104404 6:0602a9e8118b 142 motor2B=s;
12104404 6:0602a9e8118b 143 if(xya.a>angle+5) {
12104404 6:0602a9e8118b 144 dir1=1;
12104404 6:0602a9e8118b 145 dir2=0;
12104404 6:0602a9e8118b 146 } else if(xya.a<angle-5) {
12104404 6:0602a9e8118b 147 dir1=0;
12104404 6:0602a9e8118b 148 dir2=1;
12104404 6:0602a9e8118b 149 } else {
12104404 6:0602a9e8118b 150 motor1F=0;
12104404 6:0602a9e8118b 151 motor1B=0;
12104404 6:0602a9e8118b 152 motor2F=0;
12104404 6:0602a9e8118b 153 motor2B=0;
12104404 6:0602a9e8118b 154 }
12104404 6:0602a9e8118b 155 } else if(angle==0) {
12104404 6:0602a9e8118b 156 if(xya.a>angle+5 && xya.a<180) {
12104404 6:0602a9e8118b 157 s=(SPEED_TURN_MAX*abs(xya.a-angle)/180)+SPEED_TURN_MIN;
12104404 6:0602a9e8118b 158 motor1F=s;
12104404 6:0602a9e8118b 159 motor1B=s;
12104404 6:0602a9e8118b 160 motor2F=s;
12104404 6:0602a9e8118b 161 motor2B=s;
12104404 6:0602a9e8118b 162 dir1=1;
12104404 6:0602a9e8118b 163 dir2=0;
12104404 6:0602a9e8118b 164 } else if(xya.a>180 && xya.a<355) {
12104404 6:0602a9e8118b 165 s=(SPEED_TURN_MAX*abs(xya.a-359)/180)+SPEED_TURN_MIN;
12104404 6:0602a9e8118b 166 motor1F=s;
12104404 6:0602a9e8118b 167 motor1B=s;
12104404 6:0602a9e8118b 168 motor2F=s;
12104404 6:0602a9e8118b 169 motor2B=s;
12104404 6:0602a9e8118b 170 dir1=0;
12104404 6:0602a9e8118b 171 dir2=1;
12104404 6:0602a9e8118b 172 } else {
12104404 6:0602a9e8118b 173 motor1F=0;
12104404 6:0602a9e8118b 174 motor1B=0;
12104404 6:0602a9e8118b 175 motor2F=0;
12104404 6:0602a9e8118b 176 motor2B=0;
12104404 6:0602a9e8118b 177 }
12104404 6:0602a9e8118b 178 }
12104404 6:0602a9e8118b 179 }