![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
asdfhkjlk
Fork of Muestreo_Motor_CD_Tuzo_5 by
main.cpp@0:1c17e8f0fcc0, 2017-11-09 (annotated)
- Committer:
- Sumobot
- Date:
- Thu Nov 09 19:49:42 2017 +0000
- Revision:
- 0:1c17e8f0fcc0
- Child:
- 1:f7fb4980557c
Toques Toques
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 | 0:1c17e8f0fcc0 | 12 | |
Sumobot | 0:1c17e8f0fcc0 | 13 | Timer t; //Función que medirá el tiempo de operación |
Sumobot | 0:1c17e8f0fcc0 | 14 | Thread thread, thread2, thread3, thread4; //Función que se ejecutará en paralelo al programa principal |
Sumobot | 0:1c17e8f0fcc0 | 15 | float velo=0, vRef=0, err=0, p=0, control=0, b=0, c=0, Control1=0; //Variable flotante en la cual se guardará la velocidad en rpm |
Sumobot | 0:1c17e8f0fcc0 | 16 | int iteraciones=0; //iteraciones=Variable que contará el número de pulsos |
Sumobot | 0:1c17e8f0fcc0 | 17 | |
Sumobot | 0:1c17e8f0fcc0 | 18 | void controlador() { |
Sumobot | 0:1c17e8f0fcc0 | 19 | //Thread::wait(3000); |
Sumobot | 0:1c17e8f0fcc0 | 20 | while(1) { |
Sumobot | 0:1c17e8f0fcc0 | 21 | senal1=0; |
Sumobot | 0:1c17e8f0fcc0 | 22 | b = pot.read(); |
Sumobot | 0:1c17e8f0fcc0 | 23 | c = b*0.00833*1000; |
Sumobot | 0:1c17e8f0fcc0 | 24 | if (opto==1) |
Sumobot | 0:1c17e8f0fcc0 | 25 | { |
Sumobot | 0:1c17e8f0fcc0 | 26 | Thread::wait(c); |
Sumobot | 0:1c17e8f0fcc0 | 27 | senal1=1; |
Sumobot | 0:1c17e8f0fcc0 | 28 | Thread::wait(.3); |
Sumobot | 0:1c17e8f0fcc0 | 29 | senal1=0; |
Sumobot | 0:1c17e8f0fcc0 | 30 | Thread::wait(9-c); |
Sumobot | 0:1c17e8f0fcc0 | 31 | } |
Sumobot | 0:1c17e8f0fcc0 | 32 | } |
Sumobot | 0:1c17e8f0fcc0 | 33 | } |
Sumobot | 0:1c17e8f0fcc0 | 34 | |
Sumobot | 0:1c17e8f0fcc0 | 35 | void contador() { //Función ejecutada por la interrupción inter, la cual es un contador para la variable iteración |
Sumobot | 0:1c17e8f0fcc0 | 36 | iteraciones=iteraciones+1; |
Sumobot | 0:1c17e8f0fcc0 | 37 | } |
Sumobot | 0:1c17e8f0fcc0 | 38 | |
Sumobot | 0:1c17e8f0fcc0 | 39 | void filtro() { |
Sumobot | 0:1c17e8f0fcc0 | 40 | while(1){ |
Sumobot | 0:1c17e8f0fcc0 | 41 | senal=pulsos.read(); //se filtra la señal del encoder, para limpar el ruido. |
Sumobot | 0:1c17e8f0fcc0 | 42 | } |
Sumobot | 0:1c17e8f0fcc0 | 43 | } |
Sumobot | 0:1c17e8f0fcc0 | 44 | |
Sumobot | 0:1c17e8f0fcc0 | 45 | void interr() { |
Sumobot | 0:1c17e8f0fcc0 | 46 | inter.rise(&contador); //Se declara la interrupción |
Sumobot | 0:1c17e8f0fcc0 | 47 | } |
Sumobot | 0:1c17e8f0fcc0 | 48 | |
Sumobot | 0:1c17e8f0fcc0 | 49 | void vel() { //Funcion Se convierte de iteraciones a rpmn vel, ejecutada por el hilo secundario |
Sumobot | 0:1c17e8f0fcc0 | 50 | while (1) { //Se ejecuta siempre |
Sumobot | 0:1c17e8f0fcc0 | 51 | t.reset(); //Se inicializa el timer |
Sumobot | 0:1c17e8f0fcc0 | 52 | Thread::wait(1000); //Se espera 1 segundo |
Sumobot | 0:1c17e8f0fcc0 | 53 | printf("n vale: %d\n\r", iteraciones); //Se imprime el número de iteraciones para monitorear |
Sumobot | 0:1c17e8f0fcc0 | 54 | velo= iteraciones*60*t.read(); //Se convierte de iteraciones a rpm |
Sumobot | 0:1c17e8f0fcc0 | 55 | printf("La velocidad en rpm es: %f\n\r", velo); //Se imprimen las rpm |
Sumobot | 0:1c17e8f0fcc0 | 56 | printf("El Pot es: %f\n\r", pot.read()); |
Sumobot | 0:1c17e8f0fcc0 | 57 | //printf("el tiempo es: %f\n\r", t.read()); //Se imprime el tiempo de ejecución para monitorear |
Sumobot | 0:1c17e8f0fcc0 | 58 | iteraciones=0; //Se comienza la cuenta de iteraciones de 0, para el siguiente ciclo |
Sumobot | 0:1c17e8f0fcc0 | 59 | } |
Sumobot | 0:1c17e8f0fcc0 | 60 | } |
Sumobot | 0:1c17e8f0fcc0 | 61 | |
Sumobot | 0:1c17e8f0fcc0 | 62 | int main() { |
Sumobot | 0:1c17e8f0fcc0 | 63 | //vRef=600; //Hilo principal |
Sumobot | 0:1c17e8f0fcc0 | 64 | t.start(); //Se inicializa el timer |
Sumobot | 0:1c17e8f0fcc0 | 65 | thread.start(vel); //Se inicializa el hilo secundario |
Sumobot | 0:1c17e8f0fcc0 | 66 | thread2.start(filtro); |
Sumobot | 0:1c17e8f0fcc0 | 67 | thread3.start(interr); |
Sumobot | 0:1c17e8f0fcc0 | 68 | thread4.start(controlador); |
Sumobot | 0:1c17e8f0fcc0 | 69 | //p=0.4295; |
Sumobot | 0:1c17e8f0fcc0 | 70 | /*while(1) { |
Sumobot | 0:1c17e8f0fcc0 | 71 | err= vRef-velo; |
Sumobot | 0:1c17e8f0fcc0 | 72 | control= p*err; // Rango de error es de 0 a 1500; cuando el error es 1500 el angulo debe ser |
Sumobot | 0:1c17e8f0fcc0 | 73 | if(err<0){ |
Sumobot | 0:1c17e8f0fcc0 | 74 | led1=!led1; |
Sumobot | 0:1c17e8f0fcc0 | 75 | } |
Sumobot | 0:1c17e8f0fcc0 | 76 | if(err>1000){ |
Sumobot | 0:1c17e8f0fcc0 | 77 | led2=!led2; |
Sumobot | 0:1c17e8f0fcc0 | 78 | } |
Sumobot | 0:1c17e8f0fcc0 | 79 | if(err>=0 && err<600){ |
Sumobot | 0:1c17e8f0fcc0 | 80 | Control1=control/257.7; |
Sumobot | 0:1c17e8f0fcc0 | 81 | } |
Sumobot | 0:1c17e8f0fcc0 | 82 | if(err==600){ |
Sumobot | 0:1c17e8f0fcc0 | 83 | |
Sumobot | 0:1c17e8f0fcc0 | 84 | } |
Sumobot | 0:1c17e8f0fcc0 | 85 | if(err>600 && err<1000){ |
Sumobot | 0:1c17e8f0fcc0 | 86 | |
Sumobot | 0:1c17e8f0fcc0 | 87 | } */ |
Sumobot | 0:1c17e8f0fcc0 | 88 | } |