Library for controll DC motors using dual H-bridges like l298n, l293d and others. Based on Sparkfun RedBot arduino library.
Dependents: maple_motores maple_heading_pid maple_chotobot_rf_motores motores ... more
motoresDC.h@5:998fbdeb1ea4, 2016-06-13 (annotated)
- Committer:
- tabris2015
- Date:
- Mon Jun 13 23:16:02 2016 +0000
- Revision:
- 5:998fbdeb1ea4
- Parent:
- 2:b5d41b0442a7
doc changed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tabris2015 | 2:b5d41b0442a7 | 1 | |
tabris2015 | 0:b1e9ffb92a0a | 2 | #ifndef MBED_MOTORESDC_H |
tabris2015 | 0:b1e9ffb92a0a | 3 | #define MBED_MOTORESDC_H |
tabris2015 | 0:b1e9ffb92a0a | 4 | |
tabris2015 | 0:b1e9ffb92a0a | 5 | #include "mbed.h" |
tabris2015 | 2:b5d41b0442a7 | 6 | /** |
tabris2015 | 2:b5d41b0442a7 | 7 | * Libreria para controlar un driver dual de motores DC |
tabris2015 | 2:b5d41b0442a7 | 8 | * funciona con drivers como el L293D o el L298N |
tabris2015 | 2:b5d41b0442a7 | 9 | * se necesitan 6 pines del microcontrolador |
tabris2015 | 2:b5d41b0442a7 | 10 | * uno para la velocidad (PWM) y 2 para el sentido (digital) |
tabris2015 | 2:b5d41b0442a7 | 11 | * esta librería esta basada en la libreria RedBot de Sparkfun |
tabris2015 | 2:b5d41b0442a7 | 12 | * Ejemplo: |
tabris2015 | 2:b5d41b0442a7 | 13 | * @code |
tabris2015 | 2:b5d41b0442a7 | 14 | * #include "mbed.h" |
tabris2015 | 2:b5d41b0442a7 | 15 | * #include "motoresDC.h" |
tabris2015 | 2:b5d41b0442a7 | 16 | * MotoresDC carro(PA_8, PB_12, PB_13, PA_9, PB_14, PB_15); |
tabris2015 | 2:b5d41b0442a7 | 17 | * int main() |
tabris2015 | 2:b5d41b0442a7 | 18 | * { |
tabris2015 | 2:b5d41b0442a7 | 19 | * carro.conducir(0.5, 1000); // adelante por 1 segundo a media velocidad |
tabris2015 | 2:b5d41b0442a7 | 20 | * carro.conducir(-0.5, 1000); // atrás por 1 segundo a media velocidad |
tabris2015 | 2:b5d41b0442a7 | 21 | * carro.pivotar(0.6, 400); // pivota izquierda por 0.4 segundos a un 60% de la velocidad |
tabris2015 | 2:b5d41b0442a7 | 22 | * carro.conducir(-0.6, 400); // pivota derecha por 0.4 segundos a un 60% de la velocidad |
tabris2015 | 2:b5d41b0442a7 | 23 | * carro.detener(); |
tabris2015 | 2:b5d41b0442a7 | 24 | * } |
tabris2015 | 2:b5d41b0442a7 | 25 | * @endcode |
tabris2015 | 2:b5d41b0442a7 | 26 | */ |
tabris2015 | 0:b1e9ffb92a0a | 27 | class MotoresDC |
tabris2015 | 0:b1e9ffb92a0a | 28 | { |
tabris2015 | 0:b1e9ffb92a0a | 29 | public: |
tabris2015 | 2:b5d41b0442a7 | 30 | /** Instancia la clase con los pines del driver |
tabris2015 | 5:998fbdeb1ea4 | 31 | * @param MI_vel Pin para el control de velocidad del motor izquierdo. |
tabris2015 | 5:998fbdeb1ea4 | 32 | * @param MI_s1 Primer pin de control de sentido para el motor izquierdo. |
tabris2015 | 5:998fbdeb1ea4 | 33 | * @param MI_s2 Segundo pin de control de sentido para el motor izquierdo. |
tabris2015 | 5:998fbdeb1ea4 | 34 | * @param MD_vel Pin para el control de velocidad del motor derecho. |
tabris2015 | 5:998fbdeb1ea4 | 35 | * @param MD_s1 Primer pin de control de sentido para el motor derecho. |
tabris2015 | 5:998fbdeb1ea4 | 36 | * @param MD_s2 Segundo pin de control de sentido para el motor derecho. |
tabris2015 | 2:b5d41b0442a7 | 37 | */ |
tabris2015 | 0:b1e9ffb92a0a | 38 | MotoresDC(PinName MI_vel, PinName MI_s1, PinName MI_s2, |
tabris2015 | 0:b1e9ffb92a0a | 39 | PinName MD_vel, PinName MD_s1, PinName MD_s2); |
tabris2015 | 2:b5d41b0442a7 | 40 | |
tabris2015 | 0:b1e9ffb92a0a | 41 | // metodos publicos // |
tabris2015 | 2:b5d41b0442a7 | 42 | /** Configura los parámetros del uniciclo |
tabris2015 | 2:b5d41b0442a7 | 43 | * @param R Radio en cm de las ruedas. |
tabris2015 | 2:b5d41b0442a7 | 44 | * @param L Longitud del eje en cm. |
tabris2015 | 2:b5d41b0442a7 | 45 | */ |
tabris2015 | 2:b5d41b0442a7 | 46 | void setParams(float R, float L); |
tabris2015 | 2:b5d41b0442a7 | 47 | |
tabris2015 | 2:b5d41b0442a7 | 48 | /** Método para conducir con el modelo de robot diferencial a uniciclo. |
tabris2015 | 2:b5d41b0442a7 | 49 | * @param v velocidad lineal. |
tabris2015 | 2:b5d41b0442a7 | 50 | * @param w velocidad angular. |
tabris2015 | 2:b5d41b0442a7 | 51 | */ |
tabris2015 | 2:b5d41b0442a7 | 52 | void conducirUniciclo(float v, float w); |
tabris2015 | 2:b5d41b0442a7 | 53 | |
tabris2015 | 2:b5d41b0442a7 | 54 | /** Método para conducir ambos motores en la misma direccion |
tabris2015 | 2:b5d41b0442a7 | 55 | * @param velocidad valor de velocidad de ambos motores |
tabris2015 | 2:b5d41b0442a7 | 56 | */ |
tabris2015 | 0:b1e9ffb92a0a | 57 | void conducir(float velocidad); |
tabris2015 | 2:b5d41b0442a7 | 58 | |
tabris2015 | 2:b5d41b0442a7 | 59 | /** Método para conducir ambos motores en la misma direccion con una duración en ms |
tabris2015 | 2:b5d41b0442a7 | 60 | * @param velocidad valor de velocidad de ambos motores. |
tabris2015 | 2:b5d41b0442a7 | 61 | * @param duración en milisegundos. |
tabris2015 | 2:b5d41b0442a7 | 62 | */ |
tabris2015 | 0:b1e9ffb92a0a | 63 | void conducir(float velocidad, int duracion); |
tabris2015 | 2:b5d41b0442a7 | 64 | |
tabris2015 | 2:b5d41b0442a7 | 65 | /** Método para conducir ambos motores en direcciones opuestas |
tabris2015 | 2:b5d41b0442a7 | 66 | * @param velocidad valor de velocidad de ambos motores (complementarias). |
tabris2015 | 2:b5d41b0442a7 | 67 | */ |
tabris2015 | 0:b1e9ffb92a0a | 68 | void pivotar(float velocidad); |
tabris2015 | 2:b5d41b0442a7 | 69 | |
tabris2015 | 2:b5d41b0442a7 | 70 | /** Método para conducir ambos motores en direcciones opuestas con una duración en ms |
tabris2015 | 2:b5d41b0442a7 | 71 | * @param velocidad valor de velocidad de ambos motores (complementarias). |
tabris2015 | 2:b5d41b0442a7 | 72 | * @param duración en milisegundos. |
tabris2015 | 2:b5d41b0442a7 | 73 | */ |
tabris2015 | 0:b1e9ffb92a0a | 74 | void pivotar(float velocidad, int duracion); |
tabris2015 | 0:b1e9ffb92a0a | 75 | |
tabris2015 | 0:b1e9ffb92a0a | 76 | /* funciones para conducir motores independientemente */ |
tabris2015 | 2:b5d41b0442a7 | 77 | /** Conducir motor izquierdo |
tabris2015 | 2:b5d41b0442a7 | 78 | * @param velocidad valor de velocidad. |
tabris2015 | 2:b5d41b0442a7 | 79 | */ |
tabris2015 | 0:b1e9ffb92a0a | 80 | void motorIzq(float velocidad); |
tabris2015 | 2:b5d41b0442a7 | 81 | |
tabris2015 | 2:b5d41b0442a7 | 82 | /** Conducir motor izquierdo |
tabris2015 | 2:b5d41b0442a7 | 83 | * @param velocidad valor de velocidad. |
tabris2015 | 2:b5d41b0442a7 | 84 | * @param duración en milisegundos. |
tabris2015 | 2:b5d41b0442a7 | 85 | */ |
tabris2015 | 0:b1e9ffb92a0a | 86 | void motorDer(float velocidad); |
tabris2015 | 2:b5d41b0442a7 | 87 | |
tabris2015 | 2:b5d41b0442a7 | 88 | /** Conducir motor derecho |
tabris2015 | 2:b5d41b0442a7 | 89 | * @param velocidad valor de velocidad. |
tabris2015 | 2:b5d41b0442a7 | 90 | */ |
tabris2015 | 0:b1e9ffb92a0a | 91 | void motorIzq(float velocidad, int duracion); |
tabris2015 | 2:b5d41b0442a7 | 92 | |
tabris2015 | 2:b5d41b0442a7 | 93 | /** Conducir motor derecho |
tabris2015 | 2:b5d41b0442a7 | 94 | * @param velocidad valor de velocidad. |
tabris2015 | 2:b5d41b0442a7 | 95 | * @param duración en milisegundos. |
tabris2015 | 2:b5d41b0442a7 | 96 | */ |
tabris2015 | 0:b1e9ffb92a0a | 97 | void motorDer(float velocidad, int duracion); |
tabris2015 | 2:b5d41b0442a7 | 98 | |
tabris2015 | 0:b1e9ffb92a0a | 99 | /* funciones para detener los motores */ |
tabris2015 | 2:b5d41b0442a7 | 100 | /** Detiene ambos motores */ |
tabris2015 | 0:b1e9ffb92a0a | 101 | void detener(void); |
tabris2015 | 2:b5d41b0442a7 | 102 | |
tabris2015 | 2:b5d41b0442a7 | 103 | /** Frena ambos motores */ |
tabris2015 | 0:b1e9ffb92a0a | 104 | void frenar(void); |
tabris2015 | 0:b1e9ffb92a0a | 105 | |
tabris2015 | 2:b5d41b0442a7 | 106 | /** Detiene motor izquierdo */ |
tabris2015 | 0:b1e9ffb92a0a | 107 | void detenerIzq(void); |
tabris2015 | 2:b5d41b0442a7 | 108 | |
tabris2015 | 2:b5d41b0442a7 | 109 | /** Detiene motor derecho */ |
tabris2015 | 0:b1e9ffb92a0a | 110 | void detenerDer(void); |
tabris2015 | 2:b5d41b0442a7 | 111 | |
tabris2015 | 2:b5d41b0442a7 | 112 | /** Frena motor izquierdo */ |
tabris2015 | 0:b1e9ffb92a0a | 113 | void frenarIzq(void); |
tabris2015 | 2:b5d41b0442a7 | 114 | |
tabris2015 | 2:b5d41b0442a7 | 115 | /** Frena motor derecho */ |
tabris2015 | 0:b1e9ffb92a0a | 116 | void frenarDer(void); |
tabris2015 | 0:b1e9ffb92a0a | 117 | /* --------------- */ |
tabris2015 | 0:b1e9ffb92a0a | 118 | // metodos y atributos privados // |
tabris2015 | 0:b1e9ffb92a0a | 119 | private: |
tabris2015 | 2:b5d41b0442a7 | 120 | |
tabris2015 | 2:b5d41b0442a7 | 121 | /** variables para conversión del robot diferencial a uniciclo */ |
tabris2015 | 2:b5d41b0442a7 | 122 | float _R; |
tabris2015 | 2:b5d41b0442a7 | 123 | float _L; |
tabris2015 | 0:b1e9ffb92a0a | 124 | /* velocidad izquierda */ |
tabris2015 | 0:b1e9ffb92a0a | 125 | PwmOut _MI_vel; |
tabris2015 | 0:b1e9ffb92a0a | 126 | /* sentido de giro */ |
tabris2015 | 0:b1e9ffb92a0a | 127 | DigitalOut _MI_s1; // motor izq |
tabris2015 | 0:b1e9ffb92a0a | 128 | DigitalOut _MI_s2; |
tabris2015 | 0:b1e9ffb92a0a | 129 | /* velocidad derecha */ |
tabris2015 | 0:b1e9ffb92a0a | 130 | PwmOut _MD_vel; |
tabris2015 | 0:b1e9ffb92a0a | 131 | /* sentido de giro */ |
tabris2015 | 0:b1e9ffb92a0a | 132 | DigitalOut _MD_s1; |
tabris2015 | 0:b1e9ffb92a0a | 133 | DigitalOut _MD_s2; |
tabris2015 | 0:b1e9ffb92a0a | 134 | |
tabris2015 | 2:b5d41b0442a7 | 135 | |
tabris2015 | 0:b1e9ffb92a0a | 136 | /* funciones */ |
tabris2015 | 2:b5d41b0442a7 | 137 | |
tabris2015 | 2:b5d41b0442a7 | 138 | /** Conduce el motor izquierdo hacia adelante |
tabris2015 | 2:b5d41b0442a7 | 139 | * @param velocidad valor de velocidad en porcentaje. |
tabris2015 | 2:b5d41b0442a7 | 140 | */ |
tabris2015 | 0:b1e9ffb92a0a | 141 | void _izqAdelante(float velocidad); |
tabris2015 | 2:b5d41b0442a7 | 142 | |
tabris2015 | 2:b5d41b0442a7 | 143 | /** Conduce el motor izquierdo hacia atras |
tabris2015 | 2:b5d41b0442a7 | 144 | * @param velocidad valor de velocidad en porcentaje. |
tabris2015 | 2:b5d41b0442a7 | 145 | */ |
tabris2015 | 0:b1e9ffb92a0a | 146 | void _izqAtras(float velocidad); |
tabris2015 | 2:b5d41b0442a7 | 147 | |
tabris2015 | 2:b5d41b0442a7 | 148 | /** Conduce el motor derecho hacia adelante |
tabris2015 | 2:b5d41b0442a7 | 149 | * @param velocidad valor de velocidad en porcentaje. |
tabris2015 | 2:b5d41b0442a7 | 150 | */ |
tabris2015 | 0:b1e9ffb92a0a | 151 | void _derAdelante(float velocidad); |
tabris2015 | 2:b5d41b0442a7 | 152 | |
tabris2015 | 2:b5d41b0442a7 | 153 | /** Conduce el motor derecho hacia atras |
tabris2015 | 2:b5d41b0442a7 | 154 | * @param velocidad valor de velocidad en porcentaje. |
tabris2015 | 2:b5d41b0442a7 | 155 | */ |
tabris2015 | 0:b1e9ffb92a0a | 156 | void _derAtras(float velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 157 | |
tabris2015 | 0:b1e9ffb92a0a | 158 | }; |
tabris2015 | 0:b1e9ffb92a0a | 159 | #endif |