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.cpp@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 | 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 | 2:b5d41b0442a7 | 10 | _R = 3.25; |
tabris2015 | 2:b5d41b0442a7 | 11 | _L = 9.0; |
tabris2015 | 0:b1e9ffb92a0a | 12 | _MI_vel.period_ms(1); |
tabris2015 | 0:b1e9ffb92a0a | 13 | _MI_vel.write(0); |
tabris2015 | 0:b1e9ffb92a0a | 14 | _MD_vel.period_ms(1); |
tabris2015 | 0:b1e9ffb92a0a | 15 | _MD_vel.write(0); |
tabris2015 | 0:b1e9ffb92a0a | 16 | } |
tabris2015 | 2:b5d41b0442a7 | 17 | |
tabris2015 | 2:b5d41b0442a7 | 18 | void MotoresDC::setParams(float R, float L) |
tabris2015 | 2:b5d41b0442a7 | 19 | { |
tabris2015 | 2:b5d41b0442a7 | 20 | this->_R = R; |
tabris2015 | 2:b5d41b0442a7 | 21 | this->_L = L; |
tabris2015 | 2:b5d41b0442a7 | 22 | } |
tabris2015 | 2:b5d41b0442a7 | 23 | |
tabris2015 | 2:b5d41b0442a7 | 24 | void MotoresDC::conducirUniciclo(float v, float w) |
tabris2015 | 2:b5d41b0442a7 | 25 | { |
tabris2015 | 2:b5d41b0442a7 | 26 | float v_r = (2 * v + this->_L * w)/(2 * this->_R); |
tabris2015 | 2:b5d41b0442a7 | 27 | float v_l = (2 * v - this->_L * w)/(2 * this->_R); |
tabris2015 | 2:b5d41b0442a7 | 28 | |
tabris2015 | 2:b5d41b0442a7 | 29 | this->motorIzq(v_l); |
tabris2015 | 2:b5d41b0442a7 | 30 | this->motorDer(v_r); |
tabris2015 | 2:b5d41b0442a7 | 31 | } |
tabris2015 | 0:b1e9ffb92a0a | 32 | /* funciones base para conducir */ |
tabris2015 | 0:b1e9ffb92a0a | 33 | void MotoresDC::_izqAdelante(float velocidad) |
tabris2015 | 0:b1e9ffb92a0a | 34 | { |
tabris2015 | 0:b1e9ffb92a0a | 35 | _MI_s1 = 1; |
tabris2015 | 0:b1e9ffb92a0a | 36 | _MI_s2 = 0; |
tabris2015 | 0:b1e9ffb92a0a | 37 | _MI_vel = abs(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 38 | } |
tabris2015 | 0:b1e9ffb92a0a | 39 | |
tabris2015 | 0:b1e9ffb92a0a | 40 | void MotoresDC::_izqAtras(float velocidad) |
tabris2015 | 0:b1e9ffb92a0a | 41 | { |
tabris2015 | 0:b1e9ffb92a0a | 42 | _MI_s1 = 0; |
tabris2015 | 0:b1e9ffb92a0a | 43 | _MI_s2 = 1; |
tabris2015 | 0:b1e9ffb92a0a | 44 | _MI_vel = abs(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 45 | } |
tabris2015 | 0:b1e9ffb92a0a | 46 | |
tabris2015 | 0:b1e9ffb92a0a | 47 | void MotoresDC::_derAdelante(float velocidad) |
tabris2015 | 0:b1e9ffb92a0a | 48 | { |
tabris2015 | 0:b1e9ffb92a0a | 49 | _MD_s1 = 1; |
tabris2015 | 0:b1e9ffb92a0a | 50 | _MD_s2 = 0; |
tabris2015 | 0:b1e9ffb92a0a | 51 | _MD_vel = abs(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 52 | } |
tabris2015 | 0:b1e9ffb92a0a | 53 | |
tabris2015 | 0:b1e9ffb92a0a | 54 | void MotoresDC::_derAtras(float velocidad) |
tabris2015 | 0:b1e9ffb92a0a | 55 | { |
tabris2015 | 0:b1e9ffb92a0a | 56 | _MD_s1 = 0; |
tabris2015 | 0:b1e9ffb92a0a | 57 | _MD_s2 = 1; |
tabris2015 | 0:b1e9ffb92a0a | 58 | _MD_vel = abs(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 59 | } |
tabris2015 | 0:b1e9ffb92a0a | 60 | |
tabris2015 | 0:b1e9ffb92a0a | 61 | /* funciones compuestas */ |
tabris2015 | 0:b1e9ffb92a0a | 62 | /* funciones para detener los motores */ |
tabris2015 | 0:b1e9ffb92a0a | 63 | void MotoresDC::detenerIzq(void) |
tabris2015 | 0:b1e9ffb92a0a | 64 | { |
tabris2015 | 0:b1e9ffb92a0a | 65 | _MI_s1 = 0; |
tabris2015 | 0:b1e9ffb92a0a | 66 | _MI_s2 = 0; |
tabris2015 | 0:b1e9ffb92a0a | 67 | _MI_vel = 0; |
tabris2015 | 0:b1e9ffb92a0a | 68 | } |
tabris2015 | 0:b1e9ffb92a0a | 69 | |
tabris2015 | 0:b1e9ffb92a0a | 70 | void MotoresDC::detenerDer(void) |
tabris2015 | 0:b1e9ffb92a0a | 71 | { |
tabris2015 | 0:b1e9ffb92a0a | 72 | _MD_s1 = 0; |
tabris2015 | 0:b1e9ffb92a0a | 73 | _MD_s2 = 0; |
tabris2015 | 0:b1e9ffb92a0a | 74 | _MD_vel = 0; |
tabris2015 | 0:b1e9ffb92a0a | 75 | } |
tabris2015 | 0:b1e9ffb92a0a | 76 | void MotoresDC::frenarIzq(void) |
tabris2015 | 0:b1e9ffb92a0a | 77 | { |
tabris2015 | 0:b1e9ffb92a0a | 78 | _MI_s1 = 1; |
tabris2015 | 0:b1e9ffb92a0a | 79 | _MI_s2 = 1; |
tabris2015 | 0:b1e9ffb92a0a | 80 | _MI_vel = 0; |
tabris2015 | 0:b1e9ffb92a0a | 81 | } |
tabris2015 | 0:b1e9ffb92a0a | 82 | void MotoresDC::frenarDer(void) |
tabris2015 | 0:b1e9ffb92a0a | 83 | { |
tabris2015 | 0:b1e9ffb92a0a | 84 | _MD_s1 = 1; |
tabris2015 | 0:b1e9ffb92a0a | 85 | _MD_s2 = 1; |
tabris2015 | 0:b1e9ffb92a0a | 86 | _MD_vel = 0; |
tabris2015 | 0:b1e9ffb92a0a | 87 | } |
tabris2015 | 0:b1e9ffb92a0a | 88 | void MotoresDC::detener(void) |
tabris2015 | 0:b1e9ffb92a0a | 89 | { |
tabris2015 | 0:b1e9ffb92a0a | 90 | detenerIzq(); |
tabris2015 | 0:b1e9ffb92a0a | 91 | detenerDer(); |
tabris2015 | 0:b1e9ffb92a0a | 92 | } |
tabris2015 | 0:b1e9ffb92a0a | 93 | void MotoresDC::frenar(void) |
tabris2015 | 0:b1e9ffb92a0a | 94 | { |
tabris2015 | 0:b1e9ffb92a0a | 95 | frenarIzq(); |
tabris2015 | 0:b1e9ffb92a0a | 96 | frenarDer(); |
tabris2015 | 0:b1e9ffb92a0a | 97 | } |
tabris2015 | 0:b1e9ffb92a0a | 98 | |
tabris2015 | 0:b1e9ffb92a0a | 99 | /* --------------- */ |
tabris2015 | 0:b1e9ffb92a0a | 100 | |
tabris2015 | 0:b1e9ffb92a0a | 101 | /* funcion para conducir ambos motores en la misma direccion */ |
tabris2015 | 0:b1e9ffb92a0a | 102 | void MotoresDC::conducir(float velocidad) |
tabris2015 | 0:b1e9ffb92a0a | 103 | { |
tabris2015 | 0:b1e9ffb92a0a | 104 | if(velocidad > 0) |
tabris2015 | 0:b1e9ffb92a0a | 105 | { |
tabris2015 | 0:b1e9ffb92a0a | 106 | _izqAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 107 | _derAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 108 | } |
tabris2015 | 0:b1e9ffb92a0a | 109 | else |
tabris2015 | 0:b1e9ffb92a0a | 110 | { |
tabris2015 | 0:b1e9ffb92a0a | 111 | _izqAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 112 | _derAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 113 | } |
tabris2015 | 0:b1e9ffb92a0a | 114 | } |
tabris2015 | 0:b1e9ffb92a0a | 115 | void MotoresDC::conducir(float velocidad, int duracion) |
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 | _derAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 121 | } |
tabris2015 | 0:b1e9ffb92a0a | 122 | else |
tabris2015 | 0:b1e9ffb92a0a | 123 | { |
tabris2015 | 0:b1e9ffb92a0a | 124 | _izqAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 125 | _derAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 126 | } |
tabris2015 | 0:b1e9ffb92a0a | 127 | wait_ms(duracion); |
tabris2015 | 0:b1e9ffb92a0a | 128 | detenerIzq(); |
tabris2015 | 0:b1e9ffb92a0a | 129 | detenerDer(); |
tabris2015 | 0:b1e9ffb92a0a | 130 | } |
tabris2015 | 0:b1e9ffb92a0a | 131 | /* funcion para conducir motores en sentido contrario */ |
tabris2015 | 0:b1e9ffb92a0a | 132 | void MotoresDC::pivotar(float velocidad) |
tabris2015 | 0:b1e9ffb92a0a | 133 | { |
tabris2015 | 0:b1e9ffb92a0a | 134 | if(velocidad > 0) |
tabris2015 | 0:b1e9ffb92a0a | 135 | { |
tabris2015 | 0:b1e9ffb92a0a | 136 | _izqAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 137 | _derAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 138 | } |
tabris2015 | 0:b1e9ffb92a0a | 139 | else |
tabris2015 | 0:b1e9ffb92a0a | 140 | { |
tabris2015 | 0:b1e9ffb92a0a | 141 | _izqAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 142 | _derAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 143 | } |
tabris2015 | 0:b1e9ffb92a0a | 144 | } |
tabris2015 | 0:b1e9ffb92a0a | 145 | void MotoresDC::pivotar(float velocidad, int duracion) |
tabris2015 | 0:b1e9ffb92a0a | 146 | { |
tabris2015 | 0:b1e9ffb92a0a | 147 | if(velocidad > 0) |
tabris2015 | 0:b1e9ffb92a0a | 148 | { |
tabris2015 | 0:b1e9ffb92a0a | 149 | _izqAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 150 | _derAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 151 | } |
tabris2015 | 0:b1e9ffb92a0a | 152 | else |
tabris2015 | 0:b1e9ffb92a0a | 153 | { |
tabris2015 | 0:b1e9ffb92a0a | 154 | _izqAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 155 | _derAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 156 | } |
tabris2015 | 0:b1e9ffb92a0a | 157 | wait_ms(duracion); |
tabris2015 | 0:b1e9ffb92a0a | 158 | detenerIzq(); |
tabris2015 | 0:b1e9ffb92a0a | 159 | detenerDer(); |
tabris2015 | 0:b1e9ffb92a0a | 160 | } |
tabris2015 | 0:b1e9ffb92a0a | 161 | |
tabris2015 | 0:b1e9ffb92a0a | 162 | /* funciones para conducir motores independientemente */ |
tabris2015 | 0:b1e9ffb92a0a | 163 | void MotoresDC::motorIzq(float velocidad) |
tabris2015 | 0:b1e9ffb92a0a | 164 | { |
tabris2015 | 0:b1e9ffb92a0a | 165 | if(velocidad > 0) |
tabris2015 | 0:b1e9ffb92a0a | 166 | { |
tabris2015 | 0:b1e9ffb92a0a | 167 | _izqAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 168 | } |
tabris2015 | 0:b1e9ffb92a0a | 169 | else |
tabris2015 | 0:b1e9ffb92a0a | 170 | { |
tabris2015 | 0:b1e9ffb92a0a | 171 | _izqAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 172 | } |
tabris2015 | 0:b1e9ffb92a0a | 173 | } |
tabris2015 | 0:b1e9ffb92a0a | 174 | |
tabris2015 | 0:b1e9ffb92a0a | 175 | void MotoresDC::motorDer(float velocidad) |
tabris2015 | 0:b1e9ffb92a0a | 176 | { |
tabris2015 | 0:b1e9ffb92a0a | 177 | if(velocidad > 0) |
tabris2015 | 0:b1e9ffb92a0a | 178 | { |
tabris2015 | 0:b1e9ffb92a0a | 179 | _derAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 180 | } |
tabris2015 | 0:b1e9ffb92a0a | 181 | else |
tabris2015 | 0:b1e9ffb92a0a | 182 | { |
tabris2015 | 0:b1e9ffb92a0a | 183 | _derAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 184 | } |
tabris2015 | 0:b1e9ffb92a0a | 185 | } |
tabris2015 | 0:b1e9ffb92a0a | 186 | /* funciones para conducir motores independientemente con duracion */ |
tabris2015 | 0:b1e9ffb92a0a | 187 | void MotoresDC::motorIzq(float velocidad, int duracion) |
tabris2015 | 0:b1e9ffb92a0a | 188 | { |
tabris2015 | 0:b1e9ffb92a0a | 189 | if(velocidad > 0) |
tabris2015 | 0:b1e9ffb92a0a | 190 | { |
tabris2015 | 0:b1e9ffb92a0a | 191 | _izqAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 192 | } |
tabris2015 | 0:b1e9ffb92a0a | 193 | else |
tabris2015 | 0:b1e9ffb92a0a | 194 | { |
tabris2015 | 0:b1e9ffb92a0a | 195 | _izqAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 196 | } |
tabris2015 | 0:b1e9ffb92a0a | 197 | wait_ms(duracion); |
tabris2015 | 0:b1e9ffb92a0a | 198 | detenerIzq(); |
tabris2015 | 0:b1e9ffb92a0a | 199 | } |
tabris2015 | 0:b1e9ffb92a0a | 200 | void MotoresDC::motorDer(float velocidad, int duracion) |
tabris2015 | 0:b1e9ffb92a0a | 201 | { |
tabris2015 | 0:b1e9ffb92a0a | 202 | if(velocidad > 0) |
tabris2015 | 0:b1e9ffb92a0a | 203 | { |
tabris2015 | 0:b1e9ffb92a0a | 204 | _derAdelante(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 205 | } |
tabris2015 | 0:b1e9ffb92a0a | 206 | else |
tabris2015 | 0:b1e9ffb92a0a | 207 | { |
tabris2015 | 0:b1e9ffb92a0a | 208 | _derAtras(velocidad); |
tabris2015 | 0:b1e9ffb92a0a | 209 | } |
tabris2015 | 0:b1e9ffb92a0a | 210 | wait_ms(duracion); |
tabris2015 | 0:b1e9ffb92a0a | 211 | detenerDer(); |
tabris2015 | 0:b1e9ffb92a0a | 212 | } |
tabris2015 | 0:b1e9ffb92a0a | 213 | |
tabris2015 | 0:b1e9ffb92a0a | 214 | |
tabris2015 | 0:b1e9ffb92a0a | 215 | |
tabris2015 | 0:b1e9ffb92a0a | 216 |