Robot's source code
Dependencies: mbed
Asservissement/Motor.h@42:fcb48e2fc426, 2015-03-18 (annotated)
- Committer:
- Near32
- Date:
- Wed Mar 18 13:47:40 2015 +0000
- Revision:
- 42:fcb48e2fc426
- Parent:
- 40:83ce8d1072ef
- Child:
- 56:eb0600022463
liaison serie usb car le bluetooth est visiblement tr?s capricieux...; test de compilation et de debuggage par gdb en cours...
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: |
Jagang | 40:83ce8d1072ef | 9 | PwmOut pwm; |
Jagang | 40:83ce8d1072ef | 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 | */ |
Jagang | 40:83ce8d1072ef | 20 | Motor(PinName pin_pwm, PinName pin_dir): pwm(pin_pwm), dir(pin_dir) |
Near32 | 36:54f86bc6fd80 | 21 | { |
Near32 | 36:54f86bc6fd80 | 22 | inverse = false; |
Near32 | 36:54f86bc6fd80 | 23 | speed = 0.0f; |
Jagang | 40:83ce8d1072ef | 24 | pwm.period_us(10); |
Jagang | 40:83ce8d1072ef | 25 | pwm = 0.0f; |
Near32 | 36:54f86bc6fd80 | 26 | |
Near32 | 36:54f86bc6fd80 | 27 | } |
Jagang | 40:83ce8d1072ef | 28 | |
Jagang | 40:83ce8d1072ef | 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 | 42:fcb48e2fc426 | 37 | float abss = abs(s); |
Jagang | 40:83ce8d1072ef | 38 | speed = s; |
Near32 | 42:fcb48e2fc426 | 39 | pwm = abss; |
Jagang | 40:83ce8d1072ef | 40 | if(s > 0) |
Jagang | 40:83ce8d1072ef | 41 | dir = !inverse; |
Near32 | 36:54f86bc6fd80 | 42 | else |
Jagang | 40:83ce8d1072ef | 43 | dir = inverse; |
Near32 | 36:54f86bc6fd80 | 44 | } |
Near32 | 36:54f86bc6fd80 | 45 | |
Near32 | 36:54f86bc6fd80 | 46 | /** Retourne la vitesse théorique du moteur |
Near32 | 36:54f86bc6fd80 | 47 | * |
Near32 | 36:54f86bc6fd80 | 48 | * @return Vitesse du moteur entre -1 et 1 |
Near32 | 36:54f86bc6fd80 | 49 | */ |
Near32 | 36:54f86bc6fd80 | 50 | float getSpeed(){return speed;} |
Near32 | 36:54f86bc6fd80 | 51 | |
Near32 | 36:54f86bc6fd80 | 52 | /** Fixe l'invesrion de polarité |
Near32 | 36:54f86bc6fd80 | 53 | * |
Near32 | 36:54f86bc6fd80 | 54 | * Exemple : |
Near32 | 36:54f86bc6fd80 | 55 | * @code |
Near32 | 36:54f86bc6fd80 | 56 | * moteur.setSpeed(1); // le moteur tourne à pleine vitesse dans le sens trigo |
Near32 | 36:54f86bc6fd80 | 57 | * moteur.setInverse(true); // le moteur tourne à pleine vitesse dans le sens anti-trigo |
Near32 | 36:54f86bc6fd80 | 58 | * moteur.setSpeed(-1); // le moteur tourne à pleine vitesse dans le sens trigo |
Near32 | 36:54f86bc6fd80 | 59 | * @endcode |
Near32 | 36:54f86bc6fd80 | 60 | * |
Near32 | 36:54f86bc6fd80 | 61 | * @param i Inversion de polarité |
Near32 | 36:54f86bc6fd80 | 62 | */ |
Near32 | 36:54f86bc6fd80 | 63 | void setInverse(bool i) |
Near32 | 36:54f86bc6fd80 | 64 | { |
Near32 | 36:54f86bc6fd80 | 65 | inverse = i; |
Near32 | 36:54f86bc6fd80 | 66 | } |
Near32 | 36:54f86bc6fd80 | 67 | |
Near32 | 36:54f86bc6fd80 | 68 | /** Retourne l'état d'inversion de polarité |
Near32 | 36:54f86bc6fd80 | 69 | * |
Near32 | 36:54f86bc6fd80 | 70 | * @return True ou False |
Near32 | 36:54f86bc6fd80 | 71 | */ |
Jagang | 40:83ce8d1072ef | 72 | bool getInverse(){return inverse;} |
Near32 | 36:54f86bc6fd80 | 73 | |
Near32 | 36:54f86bc6fd80 | 74 | }; |