mc_control.c File Reference

#include "config.h"
#include "mc_control.h"
#include "mc_interface.h"
#include "dac\dac_drv.h"

Include dependency graph for mc_control.c:

Go to the source code of this file.

Defines

#define K_DUTY   1

Functions

void duty_cycle_filter (void)
U8 mc_control_current (U8 cur_cmd)
 use to control current , current regulation loop need parameter : Kp_cur, Ki_cur ,Kd_cur and K_cur_scal in config_motor.h need to call in Te ms
U8 mc_control_speed (U8 speed_cmd)
 use to control speed , speed regulation loop need parameter : Kp_speed, Ki_speed ,Kd_speed and K_speed_scal in config_motor.h need to call in Te ms
U8 mc_get_duty_cycle ()
 set type of regulation
void mc_regulation_loop ()
 launch speed control or no regulation
void mc_set_current_loop ()
 set type of regulation
void mc_set_open_loop ()
 set type of regulation
void mc_set_speed_loop ()
 set type of regulation

Variables

S16 cur_der = 0
S16 cur_derivative = 0
S16 cur_error = 0
 Error calculation.
S16 cur_integ = 0
S16 cur_integral = 0
S16 cur_proportional = 0
U8 duty_cycle = 0
 Parameter to set PWM Duty Cycle after regulation calculation.
U8 duty_cycle_filter_presc = 0
 Reference before filtering.
U8 duty_cycle_reference = 0
S16 last_cur_error = 0
 Variable for the last error.
S16 last_speed_error = 0
 Variable for the last error.
U8 regulation_type = OPEN_LOOP
 Define the type of regulation (OPEN_LOOP or CLOSE_LOOP).
S16 speed_der = 0
S16 speed_derivative = 0
S16 speed_error = 0
 Error calculation.
S16 speed_integ = 0
S16 speed_integral = 0
S16 speed_proportional = 0


Define Documentation

#define K_DUTY   1

Definition at line 71 of file mc_control.c.

Referenced by duty_cycle_filter().


Function Documentation

void duty_cycle_filter ( void   ) 

Definition at line 227 of file mc_control.c.

References duty_cycle, duty_cycle_filter_presc, duty_cycle_reference, and K_DUTY.

Referenced by main().

00228 {
00229   duty_cycle_filter_presc++;
00230   if   (duty_cycle_filter_presc == 30)
00231   {
00232     duty_cycle_filter_presc = 0;
00233   if (duty_cycle < duty_cycle_reference)
00234   {
00235     duty_cycle = duty_cycle + K_DUTY;
00236   }
00237   else
00238   {
00239     if (duty_cycle > duty_cycle_reference)
00240     {
00241       duty_cycle = duty_cycle - K_DUTY;
00242     }
00243   }
00244   }
00245 }

U8 mc_control_current ( U8  cur_cmd  ) 

use to control current , current regulation loop need parameter : Kp_cur, Ki_cur ,Kd_cur and K_cur_scal in config_motor.h need to call in Te ms

Returns:
value of current, duty cycle on 8 bits

Definition at line 142 of file mc_control.c.

References cur_error, cur_integ, cur_integral, cur_proportional, K_cur_scal, Ki_cur, Kp_cur, and mci_get_measured_current().

Referenced by mc_regulation_loop().

00143 {
00144   U8 Duty = 0;
00145   S32 increment = 0;
00146 
00147   // Error calculation
00148   cur_error = cur_cmd - (mci_get_measured_current());// value -255 <=> 255
00149 
00150   // proportional term calculation
00151   cur_proportional = Kp_cur*cur_error;
00152 
00153   // integral term calculation
00154   cur_integral = cur_integral + cur_error;
00155 
00156   if(cur_integral >  255) cur_integral =  255;
00157   if(cur_integral < -255) cur_integral = -255;
00158 
00159   cur_integ = Ki_cur*cur_integral;
00160 
00161   // derivative term calculation
00162   /*cur_derivative = cur_error - last_cur_error;
00163 
00164   if(cur_derivative >  255) cur_derivative =  255;
00165   if(cur_derivative < -255) cur_derivative = -255;
00166 
00167   cur_der = Kd_cur*cur_derivative;
00168 
00169   last_cur_error = cur_error;*/
00170 
00171   // Duty Cycle calculation
00172   increment = cur_proportional + cur_integ;
00173   //increment += cur_der;
00174   increment = increment >> K_cur_scal;
00175 
00176   // Variable saturation
00177   if(increment >= (S16)(255)) Duty = 255;
00178   else
00179   {
00180     if(increment <= (S16)(0)) Duty =   0;
00181     else Duty = (U8)(increment);
00182   }
00183 
00184   // return Duty Cycle
00185   return Duty;
00186 }

Here is the call graph for this function:

U8 mc_control_speed ( U8  speed_cmd  ) 

use to control speed , speed regulation loop need parameter : Kp_speed, Ki_speed ,Kd_speed and K_speed_scal in config_motor.h need to call in Te ms

Returns:
value of speed, duty cycle on 8 bits

Definition at line 82 of file mc_control.c.

References K_speed_scal, Ki_speed, Kp_speed, mci_get_measured_speed(), speed_error, speed_integ, speed_integral, and speed_proportional.

Referenced by mc_regulation_loop().

00083 {
00084   U8 Duty = 0;
00085   S32 increment = 0;
00086 
00087   // Error calculation
00088   speed_error = speed_cmd - mci_get_measured_speed();// value -255 <=> 255
00089 
00090   // proportional term calculation
00091   speed_proportional = Kp_speed*speed_error;
00092 
00093   // integral term calculation
00094 /*  speed_integral = speed_integral + speed_error;
00095 
00096   if(speed_integral >  255) speed_integral =  255;
00097   if(speed_integral < -255) speed_integral = -255;
00098 
00099   speed_integ = Ki_speed*speed_integral;
00100 */
00101   speed_integral = Ki_speed*(speed_integral + speed_error);
00102 
00103   if(speed_integral >  255) speed_integral =  255;
00104   if(speed_integral < -255) speed_integral = -255;
00105 
00106   // derivative term calculation
00107   /*speed_derivative = speed_error - last_speed_error;
00108 
00109   if(speed_derivative >  255) speed_derivative =  255;
00110   if(speed_derivative < -255) speed_derivative = -255;
00111 
00112   speed_der = Kd_speed*speed_derivative;
00113 
00114   last_speed_error = speed_error;*/
00115 
00116   // Duty Cycle calculation
00117   increment = speed_proportional + speed_integ;
00118   //increment += speed_der;
00119   increment = increment >> K_speed_scal;
00120 
00121   // Variable saturation
00122   if(increment >= (S16)(255)) Duty = 255;
00123   else
00124   {
00125     if(increment <= (S16)(0)) Duty =   0;
00126     else Duty = (U8)(increment);
00127   }
00128 
00129   // return Duty Cycle
00130   return Duty;
00131 }

Here is the call graph for this function:

U8 mc_get_duty_cycle (  ) 

set type of regulation

Definition at line 222 of file mc_control.c.

References duty_cycle.

Referenced by main().

00223 {
00224   return duty_cycle;
00225 }

void mc_regulation_loop (  ) 

launch speed control or no regulation

Postcondition:
new duty cycle on PWM

Definition at line 196 of file mc_control.c.

References CURRENT_LOOP, duty_cycle_reference, mc_control_current(), mc_control_speed(), mc_get_potentiometer_value(), mci_get_ref_speed(), OPEN_LOOP, regulation_type, and SPEED_LOOP.

Referenced by main(), mci_retry_run(), and mci_run().

00197 {
00198   switch(regulation_type)
00199   {
00200     case OPEN_LOOP     : duty_cycle_reference = mci_get_ref_speed();break;
00201     case SPEED_LOOP    : duty_cycle_reference = mc_control_speed(2*mci_get_ref_speed());break;
00202     case CURRENT_LOOP  : duty_cycle_reference = mc_control_current(mc_get_potentiometer_value());break;
00203     default : break;
00204   }
00205 }

Here is the call graph for this function:

void mc_set_current_loop (  ) 

set type of regulation

Definition at line 218 of file mc_control.c.

References CURRENT_LOOP, and regulation_type.

void mc_set_open_loop (  ) 

set type of regulation

Postcondition:
Open loop regulation Set

Definition at line 210 of file mc_control.c.

References OPEN_LOOP, and regulation_type.

void mc_set_speed_loop (  ) 

set type of regulation

Definition at line 214 of file mc_control.c.

References regulation_type, and SPEED_LOOP.


Variable Documentation

S16 cur_der = 0

Definition at line 69 of file mc_control.c.

Definition at line 68 of file mc_control.c.

Error calculation.

Definition at line 63 of file mc_control.c.

Referenced by mc_control_current().

Definition at line 66 of file mc_control.c.

Referenced by mc_control_current().

Definition at line 65 of file mc_control.c.

Referenced by mc_control_current().

Definition at line 67 of file mc_control.c.

Referenced by mc_control_current().

Parameter to set PWM Duty Cycle after regulation calculation.

Definition at line 48 of file mc_control.c.

Referenced by duty_cycle_filter(), main(), and mc_get_duty_cycle().

Reference before filtering.

Definition at line 51 of file mc_control.c.

Referenced by duty_cycle_filter().

Definition at line 50 of file mc_control.c.

Referenced by duty_cycle_filter(), main(), and mc_regulation_loop().

Variable for the last error.

Definition at line 64 of file mc_control.c.

Variable for the last error.

Definition at line 55 of file mc_control.c.

U8 regulation_type = OPEN_LOOP

Define the type of regulation (OPEN_LOOP or CLOSE_LOOP).

Definition at line 49 of file mc_control.c.

Referenced by mc_regulation_loop(), mc_set_current_loop(), mc_set_open_loop(), and mc_set_speed_loop().

Definition at line 60 of file mc_control.c.

Definition at line 59 of file mc_control.c.

Error calculation.

Definition at line 54 of file mc_control.c.

Referenced by mc_control_speed().

Definition at line 57 of file mc_control.c.

Referenced by mc_control_speed().

Definition at line 56 of file mc_control.c.

Referenced by mc_control_speed().

Definition at line 58 of file mc_control.c.

Referenced by mc_control_speed().


Generated on Wed Oct 22 15:03:59 2008 for AVR172 : Atmel BLDC control on ATAVRMC310 with ATmega32M1 by  doxygen 1.5.7.1