vaLLEY TRY

Dependencies:   mbed

Committer:
jsobiecki
Date:
Mon Mar 18 11:23:17 2019 +0000
Revision:
0:719ea21609f1
Child:
1:ef1f9f676295
initial commit

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[]);
jsobiecki 0:719ea21609f1 12 int main(){
jsobiecki 0:719ea21609f1 13
jsobiecki 0:719ea21609f1 14 button.mode(PullUp);
jsobiecki 0:719ea21609f1 15 getCountsAndReset();
jsobiecki 0:719ea21609f1 16 setSpeeds(0, 0);
jsobiecki 0:719ea21609f1 17 while(button==1);
jsobiecki 0:719ea21609f1 18
jsobiecki 0:719ea21609f1 19 //w[0] = Omega | w[1] = X | w[2] = Y
jsobiecki 0:719ea21609f1 20 //p[0] = X | p[1] = Y | p[2] = Theta
jsobiecki 0:719ea21609f1 21 //p_obj[0] = X | p_obj[1] = Y | p_obj[2] = Theta
jsobiecki 0:719ea21609f1 22 //b = Distance between wheels, enc_res = Encoder Resolution, v = Calculated speed
jsobiecki 0:719ea21609f1 23 //k_v = Speed gain, k_s = Curvature gain, wratio = Angular speed ratio control command
jsobiecki 0:719ea21609f1 24 float w[3], v, p[3], p_obj[3], theta, theta_error;
jsobiecki 0:719ea21609f1 25 const float radius = 3.5, b = 13.3, enc_res = 1440, k_v = 7, k_s = 60, sample_time = 0.05;
jsobiecki 0:719ea21609f1 26 // ===============================================================================
jsobiecki 0:719ea21609f1 27 // =================================== COORDS ====================================
jsobiecki 0:719ea21609f1 28 // ===============================================================================
jsobiecki 0:719ea21609f1 29 //Target coordinates
jsobiecki 0:719ea21609f1 30 p_obj[0] = -40, p_obj[1] = 10, p_obj[2] = 0;
jsobiecki 0:719ea21609f1 31 //Initial coordinates:
jsobiecki 0:719ea21609f1 32 p[0] = 0, p[1] = 0, p[2] = 0;
jsobiecki 0:719ea21609f1 33 // ===============================================================================
jsobiecki 0:719ea21609f1 34 // =================================== EXECUTION =================================
jsobiecki 0:719ea21609f1 35 // ===============================================================================
jsobiecki 0:719ea21609f1 36 while(1){
jsobiecki 0:719ea21609f1 37 getCountsAndReset();
jsobiecki 0:719ea21609f1 38 pc.printf("Speeds: Left=%lf Right=%lf\n", w[1], w[2]);
jsobiecki 0:719ea21609f1 39 pc.printf("Position: X=%lf Y=%lf Theta=%lf\n", p[0], p[1], p[2]);
jsobiecki 0:719ea21609f1 40
jsobiecki 0:719ea21609f1 41 //Path calculation
jsobiecki 0:719ea21609f1 42 poseEst(p, radius, enc_res, b);
jsobiecki 0:719ea21609f1 43 theta = atan2(p_obj[1]-p[1],p_obj[0]-p[0]);
jsobiecki 0:719ea21609f1 44 theta = atan2(sin(theta),cos(theta));
jsobiecki 0:719ea21609f1 45 p[2] = atan2(sin(p[2]),cos(p[2]));
jsobiecki 0:719ea21609f1 46 theta_error = theta-p[2];
jsobiecki 0:719ea21609f1 47 w[0] = k_s*(theta_error);
jsobiecki 0:719ea21609f1 48 //pc.printf("\nOmega:%lf a:%lf\n", w[0], theta);
jsobiecki 0:719ea21609f1 49 v = k_v*sqrt(pow((p_obj[0]-p[0]),2)+pow((p_obj[1]-p[1]),2));
jsobiecki 0:719ea21609f1 50 w[1] = (v-(b/2)*w[0])/radius;
jsobiecki 0:719ea21609f1 51 w[2] = (v+(b/2)*w[0])/radius;
jsobiecki 0:719ea21609f1 52 SpeedLim(w);
jsobiecki 0:719ea21609f1 53 if((fabs(p[0]-p_obj[0])+fabs(p[1]-p_obj[1])) < 1){
jsobiecki 0:719ea21609f1 54 setSpeeds(0,0);
jsobiecki 0:719ea21609f1 55 }
jsobiecki 0:719ea21609f1 56 else{
jsobiecki 0:719ea21609f1 57 setSpeeds(w[1], w[2]);
jsobiecki 0:719ea21609f1 58 }
jsobiecki 0:719ea21609f1 59 wait(sample_time);
jsobiecki 0:719ea21609f1 60 }
jsobiecki 0:719ea21609f1 61 }
jsobiecki 0:719ea21609f1 62 // ===============================================================================
jsobiecki 0:719ea21609f1 63 // =================================== FUNCTIONS =================================
jsobiecki 0:719ea21609f1 64 // ===============================================================================
jsobiecki 0:719ea21609f1 65 //Pose Estimation function
jsobiecki 0:719ea21609f1 66 void poseEst(float p[], float radius, float enc_res, float b){
jsobiecki 0:719ea21609f1 67 float deltaDl, deltaDr, deltaD, deltaT;
jsobiecki 0:719ea21609f1 68 deltaDl = ((float)countsLeft)*(2.0*M_PI*radius/enc_res);
jsobiecki 0:719ea21609f1 69 deltaDr = ((float)countsRight)*(2.0*M_PI*radius/enc_res);
jsobiecki 0:719ea21609f1 70 deltaD = (deltaDr + deltaDl)/2;
jsobiecki 0:719ea21609f1 71 deltaT = (deltaDr - deltaDl)/b;
jsobiecki 0:719ea21609f1 72 if(fabs(deltaT) == 0){
jsobiecki 0:719ea21609f1 73 p[0] = p[0] + deltaD*cos(p[2]) + deltaT/2;
jsobiecki 0:719ea21609f1 74 p[1] = p[1] + deltaD*sin(p[2]) + deltaT/2;
jsobiecki 0:719ea21609f1 75 return;
jsobiecki 0:719ea21609f1 76 }
jsobiecki 0:719ea21609f1 77 p[0] = p[0] + deltaD*(sin(deltaT/2.0f)/(deltaT/2.0f))*cos(p[2]+deltaT/2.0f);
jsobiecki 0:719ea21609f1 78 p[1] = p[1] + deltaD*(sin(deltaT/2.0f)/(deltaT/2.0f))*sin(p[2]+deltaT/2.0f);
jsobiecki 0:719ea21609f1 79 p[2] = p[2] + deltaT;
jsobiecki 0:719ea21609f1 80 }
jsobiecki 0:719ea21609f1 81 //Speed limiter function
jsobiecki 0:719ea21609f1 82 void SpeedLim(float w[]){
jsobiecki 0:719ea21609f1 83 float wratio;
jsobiecki 0:719ea21609f1 84 wratio = w[2]/w[1];
jsobiecki 0:719ea21609f1 85 if(w[2] > 150 || w[1] > 150){
jsobiecki 0:719ea21609f1 86 if(wratio < 1){
jsobiecki 0:719ea21609f1 87 w[1] = 150;
jsobiecki 0:719ea21609f1 88 w[2] = w[1]*wratio;
jsobiecki 0:719ea21609f1 89 }
jsobiecki 0:719ea21609f1 90 else if(wratio > 1){
jsobiecki 0:719ea21609f1 91 w[2] = 150;
jsobiecki 0:719ea21609f1 92 w[1] = w[2]/wratio;
jsobiecki 0:719ea21609f1 93 }
jsobiecki 0:719ea21609f1 94 else{
jsobiecki 0:719ea21609f1 95 w[2] = 150;
jsobiecki 0:719ea21609f1 96 w[1] = 150;
jsobiecki 0:719ea21609f1 97 }
jsobiecki 0:719ea21609f1 98 }
jsobiecki 0:719ea21609f1 99 if(w[2] < 50 || w[1] < 50){
jsobiecki 0:719ea21609f1 100 if(wratio < 1){
jsobiecki 0:719ea21609f1 101 w[1] = 50;
jsobiecki 0:719ea21609f1 102 w[2] = w[1]*wratio;
jsobiecki 0:719ea21609f1 103 }
jsobiecki 0:719ea21609f1 104 else if(wratio > 1){
jsobiecki 0:719ea21609f1 105 w[2] = 50;
jsobiecki 0:719ea21609f1 106 w[1] = w[2]/wratio;
jsobiecki 0:719ea21609f1 107 }
jsobiecki 0:719ea21609f1 108 else{
jsobiecki 0:719ea21609f1 109 w[2] = 50;
jsobiecki 0:719ea21609f1 110 w[1] = 50;
jsobiecki 0:719ea21609f1 111 }
jsobiecki 0:719ea21609f1 112 }
jsobiecki 0:719ea21609f1 113 }