![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mapping for TP2
Dependencies: ISR_Mini-explorer mbed
Fork of GoToPointWithAngle by
main.cpp@12:3c0ca2350624, 2017-03-27 (annotated)
- Committer:
- geotsam
- Date:
- Mon Mar 27 16:40:11 2017 +0000
- Revision:
- 12:3c0ca2350624
- Parent:
- 11:e641aa08c92e
- Child:
- 13:41f75c132135
corrected the inputs again :P
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 | |
geotsam | 12:3c0ca2350624 | 9 | int goToPointWithAngle(float target_x, float target_y, float target_angle); |
AurelienBernier | 8:109314be5b68 | 10 | |
AurelienBernier | 11:e641aa08c92e | 11 | int updateSonarValues(); |
AurelienBernier | 11:e641aa08c92e | 12 | |
AurelienBernier | 8:109314be5b68 | 13 | float alpha; //angle error |
AurelienBernier | 8:109314be5b68 | 14 | float rho; //distance from target |
AurelienBernier | 8:109314be5b68 | 15 | float beta; |
AurelienBernier | 11:e641aa08c92e | 16 | float kRho=12, ka=30, kb=-13; //Kappa values |
AurelienBernier | 8:109314be5b68 | 17 | float linear, angular, angular_left, angular_right; |
AurelienBernier | 8:109314be5b68 | 18 | float dt=0.5; |
AurelienBernier | 8:109314be5b68 | 19 | float temp; |
AurelienBernier | 8:109314be5b68 | 20 | float d2; |
AurelienBernier | 8:109314be5b68 | 21 | |
AurelienBernier | 11:e641aa08c92e | 22 | bool tooClose = false; |
AurelienBernier | 11:e641aa08c92e | 23 | |
AurelienBernier | 11:e641aa08c92e | 24 | int leftMm; |
AurelienBernier | 11:e641aa08c92e | 25 | int frontMm; |
AurelienBernier | 11:e641aa08c92e | 26 | int rightMm; |
AurelienBernier | 11:e641aa08c92e | 27 | |
AurelienBernier | 8:109314be5b68 | 28 | //Diameter of a wheel and distance between the 2 |
AurelienBernier | 8:109314be5b68 | 29 | float r=3.25, b=7.2; |
AurelienBernier | 8:109314be5b68 | 30 | |
AurelienBernier | 8:109314be5b68 | 31 | int speed=999; // Max speed at beggining of movement |
AurelienBernier | 8:109314be5b68 | 32 | |
AurelienBernier | 8:109314be5b68 | 33 | //Target example x,y values |
AurelienBernier | 8:109314be5b68 | 34 | float target_x=46.8, target_y=78.6, target_angle=1.7; |
AurelienBernier | 8:109314be5b68 | 35 | |
AurelienBernier | 4:8c56c3ba6e54 | 36 | //Timeout time; |
geotsam | 0:8bffb51cc345 | 37 | int main(){ |
AurelienBernier | 2:ea61e801e81f | 38 | initRobot(); //Initializing the robot |
geotsam | 0:8bffb51cc345 | 39 | pc.baud(9600); // baud for the pc communication |
geotsam | 0:8bffb51cc345 | 40 | |
AurelienBernier | 2:ea61e801e81f | 41 | //Resetting coordinates before moving |
AurelienBernier | 2:ea61e801e81f | 42 | theta=0; |
geotsam | 0:8bffb51cc345 | 43 | X=0; |
geotsam | 0:8bffb51cc345 | 44 | Y=0; |
geotsam | 0:8bffb51cc345 | 45 | |
AurelienBernier | 4:8c56c3ba6e54 | 46 | alpha = atan2((target_y-Y),(target_x-X))-theta; |
AurelienBernier | 4:8c56c3ba6e54 | 47 | alpha = atan(sin(alpha)/cos(alpha)); |
AurelienBernier | 4:8c56c3ba6e54 | 48 | rho = dist(X, Y, target_x, target_y); |
AurelienBernier | 6:afde4b08166b | 49 | |
AurelienBernier | 4:8c56c3ba6e54 | 50 | beta = -alpha-theta+target_angle; |
AurelienBernier | 6:afde4b08166b | 51 | //beta = atan(sin(beta)/cos(beta)); |
AurelienBernier | 8:109314be5b68 | 52 | |
AurelienBernier | 8:109314be5b68 | 53 | goToPointWithAngle(target_x, target_y, target_angle); |
AurelienBernier | 8:109314be5b68 | 54 | |
AurelienBernier | 8:109314be5b68 | 55 | //Stop at the end |
AurelienBernier | 8:109314be5b68 | 56 | leftMotor(1,0); |
AurelienBernier | 8:109314be5b68 | 57 | rightMotor(1,0); |
AurelienBernier | 8:109314be5b68 | 58 | |
AurelienBernier | 8:109314be5b68 | 59 | pc.printf("\n\r %f -- arrived!", rho); |
AurelienBernier | 8:109314be5b68 | 60 | } |
AurelienBernier | 8:109314be5b68 | 61 | |
AurelienBernier | 8:109314be5b68 | 62 | //Distance computation function |
AurelienBernier | 8:109314be5b68 | 63 | float dist(float robot_x, float robot_y, float target_x, float target_y){ |
AurelienBernier | 8:109314be5b68 | 64 | return sqrt(pow(target_y-robot_y,2) + pow(target_x-robot_x,2)); |
AurelienBernier | 8:109314be5b68 | 65 | } |
AurelienBernier | 8:109314be5b68 | 66 | |
AurelienBernier | 11:e641aa08c92e | 67 | //Updates sonar values |
AurelienBernier | 11:e641aa08c92e | 68 | int updateSonarValues() { |
AurelienBernier | 11:e641aa08c92e | 69 | leftMm = get_distance_left_sensor(); |
AurelienBernier | 11:e641aa08c92e | 70 | frontMm = get_distance_front_sensor(); |
AurelienBernier | 11:e641aa08c92e | 71 | rightMm = get_distance_right_sensor(); |
AurelienBernier | 11:e641aa08c92e | 72 | return 0; |
AurelienBernier | 11:e641aa08c92e | 73 | } |
AurelienBernier | 11:e641aa08c92e | 74 | |
geotsam | 12:3c0ca2350624 | 75 | int goToPointWithAngle(float target_x, float target_y, float target_angle) { |
AurelienBernier | 8:109314be5b68 | 76 | do { |
geotsam | 0:8bffb51cc345 | 77 | pc.printf("\n\n\r entered while"); |
AurelienBernier | 2:ea61e801e81f | 78 | |
AurelienBernier | 6:afde4b08166b | 79 | //Timer stuff |
AurelienBernier | 6:afde4b08166b | 80 | dt = t.read(); |
AurelienBernier | 6:afde4b08166b | 81 | t.reset(); |
AurelienBernier | 6:afde4b08166b | 82 | t.start(); |
AurelienBernier | 6:afde4b08166b | 83 | |
AurelienBernier | 11:e641aa08c92e | 84 | updateSonarValues(); |
AurelienBernier | 11:e641aa08c92e | 85 | if (leftMm < 100 || frontMm < 100 || rightMm < 100) { |
AurelienBernier | 11:e641aa08c92e | 86 | tooClose = true; |
AurelienBernier | 11:e641aa08c92e | 87 | } |
AurelienBernier | 11:e641aa08c92e | 88 | |
AurelienBernier | 2:ea61e801e81f | 89 | //Updating X,Y and theta with the odometry values |
geotsam | 0:8bffb51cc345 | 90 | Odometria(); |
geotsam | 3:1e0f4cb93eda | 91 | |
AurelienBernier | 4:8c56c3ba6e54 | 92 | alpha = atan2((target_y-Y),(target_x-X))-theta; |
AurelienBernier | 4:8c56c3ba6e54 | 93 | alpha = atan(sin(alpha)/cos(alpha)); |
AurelienBernier | 4:8c56c3ba6e54 | 94 | rho = dist(X, Y, target_x, target_y); |
AurelienBernier | 6:afde4b08166b | 95 | d2 = rho; |
AurelienBernier | 5:dea05b8f30d0 | 96 | beta = -alpha-theta+target_angle; |
AurelienBernier | 6:afde4b08166b | 97 | //beta = atan(sin(beta)/cos(beta)); |
AurelienBernier | 6:afde4b08166b | 98 | |
AurelienBernier | 6:afde4b08166b | 99 | |
AurelienBernier | 2:ea61e801e81f | 100 | //Computing angle error and distance towards the target value |
AurelienBernier | 4:8c56c3ba6e54 | 101 | rho += dt*(-kRho*cos(alpha)*rho); |
AurelienBernier | 4:8c56c3ba6e54 | 102 | temp = alpha; |
AurelienBernier | 6:afde4b08166b | 103 | alpha += dt*(kRho*sin(alpha)-ka*alpha-kb*beta); |
AurelienBernier | 6:afde4b08166b | 104 | beta += dt*(-kRho*sin(temp)); |
AurelienBernier | 6:afde4b08166b | 105 | pc.printf("\n\r d2=%f", d2); |
AurelienBernier | 6:afde4b08166b | 106 | pc.printf("\n\r dt=%f", dt); |
geotsam | 0:8bffb51cc345 | 107 | |
AurelienBernier | 2:ea61e801e81f | 108 | //Computing linear and angular velocities |
AurelienBernier | 4:8c56c3ba6e54 | 109 | if(alpha>=-1.5708 && alpha<=1.5708){ |
AurelienBernier | 4:8c56c3ba6e54 | 110 | linear=kRho*rho; |
AurelienBernier | 4:8c56c3ba6e54 | 111 | angular=ka*alpha+kb*beta; |
geotsam | 3:1e0f4cb93eda | 112 | } |
geotsam | 3:1e0f4cb93eda | 113 | else{ |
AurelienBernier | 4:8c56c3ba6e54 | 114 | linear=-kRho*rho; |
AurelienBernier | 4:8c56c3ba6e54 | 115 | angular=-ka*alpha-kb*beta; |
geotsam | 3:1e0f4cb93eda | 116 | } |
geotsam | 0:8bffb51cc345 | 117 | angular_left=(linear-0.5*b*angular)/r; |
geotsam | 0:8bffb51cc345 | 118 | angular_right=(linear+0.5*b*angular)/r; |
geotsam | 0:8bffb51cc345 | 119 | |
AurelienBernier | 2:ea61e801e81f | 120 | //Slowing down at the end for more precision |
AurelienBernier | 6:afde4b08166b | 121 | if (d2<25) { |
AurelienBernier | 6:afde4b08166b | 122 | speed = d2*30; |
geotsam | 0:8bffb51cc345 | 123 | } |
AurelienBernier | 2:ea61e801e81f | 124 | |
AurelienBernier | 2:ea61e801e81f | 125 | //Normalize speed for motors |
geotsam | 0:8bffb51cc345 | 126 | if(angular_left>angular_right) { |
geotsam | 0:8bffb51cc345 | 127 | angular_right=speed*angular_right/angular_left; |
geotsam | 0:8bffb51cc345 | 128 | angular_left=speed; |
geotsam | 0:8bffb51cc345 | 129 | } else { |
geotsam | 0:8bffb51cc345 | 130 | angular_left=speed*angular_left/angular_right; |
geotsam | 0:8bffb51cc345 | 131 | angular_right=speed; |
geotsam | 0:8bffb51cc345 | 132 | } |
geotsam | 0:8bffb51cc345 | 133 | |
geotsam | 0:8bffb51cc345 | 134 | pc.printf("\n\r X=%f", X); |
geotsam | 0:8bffb51cc345 | 135 | pc.printf("\n\r Y=%f", Y); |
geotsam | 0:8bffb51cc345 | 136 | |
AurelienBernier | 2:ea61e801e81f | 137 | //Updating motor velocities |
AurelienBernier | 1:f0807d5c5a4b | 138 | leftMotor(1,angular_left); |
AurelienBernier | 1:f0807d5c5a4b | 139 | rightMotor(1,angular_right); |
geotsam | 0:8bffb51cc345 | 140 | |
AurelienBernier | 7:c94070f9af78 | 141 | wait(0.2); |
AurelienBernier | 6:afde4b08166b | 142 | //Timer stuff |
AurelienBernier | 6:afde4b08166b | 143 | t.stop(); |
AurelienBernier | 11:e641aa08c92e | 144 | } while(d2>1 & !tooClose); |
AurelienBernier | 8:109314be5b68 | 145 | |
AurelienBernier | 8:109314be5b68 | 146 | return 0; |
AurelienBernier | 6:afde4b08166b | 147 | } |