00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 #ifdef __ICCAVR__ // IAR C Compiler 00012 #include "config.h" 00013 #include "inavr.h" 00014 #endif 00015 00016 #ifdef __GNUC__ // GNU C Compiler 00017 #include "config_for_gcc.h" 00018 #endif 00019 00020 00021 /* Speed control variables */ 00022 S16 speed_error=0; 00023 S16 speed_integral = 0; 00024 S16 speed_integ = 0; 00025 S16 speed_proportional = 0; 00026 00027 /**************************************************************************************/ 00028 /* Speed Control */ 00029 /**************************************************************************************/ 00035 U16 mc_control_speed_16b(U16 speed_ref , U16 speed_measure) 00036 { 00037 U16 result = 0; 00038 S16 increment = 0; 00039 00040 // Error calculation 00041 // speed_error = speed_ref - speed_measure/2 ; 00042 speed_error = speed_ref - speed_measure; 00043 00044 // proportional term calculation : Kp= 7/64=0.1 00045 speed_proportional = speed_error * 4; 00046 00047 // integral term calculation 00048 speed_integral = speed_integral + speed_error; 00049 00050 // speed integral saturation 00051 if(speed_integral > 32000) speed_integral = 32000; 00052 if(speed_integral < -32000) speed_integral = -32000; 00053 00054 speed_integ = (speed_integral - speed_integral/8 + speed_integral/32) / 16 ; 00055 00056 // amplitude calculation 00057 increment = speed_proportional + speed_integ; 00058 00059 // saturation of the PI output 00060 if (increment > 0) 00061 { 00062 if (increment <= (S16)(MAX_AMPLITUDE)) result = (S16)increment ; 00063 else result = MAX_AMPLITUDE ; 00064 } 00065 else result = 0; 00066 00067 // return Duty Cycle 00068 return result; 00069 } 00070 00071 00072 00073
1.5.3