vaLLEY TRY
Dependencies: mbed
main.cpp@12:9a47af1cc7d7, 2019-04-09 (annotated)
- Committer:
- shut
- Date:
- Tue Apr 09 16:57:53 2019 +0000
- Revision:
- 12:9a47af1cc7d7
- Parent:
- 11:ce9832af1c3b
VALLEYS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jsobiecki | 0:719ea21609f1 | 1 | #include "mbed.h" |
jsobiecki | 0:719ea21609f1 | 2 | #include "Robot.h" |
jsobiecki | 0:719ea21609f1 | 3 | #include "math.h" |
jsobiecki | 4:5a892f5ab5a8 | 4 | #include "ActiveCell.h" |
jsobiecki | 4:5a892f5ab5a8 | 5 | #include "HistogramCell.h" |
shut | 9:699054d8510b | 6 | #define M_PI 3.14159265358979323846f |
jsobiecki | 0:719ea21609f1 | 7 | //EXERCICIO 1 |
jsobiecki | 0:719ea21609f1 | 8 | //Luis Cruz N2011164454 |
jsobiecki | 4:5a892f5ab5a8 | 9 | //Jacek Sobecki N2018319609 |
jsobiecki | 0:719ea21609f1 | 10 | Serial pc(SERIAL_TX, SERIAL_RX, 115200); |
jsobiecki | 0:719ea21609f1 | 11 | DigitalIn button(PC_13); |
jsobiecki | 0:719ea21609f1 | 12 | void poseEst(float p[], float radius, float enc_res, float b); |
jsobiecki | 0:719ea21609f1 | 13 | void SpeedLim(float w[]); |
jsobiecki | 4:5a892f5ab5a8 | 14 | void initializeArrays(); |
jsobiecki | 8:ed59eb8437c4 | 15 | void calcSectors(float theta); |
shut | 6:2cd6ae395c0f | 16 | void sumForces(); |
jsobiecki | 8:ed59eb8437c4 | 17 | void updateActive(float xR, float yR,float theta); |
shut | 2:c507076bfd93 | 18 | //int ReadSensors(); |
jsobiecki | 5:1649f59c37de | 19 | //const int m = 200, n = 200, activeSize = 11; |
shut | 6:2cd6ae395c0f | 20 | //histogram size | aSize active region size |
shut | 6:2cd6ae395c0f | 21 | const int hSize = 80, aSize = 11; |
jsobiecki | 4:5a892f5ab5a8 | 22 | ActiveCell activeReg[aSize][aSize]; |
jsobiecki | 4:5a892f5ab5a8 | 23 | HistogramCell histogram[hSize][hSize]; |
jsobiecki | 5:1649f59c37de | 24 | //Repulsive force sums |
shut | 6:2cd6ae395c0f | 25 | float p[3], p_obj[3], p_final[3], fX, fY; |
shut | 6:2cd6ae395c0f | 26 | const float Fca=6;/*5*/ |
jsobiecki | 7:3a755ebe4eaf | 27 | |
jsobiecki | 7:3a755ebe4eaf | 28 | //VFH |
jsobiecki | 7:3a755ebe4eaf | 29 | const int L=2; |
jsobiecki | 10:32c65de8ff37 | 30 | float secVal[36]; |
jsobiecki | 10:32c65de8ff37 | 31 | float smooth[36]; |
jsobiecki | 0:719ea21609f1 | 32 | int main(){ |
jsobiecki | 0:719ea21609f1 | 33 | |
jsobiecki | 0:719ea21609f1 | 34 | button.mode(PullUp); |
jsobiecki | 0:719ea21609f1 | 35 | getCountsAndReset(); |
jsobiecki | 0:719ea21609f1 | 36 | setSpeeds(0, 0); |
jsobiecki | 4:5a892f5ab5a8 | 37 | initializeArrays(); |
jsobiecki | 0:719ea21609f1 | 38 | while(button==1); |
shut | 6:2cd6ae395c0f | 39 | //w[0] = Omega | w[1] = Left | w[2] = Right |
jsobiecki | 0:719ea21609f1 | 40 | //p[0] = X | p[1] = Y | p[2] = Theta |
jsobiecki | 0:719ea21609f1 | 41 | //p_obj[0] = X | p_obj[1] = Y | p_obj[2] = Theta |
jsobiecki | 0:719ea21609f1 | 42 | //b = Distance between wheels, enc_res = Encoder Resolution, v = Calculated speed |
jsobiecki | 0:719ea21609f1 | 43 | //k_v = Speed gain, k_s = Curvature gain, wratio = Angular speed ratio control command |
shut | 2:c507076bfd93 | 44 | //Cells dim: 5x5cm | |
shut | 6:2cd6ae395c0f | 45 | float w[3], v, theta, theta_error, err, integral = 0.0, k_i = 0.01/*0.02*/; |
shut | 6:2cd6ae395c0f | 46 | const float radius = 3.5, b = 13.3, enc_res = 1440, k_v = 8/*7*/, |
shut | 11:ce9832af1c3b | 47 | k_s = 12/*10*/, sample_time = 0.05, d_stalker = 5, k_f = 12.5; /*12.5*/ //VFF |
shut | 9:699054d8510b | 48 | float theta_final; |
jsobiecki | 0:719ea21609f1 | 49 | // =============================================================================== |
jsobiecki | 0:719ea21609f1 | 50 | // =================================== COORDS ==================================== |
shut | 2:c507076bfd93 | 51 | // =============================================================================== |
jsobiecki | 0:719ea21609f1 | 52 | //Target coordinates |
shut | 12:9a47af1cc7d7 | 53 | p_final[0] = 100, p_final[1] = 70, p_final[2] = 0; |
shut | 6:2cd6ae395c0f | 54 | //p_obj[0] = 20, p_obj[1] = 20, p_obj[2] = 0; |
jsobiecki | 0:719ea21609f1 | 55 | //Initial coordinates: |
shut | 12:9a47af1cc7d7 | 56 | p[0] = 20, p[1] = 70, p[2] = 0; |
jsobiecki | 0:719ea21609f1 | 57 | // =============================================================================== |
jsobiecki | 0:719ea21609f1 | 58 | // =================================== EXECUTION ================================= |
jsobiecki | 0:719ea21609f1 | 59 | // =============================================================================== |
jsobiecki | 0:719ea21609f1 | 60 | while(1){ |
jsobiecki | 0:719ea21609f1 | 61 | getCountsAndReset(); |
jsobiecki | 0:719ea21609f1 | 62 | pc.printf("Speeds: Left=%lf Right=%lf\n", w[1], w[2]); |
shut | 6:2cd6ae395c0f | 63 | pc.printf("OBJECTIVE X: %lf OBJECTIVE Y: %lf\n", p_obj[0], p_obj[1]); |
shut | 6:2cd6ae395c0f | 64 | pc.printf("Position: X=%lf Y=%lf Theta=%lf\n\n", p[0], p[1], p[2]); |
shut | 6:2cd6ae395c0f | 65 | pc.printf("Force (X): X=%lf Force(Y)=%lf\n", fX, fY); |
shut | 2:c507076bfd93 | 66 | |
jsobiecki | 0:719ea21609f1 | 67 | //Path calculation |
shut | 2:c507076bfd93 | 68 | poseEst(p, radius, enc_res, b); //Pose estimation |
shut | 9:699054d8510b | 69 | theta_final = atan2(p_final[1]-p[1],p_final[0]-p[0]); |
shut | 9:699054d8510b | 70 | theta_final = atan2(sin(theta_final),cos(theta_final)); |
shut | 9:699054d8510b | 71 | updateActive(p[0], p[1], theta_final); |
shut | 6:2cd6ae395c0f | 72 | p_obj[0] = p[0]+k_f*fX; // add parameter to relate chosen direction (VFH) to the point nearby of the robot |
shut | 6:2cd6ae395c0f | 73 | p_obj[1] = p[1]+k_f*fY; |
shut | 2:c507076bfd93 | 74 | //Control Law |
shut | 2:c507076bfd93 | 75 | err = sqrt(pow((p_obj[0]-p[0]),2)+pow((p_obj[1]-p[1]),2)) - d_stalker; //distance to the point |
jsobiecki | 0:719ea21609f1 | 76 | theta = atan2(p_obj[1]-p[1],p_obj[0]-p[0]); |
shut | 11:ce9832af1c3b | 77 | //pc.printf("theta MAIN: = %lf\n\n", theta); |
jsobiecki | 0:719ea21609f1 | 78 | theta = atan2(sin(theta),cos(theta)); |
shut | 9:699054d8510b | 79 | |
shut | 9:699054d8510b | 80 | |
shut | 9:699054d8510b | 81 | |
jsobiecki | 0:719ea21609f1 | 82 | p[2] = atan2(sin(p[2]),cos(p[2])); |
jsobiecki | 0:719ea21609f1 | 83 | theta_error = theta-p[2]; |
shut | 9:699054d8510b | 84 | theta_error = atan2(sin(theta_error),cos(theta_error)); |
shut | 11:ce9832af1c3b | 85 | //pc.printf("theta_error = %lf | p[2]= %lf\n\n", theta_error, p[2]); |
shut | 2:c507076bfd93 | 86 | w[0] = k_s*(theta_error); //direction gain |
jsobiecki | 4:5a892f5ab5a8 | 87 | integral += err; |
shut | 6:2cd6ae395c0f | 88 | v = k_v*err+k_i*integral; //Speed calculation |
jsobiecki | 0:719ea21609f1 | 89 | w[1] = (v-(b/2)*w[0])/radius; |
jsobiecki | 0:719ea21609f1 | 90 | w[2] = (v+(b/2)*w[0])/radius; |
shut | 9:699054d8510b | 91 | |
jsobiecki | 0:719ea21609f1 | 92 | SpeedLim(w); |
shut | 6:2cd6ae395c0f | 93 | //if((fabs(p[0]-p_final[0])+fabs(p[1]-p_final[1])) < 70) k_i = -0.005; |
shut | 11:ce9832af1c3b | 94 | if((fabs(p[0]-p_final[0])+fabs(p[1]-p_final[1])) < 5){ |
jsobiecki | 0:719ea21609f1 | 95 | setSpeeds(0,0); |
jsobiecki | 0:719ea21609f1 | 96 | } |
jsobiecki | 0:719ea21609f1 | 97 | else{ |
jsobiecki | 0:719ea21609f1 | 98 | setSpeeds(w[1], w[2]); |
jsobiecki | 0:719ea21609f1 | 99 | } |
jsobiecki | 0:719ea21609f1 | 100 | wait(sample_time); |
jsobiecki | 0:719ea21609f1 | 101 | } |
jsobiecki | 0:719ea21609f1 | 102 | } |
jsobiecki | 0:719ea21609f1 | 103 | // =============================================================================== |
jsobiecki | 0:719ea21609f1 | 104 | // =================================== FUNCTIONS ================================= |
jsobiecki | 0:719ea21609f1 | 105 | // =============================================================================== |
jsobiecki | 0:719ea21609f1 | 106 | //Pose Estimation function |
jsobiecki | 0:719ea21609f1 | 107 | void poseEst(float p[], float radius, float enc_res, float b){ |
jsobiecki | 0:719ea21609f1 | 108 | float deltaDl, deltaDr, deltaD, deltaT; |
shut | 9:699054d8510b | 109 | deltaDl = ((float)countsLeft)*(2.0f*M_PI*radius/enc_res); |
shut | 9:699054d8510b | 110 | deltaDr = ((float)countsRight)*(2.0f*M_PI*radius/enc_res); |
shut | 9:699054d8510b | 111 | deltaD = (deltaDr + deltaDl)/2.0f; |
jsobiecki | 0:719ea21609f1 | 112 | deltaT = (deltaDr - deltaDl)/b; |
jsobiecki | 0:719ea21609f1 | 113 | if(fabs(deltaT) == 0){ |
jsobiecki | 0:719ea21609f1 | 114 | p[0] = p[0] + deltaD*cos(p[2]) + deltaT/2; |
jsobiecki | 0:719ea21609f1 | 115 | p[1] = p[1] + deltaD*sin(p[2]) + deltaT/2; |
jsobiecki | 0:719ea21609f1 | 116 | return; |
jsobiecki | 0:719ea21609f1 | 117 | } |
jsobiecki | 0:719ea21609f1 | 118 | p[0] = p[0] + deltaD*(sin(deltaT/2.0f)/(deltaT/2.0f))*cos(p[2]+deltaT/2.0f); |
jsobiecki | 0:719ea21609f1 | 119 | p[1] = p[1] + deltaD*(sin(deltaT/2.0f)/(deltaT/2.0f))*sin(p[2]+deltaT/2.0f); |
jsobiecki | 0:719ea21609f1 | 120 | p[2] = p[2] + deltaT; |
jsobiecki | 0:719ea21609f1 | 121 | } |
jsobiecki | 0:719ea21609f1 | 122 | //Speed limiter function |
jsobiecki | 0:719ea21609f1 | 123 | void SpeedLim(float w[]){ |
jsobiecki | 0:719ea21609f1 | 124 | float wratio; |
shut | 6:2cd6ae395c0f | 125 | wratio = fabs(w[2]/w[1]); |
jsobiecki | 0:719ea21609f1 | 126 | if(w[2] > 150 || w[1] > 150){ |
jsobiecki | 0:719ea21609f1 | 127 | if(wratio < 1){ |
jsobiecki | 0:719ea21609f1 | 128 | w[1] = 150; |
jsobiecki | 0:719ea21609f1 | 129 | w[2] = w[1]*wratio; |
jsobiecki | 0:719ea21609f1 | 130 | } |
jsobiecki | 0:719ea21609f1 | 131 | else if(wratio > 1){ |
jsobiecki | 0:719ea21609f1 | 132 | w[2] = 150; |
jsobiecki | 0:719ea21609f1 | 133 | w[1] = w[2]/wratio; |
jsobiecki | 0:719ea21609f1 | 134 | } |
jsobiecki | 0:719ea21609f1 | 135 | else{ |
jsobiecki | 0:719ea21609f1 | 136 | w[2] = 150; |
jsobiecki | 0:719ea21609f1 | 137 | w[1] = 150; |
jsobiecki | 0:719ea21609f1 | 138 | } |
jsobiecki | 0:719ea21609f1 | 139 | } |
jsobiecki | 0:719ea21609f1 | 140 | if(w[2] < 50 || w[1] < 50){ |
jsobiecki | 0:719ea21609f1 | 141 | if(wratio < 1){ |
jsobiecki | 0:719ea21609f1 | 142 | w[1] = 50; |
jsobiecki | 0:719ea21609f1 | 143 | w[2] = w[1]*wratio; |
jsobiecki | 0:719ea21609f1 | 144 | } |
jsobiecki | 0:719ea21609f1 | 145 | else if(wratio > 1){ |
jsobiecki | 0:719ea21609f1 | 146 | w[2] = 50; |
jsobiecki | 0:719ea21609f1 | 147 | w[1] = w[2]/wratio; |
jsobiecki | 0:719ea21609f1 | 148 | } |
jsobiecki | 0:719ea21609f1 | 149 | else{ |
jsobiecki | 0:719ea21609f1 | 150 | w[2] = 50; |
jsobiecki | 0:719ea21609f1 | 151 | w[1] = 50; |
jsobiecki | 0:719ea21609f1 | 152 | } |
jsobiecki | 0:719ea21609f1 | 153 | } |
jsobiecki | 0:719ea21609f1 | 154 | } |
jsobiecki | 4:5a892f5ab5a8 | 155 | |
jsobiecki | 4:5a892f5ab5a8 | 156 | void initializeArrays() { |
jsobiecki | 4:5a892f5ab5a8 | 157 | for (int i = 0; i < hSize; i++) { |
jsobiecki | 4:5a892f5ab5a8 | 158 | for (int j = 0; j < hSize; j++) { |
jsobiecki | 4:5a892f5ab5a8 | 159 | histogram[i][j].calculate(i, j); |
shut | 12:9a47af1cc7d7 | 160 | if(((i >= 8 && i <= 12) && (j == 0 || j == 18)) || ((i == 8 || i == 12) && (j >= 0 && j <= 18))) histogram[i][j].cellVal=3; |
shut | 12:9a47af1cc7d7 | 161 | if(((i >= 0 && i <= 3) && (j == 18 || j == 22)) || ((i == 0 || i == 3) && (j >= 18 && j <= 22))) histogram[i][j].cellVal=3; |
shut | 12:9a47af1cc7d7 | 162 | if(((i >= 14 && i <= 20) && (j == 18 || j == 19)) || ((i == 14 || i == 20) && (j >= 18 && j <= 19))) histogram[i][j].cellVal=3; |
jsobiecki | 4:5a892f5ab5a8 | 163 | } |
jsobiecki | 4:5a892f5ab5a8 | 164 | } |
jsobiecki | 4:5a892f5ab5a8 | 165 | for (int i = 0; i < aSize; i++) { |
jsobiecki | 4:5a892f5ab5a8 | 166 | for (int j = 0; j < aSize; j++) { |
jsobiecki | 4:5a892f5ab5a8 | 167 | activeReg[i][j].calDist(i, j); |
jsobiecki | 4:5a892f5ab5a8 | 168 | } |
jsobiecki | 4:5a892f5ab5a8 | 169 | } |
jsobiecki | 4:5a892f5ab5a8 | 170 | } |
jsobiecki | 7:3a755ebe4eaf | 171 | |
jsobiecki | 5:1649f59c37de | 172 | //every time robot changes position we need to call this function to update active region and calculate forces |
jsobiecki | 4:5a892f5ab5a8 | 173 | //xR, yR - robots position in coordinates system |
jsobiecki | 8:ed59eb8437c4 | 174 | void updateActive(float xR, float yR,float theta) { |
jsobiecki | 4:5a892f5ab5a8 | 175 | int idXr = 0; |
jsobiecki | 4:5a892f5ab5a8 | 176 | int idYr = 0; |
jsobiecki | 4:5a892f5ab5a8 | 177 | for (int i = 0; i < hSize; i++) { |
jsobiecki | 4:5a892f5ab5a8 | 178 | for (int j = 0; j < hSize; j++) { |
shut | 6:2cd6ae395c0f | 179 | if (xR > histogram[i][j].x - 2.5f && xR < histogram[i][j].x + 2.5f && yR > histogram[i][j].y - 2.5f && |
shut | 6:2cd6ae395c0f | 180 | yR < histogram[i][j].y + 2.5f) { |
jsobiecki | 4:5a892f5ab5a8 | 181 | idXr = i; |
jsobiecki | 4:5a892f5ab5a8 | 182 | idYr = j; |
jsobiecki | 4:5a892f5ab5a8 | 183 | break; |
shut | 2:c507076bfd93 | 184 | } |
shut | 2:c507076bfd93 | 185 | } |
shut | 2:c507076bfd93 | 186 | } |
shut | 11:ce9832af1c3b | 187 | |
jsobiecki | 4:5a892f5ab5a8 | 188 | int m = idXr - aSize / 2; |
jsobiecki | 4:5a892f5ab5a8 | 189 | for (int k = 0; k < aSize; k++) { |
jsobiecki | 4:5a892f5ab5a8 | 190 | int n = idYr - aSize / 2; |
jsobiecki | 4:5a892f5ab5a8 | 191 | for (int l = 0; l < aSize; l++) { |
shut | 6:2cd6ae395c0f | 192 | if(m >= 0 && n >= 0 && m < hSize && n < hSize) { |
jsobiecki | 4:5a892f5ab5a8 | 193 | activeReg[k][l].cellVal = histogram[m][n].cellVal; |
jsobiecki | 4:5a892f5ab5a8 | 194 | } |
jsobiecki | 4:5a892f5ab5a8 | 195 | n++; |
jsobiecki | 4:5a892f5ab5a8 | 196 | } |
jsobiecki | 4:5a892f5ab5a8 | 197 | m++; |
jsobiecki | 4:5a892f5ab5a8 | 198 | } |
jsobiecki | 7:3a755ebe4eaf | 199 | |
jsobiecki | 7:3a755ebe4eaf | 200 | for (int i = 0; i < aSize; i++) { |
jsobiecki | 7:3a755ebe4eaf | 201 | for (int j = 0; j < aSize; j++) { |
jsobiecki | 7:3a755ebe4eaf | 202 | activeReg[i][j].calForce(); |
jsobiecki | 5:1649f59c37de | 203 | } |
jsobiecki | 5:1649f59c37de | 204 | } |
jsobiecki | 7:3a755ebe4eaf | 205 | activeReg[5][5].amplitude=0; |
jsobiecki | 7:3a755ebe4eaf | 206 | activeReg[5][5].amplitude=0; |
shut | 12:9a47af1cc7d7 | 207 | for (int j = 10; j >= 0; j--) { |
shut | 12:9a47af1cc7d7 | 208 | for (int i = 0; i < 11; i++) { |
shut | 12:9a47af1cc7d7 | 209 | cout << "[" << activeReg[i][j].cellVal << "]"; |
shut | 12:9a47af1cc7d7 | 210 | } |
shut | 12:9a47af1cc7d7 | 211 | cout << endl; |
shut | 12:9a47af1cc7d7 | 212 | } |
jsobiecki | 8:ed59eb8437c4 | 213 | calcSectors(theta); |
jsobiecki | 7:3a755ebe4eaf | 214 | } |
jsobiecki | 8:ed59eb8437c4 | 215 | void calcSectors(float theta){ |
jsobiecki | 10:32c65de8ff37 | 216 | for (int k = 0; k < 36; ++k) { |
shut | 11:ce9832af1c3b | 217 | secVal[k]=0; |
jsobiecki | 7:3a755ebe4eaf | 218 | for (int i = 0; i < aSize; ++i) { |
jsobiecki | 7:3a755ebe4eaf | 219 | for (int j = 0; j < aSize; ++j) { |
jsobiecki | 7:3a755ebe4eaf | 220 | if(activeReg[i][j].sectorK==k) |
jsobiecki | 7:3a755ebe4eaf | 221 | secVal[k]+=activeReg[i][j].amplitude; |
jsobiecki | 7:3a755ebe4eaf | 222 | } |
jsobiecki | 7:3a755ebe4eaf | 223 | } |
jsobiecki | 7:3a755ebe4eaf | 224 | } |
jsobiecki | 7:3a755ebe4eaf | 225 | |
jsobiecki | 7:3a755ebe4eaf | 226 | smooth[0]=(secVal[28]+2*secVal[29]+2*secVal[0]+2*secVal[1]+secVal[2])/5; |
jsobiecki | 7:3a755ebe4eaf | 227 | smooth[1]=(secVal[29]+2*secVal[0]+2*secVal[1]+2*secVal[2]+secVal[3])/5; |
jsobiecki | 10:32c65de8ff37 | 228 | smooth[34]=(secVal[32]+2*secVal[33]+2*secVal[34]+2*secVal[35]+secVal[0])/5; |
jsobiecki | 10:32c65de8ff37 | 229 | smooth[35]=(secVal[33]+2*secVal[34]+2*secVal[35]+2*secVal[0]+secVal[1])/5; |
jsobiecki | 10:32c65de8ff37 | 230 | for (int i = 2; i < 34; ++i) { |
jsobiecki | 7:3a755ebe4eaf | 231 | smooth[i]=(secVal[i-L]+2*secVal[i-L+1]+2*secVal[i]+2*secVal[i+L-1]+secVal[i+L])/5; |
jsobiecki | 7:3a755ebe4eaf | 232 | } |
jsobiecki | 8:ed59eb8437c4 | 233 | |
shut | 11:ce9832af1c3b | 234 | const int thresh=200;//100 |
jsobiecki | 10:32c65de8ff37 | 235 | int temp[36]; |
shut | 12:9a47af1cc7d7 | 236 | int counter[5], aux = 0; |
shut | 12:9a47af1cc7d7 | 237 | bool val_count = true; |
shut | 12:9a47af1cc7d7 | 238 | int kl[5], kr[5]; |
jsobiecki | 10:32c65de8ff37 | 239 | for(int i=0;i<36;++i){ |
shut | 11:ce9832af1c3b | 240 | //pc.printf("|%lf", smooth[i]); |
shut | 12:9a47af1cc7d7 | 241 | if(smooth[i] < thresh){ |
jsobiecki | 8:ed59eb8437c4 | 242 | temp[i]=1; |
shut | 12:9a47af1cc7d7 | 243 | counter[aux]++; |
shut | 12:9a47af1cc7d7 | 244 | val_count = true; |
shut | 11:ce9832af1c3b | 245 | } |
shut | 12:9a47af1cc7d7 | 246 | /*else if(smooth[i] >= thresh && val_count == true){ |
shut | 12:9a47af1cc7d7 | 247 | kr[aux] = i-counter[aux]; |
shut | 12:9a47af1cc7d7 | 248 | kl[aux] = i-1; |
shut | 12:9a47af1cc7d7 | 249 | temp[i]=0; |
shut | 12:9a47af1cc7d7 | 250 | aux++; |
shut | 12:9a47af1cc7d7 | 251 | }*/ |
shut | 11:ce9832af1c3b | 252 | else{ |
shut | 12:9a47af1cc7d7 | 253 | temp[i] = 0; |
shut | 12:9a47af1cc7d7 | 254 | val_count = false; |
shut | 12:9a47af1cc7d7 | 255 | } |
jsobiecki | 8:ed59eb8437c4 | 256 | } |
shut | 12:9a47af1cc7d7 | 257 | |
shut | 9:699054d8510b | 258 | float best=999; |
shut | 9:699054d8510b | 259 | float theta_deg; |
shut | 11:ce9832af1c3b | 260 | theta_deg =(theta*180.0f)/M_PI; |
shut | 11:ce9832af1c3b | 261 | pc.printf("theta (degrees): = %lf\n\n", theta_deg); |
shut | 11:ce9832af1c3b | 262 | float sectorA, sectorB; |
jsobiecki | 10:32c65de8ff37 | 263 | for (int i = 0; i < 36; ++i) { |
shut | 11:ce9832af1c3b | 264 | sectorA = 10*i; |
shut | 11:ce9832af1c3b | 265 | sectorB = 10*best; |
shut | 11:ce9832af1c3b | 266 | if(sectorA > 180) sectorA=-(360-sectorA); |
shut | 11:ce9832af1c3b | 267 | if(sectorB > 180) sectorB=-(360-sectorB); |
shut | 12:9a47af1cc7d7 | 268 | pc.printf("sectorA = %lf sectorB = %lf temp[%d] = %d smooth[%d] = %lf secVal[%d] = %lf \n", sectorA, sectorB, i, temp[i], i, smooth[i], i, secVal[i]); |
shut | 11:ce9832af1c3b | 269 | if(temp[i]!=0 && (fabs(sectorA-theta_deg)<fabs(sectorB-theta_deg))) best=i; |
jsobiecki | 8:ed59eb8437c4 | 270 | } |
shut | 12:9a47af1cc7d7 | 271 | int i=best-1; |
shut | 12:9a47af1cc7d7 | 272 | int size=1; |
shut | 12:9a47af1cc7d7 | 273 | while(temp[i]==1){ |
shut | 12:9a47af1cc7d7 | 274 | size++; |
shut | 12:9a47af1cc7d7 | 275 | i--; |
shut | 12:9a47af1cc7d7 | 276 | if(i<0) i=35; |
shut | 12:9a47af1cc7d7 | 277 | } |
shut | 12:9a47af1cc7d7 | 278 | int right=i-1; |
shut | 12:9a47af1cc7d7 | 279 | if(right<0) i=35; |
shut | 12:9a47af1cc7d7 | 280 | i=best+1; |
shut | 12:9a47af1cc7d7 | 281 | while(temp[i]==1){ |
shut | 12:9a47af1cc7d7 | 282 | size++; |
shut | 12:9a47af1cc7d7 | 283 | i++; |
shut | 12:9a47af1cc7d7 | 284 | if(i>35) i=0; |
shut | 12:9a47af1cc7d7 | 285 | } |
shut | 12:9a47af1cc7d7 | 286 | int left=i+1; |
shut | 12:9a47af1cc7d7 | 287 | if(left>35) left=0; |
shut | 12:9a47af1cc7d7 | 288 | float dir; |
shut | 12:9a47af1cc7d7 | 289 | if(size>5) //wide |
shut | 12:9a47af1cc7d7 | 290 | dir=best*10+0.5*10*5; |
shut | 12:9a47af1cc7d7 | 291 | else //narrow |
shut | 12:9a47af1cc7d7 | 292 | dir=0.5*(left*10+right*10); |
shut | 12:9a47af1cc7d7 | 293 | |
shut | 9:699054d8510b | 294 | pc.printf("best = %lf\n\n", best); |
shut | 12:9a47af1cc7d7 | 295 | fX=cos(dir*M_PI/180.0f); |
shut | 12:9a47af1cc7d7 | 296 | fY=sin(dir*M_PI/180.0f); |
jsobiecki | 8:ed59eb8437c4 | 297 | |
shut | 2:c507076bfd93 | 298 | } |