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.
Dependencies: mbed
main.cpp@1:ef1f9f676295, 2019-03-18 (annotated)
- Committer:
- jsobiecki
- Date:
- Mon Mar 18 11:29:27 2019 +0000
- Revision:
- 1:ef1f9f676295
- Parent:
- 0:719ea21609f1
- Child:
- 2:c507076bfd93
test
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 | 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 | 1:ef1f9f676295 | 14 | //test | 
| jsobiecki | 1:ef1f9f676295 | 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 | 
| jsobiecki | 0:719ea21609f1 | 26 | float w[3], v, p[3], p_obj[3], theta, theta_error; | 
| jsobiecki | 0:719ea21609f1 | 27 | const float radius = 3.5, b = 13.3, enc_res = 1440, k_v = 7, k_s = 60, sample_time = 0.05; | 
| jsobiecki | 0:719ea21609f1 | 28 | // =============================================================================== | 
| jsobiecki | 0:719ea21609f1 | 29 | // =================================== COORDS ==================================== | 
| jsobiecki | 0:719ea21609f1 | 30 | // =============================================================================== | 
| jsobiecki | 0:719ea21609f1 | 31 | //Target coordinates | 
| jsobiecki | 0:719ea21609f1 | 32 | p_obj[0] = -40, p_obj[1] = 10, p_obj[2] = 0; | 
| jsobiecki | 0:719ea21609f1 | 33 | //Initial coordinates: | 
| jsobiecki | 0:719ea21609f1 | 34 | p[0] = 0, p[1] = 0, p[2] = 0; | 
| jsobiecki | 0:719ea21609f1 | 35 | // =============================================================================== | 
| jsobiecki | 0:719ea21609f1 | 36 | // =================================== EXECUTION ================================= | 
| jsobiecki | 0:719ea21609f1 | 37 | // =============================================================================== | 
| jsobiecki | 0:719ea21609f1 | 38 | while(1){ | 
| jsobiecki | 0:719ea21609f1 | 39 | getCountsAndReset(); | 
| jsobiecki | 0:719ea21609f1 | 40 | pc.printf("Speeds: Left=%lf Right=%lf\n", w[1], w[2]); | 
| jsobiecki | 0:719ea21609f1 | 41 | pc.printf("Position: X=%lf Y=%lf Theta=%lf\n", p[0], p[1], p[2]); | 
| jsobiecki | 0:719ea21609f1 | 42 | |
| jsobiecki | 0:719ea21609f1 | 43 | //Path calculation | 
| jsobiecki | 0:719ea21609f1 | 44 | poseEst(p, radius, enc_res, b); | 
| jsobiecki | 0:719ea21609f1 | 45 | theta = atan2(p_obj[1]-p[1],p_obj[0]-p[0]); | 
| jsobiecki | 0:719ea21609f1 | 46 | theta = atan2(sin(theta),cos(theta)); | 
| jsobiecki | 0:719ea21609f1 | 47 | p[2] = atan2(sin(p[2]),cos(p[2])); | 
| jsobiecki | 0:719ea21609f1 | 48 | theta_error = theta-p[2]; | 
| jsobiecki | 0:719ea21609f1 | 49 | w[0] = k_s*(theta_error); | 
| jsobiecki | 0:719ea21609f1 | 50 | //pc.printf("\nOmega:%lf a:%lf\n", w[0], theta); | 
| jsobiecki | 0:719ea21609f1 | 51 | v = k_v*sqrt(pow((p_obj[0]-p[0]),2)+pow((p_obj[1]-p[1]),2)); | 
| jsobiecki | 0:719ea21609f1 | 52 | w[1] = (v-(b/2)*w[0])/radius; | 
| jsobiecki | 0:719ea21609f1 | 53 | w[2] = (v+(b/2)*w[0])/radius; | 
| jsobiecki | 0:719ea21609f1 | 54 | SpeedLim(w); | 
| jsobiecki | 0:719ea21609f1 | 55 | if((fabs(p[0]-p_obj[0])+fabs(p[1]-p_obj[1])) < 1){ | 
| jsobiecki | 0:719ea21609f1 | 56 | setSpeeds(0,0); | 
| jsobiecki | 0:719ea21609f1 | 57 | } | 
| jsobiecki | 0:719ea21609f1 | 58 | else{ | 
| jsobiecki | 0:719ea21609f1 | 59 | setSpeeds(w[1], w[2]); | 
| jsobiecki | 0:719ea21609f1 | 60 | } | 
| jsobiecki | 0:719ea21609f1 | 61 | wait(sample_time); | 
| jsobiecki | 0:719ea21609f1 | 62 | } | 
| jsobiecki | 0:719ea21609f1 | 63 | } | 
| jsobiecki | 0:719ea21609f1 | 64 | // =============================================================================== | 
| jsobiecki | 0:719ea21609f1 | 65 | // =================================== FUNCTIONS ================================= | 
| jsobiecki | 0:719ea21609f1 | 66 | // =============================================================================== | 
| jsobiecki | 0:719ea21609f1 | 67 | //Pose Estimation function | 
| jsobiecki | 0:719ea21609f1 | 68 | void poseEst(float p[], float radius, float enc_res, float b){ | 
| jsobiecki | 0:719ea21609f1 | 69 | float deltaDl, deltaDr, deltaD, deltaT; | 
| jsobiecki | 0:719ea21609f1 | 70 | deltaDl = ((float)countsLeft)*(2.0*M_PI*radius/enc_res); | 
| jsobiecki | 0:719ea21609f1 | 71 | deltaDr = ((float)countsRight)*(2.0*M_PI*radius/enc_res); | 
| jsobiecki | 0:719ea21609f1 | 72 | deltaD = (deltaDr + deltaDl)/2; | 
| jsobiecki | 0:719ea21609f1 | 73 | deltaT = (deltaDr - deltaDl)/b; | 
| jsobiecki | 0:719ea21609f1 | 74 | if(fabs(deltaT) == 0){ | 
| jsobiecki | 0:719ea21609f1 | 75 | p[0] = p[0] + deltaD*cos(p[2]) + deltaT/2; | 
| jsobiecki | 0:719ea21609f1 | 76 | p[1] = p[1] + deltaD*sin(p[2]) + deltaT/2; | 
| jsobiecki | 0:719ea21609f1 | 77 | return; | 
| jsobiecki | 0:719ea21609f1 | 78 | } | 
| jsobiecki | 0:719ea21609f1 | 79 | p[0] = p[0] + deltaD*(sin(deltaT/2.0f)/(deltaT/2.0f))*cos(p[2]+deltaT/2.0f); | 
| jsobiecki | 0:719ea21609f1 | 80 | p[1] = p[1] + deltaD*(sin(deltaT/2.0f)/(deltaT/2.0f))*sin(p[2]+deltaT/2.0f); | 
| jsobiecki | 0:719ea21609f1 | 81 | p[2] = p[2] + deltaT; | 
| jsobiecki | 0:719ea21609f1 | 82 | } | 
| jsobiecki | 0:719ea21609f1 | 83 | //Speed limiter function | 
| jsobiecki | 0:719ea21609f1 | 84 | void SpeedLim(float w[]){ | 
| jsobiecki | 0:719ea21609f1 | 85 | float wratio; | 
| jsobiecki | 0:719ea21609f1 | 86 | wratio = w[2]/w[1]; | 
| jsobiecki | 0:719ea21609f1 | 87 | if(w[2] > 150 || w[1] > 150){ | 
| jsobiecki | 0:719ea21609f1 | 88 | if(wratio < 1){ | 
| jsobiecki | 0:719ea21609f1 | 89 | w[1] = 150; | 
| jsobiecki | 0:719ea21609f1 | 90 | w[2] = w[1]*wratio; | 
| jsobiecki | 0:719ea21609f1 | 91 | } | 
| jsobiecki | 0:719ea21609f1 | 92 | else if(wratio > 1){ | 
| jsobiecki | 0:719ea21609f1 | 93 | w[2] = 150; | 
| jsobiecki | 0:719ea21609f1 | 94 | w[1] = w[2]/wratio; | 
| jsobiecki | 0:719ea21609f1 | 95 | } | 
| jsobiecki | 0:719ea21609f1 | 96 | else{ | 
| jsobiecki | 0:719ea21609f1 | 97 | w[2] = 150; | 
| jsobiecki | 0:719ea21609f1 | 98 | w[1] = 150; | 
| jsobiecki | 0:719ea21609f1 | 99 | } | 
| jsobiecki | 0:719ea21609f1 | 100 | } | 
| jsobiecki | 0:719ea21609f1 | 101 | if(w[2] < 50 || w[1] < 50){ | 
| jsobiecki | 0:719ea21609f1 | 102 | if(wratio < 1){ | 
| jsobiecki | 0:719ea21609f1 | 103 | w[1] = 50; | 
| jsobiecki | 0:719ea21609f1 | 104 | w[2] = w[1]*wratio; | 
| jsobiecki | 0:719ea21609f1 | 105 | } | 
| jsobiecki | 0:719ea21609f1 | 106 | else if(wratio > 1){ | 
| jsobiecki | 0:719ea21609f1 | 107 | w[2] = 50; | 
| jsobiecki | 0:719ea21609f1 | 108 | w[1] = w[2]/wratio; | 
| jsobiecki | 0:719ea21609f1 | 109 | } | 
| jsobiecki | 0:719ea21609f1 | 110 | else{ | 
| jsobiecki | 0:719ea21609f1 | 111 | w[2] = 50; | 
| jsobiecki | 0:719ea21609f1 | 112 | w[1] = 50; | 
| jsobiecki | 0:719ea21609f1 | 113 | } | 
| jsobiecki | 0:719ea21609f1 | 114 | } | 
| jsobiecki | 0:719ea21609f1 | 115 | } |