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
motoresDC.cpp@0:b1e9ffb92a0a, 2015-12-22 (annotated)
- Committer:
- tabris2015
- Date:
- Tue Dec 22 20:52:05 2015 +0000
- Revision:
- 0:b1e9ffb92a0a
- Child:
- 2:b5d41b0442a7
Library for using dual H-bridges with pwm and direction control like l298n, l293d and others
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tabris2015 | 0:b1e9ffb92a0a | 1 | #include "motoresDC.h" |
| tabris2015 | 0:b1e9ffb92a0a | 2 | |
| tabris2015 | 0:b1e9ffb92a0a | 3 | MotoresDC::MotoresDC(PinName MI_velPin, |
| tabris2015 | 0:b1e9ffb92a0a | 4 | PinName MI_s1Pin, |
| tabris2015 | 0:b1e9ffb92a0a | 5 | PinName MI_s2Pin, |
| tabris2015 | 0:b1e9ffb92a0a | 6 | PinName MD_velPin, |
| tabris2015 | 0:b1e9ffb92a0a | 7 | PinName MD_s1Pin, |
| tabris2015 | 0:b1e9ffb92a0a | 8 | 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) |
| tabris2015 | 0:b1e9ffb92a0a | 9 | { |
| tabris2015 | 0:b1e9ffb92a0a | 10 | _MI_vel.period_ms(1); |
| tabris2015 | 0:b1e9ffb92a0a | 11 | _MI_vel.write(0); |
| tabris2015 | 0:b1e9ffb92a0a | 12 | _MD_vel.period_ms(1); |
| tabris2015 | 0:b1e9ffb92a0a | 13 | _MD_vel.write(0); |
| tabris2015 | 0:b1e9ffb92a0a | 14 | } |
| tabris2015 | 0:b1e9ffb92a0a | 15 | /* funciones base para conducir */ |
| tabris2015 | 0:b1e9ffb92a0a | 16 | void MotoresDC::_izqAdelante(float velocidad) |
| tabris2015 | 0:b1e9ffb92a0a | 17 | { |
| tabris2015 | 0:b1e9ffb92a0a | 18 | _MI_s1 = 1; |
| tabris2015 | 0:b1e9ffb92a0a | 19 | _MI_s2 = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 20 | _MI_vel = abs(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 21 | } |
| tabris2015 | 0:b1e9ffb92a0a | 22 | |
| tabris2015 | 0:b1e9ffb92a0a | 23 | void MotoresDC::_izqAtras(float velocidad) |
| tabris2015 | 0:b1e9ffb92a0a | 24 | { |
| tabris2015 | 0:b1e9ffb92a0a | 25 | _MI_s1 = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 26 | _MI_s2 = 1; |
| tabris2015 | 0:b1e9ffb92a0a | 27 | _MI_vel = abs(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 28 | } |
| tabris2015 | 0:b1e9ffb92a0a | 29 | |
| tabris2015 | 0:b1e9ffb92a0a | 30 | void MotoresDC::_derAdelante(float velocidad) |
| tabris2015 | 0:b1e9ffb92a0a | 31 | { |
| tabris2015 | 0:b1e9ffb92a0a | 32 | _MD_s1 = 1; |
| tabris2015 | 0:b1e9ffb92a0a | 33 | _MD_s2 = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 34 | _MD_vel = abs(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 35 | } |
| tabris2015 | 0:b1e9ffb92a0a | 36 | |
| tabris2015 | 0:b1e9ffb92a0a | 37 | void MotoresDC::_derAtras(float velocidad) |
| tabris2015 | 0:b1e9ffb92a0a | 38 | { |
| tabris2015 | 0:b1e9ffb92a0a | 39 | _MD_s1 = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 40 | _MD_s2 = 1; |
| tabris2015 | 0:b1e9ffb92a0a | 41 | _MD_vel = abs(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 42 | } |
| tabris2015 | 0:b1e9ffb92a0a | 43 | |
| tabris2015 | 0:b1e9ffb92a0a | 44 | /* funciones compuestas */ |
| tabris2015 | 0:b1e9ffb92a0a | 45 | /* funciones para detener los motores */ |
| tabris2015 | 0:b1e9ffb92a0a | 46 | void MotoresDC::detenerIzq(void) |
| tabris2015 | 0:b1e9ffb92a0a | 47 | { |
| tabris2015 | 0:b1e9ffb92a0a | 48 | _MI_s1 = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 49 | _MI_s2 = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 50 | _MI_vel = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 51 | } |
| tabris2015 | 0:b1e9ffb92a0a | 52 | |
| tabris2015 | 0:b1e9ffb92a0a | 53 | void MotoresDC::detenerDer(void) |
| tabris2015 | 0:b1e9ffb92a0a | 54 | { |
| tabris2015 | 0:b1e9ffb92a0a | 55 | _MD_s1 = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 56 | _MD_s2 = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 57 | _MD_vel = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 58 | } |
| tabris2015 | 0:b1e9ffb92a0a | 59 | void MotoresDC::frenarIzq(void) |
| tabris2015 | 0:b1e9ffb92a0a | 60 | { |
| tabris2015 | 0:b1e9ffb92a0a | 61 | _MI_s1 = 1; |
| tabris2015 | 0:b1e9ffb92a0a | 62 | _MI_s2 = 1; |
| tabris2015 | 0:b1e9ffb92a0a | 63 | _MI_vel = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 64 | } |
| tabris2015 | 0:b1e9ffb92a0a | 65 | void MotoresDC::frenarDer(void) |
| tabris2015 | 0:b1e9ffb92a0a | 66 | { |
| tabris2015 | 0:b1e9ffb92a0a | 67 | _MD_s1 = 1; |
| tabris2015 | 0:b1e9ffb92a0a | 68 | _MD_s2 = 1; |
| tabris2015 | 0:b1e9ffb92a0a | 69 | _MD_vel = 0; |
| tabris2015 | 0:b1e9ffb92a0a | 70 | } |
| tabris2015 | 0:b1e9ffb92a0a | 71 | void MotoresDC::detener(void) |
| tabris2015 | 0:b1e9ffb92a0a | 72 | { |
| tabris2015 | 0:b1e9ffb92a0a | 73 | detenerIzq(); |
| tabris2015 | 0:b1e9ffb92a0a | 74 | detenerDer(); |
| tabris2015 | 0:b1e9ffb92a0a | 75 | } |
| tabris2015 | 0:b1e9ffb92a0a | 76 | void MotoresDC::frenar(void) |
| tabris2015 | 0:b1e9ffb92a0a | 77 | { |
| tabris2015 | 0:b1e9ffb92a0a | 78 | frenarIzq(); |
| tabris2015 | 0:b1e9ffb92a0a | 79 | frenarDer(); |
| tabris2015 | 0:b1e9ffb92a0a | 80 | } |
| tabris2015 | 0:b1e9ffb92a0a | 81 | |
| tabris2015 | 0:b1e9ffb92a0a | 82 | /* --------------- */ |
| tabris2015 | 0:b1e9ffb92a0a | 83 | |
| tabris2015 | 0:b1e9ffb92a0a | 84 | /* funcion para conducir ambos motores en la misma direccion */ |
| tabris2015 | 0:b1e9ffb92a0a | 85 | void MotoresDC::conducir(float velocidad) |
| tabris2015 | 0:b1e9ffb92a0a | 86 | { |
| tabris2015 | 0:b1e9ffb92a0a | 87 | if(velocidad > 0) |
| tabris2015 | 0:b1e9ffb92a0a | 88 | { |
| tabris2015 | 0:b1e9ffb92a0a | 89 | _izqAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 90 | _derAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 91 | } |
| tabris2015 | 0:b1e9ffb92a0a | 92 | else |
| tabris2015 | 0:b1e9ffb92a0a | 93 | { |
| tabris2015 | 0:b1e9ffb92a0a | 94 | _izqAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 95 | _derAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 96 | } |
| tabris2015 | 0:b1e9ffb92a0a | 97 | } |
| tabris2015 | 0:b1e9ffb92a0a | 98 | void MotoresDC::conducir(float velocidad, int duracion) |
| tabris2015 | 0:b1e9ffb92a0a | 99 | { |
| tabris2015 | 0:b1e9ffb92a0a | 100 | if(velocidad > 0) |
| tabris2015 | 0:b1e9ffb92a0a | 101 | { |
| tabris2015 | 0:b1e9ffb92a0a | 102 | _izqAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 103 | _derAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 104 | } |
| tabris2015 | 0:b1e9ffb92a0a | 105 | else |
| tabris2015 | 0:b1e9ffb92a0a | 106 | { |
| tabris2015 | 0:b1e9ffb92a0a | 107 | _izqAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 108 | _derAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 109 | } |
| tabris2015 | 0:b1e9ffb92a0a | 110 | wait_ms(duracion); |
| tabris2015 | 0:b1e9ffb92a0a | 111 | detenerIzq(); |
| tabris2015 | 0:b1e9ffb92a0a | 112 | detenerDer(); |
| tabris2015 | 0:b1e9ffb92a0a | 113 | } |
| tabris2015 | 0:b1e9ffb92a0a | 114 | /* funcion para conducir motores en sentido contrario */ |
| tabris2015 | 0:b1e9ffb92a0a | 115 | void MotoresDC::pivotar(float velocidad) |
| tabris2015 | 0:b1e9ffb92a0a | 116 | { |
| tabris2015 | 0:b1e9ffb92a0a | 117 | if(velocidad > 0) |
| tabris2015 | 0:b1e9ffb92a0a | 118 | { |
| tabris2015 | 0:b1e9ffb92a0a | 119 | _izqAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 120 | _derAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 121 | } |
| tabris2015 | 0:b1e9ffb92a0a | 122 | else |
| tabris2015 | 0:b1e9ffb92a0a | 123 | { |
| tabris2015 | 0:b1e9ffb92a0a | 124 | _izqAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 125 | _derAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 126 | } |
| tabris2015 | 0:b1e9ffb92a0a | 127 | } |
| tabris2015 | 0:b1e9ffb92a0a | 128 | void MotoresDC::pivotar(float velocidad, int duracion) |
| tabris2015 | 0:b1e9ffb92a0a | 129 | { |
| tabris2015 | 0:b1e9ffb92a0a | 130 | if(velocidad > 0) |
| tabris2015 | 0:b1e9ffb92a0a | 131 | { |
| tabris2015 | 0:b1e9ffb92a0a | 132 | _izqAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 133 | _derAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 134 | } |
| tabris2015 | 0:b1e9ffb92a0a | 135 | else |
| tabris2015 | 0:b1e9ffb92a0a | 136 | { |
| tabris2015 | 0:b1e9ffb92a0a | 137 | _izqAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 138 | _derAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 139 | } |
| tabris2015 | 0:b1e9ffb92a0a | 140 | wait_ms(duracion); |
| tabris2015 | 0:b1e9ffb92a0a | 141 | detenerIzq(); |
| tabris2015 | 0:b1e9ffb92a0a | 142 | detenerDer(); |
| tabris2015 | 0:b1e9ffb92a0a | 143 | } |
| tabris2015 | 0:b1e9ffb92a0a | 144 | |
| tabris2015 | 0:b1e9ffb92a0a | 145 | /* funciones para conducir motores independientemente */ |
| tabris2015 | 0:b1e9ffb92a0a | 146 | void MotoresDC::motorIzq(float velocidad) |
| tabris2015 | 0:b1e9ffb92a0a | 147 | { |
| tabris2015 | 0:b1e9ffb92a0a | 148 | if(velocidad > 0) |
| tabris2015 | 0:b1e9ffb92a0a | 149 | { |
| tabris2015 | 0:b1e9ffb92a0a | 150 | _izqAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 151 | } |
| tabris2015 | 0:b1e9ffb92a0a | 152 | else |
| tabris2015 | 0:b1e9ffb92a0a | 153 | { |
| tabris2015 | 0:b1e9ffb92a0a | 154 | _izqAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 155 | } |
| tabris2015 | 0:b1e9ffb92a0a | 156 | } |
| tabris2015 | 0:b1e9ffb92a0a | 157 | |
| tabris2015 | 0:b1e9ffb92a0a | 158 | void MotoresDC::motorDer(float velocidad) |
| tabris2015 | 0:b1e9ffb92a0a | 159 | { |
| tabris2015 | 0:b1e9ffb92a0a | 160 | if(velocidad > 0) |
| tabris2015 | 0:b1e9ffb92a0a | 161 | { |
| tabris2015 | 0:b1e9ffb92a0a | 162 | _derAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 163 | } |
| tabris2015 | 0:b1e9ffb92a0a | 164 | else |
| tabris2015 | 0:b1e9ffb92a0a | 165 | { |
| tabris2015 | 0:b1e9ffb92a0a | 166 | _derAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 167 | } |
| tabris2015 | 0:b1e9ffb92a0a | 168 | } |
| tabris2015 | 0:b1e9ffb92a0a | 169 | /* funciones para conducir motores independientemente con duracion */ |
| tabris2015 | 0:b1e9ffb92a0a | 170 | void MotoresDC::motorIzq(float velocidad, int duracion) |
| tabris2015 | 0:b1e9ffb92a0a | 171 | { |
| tabris2015 | 0:b1e9ffb92a0a | 172 | if(velocidad > 0) |
| tabris2015 | 0:b1e9ffb92a0a | 173 | { |
| tabris2015 | 0:b1e9ffb92a0a | 174 | _izqAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 175 | } |
| tabris2015 | 0:b1e9ffb92a0a | 176 | else |
| tabris2015 | 0:b1e9ffb92a0a | 177 | { |
| tabris2015 | 0:b1e9ffb92a0a | 178 | _izqAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 179 | } |
| tabris2015 | 0:b1e9ffb92a0a | 180 | wait_ms(duracion); |
| tabris2015 | 0:b1e9ffb92a0a | 181 | detenerIzq(); |
| tabris2015 | 0:b1e9ffb92a0a | 182 | } |
| tabris2015 | 0:b1e9ffb92a0a | 183 | void MotoresDC::motorDer(float velocidad, int duracion) |
| tabris2015 | 0:b1e9ffb92a0a | 184 | { |
| tabris2015 | 0:b1e9ffb92a0a | 185 | if(velocidad > 0) |
| tabris2015 | 0:b1e9ffb92a0a | 186 | { |
| tabris2015 | 0:b1e9ffb92a0a | 187 | _derAdelante(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 188 | } |
| tabris2015 | 0:b1e9ffb92a0a | 189 | else |
| tabris2015 | 0:b1e9ffb92a0a | 190 | { |
| tabris2015 | 0:b1e9ffb92a0a | 191 | _derAtras(velocidad); |
| tabris2015 | 0:b1e9ffb92a0a | 192 | } |
| tabris2015 | 0:b1e9ffb92a0a | 193 | wait_ms(duracion); |
| tabris2015 | 0:b1e9ffb92a0a | 194 | detenerDer(); |
| tabris2015 | 0:b1e9ffb92a0a | 195 | } |
| tabris2015 | 0:b1e9ffb92a0a | 196 | |
| tabris2015 | 0:b1e9ffb92a0a | 197 | |
| tabris2015 | 0:b1e9ffb92a0a | 198 | |
| tabris2015 | 0:b1e9ffb92a0a | 199 |