Dependencies:   mbed

Committer:
shut
Date:
Mon Apr 08 21:25:14 2019 +0000
Revision:
9:699054d8510b
Parent:
8:ed59eb8437c4
Child:
10:32c65de8ff37
qqqqq

Who changed what in which revision?

UserRevisionLine numberNew 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 7:3a755ebe4eaf 30 float secVal[30];
jsobiecki 7:3a755ebe4eaf 31 float smooth[30];
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 9:699054d8510b 47 k_s = 12/*10*/, sample_time = 0.05, d_stalker = 5, k_f = 12.5; /*7.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 9:699054d8510b 53 p_final[0] = 200, p_final[1] = 20, 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 9:699054d8510b 56 p[0] = 20, p[1] = 20, 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 9:699054d8510b 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 9:699054d8510b 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
shut 9:699054d8510b 92 pc.printf("w0 = %lf | w1 = %lf | w2 = %lf\n", w[0], w[1], w[2]);
jsobiecki 0:719ea21609f1 93 SpeedLim(w);
shut 6:2cd6ae395c0f 94 //if((fabs(p[0]-p_final[0])+fabs(p[1]-p_final[1])) < 70) k_i = -0.005;
shut 6:2cd6ae395c0f 95 if((fabs(p[0]-p_final[0])+fabs(p[1]-p_final[1])) < 4){
jsobiecki 0:719ea21609f1 96 setSpeeds(0,0);
jsobiecki 0:719ea21609f1 97 }
jsobiecki 0:719ea21609f1 98 else{
jsobiecki 0:719ea21609f1 99 setSpeeds(w[1], w[2]);
jsobiecki 0:719ea21609f1 100 }
jsobiecki 0:719ea21609f1 101 wait(sample_time);
jsobiecki 0:719ea21609f1 102 }
jsobiecki 0:719ea21609f1 103 }
jsobiecki 0:719ea21609f1 104 // ===============================================================================
jsobiecki 0:719ea21609f1 105 // =================================== FUNCTIONS =================================
jsobiecki 0:719ea21609f1 106 // ===============================================================================
jsobiecki 0:719ea21609f1 107 //Pose Estimation function
jsobiecki 0:719ea21609f1 108 void poseEst(float p[], float radius, float enc_res, float b){
jsobiecki 0:719ea21609f1 109 float deltaDl, deltaDr, deltaD, deltaT;
shut 9:699054d8510b 110 deltaDl = ((float)countsLeft)*(2.0f*M_PI*radius/enc_res);
shut 9:699054d8510b 111 deltaDr = ((float)countsRight)*(2.0f*M_PI*radius/enc_res);
shut 9:699054d8510b 112 deltaD = (deltaDr + deltaDl)/2.0f;
jsobiecki 0:719ea21609f1 113 deltaT = (deltaDr - deltaDl)/b;
jsobiecki 0:719ea21609f1 114 if(fabs(deltaT) == 0){
jsobiecki 0:719ea21609f1 115 p[0] = p[0] + deltaD*cos(p[2]) + deltaT/2;
jsobiecki 0:719ea21609f1 116 p[1] = p[1] + deltaD*sin(p[2]) + deltaT/2;
jsobiecki 0:719ea21609f1 117 return;
jsobiecki 0:719ea21609f1 118 }
jsobiecki 0:719ea21609f1 119 p[0] = p[0] + deltaD*(sin(deltaT/2.0f)/(deltaT/2.0f))*cos(p[2]+deltaT/2.0f);
jsobiecki 0:719ea21609f1 120 p[1] = p[1] + deltaD*(sin(deltaT/2.0f)/(deltaT/2.0f))*sin(p[2]+deltaT/2.0f);
jsobiecki 0:719ea21609f1 121 p[2] = p[2] + deltaT;
jsobiecki 0:719ea21609f1 122 }
jsobiecki 0:719ea21609f1 123 //Speed limiter function
jsobiecki 0:719ea21609f1 124 void SpeedLim(float w[]){
jsobiecki 0:719ea21609f1 125 float wratio;
shut 6:2cd6ae395c0f 126 wratio = fabs(w[2]/w[1]);
jsobiecki 0:719ea21609f1 127 if(w[2] > 150 || w[1] > 150){
jsobiecki 0:719ea21609f1 128 if(wratio < 1){
jsobiecki 0:719ea21609f1 129 w[1] = 150;
jsobiecki 0:719ea21609f1 130 w[2] = w[1]*wratio;
jsobiecki 0:719ea21609f1 131 }
jsobiecki 0:719ea21609f1 132 else if(wratio > 1){
jsobiecki 0:719ea21609f1 133 w[2] = 150;
jsobiecki 0:719ea21609f1 134 w[1] = w[2]/wratio;
jsobiecki 0:719ea21609f1 135 }
jsobiecki 0:719ea21609f1 136 else{
jsobiecki 0:719ea21609f1 137 w[2] = 150;
jsobiecki 0:719ea21609f1 138 w[1] = 150;
jsobiecki 0:719ea21609f1 139 }
jsobiecki 0:719ea21609f1 140 }
jsobiecki 0:719ea21609f1 141 if(w[2] < 50 || w[1] < 50){
jsobiecki 0:719ea21609f1 142 if(wratio < 1){
jsobiecki 0:719ea21609f1 143 w[1] = 50;
jsobiecki 0:719ea21609f1 144 w[2] = w[1]*wratio;
jsobiecki 0:719ea21609f1 145 }
jsobiecki 0:719ea21609f1 146 else if(wratio > 1){
jsobiecki 0:719ea21609f1 147 w[2] = 50;
jsobiecki 0:719ea21609f1 148 w[1] = w[2]/wratio;
jsobiecki 0:719ea21609f1 149 }
jsobiecki 0:719ea21609f1 150 else{
jsobiecki 0:719ea21609f1 151 w[2] = 50;
jsobiecki 0:719ea21609f1 152 w[1] = 50;
jsobiecki 0:719ea21609f1 153 }
jsobiecki 0:719ea21609f1 154 }
jsobiecki 0:719ea21609f1 155 }
jsobiecki 4:5a892f5ab5a8 156
jsobiecki 4:5a892f5ab5a8 157 void initializeArrays() {
jsobiecki 4:5a892f5ab5a8 158 for (int i = 0; i < hSize; i++) {
jsobiecki 4:5a892f5ab5a8 159 for (int j = 0; j < hSize; j++) {
jsobiecki 4:5a892f5ab5a8 160 histogram[i][j].calculate(i, j);
shut 9:699054d8510b 161 /*if(i>16 && i<26 && j > 0 && j<8)
shut 9:699054d8510b 162 histogram[i][j].cellVal=5;*/
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 }
jsobiecki 4:5a892f5ab5a8 187 int m = idXr - aSize / 2;
jsobiecki 4:5a892f5ab5a8 188 for (int k = 0; k < aSize; k++) {
jsobiecki 4:5a892f5ab5a8 189 int n = idYr - aSize / 2;
jsobiecki 4:5a892f5ab5a8 190 for (int l = 0; l < aSize; l++) {
shut 6:2cd6ae395c0f 191 if(m >= 0 && n >= 0 && m < hSize && n < hSize) {
jsobiecki 4:5a892f5ab5a8 192 activeReg[k][l].cellVal = histogram[m][n].cellVal;
jsobiecki 4:5a892f5ab5a8 193 }
jsobiecki 4:5a892f5ab5a8 194 n++;
jsobiecki 4:5a892f5ab5a8 195 }
jsobiecki 4:5a892f5ab5a8 196 m++;
jsobiecki 4:5a892f5ab5a8 197 }
jsobiecki 7:3a755ebe4eaf 198
jsobiecki 7:3a755ebe4eaf 199 for (int i = 0; i < aSize; i++) {
jsobiecki 7:3a755ebe4eaf 200 for (int j = 0; j < aSize; j++) {
jsobiecki 7:3a755ebe4eaf 201 activeReg[i][j].calForce();
jsobiecki 5:1649f59c37de 202 }
jsobiecki 5:1649f59c37de 203 }
jsobiecki 7:3a755ebe4eaf 204 activeReg[5][5].amplitude=0;
jsobiecki 7:3a755ebe4eaf 205 activeReg[5][5].amplitude=0;
jsobiecki 7:3a755ebe4eaf 206
jsobiecki 8:ed59eb8437c4 207 calcSectors(theta);
jsobiecki 7:3a755ebe4eaf 208 }
jsobiecki 8:ed59eb8437c4 209 void calcSectors(float theta){
jsobiecki 7:3a755ebe4eaf 210 for (int k = 0; k < 30; ++k) {
jsobiecki 7:3a755ebe4eaf 211 for (int i = 0; i < aSize; ++i) {
jsobiecki 7:3a755ebe4eaf 212 for (int j = 0; j < aSize; ++j) {
jsobiecki 7:3a755ebe4eaf 213 if(activeReg[i][j].sectorK==k)
jsobiecki 7:3a755ebe4eaf 214 secVal[k]+=activeReg[i][j].amplitude;
jsobiecki 7:3a755ebe4eaf 215 }
jsobiecki 7:3a755ebe4eaf 216 }
jsobiecki 7:3a755ebe4eaf 217 }
jsobiecki 7:3a755ebe4eaf 218
jsobiecki 7:3a755ebe4eaf 219 smooth[0]=(secVal[28]+2*secVal[29]+2*secVal[0]+2*secVal[1]+secVal[2])/5;
jsobiecki 7:3a755ebe4eaf 220 smooth[1]=(secVal[29]+2*secVal[0]+2*secVal[1]+2*secVal[2]+secVal[3])/5;
jsobiecki 7:3a755ebe4eaf 221 smooth[28]=(secVal[26]+2*secVal[27]+2*secVal[28]+2*secVal[29]+secVal[0])/5;
jsobiecki 7:3a755ebe4eaf 222 smooth[29]=(secVal[27]+2*secVal[28]+2*secVal[29]+2*secVal[0]+secVal[1])/5;
jsobiecki 7:3a755ebe4eaf 223 for (int i = 2; i < 28; ++i) {
jsobiecki 7:3a755ebe4eaf 224 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 225 }
jsobiecki 8:ed59eb8437c4 226
jsobiecki 8:ed59eb8437c4 227 const int thresh=100;
jsobiecki 8:ed59eb8437c4 228 int temp[30];
jsobiecki 8:ed59eb8437c4 229 for(int i=0;i<30;++i){
jsobiecki 8:ed59eb8437c4 230 if(smooth[i]<thresh)
jsobiecki 8:ed59eb8437c4 231 temp[i]=1;
jsobiecki 8:ed59eb8437c4 232 else
jsobiecki 8:ed59eb8437c4 233 temp[i]=0;
jsobiecki 8:ed59eb8437c4 234 }
shut 9:699054d8510b 235 float best=999;
shut 9:699054d8510b 236 float theta_deg;
shut 9:699054d8510b 237 theta_deg =theta*180.0f/3.14159265358979323846f;
shut 9:699054d8510b 238 if(theta_deg<0) theta_deg=360.0f+theta_deg;
shut 9:699054d8510b 239 pc.printf("theta corrr: = %lf\n\n", theta_deg);
shut 9:699054d8510b 240 for (int i = 0; i < 30; ++i) {
shut 9:699054d8510b 241 if(temp[i]!=0 && fabs(12*i-theta_deg)<fabs(12*best-theta_deg)) best=i;
jsobiecki 8:ed59eb8437c4 242 }
shut 9:699054d8510b 243 pc.printf("best = %lf\n\n", best);
shut 9:699054d8510b 244 fX=cos(best*12.0f*M_PI/180.0f);
shut 9:699054d8510b 245 fY=sin(best*12.0f*M_PI/180.0f);
jsobiecki 8:ed59eb8437c4 246
shut 2:c507076bfd93 247 }