![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
All the lab works are here!
Dependencies: ISR_Mini-explorer mbed
Fork of VirtualForces by
main.cpp@7:c94070f9af78, 2017-03-24 (annotated)
- Committer:
- AurelienBernier
- Date:
- Fri Mar 24 18:02:57 2017 +0000
- Revision:
- 7:c94070f9af78
- Parent:
- 6:afde4b08166b
- Child:
- 8:109314be5b68
Final values kappa;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
geotsam | 0:8bffb51cc345 | 1 | #include "mbed.h" |
geotsam | 0:8bffb51cc345 | 2 | #include "robot.h" // Initializes the robot. This include should be used in all main.cpp! |
geotsam | 0:8bffb51cc345 | 3 | #include "math.h" |
AurelienBernier | 6:afde4b08166b | 4 | |
AurelienBernier | 6:afde4b08166b | 5 | Timer t; |
AurelienBernier | 4:8c56c3ba6e54 | 6 | |
geotsam | 0:8bffb51cc345 | 7 | float dist(float robot_x, float robot_y, float target_x, float target_y); |
geotsam | 0:8bffb51cc345 | 8 | |
AurelienBernier | 4:8c56c3ba6e54 | 9 | //Timeout time; |
geotsam | 0:8bffb51cc345 | 10 | int main(){ |
AurelienBernier | 2:ea61e801e81f | 11 | initRobot(); //Initializing the robot |
geotsam | 0:8bffb51cc345 | 12 | pc.baud(9600); // baud for the pc communication |
geotsam | 0:8bffb51cc345 | 13 | |
AurelienBernier | 2:ea61e801e81f | 14 | //Target example x,y values |
AurelienBernier | 7:c94070f9af78 | 15 | float target_x=46.8, target_y=78.6, target_angle=1.7; |
geotsam | 0:8bffb51cc345 | 16 | |
AurelienBernier | 4:8c56c3ba6e54 | 17 | float alpha; //angle error |
AurelienBernier | 4:8c56c3ba6e54 | 18 | float rho; //distance from target |
geotsam | 3:1e0f4cb93eda | 19 | float beta; |
geotsam | 3:1e0f4cb93eda | 20 | //float k_linear=10, k_angular=200; |
AurelienBernier | 7:c94070f9af78 | 21 | //kb = -15 and ka = 30 tabom |
AurelienBernier | 7:c94070f9af78 | 22 | float kRho=12, ka=30, kb=-13; |
geotsam | 0:8bffb51cc345 | 23 | float linear, angular, angular_left, angular_right; |
geotsam | 3:1e0f4cb93eda | 24 | float dt=0.5; |
geotsam | 3:1e0f4cb93eda | 25 | float temp; |
AurelienBernier | 6:afde4b08166b | 26 | float d2; |
geotsam | 0:8bffb51cc345 | 27 | |
AurelienBernier | 2:ea61e801e81f | 28 | //Diameter of a wheel and distance between the 2 |
geotsam | 0:8bffb51cc345 | 29 | float r=3.25, b=7.2; |
geotsam | 0:8bffb51cc345 | 30 | |
AurelienBernier | 2:ea61e801e81f | 31 | int speed=999; // Max speed at beggining of movement |
geotsam | 0:8bffb51cc345 | 32 | |
AurelienBernier | 2:ea61e801e81f | 33 | //Resetting coordinates before moving |
AurelienBernier | 2:ea61e801e81f | 34 | theta=0; |
geotsam | 0:8bffb51cc345 | 35 | X=0; |
geotsam | 0:8bffb51cc345 | 36 | Y=0; |
geotsam | 0:8bffb51cc345 | 37 | |
AurelienBernier | 4:8c56c3ba6e54 | 38 | alpha = atan2((target_y-Y),(target_x-X))-theta; |
AurelienBernier | 4:8c56c3ba6e54 | 39 | alpha = atan(sin(alpha)/cos(alpha)); |
AurelienBernier | 4:8c56c3ba6e54 | 40 | rho = dist(X, Y, target_x, target_y); |
AurelienBernier | 6:afde4b08166b | 41 | |
AurelienBernier | 4:8c56c3ba6e54 | 42 | beta = -alpha-theta+target_angle; |
AurelienBernier | 6:afde4b08166b | 43 | //beta = atan(sin(beta)/cos(beta)); |
geotsam | 3:1e0f4cb93eda | 44 | |
geotsam | 0:8bffb51cc345 | 45 | do { |
geotsam | 0:8bffb51cc345 | 46 | pc.printf("\n\n\r entered while"); |
AurelienBernier | 2:ea61e801e81f | 47 | |
AurelienBernier | 6:afde4b08166b | 48 | //Timer stuff |
AurelienBernier | 6:afde4b08166b | 49 | dt = t.read(); |
AurelienBernier | 6:afde4b08166b | 50 | t.reset(); |
AurelienBernier | 6:afde4b08166b | 51 | t.start(); |
AurelienBernier | 6:afde4b08166b | 52 | |
AurelienBernier | 2:ea61e801e81f | 53 | //Updating X,Y and theta with the odometry values |
geotsam | 0:8bffb51cc345 | 54 | Odometria(); |
geotsam | 3:1e0f4cb93eda | 55 | |
AurelienBernier | 4:8c56c3ba6e54 | 56 | alpha = atan2((target_y-Y),(target_x-X))-theta; |
AurelienBernier | 4:8c56c3ba6e54 | 57 | alpha = atan(sin(alpha)/cos(alpha)); |
AurelienBernier | 4:8c56c3ba6e54 | 58 | rho = dist(X, Y, target_x, target_y); |
AurelienBernier | 6:afde4b08166b | 59 | d2 = rho; |
AurelienBernier | 5:dea05b8f30d0 | 60 | //beta = -alpha-theta; |
AurelienBernier | 5:dea05b8f30d0 | 61 | beta = -alpha-theta+target_angle; |
AurelienBernier | 6:afde4b08166b | 62 | //beta = atan(sin(beta)/cos(beta)); |
AurelienBernier | 6:afde4b08166b | 63 | |
AurelienBernier | 6:afde4b08166b | 64 | |
AurelienBernier | 2:ea61e801e81f | 65 | //Computing angle error and distance towards the target value |
AurelienBernier | 4:8c56c3ba6e54 | 66 | rho += dt*(-kRho*cos(alpha)*rho); |
AurelienBernier | 4:8c56c3ba6e54 | 67 | temp = alpha; |
AurelienBernier | 6:afde4b08166b | 68 | alpha += dt*(kRho*sin(alpha)-ka*alpha-kb*beta); |
AurelienBernier | 6:afde4b08166b | 69 | beta += dt*(-kRho*sin(temp)); |
AurelienBernier | 6:afde4b08166b | 70 | pc.printf("\n\r d2=%f", d2); |
AurelienBernier | 6:afde4b08166b | 71 | pc.printf("\n\r dt=%f", dt); |
geotsam | 0:8bffb51cc345 | 72 | |
AurelienBernier | 2:ea61e801e81f | 73 | //Computing linear and angular velocities |
AurelienBernier | 4:8c56c3ba6e54 | 74 | if(alpha>=-1.5708 && alpha<=1.5708){ |
AurelienBernier | 4:8c56c3ba6e54 | 75 | linear=kRho*rho; |
AurelienBernier | 4:8c56c3ba6e54 | 76 | angular=ka*alpha+kb*beta; |
geotsam | 3:1e0f4cb93eda | 77 | } |
geotsam | 3:1e0f4cb93eda | 78 | else{ |
AurelienBernier | 4:8c56c3ba6e54 | 79 | linear=-kRho*rho; |
AurelienBernier | 4:8c56c3ba6e54 | 80 | angular=-ka*alpha-kb*beta; |
geotsam | 3:1e0f4cb93eda | 81 | } |
geotsam | 0:8bffb51cc345 | 82 | angular_left=(linear-0.5*b*angular)/r; |
geotsam | 0:8bffb51cc345 | 83 | angular_right=(linear+0.5*b*angular)/r; |
geotsam | 0:8bffb51cc345 | 84 | |
AurelienBernier | 2:ea61e801e81f | 85 | //Slowing down at the end for more precision |
AurelienBernier | 6:afde4b08166b | 86 | if (d2<25) { |
AurelienBernier | 6:afde4b08166b | 87 | speed = d2*30; |
geotsam | 0:8bffb51cc345 | 88 | } |
AurelienBernier | 2:ea61e801e81f | 89 | |
AurelienBernier | 2:ea61e801e81f | 90 | //Normalize speed for motors |
geotsam | 0:8bffb51cc345 | 91 | if(angular_left>angular_right) { |
geotsam | 0:8bffb51cc345 | 92 | angular_right=speed*angular_right/angular_left; |
geotsam | 0:8bffb51cc345 | 93 | angular_left=speed; |
geotsam | 0:8bffb51cc345 | 94 | } else { |
geotsam | 0:8bffb51cc345 | 95 | angular_left=speed*angular_left/angular_right; |
geotsam | 0:8bffb51cc345 | 96 | angular_right=speed; |
geotsam | 0:8bffb51cc345 | 97 | } |
geotsam | 0:8bffb51cc345 | 98 | |
geotsam | 0:8bffb51cc345 | 99 | pc.printf("\n\r X=%f", X); |
geotsam | 0:8bffb51cc345 | 100 | pc.printf("\n\r Y=%f", Y); |
geotsam | 0:8bffb51cc345 | 101 | |
AurelienBernier | 2:ea61e801e81f | 102 | //Updating motor velocities |
AurelienBernier | 1:f0807d5c5a4b | 103 | leftMotor(1,angular_left); |
AurelienBernier | 1:f0807d5c5a4b | 104 | rightMotor(1,angular_right); |
geotsam | 0:8bffb51cc345 | 105 | |
AurelienBernier | 7:c94070f9af78 | 106 | wait(0.2); |
AurelienBernier | 6:afde4b08166b | 107 | //Timer stuff |
AurelienBernier | 6:afde4b08166b | 108 | t.stop(); |
AurelienBernier | 7:c94070f9af78 | 109 | } while(d2>1); |
geotsam | 0:8bffb51cc345 | 110 | |
AurelienBernier | 2:ea61e801e81f | 111 | //Stop at the end |
geotsam | 0:8bffb51cc345 | 112 | leftMotor(1,0); |
geotsam | 0:8bffb51cc345 | 113 | rightMotor(1,0); |
geotsam | 0:8bffb51cc345 | 114 | |
AurelienBernier | 4:8c56c3ba6e54 | 115 | pc.printf("\n\r %f -- arrived!", rho); |
geotsam | 0:8bffb51cc345 | 116 | } |
geotsam | 0:8bffb51cc345 | 117 | |
AurelienBernier | 2:ea61e801e81f | 118 | //Distance computation function |
geotsam | 0:8bffb51cc345 | 119 | float dist(float robot_x, float robot_y, float target_x, float target_y){ |
geotsam | 0:8bffb51cc345 | 120 | return sqrt(pow(target_y-robot_y,2) + pow(target_x-robot_x,2)); |
AurelienBernier | 6:afde4b08166b | 121 | } |