| BLDC control on ATAVRMC303 with ATxMega128A1

Functions
mc_control.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Functions
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 More...
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 More...
U8 | mc_get_Duty_Cycle ()
| set type of regulation More...
void | mc_regulation_loop ()
| launch speed control or no regulation More...
void | mc_set_Current_Loop ()
| set type of regulation More...
void | mc_set_Open_Loop ()
| set type of regulation More...
void | mc_set_Speed_Loop ()
| set type of regulation More...

## Function Documentation

## ◆ mc_control_current()

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 103 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().

104 {
105 U8 Duty = 0;
106 S32 increment = 0;
107
108 // Error calculation
109 cur_error = cur_cmd - (mci_get_measured_current());// value -255 <=> 255
110
111 // proportional term calculation
112 cur_proportional = Kp_cur*cur_error;
113
114 // integral term calculation
115 cur_integral = cur_integral + cur_error;
116
117 if(cur_integral > 255) cur_integral = 255;
118 if(cur_integral < -255) cur_integral = -255;
119
120 cur_integ = Ki_cur*cur_integral;
121
122 // derivative term calculation
123 /*cur_derivative = cur_error - last_cur_error;
124
125 if(cur_derivative > 255) cur_derivative = 255;
126 if(cur_derivative < -255) cur_derivative = -255;
127
128 cur_der = Kd_cur*cur_derivative;
129
130 last_cur_error = cur_error;*/
131
132 // Duty Cycle calculation
133 increment = cur_proportional + cur_integ;
134 //increment += cur_der;
135 increment = increment >> K_cur_scal;
136
137 // Variable saturation
138 if(increment >= (S16)(255)) Duty = 255;
139 else
140 {
141 if(increment <= (S16)(0)) Duty = 0;
142 else Duty = (U8)(increment);
143 }
144
145 // return Duty Cycle
146 return Duty;
147 }
mci_get_measured_current
U16 mci_get_measured_current(void)
Get the current measured in the motor.
Definition: mc_interface.c:179
cur_integ
S16 cur_integ
Definition: mc_control.c:33
Ki_cur
#define Ki_cur
Definition: config_motor.h:62
cur_error
S16 cur_error
Error calculation.
Definition: mc_control.c:30
Kp_cur
#define Kp_cur
Definition: config_motor.h:61
K_cur_scal
#define K_cur_scal
Definition: config_motor.h:75
cur_proportional
S16 cur_proportional
Definition: mc_control.c:34
cur_integral
S16 cur_integral
Definition: mc_control.c:32
Here is the call graph for this function:

## ◆ mc_control_speed()

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 48 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().

49 {
50 U8 Duty = 0;
51 S32 increment = 0;
52
53 // Error calculation
54 speed_error = speed_cmd - mci_get_measured_speed();// value -255 <=> 255
55
56 // proportional term calculation
57 speed_proportional = Kp_speed*speed_error;
58
59 // integral term calculation
60 speed_integral = speed_integral + speed_error;
61
62 if(speed_integral > 255) speed_integral = 255;
63 if(speed_integral < -255) speed_integral = -255;
64
65 speed_integ = Ki_speed*speed_integral;
66
67 // derivative term calculation
68 /*speed_derivative = speed_error - last_speed_error;
69
70 if(speed_derivative > 255) speed_derivative = 255;
71 if(speed_derivative < -255) speed_derivative = -255;
72
73 speed_der = Kd_speed*speed_derivative;
74
75 last_speed_error = speed_error;*/
76
77 // Duty Cycle calculation
78 increment = speed_proportional + speed_integ;
79 //increment += speed_der;
80 increment = increment >> K_speed_scal;
81
82 // Variable saturation
83 if(increment >= (S16)(255)) Duty = 255;
84 else
85 {
86 if(increment <= (S16)(0)) Duty = 0;
87 else Duty = (U8)(increment);
88 }
89
90 // return Duty Cycle
91 return Duty;
92 }
speed_integ
S16 speed_integ
Definition: mc_control.c:24
Ki_speed
#define Ki_speed
Definition: config_motor.h:57
speed_error
S16 speed_error
Error calculation.
Definition: mc_control.c:21
Kp_speed
#define Kp_speed
Definition: config_motor.h:56
K_speed_scal
#define K_speed_scal
Definition: config_motor.h:74
speed_integral
S16 speed_integral
Definition: mc_control.c:23
mci_get_measured_speed
U8 mci_get_measured_speed(void)
Measured of speed.
Definition: mc_interface.c:169
speed_proportional
S16 speed_proportional
Definition: mc_control.c:25
Here is the call graph for this function:

## ◆ mc_get_Duty_Cycle()

U8 mc_get_Duty_Cycle | ( |  | ) |

set type of regulation

Precondition
  none

Postcondition
  Close loop regulation Set

Definition at line 197 of file mc_control.c.

References duty_cycle.

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

198 {
199 return duty_cycle;
200 }
duty_cycle
U8 duty_cycle
Parameter to set PWM Duty Cycle after regulation calculation.
Definition: mc_control.c:17

## ◆ mc_regulation_loop()

void mc_regulation_loop | ( |  | ) |

launch speed control or no regulation

Precondition
  none

Postcondition
  new duty cycle on PWM

Definition at line 159 of file mc_control.c.

References CURRENT_LOOP, duty_cycle, mc_control_current(), mc_control_speed(), mc_get_motor_speed(), mc_get_potentiometer_value(), OPEN_LOOP, regulation_type, and SPEED_LOOP.

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

160 {
161 switch(regulation_type)
162 {
163 case OPEN_LOOP : duty_cycle = mc_get_motor_speed();break;
164 case SPEED_LOOP : duty_cycle = mc_control_speed(mc_get_motor_speed());break;
165 case CURRENT_LOOP : duty_cycle = mc_control_current(mc_get_potentiometer_value());break;
166 default : break;
167 }
168 }
duty_cycle
U8 duty_cycle
Parameter to set PWM Duty Cycle after regulation calculation.
Definition: mc_control.c:17
mc_control_current
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
Definition: mc_control.c:103
SPEED_LOOP
#define SPEED_LOOP
Definition: config_motor.h:47
CURRENT_LOOP
#define CURRENT_LOOP
Definition: config_motor.h:48
regulation_type
U8 regulation_type
Define the type of regulation (OPEN_LOOP or CLOSE_LOOP)
Definition: mc_control.c:18
mc_control_speed
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
Definition: mc_control.c:48
OPEN_LOOP
#define OPEN_LOOP
Definition: config_motor.h:46
mc_get_motor_speed
U8 mc_get_motor_speed(void)
Definition: mc_interface.c:118
mc_get_potentiometer_value
U8 mc_get_potentiometer_value(void)
Get the potentiometer value.
Definition: mc_interface.c:199
Here is the call graph for this function:

## ◆ mc_set_Current_Loop()

void mc_set_Current_Loop | ( |  | ) |

set type of regulation

Precondition
  none

Postcondition
  Position loop regulation Set

Definition at line 189 of file mc_control.c.

References CURRENT_LOOP, and regulation_type.

189 {regulation_type = CURRENT_LOOP;}
CURRENT_LOOP
#define CURRENT_LOOP
Definition: config_motor.h:48
regulation_type
U8 regulation_type
Define the type of regulation (OPEN_LOOP or CLOSE_LOOP)
Definition: mc_control.c:18

## ◆ mc_set_Open_Loop()

void mc_set_Open_Loop | ( |  | ) |

set type of regulation

Precondition
  none

Postcondition
  Open loop regulation Set

Definition at line 175 of file mc_control.c.

References OPEN_LOOP, and regulation_type.

175 {regulation_type = OPEN_LOOP;}
regulation_type
U8 regulation_type
Define the type of regulation (OPEN_LOOP or CLOSE_LOOP)
Definition: mc_control.c:18
OPEN_LOOP
#define OPEN_LOOP
Definition: config_motor.h:46

## ◆ mc_set_Speed_Loop()

void mc_set_Speed_Loop | ( |  | ) |

set type of regulation

Precondition
  none

Postcondition
  Speed loop regulation Set

Definition at line 182 of file mc_control.c.

References regulation_type, and SPEED_LOOP.

182 {regulation_type = SPEED_LOOP;}
SPEED_LOOP
#define SPEED_LOOP
Definition: config_motor.h:47
regulation_type
U8 regulation_type
Define the type of regulation (OPEN_LOOP or CLOSE_LOOP)
Definition: mc_control.c:18