This document contains 5 problems related to motor control using a PID controller. It discusses using a diode to short current spikes when a motor is turned off, how hysteresis works in a comparator circuit, the tasks used in a TRT controller, shared variables between tasks, and setting the timer period to measure motor rotations at 1000 RPM.
1. Charles Moyes (cwm55) and Mengxiang Jiang (mj294)
ECE 4760
Assignment #4
Problem 1
The diode placed across the motor shorts out spikes when the motor is turned off.
The inductor opposes a change in current because of Lenz’s Law:
di
v=L
dt
During the rising edge of the PWM cycle, the change in current is positive, and so the inductor opposes
the current in the opposite/negative direction.
During the falling edge of the PWM cycle, the change in current is negative, and so the inductor opposes
the current in the opposite/positive direction.
During the “on” and “off” portions of the PWM cycle, there is no change in current so there is no
inductive opposition to the current.
When the motor is turned off by the falling edge of the PWM cycle, the inductor opposes the current
change with current in the opposite (positive) direction. This current will be forward-biased against the
diode, and so the current spike will be shorted out.
Problem 2
The 10K potentiometer sets the comparator threshold, the 1M resistor sets the hysteresis and may need to
be adjusted.
Hysteresis makes the rising/falling edges of the waveform less sensitive to noise. The circuit acts like a
Schmitt trigger, preventing the comparator from rapidly switching on and off. The output of the LM358
opamp is fed back into its positive input, causing rapid changes in voltage to be smoothed, because the
comparator’s input is now a combination of the input state and the output state. As a result, there are now
two separate thresholds for the positive output state and the negative output state respectively.
Problem 3
TRT Task 1 interacts with the PC keyboard to take user input and set up PID parameters. It reads in
strings from the UART using fscanf(), parses them into command/value pairs, and updates the appropriate
PID state variables {Kp , Ki , Kd } accordingly.
TRT Task 2 runs the PID control loop about 50 times/sec using speed measurements from an ISR. The
following calculation is computed in the loop to update the PWM rate:
t
d
u(t) = M V (t) = Kp e(t) + Ki e(τ ) dτ + Kd e(t)
0 dt
where M V is the manipulated variable (the PWM rate), t is the present time, and e = SP − P V (SP is
the set point (desired rotation rate), and P V is the current rotation rate as measured by Timer 2). The
state variables are set by Task 1 as discussed earlier.
TRT Task 3 runs the LCD and updates it about 5/second. Task 3 displays the rotation rate on the
LCD using the string formatting sprintf() function and the LCD library functions.
An external interrupt ISR uses Timer 2 to measure the rotation time.
1
2. Problem 4
Shared variables between each task include the PID parameters (which may be changed by the keyboard
task Task 1, along with the rotation rate, which is gathered from the Timer 2 ISR and read by the LCD
task Task 3 and the PID controller task Task 2. The PID parameters include the proportional gain Kp ,
the integral gain Ki , and the derivative gain Kd .
Problem 5
The period for a motor rotation at 1000 RPM is:
60
≈ 0.06 seconds
1000
Therefore, we need the timer to not overflow within 0.06 seconds. Running at full rate, Timer 2 has an
overflow period of:
216
= 0.004 seconds
16000000
Solving for the minimum clock divider needed to maintain this rate gives:
216 · x
= 0.06 seconds ⇒ x 14.65
16000000
This tells us that a clock divider of 32 should suffice for Timer 2 to measure rotations at this rate.
2