Dependencies:   mbed

Committer:
shut
Date:
Fri Mar 22 15:22:15 2019 +0000
Revision:
2:c507076bfd93
Parent:
1:ef1f9f676295
Child:
3:a0b0e4acf5e9
Child:
4:5a892f5ab5a8
TESTING BALLS

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 0:719ea21609f1 4 #define M_PI 3.14159265358979323846
jsobiecki 0:719ea21609f1 5 //EXERCICIO 1
jsobiecki 0:719ea21609f1 6 //Luis Cruz N2011164454
jsobiecki 0:719ea21609f1 7 //Jacek Sobecki
jsobiecki 0:719ea21609f1 8 Serial pc(SERIAL_TX, SERIAL_RX, 115200);
jsobiecki 0:719ea21609f1 9 DigitalIn button(PC_13);
jsobiecki 0:719ea21609f1 10 void poseEst(float p[], float radius, float enc_res, float b);
jsobiecki 0:719ea21609f1 11 void SpeedLim(float w[]);
shut 2:c507076bfd93 12 //int ReadSensors();
shut 2:c507076bfd93 13 void updateHist(int hist[][], int active[][],int x,int y);
jsobiecki 0:719ea21609f1 14 int main(){
jsobiecki 0:719ea21609f1 15
jsobiecki 0:719ea21609f1 16 button.mode(PullUp);
jsobiecki 0:719ea21609f1 17 getCountsAndReset();
jsobiecki 0:719ea21609f1 18 setSpeeds(0, 0);
jsobiecki 0:719ea21609f1 19 while(button==1);
jsobiecki 0:719ea21609f1 20
jsobiecki 0:719ea21609f1 21 //w[0] = Omega | w[1] = X | w[2] = Y
jsobiecki 0:719ea21609f1 22 //p[0] = X | p[1] = Y | p[2] = Theta
jsobiecki 0:719ea21609f1 23 //p_obj[0] = X | p_obj[1] = Y | p_obj[2] = Theta
jsobiecki 0:719ea21609f1 24 //b = Distance between wheels, enc_res = Encoder Resolution, v = Calculated speed
jsobiecki 0:719ea21609f1 25 //k_v = Speed gain, k_s = Curvature gain, wratio = Angular speed ratio control command
shut 2:c507076bfd93 26 //Cells dim: 5x5cm |
shut 2:c507076bfd93 27 const int = m = 200, n = 200, activeSize = 11;
shut 2:c507076bfd93 28 int hist[m][n] , active[activeSize][activeSize];
shut 2:c507076bfd93 29 float w[3], v, p[3], p_obj[3], theta, theta_error, err, integral = 0.0;
shut 2:c507076bfd93 30 const float radius = 3.5, b = 13.3, enc_res = 1440, k_v = 7,
shut 2:c507076bfd93 31 k_s = 60, k_i = 1, sample_time = 0.05, Fcr = 1.0, d_stalker = 5.0;
jsobiecki 0:719ea21609f1 32 // ===============================================================================
jsobiecki 0:719ea21609f1 33 // =================================== COORDS ====================================
shut 2:c507076bfd93 34 // ===============================================================================
jsobiecki 0:719ea21609f1 35 //Target coordinates
shut 2:c507076bfd93 36 p_obj[0] = 400, p_obj[1] = 400, p_obj[2] = 0;
jsobiecki 0:719ea21609f1 37 //Initial coordinates:
shut 2:c507076bfd93 38 p[0] = 100, p[1] = 100, p[2] = 0;
jsobiecki 0:719ea21609f1 39 // ===============================================================================
jsobiecki 0:719ea21609f1 40 // =================================== EXECUTION =================================
jsobiecki 0:719ea21609f1 41 // ===============================================================================
jsobiecki 0:719ea21609f1 42 while(1){
jsobiecki 0:719ea21609f1 43 getCountsAndReset();
jsobiecki 0:719ea21609f1 44 pc.printf("Speeds: Left=%lf Right=%lf\n", w[1], w[2]);
shut 2:c507076bfd93 45 pc.printf("Odometer: X=%lf Y=%lf Theta=%lf\n", p[0], p[1], p[2]);
jsobiecki 0:719ea21609f1 46 pc.printf("Position: X=%lf Y=%lf Theta=%lf\n", p[0], p[1], p[2]);
shut 2:c507076bfd93 47
jsobiecki 0:719ea21609f1 48 //Path calculation
shut 2:c507076bfd93 49 poseEst(p, radius, enc_res, b); //Pose estimation
shut 2:c507076bfd93 50 //Control Law
shut 2:c507076bfd93 51 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 52 theta = atan2(p_obj[1]-p[1],p_obj[0]-p[0]);
jsobiecki 0:719ea21609f1 53 theta = atan2(sin(theta),cos(theta));
jsobiecki 0:719ea21609f1 54 p[2] = atan2(sin(p[2]),cos(p[2]));
jsobiecki 0:719ea21609f1 55 theta_error = theta-p[2];
shut 2:c507076bfd93 56 w[0] = k_s*(theta_error); //direction gain
shut 2:c507076bfd93 57 integral += err
shut 2:c507076bfd93 58 v = k_v*err+k_i*integral; //Speed gain
jsobiecki 0:719ea21609f1 59 w[1] = (v-(b/2)*w[0])/radius;
jsobiecki 0:719ea21609f1 60 w[2] = (v+(b/2)*w[0])/radius;
jsobiecki 0:719ea21609f1 61 SpeedLim(w);
jsobiecki 0:719ea21609f1 62 if((fabs(p[0]-p_obj[0])+fabs(p[1]-p_obj[1])) < 1){
jsobiecki 0:719ea21609f1 63 setSpeeds(0,0);
jsobiecki 0:719ea21609f1 64 }
jsobiecki 0:719ea21609f1 65 else{
jsobiecki 0:719ea21609f1 66 setSpeeds(w[1], w[2]);
jsobiecki 0:719ea21609f1 67 }
jsobiecki 0:719ea21609f1 68 wait(sample_time);
jsobiecki 0:719ea21609f1 69 }
jsobiecki 0:719ea21609f1 70 }
jsobiecki 0:719ea21609f1 71 // ===============================================================================
jsobiecki 0:719ea21609f1 72 // =================================== FUNCTIONS =================================
jsobiecki 0:719ea21609f1 73 // ===============================================================================
jsobiecki 0:719ea21609f1 74 //Pose Estimation function
jsobiecki 0:719ea21609f1 75 void poseEst(float p[], float radius, float enc_res, float b){
jsobiecki 0:719ea21609f1 76 float deltaDl, deltaDr, deltaD, deltaT;
jsobiecki 0:719ea21609f1 77 deltaDl = ((float)countsLeft)*(2.0*M_PI*radius/enc_res);
jsobiecki 0:719ea21609f1 78 deltaDr = ((float)countsRight)*(2.0*M_PI*radius/enc_res);
jsobiecki 0:719ea21609f1 79 deltaD = (deltaDr + deltaDl)/2;
jsobiecki 0:719ea21609f1 80 deltaT = (deltaDr - deltaDl)/b;
jsobiecki 0:719ea21609f1 81 if(fabs(deltaT) == 0){
jsobiecki 0:719ea21609f1 82 p[0] = p[0] + deltaD*cos(p[2]) + deltaT/2;
jsobiecki 0:719ea21609f1 83 p[1] = p[1] + deltaD*sin(p[2]) + deltaT/2;
jsobiecki 0:719ea21609f1 84 return;
jsobiecki 0:719ea21609f1 85 }
jsobiecki 0:719ea21609f1 86 p[0] = p[0] + deltaD*(sin(deltaT/2.0f)/(deltaT/2.0f))*cos(p[2]+deltaT/2.0f);
jsobiecki 0:719ea21609f1 87 p[1] = p[1] + deltaD*(sin(deltaT/2.0f)/(deltaT/2.0f))*sin(p[2]+deltaT/2.0f);
jsobiecki 0:719ea21609f1 88 p[2] = p[2] + deltaT;
jsobiecki 0:719ea21609f1 89 }
jsobiecki 0:719ea21609f1 90 //Speed limiter function
jsobiecki 0:719ea21609f1 91 void SpeedLim(float w[]){
jsobiecki 0:719ea21609f1 92 float wratio;
jsobiecki 0:719ea21609f1 93 wratio = w[2]/w[1];
jsobiecki 0:719ea21609f1 94 if(w[2] > 150 || w[1] > 150){
jsobiecki 0:719ea21609f1 95 if(wratio < 1){
jsobiecki 0:719ea21609f1 96 w[1] = 150;
jsobiecki 0:719ea21609f1 97 w[2] = w[1]*wratio;
jsobiecki 0:719ea21609f1 98 }
jsobiecki 0:719ea21609f1 99 else if(wratio > 1){
jsobiecki 0:719ea21609f1 100 w[2] = 150;
jsobiecki 0:719ea21609f1 101 w[1] = w[2]/wratio;
jsobiecki 0:719ea21609f1 102 }
jsobiecki 0:719ea21609f1 103 else{
jsobiecki 0:719ea21609f1 104 w[2] = 150;
jsobiecki 0:719ea21609f1 105 w[1] = 150;
jsobiecki 0:719ea21609f1 106 }
jsobiecki 0:719ea21609f1 107 }
jsobiecki 0:719ea21609f1 108 if(w[2] < 50 || w[1] < 50){
jsobiecki 0:719ea21609f1 109 if(wratio < 1){
jsobiecki 0:719ea21609f1 110 w[1] = 50;
jsobiecki 0:719ea21609f1 111 w[2] = w[1]*wratio;
jsobiecki 0:719ea21609f1 112 }
jsobiecki 0:719ea21609f1 113 else if(wratio > 1){
jsobiecki 0:719ea21609f1 114 w[2] = 50;
jsobiecki 0:719ea21609f1 115 w[1] = w[2]/wratio;
jsobiecki 0:719ea21609f1 116 }
jsobiecki 0:719ea21609f1 117 else{
jsobiecki 0:719ea21609f1 118 w[2] = 50;
jsobiecki 0:719ea21609f1 119 w[1] = 50;
jsobiecki 0:719ea21609f1 120 }
jsobiecki 0:719ea21609f1 121 }
jsobiecki 0:719ea21609f1 122 }
shut 2:c507076bfd93 123 void updateHist(int hist[i][j], int active[activeSize][activeSize],int x,int y){
shut 2:c507076bfd93 124 int aX=0;
shut 2:c507076bfd93 125 for(int i=x-(activeSize/2);i<x+(activeSize/2+1);i++){
shut 2:c507076bfd93 126 int aY=0;
shut 2:c507076bfd93 127 for(int j=y-(activeSize/2);j<y+(activeSize/2+1);j++){
shut 2:c507076bfd93 128 if(i>=0 && j>=0){
shut 2:c507076bfd93 129 hist[i][j]=active[aX][aY];
shut 2:c507076bfd93 130 aY++;
shut 2:c507076bfd93 131 }
shut 2:c507076bfd93 132 }
shut 2:c507076bfd93 133 aX++;
shut 2:c507076bfd93 134 }
shut 2:c507076bfd93 135 }