Robot's source code
Dependencies: mbed
Asservissement/Motor.h@36:54f86bc6fd80, 2015-03-08 (annotated)
- Committer:
- Near32
- Date:
- Sun Mar 08 21:24:27 2015 +0000
- Revision:
- 36:54f86bc6fd80
- Child:
- 40:83ce8d1072ef
Motor class; +TODO : bicycle thingy : solved.; TODO:; tester l'odometrie en mode "roue libre"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Near32 | 36:54f86bc6fd80 | 1 | #include "mbed.h" |
Near32 | 36:54f86bc6fd80 | 2 | |
Near32 | 36:54f86bc6fd80 | 3 | /** Motor |
Near32 | 36:54f86bc6fd80 | 4 | * Classe permettant de controler un moteur |
Near32 | 36:54f86bc6fd80 | 5 | */ |
Near32 | 36:54f86bc6fd80 | 6 | class Motor |
Near32 | 36:54f86bc6fd80 | 7 | { |
Near32 | 36:54f86bc6fd80 | 8 | private: |
Near32 | 36:54f86bc6fd80 | 9 | PwmOut* pwm; |
Near32 | 36:54f86bc6fd80 | 10 | DigitalOut* dir; |
Near32 | 36:54f86bc6fd80 | 11 | bool inverse; |
Near32 | 36:54f86bc6fd80 | 12 | float speed; |
Near32 | 36:54f86bc6fd80 | 13 | |
Near32 | 36:54f86bc6fd80 | 14 | public: |
Near32 | 36:54f86bc6fd80 | 15 | /** Création d'un moteur |
Near32 | 36:54f86bc6fd80 | 16 | * |
Near32 | 36:54f86bc6fd80 | 17 | * @param pwm Pin sur laquelle sort la PWM |
Near32 | 36:54f86bc6fd80 | 18 | * @param dir Pin sur laquelle sort le bit de direction |
Near32 | 36:54f86bc6fd80 | 19 | */ |
Near32 | 36:54f86bc6fd80 | 20 | Motor(PwmOut* pwm, DigitalOut* dir) |
Near32 | 36:54f86bc6fd80 | 21 | { |
Near32 | 36:54f86bc6fd80 | 22 | this->pwm = pwm; |
Near32 | 36:54f86bc6fd80 | 23 | this->dir = dir; |
Near32 | 36:54f86bc6fd80 | 24 | inverse = false; |
Near32 | 36:54f86bc6fd80 | 25 | speed = 0.0f; |
Near32 | 36:54f86bc6fd80 | 26 | this->pwm->period_us(10); |
Near32 | 36:54f86bc6fd80 | 27 | |
Near32 | 36:54f86bc6fd80 | 28 | } |
Near32 | 36:54f86bc6fd80 | 29 | virtual ~Motor(); |
Near32 | 36:54f86bc6fd80 | 30 | |
Near32 | 36:54f86bc6fd80 | 31 | /** Fixe la vitesse du moteur |
Near32 | 36:54f86bc6fd80 | 32 | * |
Near32 | 36:54f86bc6fd80 | 33 | * @param s Vitesse du moteur entre -1 et 1 |
Near32 | 36:54f86bc6fd80 | 34 | */ |
Near32 | 36:54f86bc6fd80 | 35 | void setSpeed(float s) |
Near32 | 36:54f86bc6fd80 | 36 | { |
Near32 | 36:54f86bc6fd80 | 37 | if(s<= 1.0f && s >=-1.0f) |
Near32 | 36:54f86bc6fd80 | 38 | { |
Near32 | 36:54f86bc6fd80 | 39 | speed = fabs_(s); |
Near32 | 36:54f86bc6fd80 | 40 | inverse = (s <= 0.0f); |
Near32 | 36:54f86bc6fd80 | 41 | } |
Near32 | 36:54f86bc6fd80 | 42 | else |
Near32 | 36:54f86bc6fd80 | 43 | { |
Near32 | 36:54f86bc6fd80 | 44 | speed = 0.0f; |
Near32 | 36:54f86bc6fd80 | 45 | inverse = false; |
Near32 | 36:54f86bc6fd80 | 46 | } |
Near32 | 36:54f86bc6fd80 | 47 | } |
Near32 | 36:54f86bc6fd80 | 48 | |
Near32 | 36:54f86bc6fd80 | 49 | /** Retourne la vitesse théorique du moteur |
Near32 | 36:54f86bc6fd80 | 50 | * |
Near32 | 36:54f86bc6fd80 | 51 | * @return Vitesse du moteur entre -1 et 1 |
Near32 | 36:54f86bc6fd80 | 52 | */ |
Near32 | 36:54f86bc6fd80 | 53 | float getSpeed(){return speed;} |
Near32 | 36:54f86bc6fd80 | 54 | |
Near32 | 36:54f86bc6fd80 | 55 | /** Fixe l'invesrion de polarité |
Near32 | 36:54f86bc6fd80 | 56 | * |
Near32 | 36:54f86bc6fd80 | 57 | * Exemple : |
Near32 | 36:54f86bc6fd80 | 58 | * @code |
Near32 | 36:54f86bc6fd80 | 59 | * moteur.setSpeed(1); // le moteur tourne à pleine vitesse dans le sens trigo |
Near32 | 36:54f86bc6fd80 | 60 | * moteur.setInverse(true); // le moteur tourne à pleine vitesse dans le sens anti-trigo |
Near32 | 36:54f86bc6fd80 | 61 | * moteur.setSpeed(-1); // le moteur tourne à pleine vitesse dans le sens trigo |
Near32 | 36:54f86bc6fd80 | 62 | * @endcode |
Near32 | 36:54f86bc6fd80 | 63 | * |
Near32 | 36:54f86bc6fd80 | 64 | * @param i Inversion de polarité |
Near32 | 36:54f86bc6fd80 | 65 | */ |
Near32 | 36:54f86bc6fd80 | 66 | void setInverse(bool i) |
Near32 | 36:54f86bc6fd80 | 67 | { |
Near32 | 36:54f86bc6fd80 | 68 | inverse = i; |
Near32 | 36:54f86bc6fd80 | 69 | } |
Near32 | 36:54f86bc6fd80 | 70 | |
Near32 | 36:54f86bc6fd80 | 71 | /** Retourne l'état d'inversion de polarité |
Near32 | 36:54f86bc6fd80 | 72 | * |
Near32 | 36:54f86bc6fd80 | 73 | * @return True ou False |
Near32 | 36:54f86bc6fd80 | 74 | */ |
Near32 | 36:54f86bc6fd80 | 75 | bool setInverse(){return inverse;} |
Near32 | 36:54f86bc6fd80 | 76 | |
Near32 | 36:54f86bc6fd80 | 77 | void update(float s) |
Near32 | 36:54f86bc6fd80 | 78 | { |
Near32 | 36:54f86bc6fd80 | 79 | this->setSpeed(s); |
Near32 | 36:54f86bc6fd80 | 80 | |
Near32 | 36:54f86bc6fd80 | 81 | if(inverse) |
Near32 | 36:54f86bc6fd80 | 82 | dir->write(1); |
Near32 | 36:54f86bc6fd80 | 83 | else |
Near32 | 36:54f86bc6fd80 | 84 | dir->write(0); |
Near32 | 36:54f86bc6fd80 | 85 | |
Near32 | 36:54f86bc6fd80 | 86 | pwm->write(speed); |
Near32 | 36:54f86bc6fd80 | 87 | } |
Near32 | 36:54f86bc6fd80 | 88 | }; |