Se leen comando por el puerto serial realiza las siguientes funciones según el comando: - Genera distintos tonos por un buzzer. - Controla el movimiento de un carro (con 2 motores) con comandos - Controla el movimiento de un carro (con 2 motores) con Joystick. - Lee y envía el color leido por el puerto serial al PC
Motor.cpp@0:3a37f6734913, 2021-09-03 (annotated)
- Committer:
- CCastrop1012
- Date:
- Fri Sep 03 05:22:19 2021 +0000
- Revision:
- 0:3a37f6734913
Programa finalizado y funcional.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CCastrop1012 | 0:3a37f6734913 | 1 | #include "mbed.h" |
CCastrop1012 | 0:3a37f6734913 | 2 | #include "Motor.h" |
CCastrop1012 | 0:3a37f6734913 | 3 | #include "Ticker.h" |
CCastrop1012 | 0:3a37f6734913 | 4 | |
CCastrop1012 | 0:3a37f6734913 | 5 | #define Pi 3.14159265 |
CCastrop1012 | 0:3a37f6734913 | 6 | #define Horario 1 |
CCastrop1012 | 0:3a37f6734913 | 7 | #define AntiHorario 0 |
CCastrop1012 | 0:3a37f6734913 | 8 | |
CCastrop1012 | 0:3a37f6734913 | 9 | ///************ MOTOR CONTINUO ********************************// |
CCastrop1012 | 0:3a37f6734913 | 10 | |
CCastrop1012 | 0:3a37f6734913 | 11 | /// CONSTRUCTOR |
CCastrop1012 | 0:3a37f6734913 | 12 | MotorContinuo::MotorContinuo(PinName _L1, PinName _L2, PinName _speedPin, PinName _encodin, PinName _PosInicial, int _EncodPulses) : |
CCastrop1012 | 0:3a37f6734913 | 13 | L1(_L1), L2(_L2), speedPin(_speedPin), encodin(_encodin), PosInicial(_PosInicial), EncodPulses(_EncodPulses) |
CCastrop1012 | 0:3a37f6734913 | 14 | { |
CCastrop1012 | 0:3a37f6734913 | 15 | speedPin.period_ms(2); |
CCastrop1012 | 0:3a37f6734913 | 16 | speedPin.write(0); |
CCastrop1012 | 0:3a37f6734913 | 17 | |
CCastrop1012 | 0:3a37f6734913 | 18 | }; |
CCastrop1012 | 0:3a37f6734913 | 19 | |
CCastrop1012 | 0:3a37f6734913 | 20 | |
CCastrop1012 | 0:3a37f6734913 | 21 | |
CCastrop1012 | 0:3a37f6734913 | 22 | void MotorContinuo::Forward() { L1=1; L2=0;} |
CCastrop1012 | 0:3a37f6734913 | 23 | void MotorContinuo::Back() { L1=0; L2=1;} |
CCastrop1012 | 0:3a37f6734913 | 24 | void MotorContinuo::Stop() { L1=0; L2=0;} |
CCastrop1012 | 0:3a37f6734913 | 25 | void MotorContinuo::StopT() { L1=1; L2=1;} |
CCastrop1012 | 0:3a37f6734913 | 26 | void MotorContinuo::SpeedDuty(int v) { speedPin.write(float(v/100.0));} |
CCastrop1012 | 0:3a37f6734913 | 27 | void MotorContinuo::SpinLength_ms(float t) { t++;}// Duración del giro en ms |
CCastrop1012 | 0:3a37f6734913 | 28 | void MotorContinuo::SpinLength(float t) { t++;}; |
CCastrop1012 | 0:3a37f6734913 | 29 | |
CCastrop1012 | 0:3a37f6734913 | 30 | |
CCastrop1012 | 0:3a37f6734913 | 31 | |
CCastrop1012 | 0:3a37f6734913 | 32 | ///************ MOTOR DISCRETO ********************************// |
CCastrop1012 | 0:3a37f6734913 | 33 | |
CCastrop1012 | 0:3a37f6734913 | 34 | /// CONSTRUCTOR |
CCastrop1012 | 0:3a37f6734913 | 35 | MotorDiscreto::MotorDiscreto(PinName _Dir, PinName _Step, int _NPasos, PinName _encodin, PinName _PosInicial, int _EncodPulses) : |
CCastrop1012 | 0:3a37f6734913 | 36 | Dir(_Dir), Step(_Step), NPasos(_NPasos), encodin(_encodin), PosInicial(_PosInicial), EncodPulses(_EncodPulses) |
CCastrop1012 | 0:3a37f6734913 | 37 | { |
CCastrop1012 | 0:3a37f6734913 | 38 | }; |
CCastrop1012 | 0:3a37f6734913 | 39 | |
CCastrop1012 | 0:3a37f6734913 | 40 | |
CCastrop1012 | 0:3a37f6734913 | 41 | // ************* METODOS ***************** /// |
CCastrop1012 | 0:3a37f6734913 | 42 | |
CCastrop1012 | 0:3a37f6734913 | 43 | void MotorDiscreto::moveMotor(void) |
CCastrop1012 | 0:3a37f6734913 | 44 | { |
CCastrop1012 | 0:3a37f6734913 | 45 | if (StepOnHold != 0 && _moveMotorStopped == false) |
CCastrop1012 | 0:3a37f6734913 | 46 | { |
CCastrop1012 | 0:3a37f6734913 | 47 | Step = !Step; // Se hace elcambio de estado en el pin Step |
CCastrop1012 | 0:3a37f6734913 | 48 | entradas++; // se registra cada paso efectivo el cual es valido cada 2 entradas |
CCastrop1012 | 0:3a37f6734913 | 49 | // 1ra entrada: tiempo en Bajo |
CCastrop1012 | 0:3a37f6734913 | 50 | // 2da entrada: tiempo en Alto |
CCastrop1012 | 0:3a37f6734913 | 51 | // Es decir cuando Step cumple un periodo. |
CCastrop1012 | 0:3a37f6734913 | 52 | if (entradas >= 2) // cuando se registran 2 entradas se disminuye un paso |
CCastrop1012 | 0:3a37f6734913 | 53 | { |
CCastrop1012 | 0:3a37f6734913 | 54 | StepOnHold--; // Se elimina 1 paso de los pendientes |
CCastrop1012 | 0:3a37f6734913 | 55 | entradas = 0; |
CCastrop1012 | 0:3a37f6734913 | 56 | } |
CCastrop1012 | 0:3a37f6734913 | 57 | |
CCastrop1012 | 0:3a37f6734913 | 58 | Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); // Reiniciamos el tiempo de imterrupcion |
CCastrop1012 | 0:3a37f6734913 | 59 | |
CCastrop1012 | 0:3a37f6734913 | 60 | } |
CCastrop1012 | 0:3a37f6734913 | 61 | else if (StepOnHold == 0 || _moveMotorStopped == true) // si no hay mas pasos pendientes |
CCastrop1012 | 0:3a37f6734913 | 62 | { |
CCastrop1012 | 0:3a37f6734913 | 63 | Move.detach(); // desabilita la interrupcion |
CCastrop1012 | 0:3a37f6734913 | 64 | entradas = 0; |
CCastrop1012 | 0:3a37f6734913 | 65 | } |
CCastrop1012 | 0:3a37f6734913 | 66 | } |
CCastrop1012 | 0:3a37f6734913 | 67 | |
CCastrop1012 | 0:3a37f6734913 | 68 | |
CCastrop1012 | 0:3a37f6734913 | 69 | void MotorDiscreto::Forward() { Dir=1;} |
CCastrop1012 | 0:3a37f6734913 | 70 | void MotorDiscreto::Back() { Dir=0;} |
CCastrop1012 | 0:3a37f6734913 | 71 | void MotorDiscreto::Stop() { EnablePin = 0; _moveMotorStopped = true;} |
CCastrop1012 | 0:3a37f6734913 | 72 | void MotorDiscreto::StopT() { Move.detach(); _moveMotorStopped = true;} |
CCastrop1012 | 0:3a37f6734913 | 73 | void MotorDiscreto::StepFreq(long n) { StepsBySecond = n; TStep = (1/float(n));} |
CCastrop1012 | 0:3a37f6734913 | 74 | void MotorDiscreto::RunStep(long n) { StepOnHold = n; Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); _moveMotorStopped = false; }// Duración del giro en ms |
CCastrop1012 | 0:3a37f6734913 | 75 | void MotorDiscreto::Run(float t) { float Fp = (1/TStep); StepOnHold = long(t*Fp); Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); _moveMotorStopped = false; } |
CCastrop1012 | 0:3a37f6734913 | 76 | void MotorDiscreto::RunRound(int n) { StepOnHold = (NPasos * n); Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); _moveMotorStopped = false; } |
CCastrop1012 | 0:3a37f6734913 | 77 | long MotorDiscreto::getStepOnHold(void){return StepOnHold; } |
CCastrop1012 | 0:3a37f6734913 | 78 | |
CCastrop1012 | 0:3a37f6734913 | 79 | void Ustep(int resolucion, DigitalOut* _M0, DigitalOut* _M1, DigitalOut* _M2) |
CCastrop1012 | 0:3a37f6734913 | 80 | { |
CCastrop1012 | 0:3a37f6734913 | 81 | |
CCastrop1012 | 0:3a37f6734913 | 82 | switch(resolucion) |
CCastrop1012 | 0:3a37f6734913 | 83 | { |
CCastrop1012 | 0:3a37f6734913 | 84 | case 1: *_M0 = 0; *_M1 = 0; *_M2 = 0; |
CCastrop1012 | 0:3a37f6734913 | 85 | break; |
CCastrop1012 | 0:3a37f6734913 | 86 | case 2: *_M0 = 1; *_M1 = 0; *_M2 = 0; |
CCastrop1012 | 0:3a37f6734913 | 87 | break; |
CCastrop1012 | 0:3a37f6734913 | 88 | case 4: *_M0 = 0; *_M1 = 1; *_M2 = 0; |
CCastrop1012 | 0:3a37f6734913 | 89 | break; |
CCastrop1012 | 0:3a37f6734913 | 90 | case 8: *_M0 = 1; *_M1 = 1; *_M2 = 1; |
CCastrop1012 | 0:3a37f6734913 | 91 | break; |
CCastrop1012 | 0:3a37f6734913 | 92 | case 16: *_M0 = 0; *_M1 = 0; *_M2 = 1; |
CCastrop1012 | 0:3a37f6734913 | 93 | break; |
CCastrop1012 | 0:3a37f6734913 | 94 | case 32: *_M0 = 1; *_M1 = 0; *_M2 = 1; |
CCastrop1012 | 0:3a37f6734913 | 95 | break; |
CCastrop1012 | 0:3a37f6734913 | 96 | |
CCastrop1012 | 0:3a37f6734913 | 97 | default: *_M0 = 0; *_M1 = 0; *_M2 = 0; |
CCastrop1012 | 0:3a37f6734913 | 98 | |
CCastrop1012 | 0:3a37f6734913 | 99 | |
CCastrop1012 | 0:3a37f6734913 | 100 | } |
CCastrop1012 | 0:3a37f6734913 | 101 | |
CCastrop1012 | 0:3a37f6734913 | 102 | |
CCastrop1012 | 0:3a37f6734913 | 103 | } |
CCastrop1012 | 0:3a37f6734913 | 104 | |
CCastrop1012 | 0:3a37f6734913 | 105 | |
CCastrop1012 | 0:3a37f6734913 | 106 | ///************ TRACCIONDISCRETA ********************************// |
CCastrop1012 | 0:3a37f6734913 | 107 | |
CCastrop1012 | 0:3a37f6734913 | 108 | |
CCastrop1012 | 0:3a37f6734913 | 109 | /// ***************** CONSTRUCTOR ********************** /// |
CCastrop1012 | 0:3a37f6734913 | 110 | TraccionD::TraccionD(PinName _StepMI, PinName _DirMI, PinName _StepMD, PinName _DirMD, int _NPasos, float _r, float _L ): |
CCastrop1012 | 0:3a37f6734913 | 111 | StepMI(_StepMI), DirMI(_DirMI), StepMD(_StepMD), DirMD(_DirMD), NPasos(_NPasos), r(_r), L(_L) |
CCastrop1012 | 0:3a37f6734913 | 112 | { |
CCastrop1012 | 0:3a37f6734913 | 113 | // 1. se halla la Relacion(Rl) de distacia entre los dos Radios(r y L) |
CCastrop1012 | 0:3a37f6734913 | 114 | Rl = (L / r); |
CCastrop1012 | 0:3a37f6734913 | 115 | |
CCastrop1012 | 0:3a37f6734913 | 116 | }; |
CCastrop1012 | 0:3a37f6734913 | 117 | |
CCastrop1012 | 0:3a37f6734913 | 118 | // ************* METODOS ***************** /// |
CCastrop1012 | 0:3a37f6734913 | 119 | |
CCastrop1012 | 0:3a37f6734913 | 120 | void TraccionD::moveMotor(void) |
CCastrop1012 | 0:3a37f6734913 | 121 | { |
CCastrop1012 | 0:3a37f6734913 | 122 | if (StepOnHold != 0) |
CCastrop1012 | 0:3a37f6734913 | 123 | { |
CCastrop1012 | 0:3a37f6734913 | 124 | |
CCastrop1012 | 0:3a37f6734913 | 125 | |
CCastrop1012 | 0:3a37f6734913 | 126 | StepMI = !StepMI; // Se hace elcambio de estado en el pin Step para ambos motores |
CCastrop1012 | 0:3a37f6734913 | 127 | StepMD = StepMI; |
CCastrop1012 | 0:3a37f6734913 | 128 | |
CCastrop1012 | 0:3a37f6734913 | 129 | entradas++; // se registra cada paso efectivo el cual es valido cada 2 entradas |
CCastrop1012 | 0:3a37f6734913 | 130 | // 1ra entrada: tiempo en Bajo |
CCastrop1012 | 0:3a37f6734913 | 131 | // 2da entrada: tiempo en Alto |
CCastrop1012 | 0:3a37f6734913 | 132 | // Es decir cuando Step cumple un periodo. |
CCastrop1012 | 0:3a37f6734913 | 133 | if (entradas >= 2) // cuando se registran 2 entradas se disminuye un paso |
CCastrop1012 | 0:3a37f6734913 | 134 | { |
CCastrop1012 | 0:3a37f6734913 | 135 | StepOnHold--; // Se elimina 1 paso de los pendientes |
CCastrop1012 | 0:3a37f6734913 | 136 | entradas = 0; |
CCastrop1012 | 0:3a37f6734913 | 137 | } |
CCastrop1012 | 0:3a37f6734913 | 138 | |
CCastrop1012 | 0:3a37f6734913 | 139 | Move.attach(callback(this,&TraccionD::moveMotor), (TStep/2) ); // Reiniciamos el tiempo de imterrupcion |
CCastrop1012 | 0:3a37f6734913 | 140 | |
CCastrop1012 | 0:3a37f6734913 | 141 | } |
CCastrop1012 | 0:3a37f6734913 | 142 | else if (StepOnHold == 0) // si no hay mas pasos para dar se deshabilita las interrupciones. |
CCastrop1012 | 0:3a37f6734913 | 143 | { |
CCastrop1012 | 0:3a37f6734913 | 144 | Move.detach(); |
CCastrop1012 | 0:3a37f6734913 | 145 | } |
CCastrop1012 | 0:3a37f6734913 | 146 | } |
CCastrop1012 | 0:3a37f6734913 | 147 | |
CCastrop1012 | 0:3a37f6734913 | 148 | |
CCastrop1012 | 0:3a37f6734913 | 149 | void TraccionD::Forward() { DirMI=1; DirMD=1;} // Configua de giro de ambos motores el el mismo sentido para avanzar hacia adelante |
CCastrop1012 | 0:3a37f6734913 | 150 | void TraccionD::Back() { DirMI=0; DirMD=0;} // Configua el sentido giro en ambos motores contrario al de Forward(), para avanzar hacia atras |
CCastrop1012 | 0:3a37f6734913 | 151 | void TraccionD::Left() { DirMI=0; DirMD=1;} // Configura el sentido de giro de ambos motores de manera contraria para girar hacia la izquierda |
CCastrop1012 | 0:3a37f6734913 | 152 | void TraccionD::Right() { DirMI=1; DirMD=0;} // Configura el sentido de giro de ambos motores de manera contraria e inverso al de Left() para girar hacia la derecha |
CCastrop1012 | 0:3a37f6734913 | 153 | void TraccionD::Stop() { EnablePinM1 = 0; EnablePinM2 = 0;} |
CCastrop1012 | 0:3a37f6734913 | 154 | void TraccionD::StopT() { Move.detach();} |
CCastrop1012 | 0:3a37f6734913 | 155 | void TraccionD::StepFreq(long n) { StepsBySecond = n; TStep = (1/float(n));} |
CCastrop1012 | 0:3a37f6734913 | 156 | void TraccionD::RunStep(long n){ StepOnHold = n; Move.attach(callback(this,&TraccionD::moveMotor), (TStep/2) ); }// Duración del giro en ms |
CCastrop1012 | 0:3a37f6734913 | 157 | void TraccionD::Run(float t) { float Fp = (1/TStep); StepOnHold = long(t*Fp); Move.attach(callback(this,&TraccionD::moveMotor), (TStep/2) ); } |
CCastrop1012 | 0:3a37f6734913 | 158 | void TraccionD::RunRound(int n) { StepOnHold = (NPasos * n); Move.attach(callback(this,&TraccionD::moveMotor), (TStep/2) ); } |
CCastrop1012 | 0:3a37f6734913 | 159 | long TraccionD::getStepOnHold(void){return StepOnHold; } |
CCastrop1012 | 0:3a37f6734913 | 160 | |
CCastrop1012 | 0:3a37f6734913 | 161 | void TraccionD::Giro(long grados, bool sentido) |
CCastrop1012 | 0:3a37f6734913 | 162 | { |
CCastrop1012 | 0:3a37f6734913 | 163 | |
CCastrop1012 | 0:3a37f6734913 | 164 | StepOnHold = grados * int(Rl * NPasos); |
CCastrop1012 | 0:3a37f6734913 | 165 | StepOnHold /= 360; |
CCastrop1012 | 0:3a37f6734913 | 166 | if(sentido == true) Right(); else if (sentido == false) Left(); |
CCastrop1012 | 0:3a37f6734913 | 167 | Move.attach(callback(this,&TraccionD::moveMotor), (TStep/2) ); |
CCastrop1012 | 0:3a37f6734913 | 168 | |
CCastrop1012 | 0:3a37f6734913 | 169 | } |
CCastrop1012 | 0:3a37f6734913 | 170 | |
CCastrop1012 | 0:3a37f6734913 | 171 | |
CCastrop1012 | 0:3a37f6734913 | 172 | |
CCastrop1012 | 0:3a37f6734913 | 173 | |
CCastrop1012 | 0:3a37f6734913 | 174 | |
CCastrop1012 | 0:3a37f6734913 | 175 | |
CCastrop1012 | 0:3a37f6734913 | 176 | |
CCastrop1012 | 0:3a37f6734913 | 177 | |
CCastrop1012 | 0:3a37f6734913 | 178 | |
CCastrop1012 | 0:3a37f6734913 | 179 | |
CCastrop1012 | 0:3a37f6734913 | 180 |