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.
main.cpp
00001 #include "mbed.h" 00002 #include "Motor.h" 00003 #include "TFTLCD.h" 00004 00005 //******* puerto serie *********// 00006 Serial Mbed(PB_6, PB_7); 00007 00008 //******* Entrada de Interrupcion ENpulsosterna (pulso desde el encoder )*********// 00009 InterruptIn Encoder(PC_9); 00010 00011 //******* Led depuración *********// 00012 DigitalOut LED(PD_9); 00013 00014 //******* Button para depuración *********// 00015 DigitalIn Button(PA_0); 00016 00017 //******* Creacion del objeto de la Clase Motor *********// 00018 MotorContinuo MotorDC(PD_13, PD_11, PB_15, NC,NC,0); 00019 00020 Ticker Time; 00021 //******* Declaración de Constantes y variables *********// 00022 00023 #define NPulsesEncoder 12 00024 #define Nvel 9 /// Cantidad de Velocidades o variaciones del escalon 00025 float EndTime = 12.0; /// Tiempo total en el que se realizara todo el muestreo y se ejecutaran 00026 /// Las distintas variaciones del impulso (velocidades) 00027 #define NSamples 600 /// Numero de muestras a tomar. 00028 #define PulseTime float(EndTime / Nvel) /// Tiempo que dura cada pulso o variacion de velocidad 00029 00030 long Npulsos = 0; /// Almacena la cantidad de pulsos generada por el sensor de herradura en el encoder 00031 00032 int muestras[NSamples]; /// vector en donde se almacenan las 200 muestras de tipo long 00033 float RPM[NSamples]; /// Vector que almacena la convercion de pulsos a RPM del motor 00034 float FactorConvercion; 00035 float Ts; /// Sample Time 00036 float PVel[Nvel] = {5.0, 25.3, 82.5, 56.5, 100, 10.0, 75.7, 30.2, 5}; /// Porcentaje de velocidad: esta es la variable independiente de control. 00037 /// Es nuestra señal de entrada. 00038 /// Estos son los valores en porcentaje dentro del rango de 0 a 100, la cual indica el DUTY_CICLE de la 00039 /// Señal cuadrada que gobierna el movimiento del motor. 00040 00041 int NextSample = 0; /// Indice que se Desplaza a la siguiente Muestra 00042 00043 00044 //******* Declaración de funciones *********// 00045 00046 00047 void config(); 00048 void Encoder_Int(); 00049 void Sample_Int(); 00050 00051 00052 //******* Definicion de funciones *********// 00053 00054 00055 void Encoder_Int() /// Interrupcioin Externa del sensor de Herradura sobre el encoder 00056 { 00057 Npulsos ++; 00058 } 00059 00060 00061 void Sample_Int() 00062 { 00063 // Mientras no excedamos el numero de muestras almacenamos Npulsos tomados en 00064 // la posicion NextSample del vector muestras 00065 if (NextSample < NSamples) muestras[NextSample++] = Npulsos; 00066 Npulsos = 0; 00067 00068 } 00069 00070 00071 void config() 00072 { 00073 // start program messsage 00074 Mbed.printf("Lets Work!!...\n"); 00075 00076 // Set External Interruption to rise flank 00077 Encoder.rise(&Encoder_Int); 00078 00079 00080 // Set a Motor movement direction 00081 MotorDC.Forward(); 00082 00083 // Sample Time 00084 int TotalSamples = NSamples; 00085 Ts = float(EndTime / TotalSamples); 00086 Mbed.printf("Sample Time : %.3f!!...\n!", Ts); 00087 00088 // Pulse Time 00089 Mbed.printf("Pulse Time : %.3f!!...\n!", PulseTime); 00090 00091 // FactorConvercion = 60 segundos / Tiempo de muestreo * Numero de pulsos del encoder por vuelta 00092 FactorConvercion = float (60/(Ts * NPulsesEncoder)); 00093 Mbed.printf("FactorConvercion : %.3f!!...\n!", FactorConvercion); 00094 } 00095 00096 00097 00098 int main() { 00099 00100 00101 00102 config(); 00103 while (1) 00104 { 00105 00106 while(Button == 1) /// No ejecuta el código hasta presionar el boton 00107 { 00108 00109 for(int v = 0 ; v < Nvel ; v++ ) 00110 { 00111 // Set Speed of Motor modifying PWM Duty Cicle 00112 MotorDC.SpeedDuty(PVel[v]); 00113 // Set Time Interrupt each Ts seconds and callback Sample_Int() function. 00114 if (v == 0) Time.attach(&Sample_Int, Ts); 00115 00116 00117 Mbed.printf("Motor Velocidad = %0.2f%% : " , PVel[v]); 00118 LED = 1; 00119 // We wait duration Pulse Time 00120 wait(PulseTime); 00121 00122 } 00123 00124 Time.detach(); 00125 MotorDC.SpeedDuty(0); 00126 LED = 0; 00127 for(NextSample = 0; NextSample < NSamples; NextSample++ ) 00128 { 00129 RPM[NextSample] = (muestras[NextSample] * FactorConvercion); 00130 Mbed.printf("Muestra %d : %d Pulses %0.3f RPM \n", NextSample, muestras[NextSample], RPM[NextSample]); wait_ms(10); 00131 00132 } 00133 00134 00135 } 00136 00137 // Mbed.printf("End of Work!!...\n!"); 00138 00139 00140 00141 } 00142 }
Generated on Sat Aug 13 2022 13:38:26 by
1.7.2