M4. Position Control For DC Servo Motors
M4. Position 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 to read encoder signals and implement
the PI/PD controller.
✓ Verify position control of a DC servo motor in a real model.
4.2. One axis servo system using dc servo motors
4.2.1 DC motor dynamics
𝐽𝑀
𝑇𝑀
107
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
108
G p (s)
yd (mm)
pwm y (mm)
Gc1 ( s ) Gc 2 ( s ) GH ( s ) GM ( s ) GL ( s )
0 − 100%
velocity loop
position loop
109
Fig. 4.7: Pulse diagram of 2x encoding
➢ 4X Encoding: using 2 external interrupts for 2 channels A and B
110
Fig. 4.9: The state transition diagram
The figure below (Fig. 4.10) is the code example of 4x encoder reading for channel
A. To complete reading encoder in 4x mode, we have to add another external interrupt
for channel B, the code for channel B is almost similar.
Note that: - Channel A is connected to GPIOB, PIN 4
- Channel B is connected to GPIOB, PIN 6
4.3.2. Calculating velocity/position
We use the following equation for estimating velocity of the motor:
60 CountValue
= (RPM) (4.6)
T MaxCnt
where, CountValue is the number of pulses counted in T (s). T is also timer interrupt
for reading encoder, and in this experiment, T is configured as 0.005 (s). MaxCnt equals
to encoder resolution multiplied by its mode (x1, x2 or x4).
111
Fig. 4.10: Code example of channel A encoding in 4x mode
From Fig. (4.10), we can see that PosCnt is the number of rounds of the motor at the
moment. CountValue is the number of pulses of the present rotation, CountValue will
be reset when the rotation is completed. Therefore, we can calculate the position of the
motor as follow:
CountValue
pos = PosCnt 2 + 2 (rad ) (4.7)
MaxCnt
Based on the rotation of the motor (rad), Eq. (4.7), students have to calculate the
translation of the load (mm).
4.4. PID controller
112
4.4.1 PID calculation
The mathematic equation of the PID controller:
t
de(t)
u(t) = K P e ( t ) + K I e(t)d(t) + K D (4.8)
0
dt
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.9)
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)
113
k
u Ik = K I Tei = u kI −1 + K ITek (4.12)
i =1
c) Trapezoidal approximation:
k
ei−1 + ei e +e
u Ik = KI T 2
= u kI −1 + K IT k −1 k
2
(4.14)
i =1
Code example:
114
u D f (k) = (1 − )u D f (k − 1) + u D (k) (4.16)
4.4.3 Anti-windup
PID
e u û
e
Ki
edt
ereset
Kb
From the block, we draw out the equation to calculate the anti-windup for I term
t
u (t) = K Ie( ) + K be reset ( ) d
I
115
Fig 4.17: Schematic of all I/O pinouts
Programmer/Debugger
116
Fig 4.19: H-bridge pinouts
Specifications:
Investigating the components and hardware connection of the system, each group
has to draw its schematic and include it in the report.
117
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.
118
Step 3: SWD configuration for programming
119
PWM settings: A PWM channel is used for the H-bridge. In this case, TIM3_CH2
(PC7) is configured as the following figures:
120
USART settings: Using UART to communicate with computer
121
Timer Interrupt: Timer 4 is adopted to configure a cyclic interrupt 5 (ms). Using
Eq. 3.17 to calculate the setting parameters as follow
122
Step 4: Code generation
123
✓ 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 functions of the system including H-bridge and the motor
(similar to the experiment of velocity control) and the load.
✓ Write a PI algorithm with anti-windup (similar to the experiment of velocity
control) for the inner loop (velocity control loop) and PD controller with low pass
filter for the outer loop (position control loop).
3.6. Report
Student have to show all their results including the hardware (schematic), obtained
transfer functions, calculations for PI/PD parameters, the code and the system
responses.
124
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;
}
// Ham ngat Uart
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
uint8_t i;
if(huart->Instance == USART1) { //uart1
125
// 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:
break;
}
HAL_UART_Receive_IT(&huart1,(uint8_t*)Rx_data,1);
}
}
126