PROCESAMIENTO_DIGITAL
/
Muestreo_Motor_CD_Tuzo_3
Control Motor CD
Fork of Muestreo_Motor_CD_Tuzo_2 by
main.cpp@1:f7fb4980557c, 2017-11-10 (annotated)
- Committer:
- Sumobot
- Date:
- Fri Nov 10 00:39:50 2017 +0000
- Revision:
- 1:f7fb4980557c
- Parent:
- 0:1c17e8f0fcc0
wijdweoi; ; ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sumobot | 0:1c17e8f0fcc0 | 1 | #include "mbed.h" |
Sumobot | 0:1c17e8f0fcc0 | 2 | #include "rtos.h" |
Sumobot | 0:1c17e8f0fcc0 | 3 | |
Sumobot | 0:1c17e8f0fcc0 | 4 | DigitalOut led1(LED1); |
Sumobot | 0:1c17e8f0fcc0 | 5 | DigitalOut led2(LED2); |
Sumobot | 0:1c17e8f0fcc0 | 6 | InterruptIn inter(p5); //Se declara la variable de la interrupción |
Sumobot | 0:1c17e8f0fcc0 | 7 | DigitalOut senal(p7); //Se declara el pin el cual insertará la señal al pin de la interrupción |
Sumobot | 0:1c17e8f0fcc0 | 8 | DigitalIn pulsos(p6); //Pin en el cual se insertarán los pulsos del encoder. |
Sumobot | 0:1c17e8f0fcc0 | 9 | DigitalOut senal1(p21); |
Sumobot | 0:1c17e8f0fcc0 | 10 | DigitalIn opto(p23); |
Sumobot | 0:1c17e8f0fcc0 | 11 | AnalogIn pot(p19); |
Sumobot | 1:f7fb4980557c | 12 | DigitalOut prueba1(p10); |
Sumobot | 1:f7fb4980557c | 13 | InterruptIn prueba2(p9); |
Sumobot | 0:1c17e8f0fcc0 | 14 | |
Sumobot | 0:1c17e8f0fcc0 | 15 | Timer t; //Función que medirá el tiempo de operación |
Sumobot | 1:f7fb4980557c | 16 | //Thread thread; |
Sumobot | 1:f7fb4980557c | 17 | //Thread thread2; |
Sumobot | 1:f7fb4980557c | 18 | //Thread thread3; |
Sumobot | 1:f7fb4980557c | 19 | Thread thread4; //Función que se ejecutará en paralelo al programa principal |
Sumobot | 1:f7fb4980557c | 20 | Thread thread5; |
Sumobot | 1:f7fb4980557c | 21 | float velo=0, vRef=0, err=0, p=0, control=0, b, c, Control1=0; //Variable flotante en la cual se guardará la velocidad en rpm |
Sumobot | 0:1c17e8f0fcc0 | 22 | int iteraciones=0; //iteraciones=Variable que contará el número de pulsos |
Sumobot | 0:1c17e8f0fcc0 | 23 | |
Sumobot | 1:f7fb4980557c | 24 | /*void Prueba() { |
Sumobot | 1:f7fb4980557c | 25 | senal1=1; |
Sumobot | 1:f7fb4980557c | 26 | wait(.0003); |
Sumobot | 1:f7fb4980557c | 27 | senal1=0; |
Sumobot | 1:f7fb4980557c | 28 | led2=!led2; |
Sumobot | 1:f7fb4980557c | 29 | }*/ |
Sumobot | 1:f7fb4980557c | 30 | |
Sumobot | 0:1c17e8f0fcc0 | 31 | void controlador() { |
Sumobot | 0:1c17e8f0fcc0 | 32 | //Thread::wait(3000); |
Sumobot | 0:1c17e8f0fcc0 | 33 | while(1) { |
Sumobot | 1:f7fb4980557c | 34 | //prueba2.rise(&Prueba); |
Sumobot | 0:1c17e8f0fcc0 | 35 | } |
Sumobot | 0:1c17e8f0fcc0 | 36 | } |
Sumobot | 0:1c17e8f0fcc0 | 37 | |
Sumobot | 0:1c17e8f0fcc0 | 38 | void contador() { //Función ejecutada por la interrupción inter, la cual es un contador para la variable iteración |
Sumobot | 0:1c17e8f0fcc0 | 39 | iteraciones=iteraciones+1; |
Sumobot | 0:1c17e8f0fcc0 | 40 | } |
Sumobot | 0:1c17e8f0fcc0 | 41 | |
Sumobot | 1:f7fb4980557c | 42 | void pruebas() { |
Sumobot | 1:f7fb4980557c | 43 | while(1){ |
Sumobot | 1:f7fb4980557c | 44 | senal=pulsos.read(); //se filtra la señal del encoder, para limpar el ruido. |
Sumobot | 1:f7fb4980557c | 45 | } |
Sumobot | 1:f7fb4980557c | 46 | } |
Sumobot | 1:f7fb4980557c | 47 | |
Sumobot | 0:1c17e8f0fcc0 | 48 | void filtro() { |
Sumobot | 0:1c17e8f0fcc0 | 49 | while(1){ |
Sumobot | 1:f7fb4980557c | 50 | prueba1=opto.read(); //se filtra la señal del encoder, para limpar el ruido. |
Sumobot | 0:1c17e8f0fcc0 | 51 | } |
Sumobot | 0:1c17e8f0fcc0 | 52 | } |
Sumobot | 0:1c17e8f0fcc0 | 53 | |
Sumobot | 0:1c17e8f0fcc0 | 54 | void interr() { |
Sumobot | 0:1c17e8f0fcc0 | 55 | inter.rise(&contador); //Se declara la interrupción |
Sumobot | 0:1c17e8f0fcc0 | 56 | } |
Sumobot | 0:1c17e8f0fcc0 | 57 | |
Sumobot | 1:f7fb4980557c | 58 | /*void vel() { //Funcion Se convierte de iteraciones a rpmn vel, ejecutada por el hilo secundario |
Sumobot | 0:1c17e8f0fcc0 | 59 | while (1) { //Se ejecuta siempre |
Sumobot | 0:1c17e8f0fcc0 | 60 | t.reset(); //Se inicializa el timer |
Sumobot | 0:1c17e8f0fcc0 | 61 | Thread::wait(1000); //Se espera 1 segundo |
Sumobot | 0:1c17e8f0fcc0 | 62 | printf("n vale: %d\n\r", iteraciones); //Se imprime el número de iteraciones para monitorear |
Sumobot | 0:1c17e8f0fcc0 | 63 | velo= iteraciones*60*t.read(); //Se convierte de iteraciones a rpm |
Sumobot | 0:1c17e8f0fcc0 | 64 | printf("La velocidad en rpm es: %f\n\r", velo); //Se imprimen las rpm |
Sumobot | 0:1c17e8f0fcc0 | 65 | printf("El Pot es: %f\n\r", pot.read()); |
Sumobot | 0:1c17e8f0fcc0 | 66 | //printf("el tiempo es: %f\n\r", t.read()); //Se imprime el tiempo de ejecución para monitorear |
Sumobot | 0:1c17e8f0fcc0 | 67 | iteraciones=0; //Se comienza la cuenta de iteraciones de 0, para el siguiente ciclo |
Sumobot | 0:1c17e8f0fcc0 | 68 | } |
Sumobot | 1:f7fb4980557c | 69 | }*/ |
Sumobot | 0:1c17e8f0fcc0 | 70 | |
Sumobot | 0:1c17e8f0fcc0 | 71 | int main() { |
Sumobot | 0:1c17e8f0fcc0 | 72 | //vRef=600; //Hilo principal |
Sumobot | 1:f7fb4980557c | 73 | //t.start(); //Se inicializa el timer |
Sumobot | 1:f7fb4980557c | 74 | //thread.start(vel); //Se inicializa el hilo secundario |
Sumobot | 1:f7fb4980557c | 75 | //thread2.start(filtro); |
Sumobot | 1:f7fb4980557c | 76 | //thread3.start(interr); |
Sumobot | 0:1c17e8f0fcc0 | 77 | thread4.start(controlador); |
Sumobot | 1:f7fb4980557c | 78 | thread5.start(filtro); |
Sumobot | 1:f7fb4980557c | 79 | |
Sumobot | 1:f7fb4980557c | 80 | while(1){ |
Sumobot | 1:f7fb4980557c | 81 | led1=!led1; |
Sumobot | 1:f7fb4980557c | 82 | |
Sumobot | 1:f7fb4980557c | 83 | } |
Sumobot | 1:f7fb4980557c | 84 | } |
Sumobot | 0:1c17e8f0fcc0 | 85 | //p=0.4295; |
Sumobot | 0:1c17e8f0fcc0 | 86 | /*while(1) { |
Sumobot | 0:1c17e8f0fcc0 | 87 | err= vRef-velo; |
Sumobot | 0:1c17e8f0fcc0 | 88 | control= p*err; // Rango de error es de 0 a 1500; cuando el error es 1500 el angulo debe ser |
Sumobot | 0:1c17e8f0fcc0 | 89 | if(err<0){ |
Sumobot | 0:1c17e8f0fcc0 | 90 | led1=!led1; |
Sumobot | 0:1c17e8f0fcc0 | 91 | } |
Sumobot | 0:1c17e8f0fcc0 | 92 | if(err>1000){ |
Sumobot | 0:1c17e8f0fcc0 | 93 | led2=!led2; |
Sumobot | 0:1c17e8f0fcc0 | 94 | } |
Sumobot | 0:1c17e8f0fcc0 | 95 | if(err>=0 && err<600){ |
Sumobot | 0:1c17e8f0fcc0 | 96 | Control1=control/257.7; |
Sumobot | 0:1c17e8f0fcc0 | 97 | } |
Sumobot | 0:1c17e8f0fcc0 | 98 | if(err==600){ |
Sumobot | 0:1c17e8f0fcc0 | 99 | |
Sumobot | 0:1c17e8f0fcc0 | 100 | } |
Sumobot | 0:1c17e8f0fcc0 | 101 | if(err>600 && err<1000){ |
Sumobot | 0:1c17e8f0fcc0 | 102 | |
Sumobot | 0:1c17e8f0fcc0 | 103 | } */ |
Sumobot | 1:f7fb4980557c | 104 |