Guillaume Chauvon
/
Asserve12
asser1
odometrie.cpp@0:6ca63d45f0ee, 2018-12-11 (annotated)
- Committer:
- Coconara
- Date:
- Tue Dec 11 19:12:55 2018 +0000
- Revision:
- 0:6ca63d45f0ee
- Child:
- 1:0690cf83f060
asser
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Coconara | 0:6ca63d45f0ee | 1 | #include "mbed.h" |
Coconara | 0:6ca63d45f0ee | 2 | #include "odometrie.h" |
Coconara | 0:6ca63d45f0ee | 3 | #include "hardware.h" |
Coconara | 0:6ca63d45f0ee | 4 | #include "reglages.h" |
Coconara | 0:6ca63d45f0ee | 5 | #include "math_precalc.h" |
Coconara | 0:6ca63d45f0ee | 6 | |
Coconara | 0:6ca63d45f0ee | 7 | long int x_actuel; |
Coconara | 0:6ca63d45f0ee | 8 | long int y_actuel; |
Coconara | 0:6ca63d45f0ee | 9 | double angle; // angle du robot |
Coconara | 0:6ca63d45f0ee | 10 | |
Coconara | 0:6ca63d45f0ee | 11 | |
Coconara | 0:6ca63d45f0ee | 12 | long int nbr_tick_D_prec; |
Coconara | 0:6ca63d45f0ee | 13 | long int nbr_tick_G_prec; |
Coconara | 0:6ca63d45f0ee | 14 | |
Coconara | 0:6ca63d45f0ee | 15 | void init_odometrie() |
Coconara | 0:6ca63d45f0ee | 16 | { |
Coconara | 0:6ca63d45f0ee | 17 | x_actuel=X_INIT; |
Coconara | 0:6ca63d45f0ee | 18 | y_actuel=Y_INIT; |
Coconara | 0:6ca63d45f0ee | 19 | angle=0; |
Coconara | 0:6ca63d45f0ee | 20 | nbr_tick_D_prec=0; |
Coconara | 0:6ca63d45f0ee | 21 | nbr_tick_G_prec=0; |
Coconara | 0:6ca63d45f0ee | 22 | } |
Coconara | 0:6ca63d45f0ee | 23 | |
Coconara | 0:6ca63d45f0ee | 24 | void actualise_position() |
Coconara | 0:6ca63d45f0ee | 25 | { |
Coconara | 0:6ca63d45f0ee | 26 | /* |
Coconara | 0:6ca63d45f0ee | 27 | on suppose les valeurs de vd et vg constantes pendant t, la trajectoire decrite par le robot est alors un cercle |
Coconara | 0:6ca63d45f0ee | 28 | */ |
Coconara | 0:6ca63d45f0ee | 29 | |
Coconara | 0:6ca63d45f0ee | 30 | /*------recuperation de la rotation des roues---------*/ |
Coconara | 0:6ca63d45f0ee | 31 | long int nbr_tick_D=get_nbr_tick_D(); |
Coconara | 0:6ca63d45f0ee | 32 | long int nbr_tick_G=get_nbr_tick_G(); |
Coconara | 0:6ca63d45f0ee | 33 | |
Coconara | 0:6ca63d45f0ee | 34 | //calcul du nombre de tick |
Coconara | 0:6ca63d45f0ee | 35 | long int nbr_tick_D_actuel=nbr_tick_D-nbr_tick_D_prec; |
Coconara | 0:6ca63d45f0ee | 36 | long int nbr_tick_G_actuel=nbr_tick_G-nbr_tick_G_prec; |
Coconara | 0:6ca63d45f0ee | 37 | |
Coconara | 0:6ca63d45f0ee | 38 | //sauvegarde |
Coconara | 0:6ca63d45f0ee | 39 | nbr_tick_D_prec=nbr_tick_D; |
Coconara | 0:6ca63d45f0ee | 40 | nbr_tick_G_prec=nbr_tick_G; |
Coconara | 0:6ca63d45f0ee | 41 | |
Coconara | 0:6ca63d45f0ee | 42 | double dep_roue_G = nbr_tick_G_actuel * DISTANCE_PAR_TICK_G; // deplacement des roues |
Coconara | 0:6ca63d45f0ee | 43 | double dep_roue_D = nbr_tick_D_actuel * DISTANCE_PAR_TICK_D; |
Coconara | 0:6ca63d45f0ee | 44 | |
Coconara | 0:6ca63d45f0ee | 45 | |
Coconara | 0:6ca63d45f0ee | 46 | /*------calcul de la trajectoire---------*/ |
Coconara | 0:6ca63d45f0ee | 47 | |
Coconara | 0:6ca63d45f0ee | 48 | // determination du cercle décrit par la trajectoire et de la vitesse du robot sur ce cercle |
Coconara | 0:6ca63d45f0ee | 49 | if (dep_roue_G != dep_roue_D){ |
Coconara | 0:6ca63d45f0ee | 50 | |
Coconara | 0:6ca63d45f0ee | 51 | double R = 0; // rayon du cercle decrit par la trajectoire |
Coconara | 0:6ca63d45f0ee | 52 | double d = 0; // vitesse du robot |
Coconara | 0:6ca63d45f0ee | 53 | double cx = 0; // position du centre du cercle decrit par la trajectoire |
Coconara | 0:6ca63d45f0ee | 54 | double cy = 0; |
Coconara | 0:6ca63d45f0ee | 55 | |
Coconara | 0:6ca63d45f0ee | 56 | R = ECART_ROUE / 2 * (dep_roue_D + dep_roue_G) / (dep_roue_D - dep_roue_G); // rayon du cercle |
Coconara | 0:6ca63d45f0ee | 57 | cx = x_actuel - R * sin(angle); |
Coconara | 0:6ca63d45f0ee | 58 | cy = y_actuel + R * cos(angle); |
Coconara | 0:6ca63d45f0ee | 59 | d = (dep_roue_G + dep_roue_D) / 2; |
Coconara | 0:6ca63d45f0ee | 60 | |
Coconara | 0:6ca63d45f0ee | 61 | // mise à jour des coordonnées du robot |
Coconara | 0:6ca63d45f0ee | 62 | if (dep_roue_G + dep_roue_D != 0){ |
Coconara | 0:6ca63d45f0ee | 63 | angle += d / R; |
Coconara | 0:6ca63d45f0ee | 64 | } |
Coconara | 0:6ca63d45f0ee | 65 | else{ |
Coconara | 0:6ca63d45f0ee | 66 | angle += dep_roue_D * 2 / ECART_ROUE; |
Coconara | 0:6ca63d45f0ee | 67 | } |
Coconara | 0:6ca63d45f0ee | 68 | |
Coconara | 0:6ca63d45f0ee | 69 | angle = borne_angle(angle); |
Coconara | 0:6ca63d45f0ee | 70 | |
Coconara | 0:6ca63d45f0ee | 71 | x_actuel = (int) (cx + R * sin(angle) + 0.5); |
Coconara | 0:6ca63d45f0ee | 72 | y_actuel = (int) (cy - R * cos(angle) + 0.5); |
Coconara | 0:6ca63d45f0ee | 73 | } |
Coconara | 0:6ca63d45f0ee | 74 | else if (dep_roue_G == dep_roue_D){ // cas où la trajectoire est une parfaite ligne droite |
Coconara | 0:6ca63d45f0ee | 75 | x_actuel += (int) (dep_roue_G * cos(angle) + 0.5); |
Coconara | 0:6ca63d45f0ee | 76 | y_actuel += (int) (dep_roue_D * sin(angle) + 0.5); |
Coconara | 0:6ca63d45f0ee | 77 | } |
Coconara | 0:6ca63d45f0ee | 78 | |
Coconara | 0:6ca63d45f0ee | 79 | printf("tick d : %d, tick g : %d, x : %d, y : %d. angle : %lf\n", nbr_tick_D, nbr_tick_G, x_actuel, y_actuel, angle*180/3.14); |
Coconara | 0:6ca63d45f0ee | 80 | } |
Coconara | 0:6ca63d45f0ee | 81 | |
Coconara | 0:6ca63d45f0ee | 82 | double borne_angle(double angle) |
Coconara | 0:6ca63d45f0ee | 83 | { |
Coconara | 0:6ca63d45f0ee | 84 | if (angle > 3.14) { |
Coconara | 0:6ca63d45f0ee | 85 | angle -= 2*3.14; |
Coconara | 0:6ca63d45f0ee | 86 | } |
Coconara | 0:6ca63d45f0ee | 87 | else if (angle <= -3.14) { |
Coconara | 0:6ca63d45f0ee | 88 | angle += 2*3.14; |
Coconara | 0:6ca63d45f0ee | 89 | } |
Coconara | 0:6ca63d45f0ee | 90 | return angle; |
Coconara | 0:6ca63d45f0ee | 91 | } |
Coconara | 0:6ca63d45f0ee | 92 | |
Coconara | 0:6ca63d45f0ee | 93 | long int get_x_actuel() |
Coconara | 0:6ca63d45f0ee | 94 | { |
Coconara | 0:6ca63d45f0ee | 95 | return x_actuel; |
Coconara | 0:6ca63d45f0ee | 96 | } |
Coconara | 0:6ca63d45f0ee | 97 | |
Coconara | 0:6ca63d45f0ee | 98 | long int get_y_actuel() |
Coconara | 0:6ca63d45f0ee | 99 | { |
Coconara | 0:6ca63d45f0ee | 100 | return y_actuel; |
Coconara | 0:6ca63d45f0ee | 101 | } |
Coconara | 0:6ca63d45f0ee | 102 | |
Coconara | 0:6ca63d45f0ee | 103 | double get_angle() |
Coconara | 0:6ca63d45f0ee | 104 | { |
Coconara | 0:6ca63d45f0ee | 105 | return angle*180/3.14; |
Coconara | 0:6ca63d45f0ee | 106 | } |