00001 /*This file has been prepared for Doxygen automatic documentation generation.*/ 00022 #include <inavr.h> 00023 #include <ioavr.h> 00024 #include "stdint.h" 00025 #include "pid.h" 00026 00032 00033 #define K_P 1.00 00035 #define K_I 0.00 00037 #define K_D 0.00 00038 00041 struct GLOBAL_FLAGS { 00043 uint8_t pidTimer:1; 00044 uint8_t dummy:7; 00045 } gFlags = {0, 0}; 00046 00048 struct PID_DATA pidData; 00049 00056 00057 #define TIME_INTERVAL 157 00058 00061 #pragma vector = TIMER0_OVF_vect 00062 __interrupt void TIMER0_OVF_ISR( void ) 00063 { 00064 static uint16_t i = 0; 00065 if(i < TIME_INTERVAL) 00066 i++; 00067 else{ 00068 gFlags.pidTimer = TRUE; 00069 i = 0; 00070 } 00071 } 00072 00075 void Init(void) 00076 { 00077 pid_Init(K_P * SCALING_FACTOR, K_I * SCALING_FACTOR , K_D * SCALING_FACTOR , &pidData); 00078 00079 // Set up timer, enable timer/counte 0 overflow interrupt 00080 TCCR0A = (1<<CS00); 00081 TIMSK0 = (1<<TOIE0); 00082 TCNT0 = 0; 00083 } 00084 00090 int16_t Get_Reference(void) 00091 { 00092 return 8; 00093 } 00094 00099 int16_t Get_Measurement(void) 00100 { 00101 return 4; 00102 } 00103 00109 void Set_Input(int16_t inputValue) 00110 { 00111 ; 00112 } 00113 00114 00117 void main(void) 00118 { 00119 int16_t referenceValue, measurementValue, inputValue; 00120 Init(); 00121 __enable_interrupt(); 00122 00123 while(1){ 00124 00125 // Run PID calculations once every PID timer timeout 00126 if(gFlags.pidTimer) 00127 { 00128 referenceValue = Get_Reference(); 00129 measurementValue = Get_Measurement(); 00130 00131 inputValue = pid_Controller(referenceValue, measurementValue, &pidData); 00132 00133 Set_Input(inputValue); 00134 00135 gFlags.pidTimer = FALSE; 00136 } 00137 } 00138 } 00139