M4. Velocity Control For DC Servo Motors
M4. Velocity Control For DC Servo Motors
4.1 CONTENTS
✓ Empirical modeling of a single axis servo system.
✓ Using Matlab/Simulink to illustrate the system’s performance
✓ Programming using STM32F103
✓ Velocity control of a DC servo motor
𝐽𝑀
𝑇𝑀
126
The block diagram of a DC servo motor:
TL
u 1 i TM 1
Km
Ls + R Ls + R
Ke
Km
( s) Rb K
G ( s) = = (4.4)
U (s) L J ( e s + 1)( m s + 1)
s + 1 s + 1
R b
L
where, e = : electrical time constant (s)
R
J
m = : mechanical time constant (s)
b
normally, m e
127
Table 4.1. The characteristics of the DC servo motor
No. Parameters Symbol Units DCM50205
1 Continuous Torque (Max) TC N.m 0.25
2 Peak Torque (Stall) TPK N.m 1.59
4 Rated Speed SR rpm 3400
5 Rotor Inertia JM kg.m2 3.11 x 10-5
10 Rated Voltage E V 24
11 Rated Current I A 2.95
12 Torque Constant KT N.m/A 52 x 10-3
13 Resistance RT Ω 0.8
15 Peak Current (Stall) IP A 21.6
16 Encoder Resolution - Steps/rev. 1000
The rotary load with the parameters:
▪ Weight: m = 0.81774 kg
▪ Material: Steel CT3
▪ Radius: R =0.025 m
128
Fig. 4.5: Appearance of a DC servo motor and encoder output signals
130
Fig. 4.10: Code example of channel A encoding in 4x mode
When implementing the PID controller in practice, the input variable (error) is obtained
by sampling the plant’s output at the sample rate. Then, the PID algorithm is also
calculated at the same rate. At the step kth, we have:
u k = u Pk + u Dk + u Ik (4.8)
131
KP
e(t ) t u (t ) uˆ(t )
K I e( )d
0
de(t )
KD
dt
t t t
(k-1)T kT (k-1)T kT (k-1)T kT
a) b) c)
c) Trapezoidal approximation:
k
ei−1 + ei e +e
u Ik = K I T 2
= u kI −1 + K IT k −1 k
2
(4.13)
i =1
Code example:
132
Fig 4.13: Code example of a simple PID algorithm
4.4.2 Low pass filter for D term:
D
E (s) U D (s) 1 U f (s)
KDs
Ns + 1
Kb
From the block, we draw out the equation to calculate the anti-windup for I term
133
t
u (t) = K Ie( ) + K be reset ( ) d
I
N MCB
PC 5V DC 24V DC
GND 5V +5V
+24V GND 24V
PB6 PB4
EXTI9_5 EXTI4
GND 24V
encoder
H-Bridge
Specifications:
135
4.5.2 Microcontroller programming
Using STM32CUBEMX to create a project
Step 1: Create a project using STM32F103RCT
136
Step 2: Clock configuration. Note that it is only an example, students can set up clocks
with other parameters but in that case, from now on, all calculations related to clocks
have to be modified.
137
Enable external interrupt to read encoder
PWM settings: A PWM channel is used for the H-bridge. In this case, TIM3_CH2
(PC7) is configured as the following figures:
138
Timer_clock (APB1)
Timer_tick = (4.17)
Prescaller + 1
Timer_tick
f PWM = (4.18)
Counter Period + 1
Example: Timer_clock (APB1) = 24 Mhz (Clock configuration)
Prescaller = 11; Counter Period =100
24 106
Timer_tick = = 2 106 (Hz)
11 + 1
2 106
f PWM = 19.8 (KHz)
100 + 1
139
Timer Interrupt: Timer 4 is adopted to configure a cyclic interrupt 5 (ms). Using
Eq. 4.17 to calculate the setting parameters as follow
140
Enable TIM4 interrupt
141
142
4.5.3 Velocity control
Students have to modify the given code including:
✓ 2 external interrupts for encoder reading (x4 mode): EXTI4_IRQHandler,
EXTI9_5_IRQHandler. Using sample code in Fig. 4.12
✓ Velocity estimation in TIM4_IRQHandler using Eq. (4.19)
✓ HAL_UART_RxCpltCallback: based on the sample code to communicate with
Visual C# interface (appendix)
✓ Open the Visual studio program, choose parameters for RS232 connection. Note
that the BaudRate has to be compatible with the one already configured in
USART1.
✓ Identify the transfer function of the H-bridge and the motor by applying the pwm
signal 100% and plotting the velocity response. From the response, calculating
the parameters of the transfer function including gain (K) and time constant ( )
✓ Write a PID algorithm based on the sample code in Fig. 4.15. However, students
must improve the algorithm by adding the filter for D-term (Eq. 4.28) or anti-
windup for I-term (Eq. 4.29)
4.6. Report
Student have to show all their results including the obtained transfer function, PID
parameters, PID code and the controlled velocity response.
143
APPENDIX
USART1 COMUNICATION
int16_t DesiredSpeed;
char Rx_indx, Rx_Buffer[20],Rx_data[2];
float DesiredPos;
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1, (uint8_t*)&ch,1,100);
return ch;
}
switch(Rx_data[0]) {
/* dung dong co */
case 'e':
run =false;
break;
144
/* dong co chay */
case 'r':
run = true;
break;
case 'b':
// reset();
break;
case 's':
DesiredPos = atoi(Rx_Buffer);
memset(Rx_Buffer, 0, sizeof(Rx_Buffer));
Rx_indx = 0;
break;
case 'v':
DesiredSpeed = atoi(Rx_Buffer);
memset(Rx_Buffer, 0, sizeof(Rx_Buffer));
Rx_indx = 0;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
case '-':
Rx_Buffer[Rx_indx++] |= Rx_data[0];
break;
default:
145
break;
}
HAL_UART_Receive_IT(&huart1,(uint8_t*)Rx_data,1);
}
}
146