Robot's source code
Dependencies: mbed
Asservissement/Motor.h@123:55e5e9acc541, 2015-05-11 (annotated)
- Committer:
- Jagang
- Date:
- Mon May 11 20:32:11 2015 +0000
- Revision:
- 123:55e5e9acc541
- Parent:
- 62:454cd844fe1e
Maj AI; Repositionnement du d?part (1000,177); Ajout de l'action sur les claps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jagang | 62:454cd844fe1e | 1 | #ifndef MOTOR_H |
Jagang | 62:454cd844fe1e | 2 | #define MOTOR_H |
Jagang | 62:454cd844fe1e | 3 | |
Near32 | 36:54f86bc6fd80 | 4 | #include "mbed.h" |
Near32 | 36:54f86bc6fd80 | 5 | |
Near32 | 36:54f86bc6fd80 | 6 | /** Motor |
Near32 | 36:54f86bc6fd80 | 7 | * Classe permettant de controler un moteur |
Near32 | 36:54f86bc6fd80 | 8 | */ |
Near32 | 36:54f86bc6fd80 | 9 | class Motor |
Near32 | 36:54f86bc6fd80 | 10 | { |
Near32 | 36:54f86bc6fd80 | 11 | private: |
Jagang | 40:83ce8d1072ef | 12 | PwmOut pwm; |
Jagang | 40:83ce8d1072ef | 13 | DigitalOut dir; |
Near32 | 36:54f86bc6fd80 | 14 | bool inverse; |
Near32 | 36:54f86bc6fd80 | 15 | float speed; |
Near32 | 36:54f86bc6fd80 | 16 | |
Near32 | 36:54f86bc6fd80 | 17 | public: |
Near32 | 36:54f86bc6fd80 | 18 | /** Création d'un moteur |
Near32 | 36:54f86bc6fd80 | 19 | * |
Near32 | 36:54f86bc6fd80 | 20 | * @param pwm Pin sur laquelle sort la PWM |
Near32 | 36:54f86bc6fd80 | 21 | * @param dir Pin sur laquelle sort le bit de direction |
Near32 | 36:54f86bc6fd80 | 22 | */ |
Jagang | 40:83ce8d1072ef | 23 | Motor(PinName pin_pwm, PinName pin_dir): pwm(pin_pwm), dir(pin_dir) |
Near32 | 36:54f86bc6fd80 | 24 | { |
Near32 | 36:54f86bc6fd80 | 25 | inverse = false; |
Near32 | 36:54f86bc6fd80 | 26 | speed = 0.0f; |
Jagang | 40:83ce8d1072ef | 27 | pwm.period_us(10); |
Jagang | 40:83ce8d1072ef | 28 | pwm = 0.0f; |
Near32 | 36:54f86bc6fd80 | 29 | |
Near32 | 36:54f86bc6fd80 | 30 | } |
Jagang | 40:83ce8d1072ef | 31 | |
Jagang | 40:83ce8d1072ef | 32 | virtual ~Motor(){} |
Near32 | 36:54f86bc6fd80 | 33 | |
Near32 | 36:54f86bc6fd80 | 34 | /** Fixe la vitesse du moteur |
Near32 | 36:54f86bc6fd80 | 35 | * |
Near32 | 36:54f86bc6fd80 | 36 | * @param s Vitesse du moteur entre -1 et 1 |
Near32 | 36:54f86bc6fd80 | 37 | */ |
Near32 | 36:54f86bc6fd80 | 38 | void setSpeed(float s) |
Near32 | 36:54f86bc6fd80 | 39 | { |
Near32 | 42:fcb48e2fc426 | 40 | float abss = abs(s); |
Jagang | 40:83ce8d1072ef | 41 | speed = s; |
Near32 | 42:fcb48e2fc426 | 42 | pwm = abss; |
Jagang | 40:83ce8d1072ef | 43 | if(s > 0) |
Jagang | 40:83ce8d1072ef | 44 | dir = !inverse; |
Near32 | 36:54f86bc6fd80 | 45 | else |
Jagang | 40:83ce8d1072ef | 46 | dir = inverse; |
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; |
Jagang | 56:eb0600022463 | 69 | |
Jagang | 56:eb0600022463 | 70 | setSpeed(speed); |
Near32 | 36:54f86bc6fd80 | 71 | } |
Near32 | 36:54f86bc6fd80 | 72 | |
Near32 | 36:54f86bc6fd80 | 73 | /** Retourne l'état d'inversion de polarité |
Near32 | 36:54f86bc6fd80 | 74 | * |
Near32 | 36:54f86bc6fd80 | 75 | * @return True ou False |
Near32 | 36:54f86bc6fd80 | 76 | */ |
Jagang | 40:83ce8d1072ef | 77 | bool getInverse(){return inverse;} |
Near32 | 36:54f86bc6fd80 | 78 | |
Jagang | 62:454cd844fe1e | 79 | }; |
Jagang | 62:454cd844fe1e | 80 | |
Jagang | 62:454cd844fe1e | 81 | |
Jagang | 62:454cd844fe1e | 82 | #endif |