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 #define get_values 00004 //******* puerto serie *********// 00005 Serial Mbed(PB_6, PB_7); 00006 00007 //******* Entrada de Interrupcion ENpulsosterna (pulso desde el encoder )*********// 00008 InterruptIn Encoder(PC_9); 00009 00010 //******* Analog IN *********// 00011 00012 00013 //******* Led depuración *********// 00014 DigitalOut LED(PD_9); 00015 00016 //******* Button para depuración *********// 00017 DigitalIn Button(PA_0); 00018 00019 //******* Creacion del objeto de la Clase Motor *********// 00020 MotorContinuo MotorDC(PD_13, PD_11, PB_15, NC,NC,0); 00021 00022 Timeout Time; 00023 //******* Declaración de Constantes y variables *********// 00024 00025 #define NPulsesEncoder 12 00026 #define Nvel 1 /// Cantidad de Velocidades o variaciones del escalon 00027 float EndTime = 0.83; /// Tiempo total en el que se realizara todo el muestreo y se ejecutaran 00028 /// Las distintas variaciones del impulso (velocidades) 00029 #define NSamples 830 /// Numero de muestras a tomar. 00030 #define PulseTime float(EndTime / Nvel) /// Tiempo que dura cada pulso o variacion de velocidad 00031 00032 long Npulsos = 0; /// Almacena la cantidad de pulsos generada por el sensor de herradura en el encoder 00033 int PVelocidadAplicada[NSamples]; /// Almaena Las velocidades que se aplicaron en cada muestra 00034 int muestras[NSamples]; /// vector en donde se almacenan las 200 muestras de tipo long 00035 float RPM[NSamples]; /// Vector que almacena la convercion de pulsos a RPM del motor 00036 float RPS[NSamples]; /// Vector que almacena la convercion de pulsos a RPM del motor 00037 float FactorConvercion; /// Almacena el factor de conversion de numero de pulsos a RPM 00038 float Ts; /// Sample Time 00039 int PVel[Nvel] = {100};//, 25, 50, 75, 100, 85, 60, 30, 10, 5}; /// Porcentaje de velocidad: esta es la variable independiente de control. 00040 /// Es nuestra señal de entrada. puede ser float 00041 /// Estos son los valores en porcentaje dentro del rango de 0 a 100, la cual indica el DUTY_CICLE de la 00042 /// Señal cuadrada que gobierna el movimiento del motor. 00043 int velActual; /// Almacena de manera temporal la velcidad que se esta aplicando. 00044 int NextSample = 0; /// Indice que se Desplaza a la siguiente Muestra 00045 int v; /// Indice que desplaza la Velocidad en PVel[] 00046 00047 //******* Declaración de funciones *********// 00048 00049 00050 void config(); 00051 void Encoder_Int(); 00052 void Sample_Int(); 00053 00054 00055 //******* Definicion de funciones *********// 00056 00057 00058 void Encoder_Int() /// Interrupcioin Externa del sensor de Herradura sobre el encoder 00059 { 00060 Npulsos ++; 00061 } 00062 00063 00064 void Sample_Int() 00065 { 00066 // Mientras no excedamos el numero de muestras almacenamos Npulsos tomados en 00067 // la posicion NextSample del vector muestras 00068 if (NextSample < NSamples) 00069 { 00070 muestras[NextSample] = Npulsos; 00071 PVelocidadAplicada[NextSample++] = PVel[v]; 00072 } 00073 Npulsos = 0; 00074 Time.attach(&Sample_Int, Ts); 00075 } 00076 00077 00078 void config() 00079 { 00080 // start program messsage 00081 Mbed.printf("Lets Work!!...\n"); 00082 00083 // Set External Interruption to rise flank 00084 Encoder.fall(&Encoder_Int); 00085 00086 00087 // Set a Motor movement direction 00088 MotorDC.Forward(); 00089 00090 #ifdef depuration 00091 // Sample Time 00092 int TotalSamples = NSamples; 00093 Ts = float(EndTime / TotalSamples); 00094 Mbed.printf("Sample Time : %.3f!!...\n!", Ts); 00095 00096 // Pulse Time 00097 Mbed.printf("Pulse Time : %.3f!!...\n!", PulseTime); 00098 00099 // FactorConvercion = 60 segundos / Tiempo de muestreo * Numero de pulsos del encoder por vuelta 00100 FactorConvercion = float (60/(Ts * NPulsesEncoder)); 00101 Mbed.printf("FactorConvercion : %.3f!!...\n!", FactorConvercion); 00102 #endif 00103 00104 #ifdef get_values 00105 00106 // Sample Time 00107 int TotalSamples = NSamples; 00108 Ts = float(EndTime / TotalSamples); 00109 // FactorConvercion = 60 segundos / Tiempo de muestreo * Numero de pulsos del encoder por vuelta 00110 FactorConvercion = float (60/(Ts * NPulsesEncoder)); 00111 00112 Mbed.printf("\t\tCurva Caracteristica del Motor \n\n Modelo del Motor : 40ZYN06 MaxVoltage Supply : 24V Current : 1.5A MaxRPM : 3700\n"); 00113 Mbed.printf("\n\tTest Description\n\n Test Voltage Supply: 19.43V Test Current Supply: 1.5A \nEncoder Pulses : %d # de Velocidades Aplicadas: %d ", NPulsesEncoder, Nvel); 00114 Mbed.printf("\nTotal Test Time: %.0fs Samples: %d Sample Time : %0.3fs Duracion de Cada Velocidad: %.2fs Factor de Conversion(Pulses To RPM): %0.2f \n\n", EndTime, NSamples, Ts, PulseTime, FactorConvercion); 00115 Mbed.printf("\nPorcentaje de WeidthPulse Aplicadas durante el test(ordenado) :\n\t"); 00116 for(int n = 0; n < Nvel; n++)Mbed.printf("\t%d%%,",PVel[n]); 00117 Mbed.printf("\n\t11 Mayo 2019 - Elaborado por Cristian Kevin Castro Parra\n"); 00118 00119 #endif 00120 00121 } 00122 00123 00124 00125 int main() { 00126 00127 00128 00129 config(); 00130 while (1) 00131 { 00132 00133 while(Button == 1) /// No ejecuta el código hasta presionar el boton 00134 { 00135 00136 for(v = 0 ; v < Nvel ; v++ ) 00137 { 00138 // Set Speed of Motor modifying PWM Duty Cicle 00139 MotorDC.SpeedDuty(PVel[v]); 00140 // Set Time Interrupt each Ts seconds and callback Sample_Int() function. 00141 if (v == 0) Time.attach(&Sample_Int, Ts); 00142 00143 00144 Mbed.printf("\nMotor Velocidad = %d%% : " , PVel[v]); 00145 00146 // We wait duration Pulse Time 00147 wait(PulseTime); 00148 00149 } 00150 00151 Time.detach(); 00152 MotorDC.SpeedDuty(0); 00153 LED = 0; 00154 00155 #ifdef depuration 00156 00157 for(NextSample = 0; NextSample < NSamples; NextSample++ ) 00158 { 00159 RPM[NextSample] = (muestras[NextSample] * FactorConvercion); 00160 Mbed.printf("Muestra %d : %d Pulses %0.3f RPM \n", NextSample, muestras[NextSample], RPM[NextSample]); wait_ms(10); 00161 00162 } 00163 #endif 00164 00165 #ifdef get_values 00166 Mbed.printf("\n\nVelocidad\tPulsos\tRPS\tRPM Obtenidos durante el Test: \n"); 00167 for(NextSample = 0; NextSample < NSamples; NextSample++ ) 00168 { 00169 RPM[NextSample] = (muestras[NextSample] * FactorConvercion); 00170 RPS[NextSample] = (muestras[NextSample]/ float(NPulsesEncoder * Ts)); 00171 } 00172 00173 for(NextSample = 0; NextSample < NSamples; NextSample++ ) 00174 { 00175 Mbed.printf("%d, %d, %.3f, %0.3f\n",PVelocidadAplicada[NextSample], muestras[NextSample], RPS[NextSample], RPM[NextSample]); wait_ms(5); 00176 00177 } 00178 /* 00179 00180 Mbed.printf("\nRPM Obtenidos durante el Test: \n"); 00181 for(NextSample = 0; NextSample < NSamples; NextSample++ ) 00182 { 00183 RPM[NextSample] = (muestras[NextSample] * FactorConvercion); 00184 Mbed.printf("%0.0f\n",RPM[NextSample]); wait_ms(1); 00185 00186 } 00187 */ 00188 #endif 00189 00190 00191 } 00192 00193 // Mbed.printf("End of Work!!...\n!"); 00194 00195 00196 00197 } 00198 }
Generated on Mon Nov 27 2023 02:43:39 by
1.7.2