Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: maple_motores maple_heading_pid maple_chotobot_rf_motores motores ... more
Revision 0:b1e9ffb92a0a, committed 2015-12-22
- Comitter:
- tabris2015
- Date:
- Tue Dec 22 20:52:05 2015 +0000
- Child:
- 1:5715aefb07ca
- Child:
- 3:103418a6482f
- Commit message:
- Library for using dual H-bridges with pwm and direction control like l298n, l293d and others
Changed in this revision
| motoresDC.cpp | Show annotated file Show diff for this revision Revisions of this file |
| motoresDC.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/motoresDC.cpp Tue Dec 22 20:52:05 2015 +0000
@@ -0,0 +1,199 @@
+#include "motoresDC.h"
+
+ MotoresDC::MotoresDC(PinName MI_velPin,
+ PinName MI_s1Pin,
+ PinName MI_s2Pin,
+ PinName MD_velPin,
+ PinName MD_s1Pin,
+ PinName MD_s2Pin):_MI_vel(MI_velPin), _MI_s1(MI_s1Pin), _MI_s2(MI_s2Pin), _MD_vel(MD_velPin), _MD_s1(MD_s1Pin), _MD_s2(MD_s2Pin)
+ {
+ _MI_vel.period_ms(1);
+ _MI_vel.write(0);
+ _MD_vel.period_ms(1);
+ _MD_vel.write(0);
+ }
+ /* funciones base para conducir */
+ void MotoresDC::_izqAdelante(float velocidad)
+ {
+ _MI_s1 = 1;
+ _MI_s2 = 0;
+ _MI_vel = abs(velocidad);
+ }
+
+ void MotoresDC::_izqAtras(float velocidad)
+ {
+ _MI_s1 = 0;
+ _MI_s2 = 1;
+ _MI_vel = abs(velocidad);
+ }
+
+ void MotoresDC::_derAdelante(float velocidad)
+ {
+ _MD_s1 = 1;
+ _MD_s2 = 0;
+ _MD_vel = abs(velocidad);
+ }
+
+ void MotoresDC::_derAtras(float velocidad)
+ {
+ _MD_s1 = 0;
+ _MD_s2 = 1;
+ _MD_vel = abs(velocidad);
+ }
+
+ /* funciones compuestas */
+ /* funciones para detener los motores */
+ void MotoresDC::detenerIzq(void)
+ {
+ _MI_s1 = 0;
+ _MI_s2 = 0;
+ _MI_vel = 0;
+ }
+
+ void MotoresDC::detenerDer(void)
+ {
+ _MD_s1 = 0;
+ _MD_s2 = 0;
+ _MD_vel = 0;
+ }
+ void MotoresDC::frenarIzq(void)
+ {
+ _MI_s1 = 1;
+ _MI_s2 = 1;
+ _MI_vel = 0;
+ }
+ void MotoresDC::frenarDer(void)
+ {
+ _MD_s1 = 1;
+ _MD_s2 = 1;
+ _MD_vel = 0;
+ }
+ void MotoresDC::detener(void)
+ {
+ detenerIzq();
+ detenerDer();
+ }
+ void MotoresDC::frenar(void)
+ {
+ frenarIzq();
+ frenarDer();
+ }
+
+ /* --------------- */
+
+ /* funcion para conducir ambos motores en la misma direccion */
+ void MotoresDC::conducir(float velocidad)
+ {
+ if(velocidad > 0)
+ {
+ _izqAdelante(velocidad);
+ _derAdelante(velocidad);
+ }
+ else
+ {
+ _izqAtras(velocidad);
+ _derAtras(velocidad);
+ }
+ }
+ void MotoresDC::conducir(float velocidad, int duracion)
+ {
+ if(velocidad > 0)
+ {
+ _izqAdelante(velocidad);
+ _derAdelante(velocidad);
+ }
+ else
+ {
+ _izqAtras(velocidad);
+ _derAtras(velocidad);
+ }
+ wait_ms(duracion);
+ detenerIzq();
+ detenerDer();
+ }
+ /* funcion para conducir motores en sentido contrario */
+ void MotoresDC::pivotar(float velocidad)
+ {
+ if(velocidad > 0)
+ {
+ _izqAdelante(velocidad);
+ _derAtras(velocidad);
+ }
+ else
+ {
+ _izqAtras(velocidad);
+ _derAdelante(velocidad);
+ }
+ }
+ void MotoresDC::pivotar(float velocidad, int duracion)
+ {
+ if(velocidad > 0)
+ {
+ _izqAdelante(velocidad);
+ _derAtras(velocidad);
+ }
+ else
+ {
+ _izqAtras(velocidad);
+ _derAdelante(velocidad);
+ }
+ wait_ms(duracion);
+ detenerIzq();
+ detenerDer();
+ }
+
+ /* funciones para conducir motores independientemente */
+ void MotoresDC::motorIzq(float velocidad)
+ {
+ if(velocidad > 0)
+ {
+ _izqAdelante(velocidad);
+ }
+ else
+ {
+ _izqAtras(velocidad);
+ }
+ }
+
+ void MotoresDC::motorDer(float velocidad)
+ {
+ if(velocidad > 0)
+ {
+ _derAdelante(velocidad);
+ }
+ else
+ {
+ _derAtras(velocidad);
+ }
+ }
+ /* funciones para conducir motores independientemente con duracion */
+ void MotoresDC::motorIzq(float velocidad, int duracion)
+ {
+ if(velocidad > 0)
+ {
+ _izqAdelante(velocidad);
+ }
+ else
+ {
+ _izqAtras(velocidad);
+ }
+ wait_ms(duracion);
+ detenerIzq();
+ }
+ void MotoresDC::motorDer(float velocidad, int duracion)
+ {
+ if(velocidad > 0)
+ {
+ _derAdelante(velocidad);
+ }
+ else
+ {
+ _derAtras(velocidad);
+ }
+ wait_ms(duracion);
+ detenerDer();
+ }
+
+
+
+
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/motoresDC.h Tue Dec 22 20:52:05 2015 +0000
@@ -0,0 +1,63 @@
+/** motoresDC class
+* libreria para controlar un driver dual de motores DC
+* funciona con drivers como el L293D o el L298N
+* se necesitan 6 pines del microcontrolador
+* uno para la velocidad (PWM) y 2 para el sentido (digital)
+* esta librería esta basada en la libreria RedBot de Sparkfun
+
+*/
+#ifndef MBED_MOTORESDC_H
+#define MBED_MOTORESDC_H
+
+#include "mbed.h"
+
+class MotoresDC
+{
+ public:
+ /**inicia la clase con los pines del driver**/
+ MotoresDC(PinName MI_vel, PinName MI_s1, PinName MI_s2,
+ PinName MD_vel, PinName MD_s1, PinName MD_s2);
+ // metodos publicos //
+ /* funcion para conducir ambos motores en la misma direccion */
+ void conducir(float velocidad);
+ void conducir(float velocidad, int duracion);
+ /* funcion para conducir motores en sentido contrario */
+ void pivotar(float velocidad);
+ void pivotar(float velocidad, int duracion);
+
+ /* funciones para conducir motores independientemente */
+ void motorIzq(float velocidad);
+ void motorDer(float velocidad);
+ /* funciones para conducir motores independientemente con duracion */
+ void motorIzq(float velocidad, int duracion);
+ void motorDer(float velocidad, int duracion);
+ /* funciones para detener los motores */
+ void detener(void);
+ void frenar(void);
+
+ void detenerIzq(void);
+ void detenerDer(void);
+ void frenarIzq(void);
+ void frenarDer(void);
+ /* --------------- */
+ // metodos y atributos privados //
+ private:
+ /* velocidad izquierda */
+ PwmOut _MI_vel;
+ /* sentido de giro */
+ DigitalOut _MI_s1; // motor izq
+ DigitalOut _MI_s2;
+ /* velocidad derecha */
+ PwmOut _MD_vel;
+ /* sentido de giro */
+ DigitalOut _MD_s1;
+ DigitalOut _MD_s2;
+
+ /* funciones */
+ void _izqAdelante(float velocidad);
+ void _izqAtras(float velocidad);
+ void _derAdelante(float velocidad);
+ void _derAtras(float velocidad);
+
+};
+#endif
\ No newline at end of file