You are on page 1of 6

TEE 451 - Control Systems

Winter 2021

Instructor: Michael McCourt Assignment: Lab 3


Motor Control 1

1 Introduction to Motor Control Lab 1


Controlling the speed of a motor is one of the most important applications in control systems. Motor speed
control has applications in manufacturing, transportation, robotics, aerospace, and many other industries.
In this lab, you will control the speed of the motor measured in revolutions per minute (RPMs). We can
use two Arduino PWM signals and a motor driver in order to control the direction and magnitude of
the current through the motor. When we increase the current, the motor rotates more quickly. When
we supply a negative current, the motor rotates in the opposite direction as when a positive current is
supplied.
In order to implement feedback control, we need to measure the speed of the motor. One method to
do this is by using a rotary encoder. The rotary encoder works by measuring the rotation of the motor
and translating it into a digital signal. The signal is generated by a light wave passing through a series
of holes in a disc that rotates with the motor. When the light passes through the disc, the hole is lined
up with the light emitter. When the light does not pass through, it is not lined up with the hole. You
can read more about rotary encoders at the links below.
https://en.wikipedia.org/wiki/Rotary_encoder
https://howtomechatronics.com/tutorials/arduino/rotary-encoder-works-use-arduino/

There are actually two waves and two signals being generated simultaneously. There is a phase shift
between the two that allows us to identify the direction that the motor is rotating. For this lab, we will
control the motor speed in the positive direction.

2 Lab Safety
This lab has potential safety concerns due to electric current and the rotating motor. When the power
adapter is connected, make sure to not touch any wires or circuit components as there is a risk of electric
shock. Before touching circuit components, make sure to disconnect the power adapter. If you must
leave the room, you can unplug the power adapter which eliminates most risks. Finally, be careful when
handling parts with sharp edges including the pins on integrated circuits.

3 Motor Test Circuit


In this lab, you will use the L293D motor driver. While the Arduino is a useful tool, it cannot supply
enough current to power the motor. The motor driver acts as a relay that is enabled by the Arduino,
but the actual current through the motor is supplied by the external 9V power adapter. There are other
benefits to the circuit we will be using (Figure 1). For example, you can supply both positive and negative
currents by using two PWM signals from the Arduino. When the two PWM signals are the same (either

page 1 of 6
Control Systems Winter 2021

high or low) no current will flow through the motor. When the first PWM signal is high and the second
PWM signal is low, current will flow in one direction and the motor will rotate. When the first PWM
signal is low and the second PWM signal is high, current will flow in the opposite direction and the motor
will rotate in the opposite direction. You can find more information and the datasheet for the motor
driver at the following link.
https://www.adafruit.com/product/807

Figure 1: The L293D motor driver can be connected in this way to control the current through the motor.

1. Build the circuit in Figure 1 with the motor driver, four 1N4007 diodes, motor (indicated with an
M in a circle), and the 9V power adapter.
2. Connect Vcc1 to the 5V supply on the Arduino. Connect the Arduino ground to any ground
connection on the motor driver.
3. Also connect pin 1 to the 5V source coming from the Arduino. This is an enable input that must
be high to enable the motor driver.
4. Connect pins 2 and 7 to the PWM signals coming from the Arduino (Arduino pins D2 through
D13).
5. The two motor supply pins (- and +) are indicated in Figure 2. These should be connected to pins
3 and 6 of the motor driver.
6. Make sure to connect all pins as indicated. There are four pins that must be grounded. Verify that
all grounds are connected together in the circuit.

page 2 of 6
Control Systems Winter 2021

7. Connect Vcc2 to the 9V supply from the power adapter using the barrel jack to bare wire adapter.
The ground from this source must be connected to one of the ground connections on the driver.

8. With this circuit, the maximum current through the motor will be near 1A. It is important to never
touch the circuit components or wires when the power adapter is connected.

9. Attach jumper wires to the motor encoder wires as extensions to the pins. The motor encoder
requires 5V and ground (from the Arduino). It provides two encoder signals (A and B) that can
be connected to the digital pins on the Arduino (pins D2 to D13). You can see the encoder pins
labeled in Figure 2.

Figure 2: This image shows the wiring for the motor and encoder.

4 Reading the Encoder


For this lab, we will use the rotary encoder to measure the speed of the motor in RPMs. One method
of doing this involves counting the number of rising edges over some time period and then dividing by
the time elapsed. Based on the design of this particular encoder, there are 408 encoder periods in one
1
rotation. Alternatively, the rotational distance between two rising edges is 408 revolutions. The Arduino
provides an encoder library to simplify this process for you. Make sure that you have this library installed.
After you run the line a = arduino, note the communication port (com1, com2, etc.) and run the line
listArduinoLibraries in the command window. The function will print a cell array that should
contain the package RotaryEncoder if it is installed. Assuming that is installed, you can clear the
variable a and run the line:
a = arduino(’com1’,’Mega2560’,’Libraries’,listArduinoLibraries);
Make sure to change com1 to your particular communication channel. This may take a minute to run as
Matlab updates the libraries on the Arduino. If you do not have the encoder library listed, contact the
instructor for help on this. For more information about using the encoder library in Matlab, refer to the
link below.
https://www.mathworks.com/help/supportpkg/arduinoio/ug/control-rotary-encoder-knob.
html

1. Follow the instructions above to update the Adruino libraries.

2. Use Figure 2 to connect the +5V and ground from the Arduino to the encoder. Connect the encoder
signals (A and B) to two digital pins on the Arduino. In the example below this is D2 and D3.

page 3 of 6
Control Systems Winter 2021

3. Setup an encoder object by running the line:


encoder = rotaryEncoder(a,’D2’,’D3’,408)

4. You can test the encoder object by running readCount(encoder) and readSpeed(encoder).
Assuming the motor is not rotating, the speed will read zero RPMs.

5. Select two Arduino digital pins to be used as PWM pins to send signals to the motor driver. Use
the function configurePin() to set both pins to be a PWM signal.

6. Use the function writePWMDutyCycle() to set the PWM cycles for the two pins. Test setting one
pin high (1) and the other low (0) and note the direction of the motor rotation. Test the opposite
configuration and note that the motor rotates in the opposite direction.

7. For each motor direction, run readSpeed(encoder) to identify the direction of positive rotation.
It is recommended to use the positive rotational direction in this lab.

8. As a check on your work, with the max PWM signal supplied to the motor, you should see 80-120
RPMs for the motor.

5 Motor System Identification


A reasonably accurate model of the motor is a single integrator with a gain KG as shown in the equation
below. A more accurate model would have a single pole near zero, but the single integrator model is
accurate enough for our purposes. In order to identify the system model, we can collect data to estimate
the gain KG .
KG
G(s) =
s
Recall that the effective voltage applied to the motor can be found as the 9V supply multiplied by the
duty cycle. When 70% duty cycle is applied, the effective voltage is 9*0.7 = 6.3V. A slightly more accurate
calculation would account for the voltage drop across the diodes. As there are two diodes, the drop would
be 9-0.7*2 = 7.6V. Again, when 70% duty cycle is applied, the effective voltage is 7.6*0.7 = 5.32V.

1. Select three PWM duty cycles between 30% and 100%. For each case, you will run a test where
you will set one PWM signal to the chosen duty cycle and record the speed in RPMs in a vector for
about 10 seconds. Average this vector to get an approximate speed.

2. For each test, calculate the effective voltage applied to the motor.

3. For each test, calculate the gain KG as the average RPMs divided by the effective voltage.

4. These are three estimates of the system gain. You can average these to get a reasonable approxi-
mation of the system gain.

5. Save your calculations and explain them in your report.

6. When finished, make sure to set both Arduino PWM signals to 0% duty cycle.

page 4 of 6
Control Systems Winter 2021

6 Control Design and Simulation


In this part of the lab, you will design a proportional controller to meet a specific objective. You may
use any method you learned in this class, but explain your process in your report. Note that this closed
loop system will be a first order system. You will test this controller by simulating in Simulink with three
different test inputs.

1. Design a proportional controller that achieves a closed loop time constant of less than 0.2 seconds.

2. In Simulink, create a block representing the motor as a single integrator with a gain KG that you
found previously.

3. Add a gain block for the proportional controller and set the calculated value.

4. Create a feedback loop with a step input, summing block, and scope to show the output.

5. Test the feedback loop with a step input with desired final value of 60 RPM to verify that closed
loop control is working as intended.

6. Add a saturation block between the controller and plant that limits the applied voltage to ±9V.

7. At this point, you may want to change the gain KP to improve performance. If you do change the
gain, document your reasoning for doing so.

8. Simulate the control system with a reference step input with initial value 0 RPM and final value 60
RPM. Plot both the reference input and system output on the same set of axes and include in your
report.

9. Simulate the control system with a square wave input that alternates between 40 RPM and 80 RPM.
Adjust the period to make sure the output settles for 2-5 seconds before switching. Run for three
full periods, i.e. three times at each setpoint. Plot both the reference input and system output on
the same set of axes and include in your report.

10. Test with a ramp input that increases the desired RPM from 30 to 90 linearly over about 20 seconds.
When the desired RPMs hits 90, you can stop the test. Plot both the reference input and system
output on the same set of axes and include in your report.

7 Motor Control Physical Experiment


For this part of the lab, you will implement your proportional controller on the physical motor. You will
run the control loop with three different reference inputs. Be sure to save all data and figures from your
experimental results.

1. Implement your proportional controller in Matlab. Recall, this requires calculating the error in
RPMs and multiplying by the proportional gain used in simulation.

2. Convert the calculated voltage to a PWM duty cycle and set the Arduino PWM duty cycles appro-
priately. Include checks for out of range duty cycles to avoid errors.

3. Ensure all wires are connected correctly as you did earlier in this lab.

page 5 of 6
Control Systems Winter 2021

4. Run the control system with a step input with initial value 0 RPM and final value 60 RPM to
ensure that your controller works. If you would like to change your gain, you may, but you must
document the reasons for making the change. As a note, you may have some steady state error and
some output jitter due to noise. It is recommended to prioritize low noise jitter over low steady
state error. Whatever you decide, comment in your report.

5. Repeat the step input test and run for about 10 seconds. Collect the reference input and system
output, plot on the same set of axes, and include in your report.

6. Test with a square wave input that alternates between 40 RPM and 80 RPM. Adjust the timing to
allow the output to settle at each setpoint for 2-5 seconds before switching. Allow the simulation
to run for three full periods of the reference input. Collect the reference input and system output,
plot on the same set of axes, and include in your report.

7. Test with a ramp input that increases the desired RPM from 30 to 90 linearly over about 20 seconds.
Plot this data and include in your report.

8. Be sure to compare all test results to your simulation results. Use the language you are learning in
class including performance concepts, steady state error, sensitivity, noise, disturbance, etc.

8 Lab Report
This lab is the first part of a two part lab. You do not need to write and turn in a lab report for this lab.
Instead, you should document your observations and save your data. You will write a portion in your
next lab report that summarizes your work on this lab. You should especially focus on what you did for
system identification, control design, control simulation, and the physical control test. Make sure to save
all data, code, and figures that you generated in this lab. Your lab report will summarize what you did
for Lab 3 and Lab 4.

page 6 of 6

You might also like