Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of IEEE_14_Freescale by
robot.cpp@5:74c8ce39334b, 2013-11-17 (annotated)
- Committer:
- soonerbot
- Date:
- Sun Nov 17 21:48:30 2013 +0000
- Revision:
- 5:74c8ce39334b
- Parent:
- 4:adc885f4ab75
- Child:
- 6:62d498ee97cf
Much better straight line movement
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
soonerbot | 1:c28fac16a109 | 1 | #include "robot.h" |
soonerbot | 1:c28fac16a109 | 2 | |
soonerbot | 1:c28fac16a109 | 3 | #define MIN(a,b) ((b<a)?(b):(a)) |
soonerbot | 1:c28fac16a109 | 4 | #define MAX(a,b) ((b>a)?(b):(a)) |
soonerbot | 1:c28fac16a109 | 5 | |
soonerbot | 3:a223b0bf8256 | 6 | robot::robot() : spi(PTD2, PTD3, PTD1), bigenc(spi,PTD0), gyro(PTE0, PTE1), |
soonerbot | 2:7c6b494f9005 | 7 | right(bigenc,0,1,PTA5), left(bigenc,2,3,PTA4){ |
soonerbot | 1:c28fac16a109 | 8 | |
soonerbot | 1:c28fac16a109 | 9 | bigenc.setDirections(1,-1,1,1); |
soonerbot | 1:c28fac16a109 | 10 | left.setReversed(1); |
soonerbot | 1:c28fac16a109 | 11 | x=y=rot=0; |
soonerbot | 4:adc885f4ab75 | 12 | pfac=0.00035; |
soonerbot | 4:adc885f4ab75 | 13 | ifac=0.00000001; |
soonerbot | 4:adc885f4ab75 | 14 | dfac=0.000001; |
soonerbot | 4:adc885f4ab75 | 15 | |
soonerbot | 5:74c8ce39334b | 16 | angfac=0.0000016; |
soonerbot | 1:c28fac16a109 | 17 | //blah |
soonerbot | 1:c28fac16a109 | 18 | } |
soonerbot | 1:c28fac16a109 | 19 | |
soonerbot | 1:c28fac16a109 | 20 | |
soonerbot | 4:adc885f4ab75 | 21 | int robot::driveForward(double desangle, int distance){ |
soonerbot | 1:c28fac16a109 | 22 | bigenc.resetAll(); |
soonerbot | 1:c28fac16a109 | 23 | const int* constbuf = bigenc.getVals(); |
soonerbot | 1:c28fac16a109 | 24 | int prev[4]={0,0,0,0}; |
soonerbot | 1:c28fac16a109 | 25 | int distTraveled=0, i; |
soonerbot | 5:74c8ce39334b | 26 | double maxPow=0.5; |
soonerbot | 4:adc885f4ab75 | 27 | int loopcount=0; |
soonerbot | 4:adc885f4ab75 | 28 | double minmain=0.05; |
soonerbot | 4:adc885f4ab75 | 29 | double minalt=0.05; |
soonerbot | 4:adc885f4ab75 | 30 | |
soonerbot | 4:adc885f4ab75 | 31 | int targetang = desangle*4050000.0/360.0;//gyro.getZ(); |
soonerbot | 4:adc885f4ab75 | 32 | int startang = targetang; |
soonerbot | 4:adc885f4ab75 | 33 | double angle=double(startang)*2*3.14159/4050000.0; |
soonerbot | 4:adc885f4ab75 | 34 | double targx = x + double(distance)*0.0035362*cos(angle)*1.5; |
soonerbot | 4:adc885f4ab75 | 35 | double targy = y + double(distance)*0.0035362*sin(angle)*1.5; |
soonerbot | 4:adc885f4ab75 | 36 | int invfactor = 0; |
soonerbot | 4:adc885f4ab75 | 37 | if(distance<=0) { |
soonerbot | 4:adc885f4ab75 | 38 | invfactor = 2025000; |
soonerbot | 4:adc885f4ab75 | 39 | } |
soonerbot | 5:74c8ce39334b | 40 | double realfac=0; |
soonerbot | 4:adc885f4ab75 | 41 | |
soonerbot | 4:adc885f4ab75 | 42 | int pmain=distance; |
soonerbot | 4:adc885f4ab75 | 43 | int imain=0; |
soonerbot | 4:adc885f4ab75 | 44 | int dmain=0; |
soonerbot | 4:adc885f4ab75 | 45 | int plast=distance; |
soonerbot | 4:adc885f4ab75 | 46 | |
soonerbot | 4:adc885f4ab75 | 47 | int pterm=distance; |
soonerbot | 4:adc885f4ab75 | 48 | int iterm=0; |
soonerbot | 4:adc885f4ab75 | 49 | int dterm=0; |
soonerbot | 4:adc885f4ab75 | 50 | const int ptol = 75; |
soonerbot | 4:adc885f4ab75 | 51 | const int dtol = 10; |
soonerbot | 5:74c8ce39334b | 52 | while(/*(pterm+pmain <= -ptol || pterm >= ptol) || (dterm <= -dtol || dterm >= dtol) || */(pmain <= -ptol || pmain >= ptol) || (dmain <= -dtol || dmain >= dtol)|| (realfac <= -0.05 || realfac >= 0.05) ){ |
soonerbot | 4:adc885f4ab75 | 53 | //maxPow=MAX(double(distance-distTraveled-2000)/15000.0,minspeed); |
soonerbot | 4:adc885f4ab75 | 54 | //left.setPower(MIN(power,maxPow)); |
soonerbot | 4:adc885f4ab75 | 55 | //right.setPower(MIN(power,maxPow)); |
soonerbot | 4:adc885f4ab75 | 56 | //DBGPRINT("=%d of %d [%f] (%d, %d) \t{%f,\t%f,/t%f}",distTraveled,distance, maxPow, constbuf[0], constbuf[1],x,y,rot*180.0/3.14159); |
soonerbot | 1:c28fac16a109 | 57 | for(i=0;i<4;i++) |
soonerbot | 1:c28fac16a109 | 58 | prev[i]=constbuf[i]; |
soonerbot | 1:c28fac16a109 | 59 | //wait(0.05); |
soonerbot | 1:c28fac16a109 | 60 | constbuf = bigenc.getVals(); |
soonerbot | 4:adc885f4ab75 | 61 | |
soonerbot | 5:74c8ce39334b | 62 | pmain = distance - (constbuf[0]+constbuf[1])/2; |
soonerbot | 5:74c8ce39334b | 63 | dmain = ((constbuf[0]+constbuf[1]) - (prev[0]+prev[1]))/2; |
soonerbot | 4:adc885f4ab75 | 64 | imain += pmain; |
soonerbot | 4:adc885f4ab75 | 65 | |
soonerbot | 5:74c8ce39334b | 66 | //pterm = distance - constbuf[1]; |
soonerbot | 5:74c8ce39334b | 67 | //dterm = constbuf[1] - prev[1]; |
soonerbot | 5:74c8ce39334b | 68 | //iterm += pterm; |
soonerbot | 4:adc885f4ab75 | 69 | |
soonerbot | 4:adc885f4ab75 | 70 | //pterm = constbuf[0] - constbuf[1]; |
soonerbot | 4:adc885f4ab75 | 71 | //dterm = pterm - (prev[0] - prev[1]); |
soonerbot | 4:adc885f4ab75 | 72 | //iterm += pterm; |
soonerbot | 4:adc885f4ab75 | 73 | |
soonerbot | 5:74c8ce39334b | 74 | //DBGPRINT("%d like %f\n\r", gyro.compZ(invfactor+atan2(targy-y,targx-x)*4050000.0/(2*3.14159)),angle*180/3.14159); |
soonerbot | 5:74c8ce39334b | 75 | |
soonerbot | 5:74c8ce39334b | 76 | realfac = (gyro.compZ(invfactor+atan2(targy-y,targx-x)*4050000.0/(2*3.14159)))*angfac; |
soonerbot | 4:adc885f4ab75 | 77 | |
soonerbot | 5:74c8ce39334b | 78 | double leftpow = MAX(MIN(pfac*pmain+ifac*imain+dfac*dmain,maxPow),-maxPow)-realfac; |
soonerbot | 5:74c8ce39334b | 79 | double rightpow = MAX(MIN(pfac*pmain+ifac*imain+dfac*dmain,maxPow),-maxPow)+realfac; |
soonerbot | 5:74c8ce39334b | 80 | if((pmain <= -ptol || pmain >= ptol) || (dmain <= -dtol || dmain >= dtol) || (realfac <= -0.05 || realfac >= 0.05) ){ |
soonerbot | 4:adc885f4ab75 | 81 | if (leftpow>0){ |
soonerbot | 4:adc885f4ab75 | 82 | if (leftpow<minalt){ |
soonerbot | 4:adc885f4ab75 | 83 | leftpow=minalt; |
soonerbot | 4:adc885f4ab75 | 84 | } |
soonerbot | 4:adc885f4ab75 | 85 | }else if (leftpow>-minalt){ |
soonerbot | 4:adc885f4ab75 | 86 | leftpow=-minalt; |
soonerbot | 4:adc885f4ab75 | 87 | } |
soonerbot | 4:adc885f4ab75 | 88 | if (rightpow>0){ |
soonerbot | 4:adc885f4ab75 | 89 | if (rightpow<minmain){ |
soonerbot | 4:adc885f4ab75 | 90 | rightpow=minmain; |
soonerbot | 4:adc885f4ab75 | 91 | } |
soonerbot | 4:adc885f4ab75 | 92 | }else if (rightpow>-minmain){ |
soonerbot | 4:adc885f4ab75 | 93 | rightpow=-minmain; |
soonerbot | 4:adc885f4ab75 | 94 | } |
soonerbot | 4:adc885f4ab75 | 95 | } |
soonerbot | 4:adc885f4ab75 | 96 | left.setPower(leftpow); |
soonerbot | 4:adc885f4ab75 | 97 | right.setPower(rightpow); |
soonerbot | 1:c28fac16a109 | 98 | //int deltaTraveled=MAX(MIN(MIN(-constbuf[0]+prev[0],-constbuf[1]+prev[1]),MIN(constbuf[2]-prev[2],constbuf[3]-prev[3])),0); |
soonerbot | 4:adc885f4ab75 | 99 | int deltaTraveled=(constbuf[0]-prev[0]+constbuf[1]-prev[1])/2; |
soonerbot | 4:adc885f4ab75 | 100 | //DBGPRINT("\t %d\r\n",deltaTraveled); |
soonerbot | 1:c28fac16a109 | 101 | addforward(double(deltaTraveled)*0.0035362); |
soonerbot | 1:c28fac16a109 | 102 | distTraveled+=deltaTraveled; |
soonerbot | 4:adc885f4ab75 | 103 | loopcount++; |
soonerbot | 4:adc885f4ab75 | 104 | if((dmain<5&&dmain>-5)&&minmain<0.2){ |
soonerbot | 5:74c8ce39334b | 105 | minmain+=0.003; |
soonerbot | 4:adc885f4ab75 | 106 | } else if (minmain>=0.05) { |
soonerbot | 5:74c8ce39334b | 107 | minmain-=0.003; |
soonerbot | 4:adc885f4ab75 | 108 | } |
soonerbot | 4:adc885f4ab75 | 109 | if((dterm<5&&dterm>-5)&&minalt<0.2){ |
soonerbot | 5:74c8ce39334b | 110 | minalt+=0.003; |
soonerbot | 4:adc885f4ab75 | 111 | } else if (minalt>=0.05) { |
soonerbot | 5:74c8ce39334b | 112 | minalt-=0.003; |
soonerbot | 4:adc885f4ab75 | 113 | } |
soonerbot | 1:c28fac16a109 | 114 | } |
soonerbot | 4:adc885f4ab75 | 115 | DBGPRINT("Loops: %d\r\n",loopcount); |
soonerbot | 1:c28fac16a109 | 116 | left.brake(); |
soonerbot | 1:c28fac16a109 | 117 | right.brake(); |
soonerbot | 5:74c8ce39334b | 118 | wait(0.5); |
soonerbot | 5:74c8ce39334b | 119 | for(i=0;i<4;i++) |
soonerbot | 5:74c8ce39334b | 120 | prev[i]=constbuf[i]; |
soonerbot | 5:74c8ce39334b | 121 | //wait(0.05); |
soonerbot | 5:74c8ce39334b | 122 | constbuf = bigenc.getVals(); |
soonerbot | 5:74c8ce39334b | 123 | DBGPRINT("loss of %d and %d\n\r",constbuf[0]-prev[0],constbuf[1]-prev[1]); |
soonerbot | 1:c28fac16a109 | 124 | return 0; |
soonerbot | 1:c28fac16a109 | 125 | } |
soonerbot | 1:c28fac16a109 | 126 | |
soonerbot | 4:adc885f4ab75 | 127 | void robot::addforward(double dist){ |
soonerbot | 4:adc885f4ab75 | 128 | double angle=double(gyro.getZ())*2*3.14159/4050000.0; |
soonerbot | 1:c28fac16a109 | 129 | x+=dist*cos(angle); |
soonerbot | 1:c28fac16a109 | 130 | y+=dist*sin(angle); |
soonerbot | 1:c28fac16a109 | 131 | rot=angle; |
soonerbot | 1:c28fac16a109 | 132 | } |
soonerbot | 1:c28fac16a109 | 133 | |
soonerbot | 1:c28fac16a109 | 134 | int robot::moveTo(double xInches, double yInches){ |
soonerbot | 1:c28fac16a109 | 135 | double power=.2; |
soonerbot | 1:c28fac16a109 | 136 | turntowards(xInches,yInches); |
soonerbot | 1:c28fac16a109 | 137 | double distance=sqrt(pow(xInches-x,2)+pow(yInches-y,2))/0.0035362; |
soonerbot | 1:c28fac16a109 | 138 | double angle=atan2(xInches-x,yInches-y)*180.0/3.14159; |
soonerbot | 4:adc885f4ab75 | 139 | double currangle=double(gyro.getZ())*2*3.14159/4050000.0; |
soonerbot | 1:c28fac16a109 | 140 | bigenc.resetAll(); |
soonerbot | 1:c28fac16a109 | 141 | const int* constbuf = bigenc.getVals(); |
soonerbot | 1:c28fac16a109 | 142 | int prev[4]={0,0,0,0}; |
soonerbot | 1:c28fac16a109 | 143 | int distTraveled=0, i; |
soonerbot | 1:c28fac16a109 | 144 | double maxPow; |
soonerbot | 1:c28fac16a109 | 145 | DBGPRINT("going %f at angle %f from current of %f\r\n",distance,angle,currangle); |
soonerbot | 1:c28fac16a109 | 146 | while(distTraveled<distance){ |
soonerbot | 1:c28fac16a109 | 147 | angle=atan2(xInches-x,yInches-y)*180.0/3.14159; |
soonerbot | 4:adc885f4ab75 | 148 | currangle=double(gyro.getZ())*2*3.14159/4050000.0; |
soonerbot | 1:c28fac16a109 | 149 | maxPow=MAX(double(distance-distTraveled-2000)/15000.0,0.1); |
soonerbot | 1:c28fac16a109 | 150 | if(currangle>angle+2.0){ //too far to the right, brake left |
soonerbot | 1:c28fac16a109 | 151 | left.brake(); |
soonerbot | 1:c28fac16a109 | 152 | } else { |
soonerbot | 1:c28fac16a109 | 153 | left.setPower(MIN(power,maxPow)); |
soonerbot | 1:c28fac16a109 | 154 | } |
soonerbot | 1:c28fac16a109 | 155 | if(currangle<angle-2){ |
soonerbot | 1:c28fac16a109 | 156 | right.brake(); |
soonerbot | 1:c28fac16a109 | 157 | } else { |
soonerbot | 1:c28fac16a109 | 158 | right.setPower(MIN(power,maxPow)); |
soonerbot | 1:c28fac16a109 | 159 | } |
soonerbot | 1:c28fac16a109 | 160 | DBGPRINT("=%d of %d [%f] (%d, %d, %d, %d) \t{%f,\t%f,\t%f}\r\n",distTraveled,distance, maxPow, constbuf[0], constbuf[1], constbuf[2], constbuf[3],x,y,rot*180.0/3.14159); |
soonerbot | 1:c28fac16a109 | 161 | for(i=0;i<4;i++) |
soonerbot | 1:c28fac16a109 | 162 | prev[i]=constbuf[i]; |
soonerbot | 1:c28fac16a109 | 163 | //wait(0.05); |
soonerbot | 1:c28fac16a109 | 164 | constbuf = bigenc.getVals(); |
soonerbot | 1:c28fac16a109 | 165 | int deltaTraveled=MAX((MIN(-constbuf[0]+prev[0],-constbuf[1]+prev[1])+MIN(constbuf[2]-prev[2],constbuf[3]-prev[3]))/2,0); |
soonerbot | 1:c28fac16a109 | 166 | addforward(double(deltaTraveled)*0.0035362); |
soonerbot | 1:c28fac16a109 | 167 | distTraveled+=deltaTraveled; |
soonerbot | 1:c28fac16a109 | 168 | //angle=atan2(xInches-x,yInches-y)*180.0/3.14159; |
soonerbot | 1:c28fac16a109 | 169 | } |
soonerbot | 1:c28fac16a109 | 170 | left.brake(); |
soonerbot | 1:c28fac16a109 | 171 | right.brake(); |
soonerbot | 1:c28fac16a109 | 172 | return 0; |
soonerbot | 1:c28fac16a109 | 173 | |
soonerbot | 1:c28fac16a109 | 174 | } |
soonerbot | 1:c28fac16a109 | 175 | |
soonerbot | 1:c28fac16a109 | 176 | int robot::turntowards(double xInches, double yInches){ |
soonerbot | 4:adc885f4ab75 | 177 | double currangle=double(gyro.getZ())*2*3.14159/4050000.0; |
soonerbot | 1:c28fac16a109 | 178 | double angle=atan2(xInches-x,yInches-y)*180.0/3.14159; |
soonerbot | 1:c28fac16a109 | 179 | double finangle=angle; |
soonerbot | 1:c28fac16a109 | 180 | /*if(int(currangle-angle)%360>180){ //needs to turn positive degrees |
soonerbot | 1:c28fac16a109 | 181 | finangle=currangle+double(int(angle-currangle)%360); |
soonerbot | 1:c28fac16a109 | 182 | } else {//negative degrees |
soonerbot | 1:c28fac16a109 | 183 | finangle=currangle-double(int(currangle-angle)%360); |
soonerbot | 1:c28fac16a109 | 184 | }*/ |
soonerbot | 1:c28fac16a109 | 185 | double acc=turn(0.4,finangle); |
soonerbot | 1:c28fac16a109 | 186 | if(acc<-0.75 && acc>0.75){ |
soonerbot | 1:c28fac16a109 | 187 | acc=turn(0.3,finangle); |
soonerbot | 1:c28fac16a109 | 188 | } |
soonerbot | 1:c28fac16a109 | 189 | |
soonerbot | 1:c28fac16a109 | 190 | |
soonerbot | 1:c28fac16a109 | 191 | return 1; |
soonerbot | 1:c28fac16a109 | 192 | } |
soonerbot | 1:c28fac16a109 | 193 | |
soonerbot | 1:c28fac16a109 | 194 | |
soonerbot | 1:c28fac16a109 | 195 | double robot::turn(double power, double degrees){ |
soonerbot | 1:c28fac16a109 | 196 | bigenc.resetAll(); |
soonerbot | 1:c28fac16a109 | 197 | const int* constbuf = bigenc.getVals(); |
soonerbot | 1:c28fac16a109 | 198 | int prev[4]={0,0,0,0}; |
soonerbot | 1:c28fac16a109 | 199 | int startz; |
soonerbot | 4:adc885f4ab75 | 200 | startz=gyro.getZ(); |
soonerbot | 1:c28fac16a109 | 201 | int gyroticks=(degrees*4050000)/360; |
soonerbot | 1:c28fac16a109 | 202 | double maxPow; |
soonerbot | 1:c28fac16a109 | 203 | int nowz=startz; |
soonerbot | 1:c28fac16a109 | 204 | int dir=0; |
soonerbot | 1:c28fac16a109 | 205 | if(gyroticks>startz){ |
soonerbot | 1:c28fac16a109 | 206 | right.setPower(-power); |
soonerbot | 1:c28fac16a109 | 207 | left.setPower(power); |
soonerbot | 1:c28fac16a109 | 208 | dir=1; |
soonerbot | 1:c28fac16a109 | 209 | } else { |
soonerbot | 1:c28fac16a109 | 210 | right.setPower(power); |
soonerbot | 1:c28fac16a109 | 211 | left.setPower(-power); |
soonerbot | 1:c28fac16a109 | 212 | dir=-1; |
soonerbot | 1:c28fac16a109 | 213 | } |
soonerbot | 1:c28fac16a109 | 214 | while((gyroticks-nowz)*dir>0){ |
soonerbot | 1:c28fac16a109 | 215 | maxPow=MAX(double(abs(gyroticks-nowz))/4050000.0,0.25); |
soonerbot | 1:c28fac16a109 | 216 | if(gyroticks<nowz){ |
soonerbot | 1:c28fac16a109 | 217 | right.setPower(-MIN(power,maxPow)); |
soonerbot | 1:c28fac16a109 | 218 | left.setPower(MIN(power,maxPow)); |
soonerbot | 1:c28fac16a109 | 219 | } else { |
soonerbot | 1:c28fac16a109 | 220 | right.setPower(MIN(power,maxPow)); |
soonerbot | 1:c28fac16a109 | 221 | left.setPower(-MIN(power,maxPow)); |
soonerbot | 1:c28fac16a109 | 222 | } |
soonerbot | 1:c28fac16a109 | 223 | DBGPRINT("_%d of %d {%f, %f, %f}\r\n",(gyroticks-nowz)*dir, gyroticks,x,y,rot*180.0/3.14159); |
soonerbot | 1:c28fac16a109 | 224 | for(int i=0;i<4;i++) |
soonerbot | 1:c28fac16a109 | 225 | prev[i]=constbuf[i]; |
soonerbot | 1:c28fac16a109 | 226 | constbuf = bigenc.getVals(); |
soonerbot | 1:c28fac16a109 | 227 | int deltaTraveled; |
soonerbot | 1:c28fac16a109 | 228 | if(gyroticks<nowz){ |
soonerbot | 1:c28fac16a109 | 229 | deltaTraveled=(MIN(-constbuf[0]+prev[0],-constbuf[1]+prev[1])-MIN(-constbuf[2]+prev[2],-constbuf[3]+prev[3]))/2; |
soonerbot | 1:c28fac16a109 | 230 | } else { |
soonerbot | 1:c28fac16a109 | 231 | deltaTraveled=(-MIN(constbuf[0]-prev[0],constbuf[1]-prev[1])+MIN(constbuf[2]-prev[2],constbuf[3]-prev[3]))/2; |
soonerbot | 1:c28fac16a109 | 232 | } |
soonerbot | 1:c28fac16a109 | 233 | addforward(double(deltaTraveled)*0.0035362); |
soonerbot | 4:adc885f4ab75 | 234 | nowz=gyro.getZ(); |
soonerbot | 1:c28fac16a109 | 235 | } |
soonerbot | 1:c28fac16a109 | 236 | right.brake(); |
soonerbot | 1:c28fac16a109 | 237 | left.brake(); |
soonerbot | 1:c28fac16a109 | 238 | return (gyroticks-nowz)*dir; |
soonerbot | 1:c28fac16a109 | 239 | } |