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 "string.h" 00003 00004 #define REFERENCEVOLTAGE 3.3 // Max Output voltage from DAC 00005 00006 AnalogIn y(PTB3); // Analog Input 00007 AnalogOut u(PTE30); // Analog Output 00008 Serial HC06(PTE0,PTE1); 00009 PwmOut led1(LED_RED); 00010 00011 int Kp, Ki, Kd, Sp; 00012 float pid,o,ai,ad,ap,med,err; 00013 float err_v; 00014 char buffer[6]; 00015 00016 /* Funciones */ 00017 void cleanBuffer(char *buffer, int count) { 00018 for(int i=0; i < count; i++) { 00019 buffer[i] = '\0'; 00020 } 00021 } 00022 00023 void PID(int Sp, int Kp, int Ki, int Kd) { 00024 00025 med = y.read()*999; // med = Ist-Wert (medido) 00026 err = (Sp-med); // err = Soll-Wert (Sp) - Ist-Wert (med) = Fehler/Reglerabweichung 00027 ap = Kp * err * 0.01f; // Proportional 00028 ai = (Ki * err * 0.01f) + ai; // Integral (aktueller Fehler wird immer weiter aufsummiert) 00029 ad = Kd * (err - err_v) * 0.01f; // Differential (groesse der Fehlerabweichung (= Geschwindigkeit der Aenderung) 00030 pid = (ap + ai + ad); 00031 00032 if(pid <= 0) { 00033 pid = 0; 00034 } 00035 if(pid > 999) { 00036 pid = 999; 00037 } 00038 00039 // Normalize PID-Output 00040 err_v = err; 00041 o = pid / 999; 00042 u.write(o); // Write PID-value to Analog Output 00043 00044 if(err > -2 && err < 2) { 00045 led1 = 0.9; 00046 } 00047 else { 00048 led1 = 1; 00049 } 00050 00051 HC06.printf("SetPoint: %d \r\n",Sp); 00052 HC06.printf("Entrada: %0.2f \r\n",med); 00053 HC06.printf("Error: %0.2f \r\n",err); 00054 HC06.printf("Salida: %0.2f \r\n",pid); 00055 HC06.printf("DAC-Output: %0.2f V \r\n",o); 00056 HC06.printf("Voltaje: %0.2f V \r\n",o*REFERENCEVOLTAGE); // Resulting voltage (0V ... 3.3V) 00057 HC06.printf("\r\n"); 00058 00059 wait_ms(500); 00060 } 00061 00062 00063 00064 int main() { 00065 00066 led1 = 1; 00067 HC06.baud(9600); 00068 00069 int readptr = 0; 00070 char c; 00071 00072 00073 HC06.printf("\r\nEnter SetPoint value (Sp): "); 00074 while( (c = HC06.getc()) != '\n') { // Get characters and write them to buffer[]-Chararray 00075 buffer[readptr++] = c; 00076 } 00077 Sp = atoi(buffer); // Convert string (= Char[]-Array) to integer 00078 readptr = 0; // Reset String-Characterindex 00079 cleanBuffer(buffer,6); // Set all string-characters to \0 that no false chars remain for next use of buffer 00080 00081 00082 HC06.printf("\r\nEnter Proportional-Factor value (Kp): "); 00083 while( (c = HC06.getc()) != '\n') { 00084 buffer[readptr++] = c; 00085 } 00086 Kp = atoi(buffer); 00087 readptr = 0; 00088 cleanBuffer(buffer,6); 00089 00090 00091 HC06.printf("\r\nEnter Integral-Factor value (Ki): "); 00092 while( (c = HC06.getc()) != '\n') { 00093 buffer[readptr++] = c; 00094 } 00095 Ki = atoi(buffer); 00096 readptr = 0; 00097 cleanBuffer(buffer,6); 00098 00099 00100 HC06.printf("\r\nEnter Differential-Factor value (Kd): "); 00101 while( (c = HC06.getc()) != '\n') { 00102 buffer[readptr++] = c; 00103 } 00104 Kd = atoi(buffer); 00105 readptr = 0; 00106 cleanBuffer(buffer,6); 00107 00108 HC06.printf("\r\nStarting PID-controlling to Sp = %d with Kp = %d / Ki = %d / Kd = %d\r\n", Sp, Kp, Ki, Kd); 00109 00110 while(1){ 00111 if (HC06.readable()) { // Wait for any key sent to get new Sp value 00112 HC06.getc(); // Erase first key from buffer (dummy signal-key typed to indicate the following new Sp-input) 00113 00114 HC06.printf("\r\n\r\n\r\nEnter a new SetPoint value (Sp): "); 00115 while( (c = HC06.getc()) != '\n') { 00116 buffer[readptr++] = c; 00117 } 00118 Sp = atoi(buffer); 00119 readptr = 0; 00120 cleanBuffer(buffer,6); 00121 00122 HC06.printf("\r\nSuccessfully changed Setpoint-Value to Sp = %d (= %0.2f V)\r\n\r\n", Sp, (Sp*REFERENCEVOLTAGE/999)); 00123 } 00124 00125 PID(Sp, Kp, Ki, Kd); 00126 } 00127 }
Generated on Tue Oct 25 2022 19:28:01 by
1.7.2