Proyecto final de semestre: Se desarrollo un vehículo que recibe comandos por el puerto serial y realiza las siguientes funciones según el comando sea el comando recibido: - 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 - Muestra los colores leídos por una pantalla FFT instalada en el vehículo.

Dependencies:   mbed UniGraphic

Committer:
CCastrop1012
Date:
Fri Sep 03 05:26:27 2021 +0000
Revision:
0:3a420fc22672
Proyecto de final de semestre, finalizado y funcional.

Who changed what in which revision?

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