Julien VILLEMEJANE
/
PIMS2020_Pointage_v2
Pointage Laser - Manip PIMS avec PID numerique
main2.cpp
- Committer:
- villemejane
- Date:
- 2020-12-17
- Revision:
- 1:d2ed8dcf965a
- Parent:
- 0:44e787b4e1d0
- Child:
- 2:e9cff52e8ee2
File content as of revision 1:d2ed8dcf965a:
#include "mbed.h" #include "dsp.h" #define maxX 0.5 #define PER_ACQ 0.0001 #define PER_STEP 0.001 #define PER_LED 2000 #define MAX_CHAR 128 /* Modules pour Asservissement */ Ticker tik_asst; AnalogIn inX(PC_2); AnalogIn inY(PC_0); AnalogOut outX(PA_4); //AnalogOut outY(PA_5); DigitalOut out_led(D13); arm_pid_instance_f32 pidX; arm_pid_instance_f32 pidY; /* Modules pour communication avec Matlab */ Serial pc(USBTX, USBRX); //TX - transmission, RX - reception InterruptIn button (USER_BUTTON); /* Variables globales pour Asservissement */ double Kp = 1.0; /* Variables gloables pour Rampe / Step */ double valX; int signeX; double pasX = 0.01; /* Variables globales pour communication avec Matlab */ char g_value[MAX_CHAR]; int g_index=0; char g_ch; int g_t_temp=0; char data_ok = 0; int periode_led = PER_LED; /* Fonction d'interruption PID - X et Y */ void controlLoop(void) { out_led = 1; //Process the PID controller float outxx = arm_pid_f32(&pidX, (float) inX.read()-0.5); float outyy = arm_pid_f32(&pidY, (float) inY.read()-0.5); //Range limit the output if (outxx < -0.5) outxx = -0.5; else if (outxx > 0.5) outxx = 0.5; if (outyy < -0.5) outyy = -0.5; else if (outyy > 0.5) outyy = 0.5; //Set the new output duty cycle outX.write(outxx+0.5); //outY.write(outyy+0.5); out_led = 0; g_t_temp++; } /* Fonction d'interruption de test - Rampe */ void stepLoop(void){ out_led = 1; valX = valX + signeX * pasX; if(valX > maxX){ signeX = -1; } if(valX < 0.0){ signeX = 1; } outX.write(valX); out_led = 0; g_t_temp++; } /* Fonction d'interruption de test - NOTHING */ void noLoop(void){ g_t_temp++; } /* Fonction d'interruption pour la communication RS232 avec Matlab */ void IT_Rx_Matlab(){ tik_asst.detach(); g_ch = pc.getc(); // read it if ((g_index<MAX_CHAR-1) and (data_ok == 0)){ g_value[g_index]=g_ch; // put it into the value array and increment the index g_index++; } if(g_ch == '\n'){ g_value[g_index] = '\0'; g_index=0; data_ok = 1; } tik_asst.attach(&controlLoop, PER_ACQ); } /* Fonction d'interruption pour bouton */ void IT_button_pressed(void){ int k=0; do{ pc.putc(g_value[k]); k++; } while (g_value[k]!='\0'); // loop until the '\n' character } /* Fonction principale */ int main() { valX = 0; signeX = 1; pc.baud(9600); pc.attach(&IT_Rx_Matlab); strcpy(g_value, "Coucou"); g_value[6] = '\r'; g_value[7] = '\n'; //Initialize the PID instance structure pidX.Kp = 1.0; pidX.Ki = 0.001; pidX.Kd = 0.0; arm_pid_init_f32(&pidX, 1); pidY.Kp = 1.0; pidY.Ki = 0.001; pidY.Kd = 0.0; arm_pid_init_f32(&pidY, 1); //Run the PID control loop every 1ms // tik_asst.attach(&stepLoop, PER_STEP); button.fall(&IT_button_pressed); tik_asst.attach(&controlLoop, PER_ACQ); //tik_asst.attach(&noLoop, PER_ACQ); pc.printf("im here !!\r\n"); while (true) { if(g_t_temp>=periode_led) // LED blinking { g_t_temp=0; out_led = !out_led; } if(data_ok){ if(g_value[0] == 'P'){ // Mode PID periode_led = 10000; pc.printf("P_OK!"); } if(g_value[0] == 'S'){ // Mode STEP periode_led = 2000; pc.printf("S_OK!"); } data_ok = 0; g_index = 0; } } }