You are on page 1of 10

Proceedings of the ASME 2017 Dynamic Systems and Control Conference

DSCC2017
October 11-13, 2017, Tysons, Virginia, USA

DSCC2017-5140

TEACHING REAL-TIME CONTROL USING ARDUINO: TIMER ISR VS.


DELAYMICROSECONDS

Ryan W. Krauss

Associate Professor
Product Design and Manufactoring Engineering
Grand Valley State University
Grand Rapids, MI 49504
Email: kraussry@gvsu.edu

ABSTRACT Secondary Research Questions


Arduino microcontrollers are inexpensive and easy to pro- There are two secondary questions that this paper also ad-
gram, making them very popular among hobbyists and "mak- dresses. First, is there a faster way to print data to the Arduino se-
ers". Arduinos are also surprisingly capable when it comes to rial monitor than repetitively calling the Serial.print func-
creating real-time feedback control systems. This paper investi- tion once per variable. The other secondary question is how ac-
gates several facets of using Arduino microcontrollers to teach curately can an Arduino perform real-time control with digital
students to create real-time control systems. A simplified ap- update frequencies in the hundreds of Hz?
proach to enforcing the real-time execution of a control law is
introduced based on the delayMicroseconds function and
its accuracy is compared to the standard timer interrupt ap- Introduction
proach. Initial assessment data is presented on whether or not Arduino micro controllers are low-cost and easy to program,
the delayMicroseconds approach is easier for students to making them extremely popular among "makers" and hobbyists.
understand. The accuracy of the Arduino’s built-in timing func- There are many, many code examples and tutorials on the inter-
tion micros is also investigated. net. A name-brand Arduino costs roughly $25 and decent qual-
ity knock-offs are available for $12 or less. Despite their low
cost, Arduinos are surprisingly useful for creating real-time con-
trol systems with digital update frequencies up to 500 Hz. All
Primary Research Questions of this makes Arduinos great candidates for student projects and
This paper seeks to answer several questions, some of which student-owned or take-home dynamic systems and controls ex-
are technical and some are pedagogical. The first technical ques- periments.
tion is how accurate is using the built-in function micros to This paper addresses several challenges associated with
measure the execution time of a chunk of code on an Arduino. teaching students to design and create real-time control systems
Another primary technical question is how accurate is using the using Arduinos. The first challenge is that students need to be
delayMicroseconds function to enforce real-time execution able to time how long it takes certain chunks of code to execute
of a control law compared to using a timer interrupt? so that they can make data-based decisions on how to improve
The primary pedagogical question is whether or not us- their control systems. An easy way to measure execution time
ing delayMicroseconds is easier for students to understand on an Arduino is to send a digital pin high at the start of a code
than using a timer interrupt. block and then bring it back low at the end of the block. Execu-

1 Copyright © 2017 ASME

Downloaded From: http://proceedings.asmedigitalcollection.asme.org/ on 01/02/2018 Terms of Use: http://www.asme.org/about-asme/terms-of-use


tion time can then be measured using an oscilloscope. However, Course flipping has been explored as a means for making time
it is unlikely that students would have access to an oscilloscope for experiments and hands-on projects in controls courses while
at home. So, if execution time could be measured without addi- still providing a foundation of control theory [1, 4]. Courses flip-
tional equipment it would be beneficial for take-home projects. ping refers to having students watch lecture videos or somehow
This papers investigates the accuracy of using the micros com- learn the theoretical material before coming to class. This frees
mand on the Arduino to measure code execution times with ac- class time to be used for experimental projects. This paper high-
curacy down to fractions of a millisecond, to avoid the need of lights some of what students could learn during those experimen-
an oscilloscope. tal projects.
Another challenge in teaching real-time control system de- Many approaches to bringing physical experiments into the
sign is helping students understand how the timing is enforced. controls classroom have been presented in the literature [5, 6, 7].
The Arduino Uno has three on-board timers that can be used in Particular emphasis has been placed lately on student-owned or
real-time control using timer interrupts and an interrupt service take-home experiments [8, 9, 10, 11, 12].
routine (ISR). Depending on the depth of the students’ under- Arduino microcontrollers are a great fit for take-home or
standing of the Arduino, timer interrupts may be challenging for student-owned experiments because of their low cost and ease
two reasons. First, configuring the timers is somewhat compli- of use. They are also surprisingly capable for feedback con-
cated and not as well documented as most other aspects of the trol [13, 14].
Arduino. Each of the timers also serves some built-in purpose
on the Arduino, so modifying the timers can affect other func-
tions. The second reason that using a timer interrupt may be Investigating the Accuracy of the micros Function
challenging is that some students may be confused by control The first question this paper seeks to answer is whether or
code that depends on multiple interrupts. This part of the work not the micros function on the Arduino can be used for accurate
was inspired by a lab where students where using the Arduino measurement of code execution time. Part of designing real-time
to control a DC motor/encoder system. The fact that there was control algorithms is ensuring that all calculations, the reading of
one interrupt to service the encoder and another to service the all sensors, and the sending of all actuator commands can reliably
timer seemed to cause confusion. An approach that uses the be completed within the desired digital update period.
delayMicroseconds function rather than a timer interrupt One way of testing this on an Arduino (which works with
was developed to reduce this confusion. This paper presents most microcontrollers) is to send a digital pin high at the start of
data on the accuracy of using delayMicroseconds vs. a some portion of the code and then send the pin back low at the
timer interrupt along with assessment data on whether or not the end. An oscilloscope is then used to monitor how long the pin
delayMicroseconds approach was in fact better understood stays high. Capturing this signal over multiple loops through the
by the students. code can lead to an assessment of the reliability of the timing. If
While using an Arduino for prototyping, control tuning, de- the micros function can provide accurate and trustworthy tim-
bugging, or system identification, it is typical to print data to the ing in the range of 0.1 to 0.01 milliseconds, then an oscilloscope
serial monitor. This is the easiest way to get data from the Ar- is not needed.
duino to a computer for further analysis. Over the course of this In order to test the accuracy of the micros function, the
work, it became apparent that this printing can consume a consid- measurement times from micros were compared to oscillo-
erable amount of the real-time period between digital time steps. scope measurements while an Arduino was used for position
An approach based on using sprintf is shown to cut the print- control of a DC motor/encoder system (shown in Figure 1). The
ing time in half compared to repeatedly calling the Arduino’s main control code was placed in the Arduino’s loop function.
built-in Serial.print function. It is worth noting that using The code was divided into two main sections: calculations and
the micros function made it easy to verify that sprintf is printing.
faster than calling Serial.print multiple times.
A secondary contribution of this paper is to provide data that
shows the accuracy of using an Arduino for real-time control Course Description
with digital update frequencies in the range of 200-500 Hz. The students referred to in this paper are taking a required,
junior-level course on dynamic systems and control. This course
is their first introduction to the Laplace transform, dynamic sys-
Literature Review tems concepts, and control design. The course includes three
Controls students often have problems taking the theory they credits of lecture and a one credit lab.
have learned and using it to create real feedback control systems. It is worth noting that the students have used Arduinos in
This problem is known as the "theory-practice gap" [1]. Exper- two previous courses: a robotics project in the freshmen intro-
iments play a key role in bridging the theory-practice gap [2, 3]. duction to engineering course and a sophomore level course on

2 Copyright © 2017 ASME

Downloaded From: http://proceedings.asmedigitalcollection.asme.org/ on 01/02/2018 Terms of Use: http://www.asme.org/about-asme/terms-of-use


Read All
Sensors
H-bridge

Perform Control dt1


Law Calculations

Send Actuator
dt
Commands

DC motor
with encoder Transmit/Print dt2
Data (optional)
Arduino
Battery holder knock-off
Wait for Next
Figure 1. Picture of the DC motor/encoder system used in the lab portion Time Step
of the course associated with this paper.

Figure 2. Block diagram of the steps needed to execute one time step of
introduction to digital systems. the control law. dt1 refers to the calculation time, dt_2 refers to the time
to print data to the serial monitor, and dt is the entire digital time step for
one period.
System Description
The experimental system for this paper is shown in Figure 1.
The other approach introduced in this paper is to use a very
The system consists of a DC motor with an encoder powered by
precise delay at the end of each time through the main loop
an H-bridge. An Arduino Uno knock-off is used for all control
function so that one pass through the loop is exactly the de-
calculations and all data acquisition. A USB-to-serial connec-
sired digital update period dt. The primary challenge of using
tion allows data from the Arduino to be sent back to a laptop or
this approach is that the time for one execution of the loop
desktop.
function can vary depending on other interrupts happening in
the code. For the DC motor/encoder system, encoder interrupts
will affect loop time. To overcome this, the new approach calls
Control Implementation and Testing Approach the micros function to get the time stamp at the start of the
There are two functions that are required in all Arduino pro- loop. Once the control law code has completed and all data
grams. A user can add as many supplemental functions as they has been printed to the serial monitor, micros is called again
want, but setup and loop are required. The Arduino runs the and the difference between the two calls to micros gives the
setup function once when it is first powered on or whenever elapsed time for the current pass through the loop. The amount
there is a new serial connection. After the setup function com- of time before the next digital period is than calculated and the
pletes, the loop function is called over and over indefinitely. delayMicroseconds function is called to force the Arduino
The primary challenge in using an Arduino for real-time to wait for precisely the right amount of time until the next time
control is ensuring that the portions of the code associated with step.
the control law are executed at hard real-time intervals and only
All of the steps necessary to execute the control law and print
once per time step. This paper compares two approaches for en-
the data for one time step are illustrated in Figure 2. Examples
forcing the real-time execution. The more standard approach is to
of the timing signals coming from the oscilloscope are shown in
use a timer interrupt and have the interrupt set a flag in the timer
Figure 3.
ISR. The control law is then placed inside an if statement in
the main loop function. If the timer ISR has not yet happened,
the Arduino will cycle rapidly through the loop function, es- Calculation Time Results
sentially doing nothing other than checking the value of the flag The calculations portion includes everything necessary to
variable. Once the flag variable is found to be true, the control actually perform the PD control: finding the error, numerically
law is executed once and the flag is cleared. This approach was differentiating the error, calculating the command voltage, and
presented at DSCC 2014 [13]. sending the voltage to the H-bridge. The printing section is the

3 Copyright © 2017 ASME

Downloaded From: http://proceedings.asmedigitalcollection.asme.org/ on 01/02/2018 Terms of Use: http://www.asme.org/about-asme/terms-of-use


250
dt ch 1
Oscilloscope Timing Signal

ch 2
waiting

Frequency of Occurence
dt1 dt2 200
1
150

100

50

0 0
0.16 0.18 0.20 0.22 0.24 0.26 0.28 0.30
0 2 4 6 Time (ms)
Time (ms)
Figure 4. Histogram of calculation time dt1 measured using the built-in
Figure 3. Illustration of the calculation time dt1 , printing time dt2 , time micros function of the Arduino.
step dt , and the waiting time for the control law.

180
160
Frequency of Occurence
time it takes to print all of the real-time data to the serial monitor
for transmission to the computer. If the Arduino were function- 140
ing entirely on its own in an embedded system, printing the data 120
would not be necessary. However, if the real-time data is needed
100
for tuning, debugging, or pedagogical purposes, printing to the
serial monitor is the easiest way to get data from the Arduino 80
back to the computer via USB. 60
The time to execute both portions of code was measured in 40
two ways: using the micros function and toggling a digital pin
high and low. At the start of the calculations portion, a pin is 20
sent high and the current time in microseconds is recorded using 0
0.14 0.16 0.18 0.20 0.22 0.24 0.26 0.28 0.30
micros. At the end of the calculations code, the calculations
Time (ms)
timing pin is sent back low and the current time is recorded using
micros. The calculation time in milliseconds is then ((ending Figure 5. Histogram of calculation time dt1 measured using the USB
micros) - (beginning micros))/1000. The beginning and end oscilloscope and toggling digital pins high and low on the Arduino.
of the printing time is similarly measured using both micros
and an oscilloscope with a different timing pin.
These two measurements are then compared for each time tical, but has several similarities. The minimum, maximum, and
step over a step response test that lasts two seconds and includes mean are all roughly the same. Both sets of data show a large set
500 time steps (the controller is running at 250 Hz for two sec- of points near the mean and a smaller set near the maximum.
onds). The results are summarized for the calculation time in four In order to better compare the data sets and account for ac-
histograms shown in Figures 4-7. Figure 4 shows the histogram tual variation caused by encoder interrupts, the differences be-
for the calculation time using the micros function. The mean tween the micros measurement and the oscilloscope measure-
value is 0.2386 ms, the maximum is 0.280 ms, and the minimum ment were compared for each pass through the loop. A percent
is 0.160 ms. Some variation is expected because the calculations difference was calculated by using an average of the means of
can be interrupted by the interrupt service routine that responds the micros and oscilloscope measurements as a denominator.
to changes of the encoder signals. While the minimum is 33% The histogram of percent difference values is shown in Figure 6.
below the mean, this is primarily because the mean is so small. Nearly all of the data is contained within the interval ±3%. The
The difference between the mean and the minimum is 0.079 mil- maximum is +4.5%.
liseconds (79 microseconds). A range of ±5% seems reasonable, but it is also worth noting
Figure 5 shows the calculation times as measured using the that the small denominator for dt1 makes the percent differences
USB oscilloscope and pin toggling. The distribution is not iden- larger. To clarify this, Figure 6 shows the actual difference be-

4 Copyright © 2017 ASME

Downloaded From: http://proceedings.asmedigitalcollection.asme.org/ on 01/02/2018 Terms of Use: http://www.asme.org/about-asme/terms-of-use


100 500
400

Encoder
300

θ from
Frequency of Occurence

80 200
100
60 0
0.280 200 400 600 800 1000

calc time dt1 (ms)


40 0.26
0.24

Arduino
0.22
20 0.20
0.18
0.16
0−4 −3 −2 −1 0 1 2 0 200 400 600 800 1000
3 4 5 Time (ms)
Percent Difference
Figure 8. Plot of dt1 and encoder count vs. time, showing that dt1 varies
Figure 6. Histogram of percent difference between the Arduino the most when the encoder interrupt is happening the most.
micros measurement and the USB oscilloscope measurement for cal-
culation time dt1 .
Printing Time Results
100 Some means of transmitting data from the Arduino back to a
laptop or desktop computer is needed in order to use the Arduino
real-time approach for control tuning, debugging, data acquisi-
Frequency of Occurence

80 tion, or comparing with simulations. The easiest way to transmit


data from the Arduino is to use the serial monitor and the serial-
60 to-USB interface. Printing data to the serial monitor in comma
or tab delimited rows allows the data to be copied and pasted into
40 a text editor or spreadsheet program on the computer. The data
can then be read into Python, Matlab, or a spreadsheet program
for further analysis.
20 Figures 9-12 show the results for printing time dt2 . In many
respects, the data is similar to that for dt1 . The variation between
0 using the micros function and measuring using an oscilloscope
−0.010 −0.005 0.000 0.005 0.010 0.015
seems fairly small. It is worth noting that for this simple appli-
Time (ms)
cation the time to print the data is significantly larger than the
Figure 7. Histogram of actual difference in milliseconds between the Ar- time to do the control calculations, i.e. dt2 is larger than dt1 by a
duino micros measurement and the USB oscilloscope measurement factor of 2.
for calculation time dt1 . This graph is show because the small denomina-
tor for Figure 6 might make the data look worse than it really is.
Reducing Printing Time using sprintf
The magnitude of the printing time dt2 raises the ques-
tion of whether or not printing time can be reduced. The eas-
tween the micros and oscilloscope measurements for each pass
iest way to print to the serial monitor of an Arduino is to use
through the loop. This is same as the numerator of the percent
the built-in functions Serial.print or Serial.println.
difference data and the units are in milliseconds. The maximum
The functions can take a variable of nearly any data type as the
difference is 0.011 ms (11 microseconds).
input and convert it to ASCII text to send to the serial mon-
Figure 8 shows the calculation time dt1 and the encoder
itor. Serial.print can only print one variable at a time,
count plotted vs. time. The main purpose of this plot is to show
so the easiest way to print comma delimited text is to call
that since the encoder interrupt can affect execution time, dt1
Serial.print multiple times:
varies the most when the motor angle is changing the most. Also
note that the minimum values for dt1 occur when the encoder is Serial.print(t);
holding steady at zero before the step input happens. This makes Serial.print(“,”);
sense if floating point calculations involving multiplying by zero Serial.print(dt1);
are optimized by the compiler. Serial.print(“,”);

5 Copyright © 2017 ASME

Downloaded From: http://proceedings.asmedigitalcollection.asme.org/ on 01/02/2018 Terms of Use: http://www.asme.org/about-asme/terms-of-use


300 100
250
Frequency of Occurence

Frequency of Occurence
80
200
60
150
40
100

50 20
0 0 −0.5
1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 0.0 0.5 1.0
Time (ms) Percent Difference
Figure 9. Histogram of printing time dt2 measured using the built-in Figure 11. Histogram of percent difference between the Arduino
micros function of the Arduino. micros measurement and the USB oscilloscope measurement for
printing time dt2 .
300
100
250
Frequency of Occurence

Frequency of Occurence
200 80

150 60
100
40
50
20
0
1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4
Time (ms) 0
−0.014 −0.007 0.000 0.007 0.014 0.021
Figure 10. Histogram of printing time dt2 measured using the USB os- Time (ms)
cilloscope and toggling digital pins high and low on the Arduino.
Figure 12. Histogram of actual difference in milliseconds between the
Arduino micros measurement and the USB oscilloscope measure-
... ment for printing time dt2 .
Serial.print(’\n’);
Note that there is not a printf equivalent on the Arduino. where the helper function three_digits is
While sprintf is supported, the version that works with the
Arduino cannot handle floating point numbers. An approach int three_digits(float floatin){
based on using sprintf was attempted to see if the printing float float_part;
time could be reduced. The approach begins with creating a int out_digits;
character array that will contain one line of serial output. A float_part = floatin - (int)floatin;
helper function will be needed to find the mantissa of floating out_digits = (int)(1000*float_part);
point numbers. Floating point numbers will then be printed to return out_digits;
the character array in two pieces, the integer portion and the man- }
tissa, separated by a decimal point:
Figure 13 show the histogram for dt2 using the sprintf
sprintf(buffer, "%d.%03d”, (int)dt1, approach. Comparing this to Figure 10 confirms that sprintf
three_digits(dt1)); is roughly twice as fast as Serial.print.

6 Copyright © 2017 ASME

Downloaded From: http://proceedings.asmedigitalcollection.asme.org/ on 01/02/2018 Terms of Use: http://www.asme.org/about-asme/terms-of-use


160 120
140
100
Frequency of Occurence

Frequency of Occurence
120
80
100
80 60
60
40
40
20
20
0 0
0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 3.97 3.98 3.99 4.00 4.01 4.02 4.03 4.04 4.05
Time (ms) Time (ms)
Figure 13. Histogram of printing time dt2 using sprintf rather than Figure 14. Histogram of entire digital period dt using the
multiple calls to Serial.print delayMicroseconds approach.

Investigating the Accuracy of the delayMicroseconds


Approach
As mentioned in the introduction, there at least two rea- all control calculations and all printing to the serial monitor has
sons why using a timer interrupt to control the real-time exe- concluded for the current time step, the time is grabbed again us-
cution of the control law might be confusing for students. First, ing micros. Taking the difference between these time stamps
configuring the timer is somewhat complicated and poorly doc- tells the code how much time has elapsed since the start of the
umented. Additionally, modifying the parameters of one of current time period. Subtracting the elapsed time from the de-
the three built-in timers may affect other functions of the Ar- sired time step allows the required pause to be determined. The
duino. For example, any changes to the configuration of timer0 delayMicroseconds function can then be called with how
will alter the behavior of the millis, micros, delay, and long the code should pause. There are two technical challenges
delayMicroseconds functions. A second challenge for stu- associated with this approach. The first is that there are limita-
dents is that having multiple interrupts may be confusing. The tions on the accuracy of delayMicroseconds. The second
DC motor/encoder system uses an interrupt to read the encoder. challenge is that calling the delayMicroseconds function
Having a second interrupt that controls the real-time execution of actually takes a small amount of time.
the controller seemed to cause confusion.
Figures 14 and 15 show histograms of the digital period dt
It is important when using a timer interrupt that the ISR be
for the delayMicroseconds and timer ISR approaches for a
as short as possible. Failure to do this can interfere with reading
goal control frequency of 250 Hz. The delayMicroseconds
the encoder [13]. Essentially, while the control law is perform-
approach shows slightly more variation in dt than the timer
ing calculations in the timer ISR, the encoder is not being read.
ISR approach, but both maintain the timing within ±4% of the
The solution to this problem is to have the timer ISR set a flag
mean. The delayMicroseconds approach has a mean pe-
variable that tells the main loop code that a timer interrupt has
riod of 4.002 ms with a max of 4.041 ms and a min of 3.974
occurred. The control law is then placed inside an if statement
ms. The max is 0.0389 ms (0.97%) above the mean and the min
in the main loop so that the control law is only calculated after
is 0.0283 ms (0.71%) below the mean. The timer ISR approach
a timer interrupt has occurred. Until the next interrupt occurs,
has a mean of 3.965 ms with a max of 3.979 ms and a min of
the code loops very quickly through the loop function checking
3.952 ms. The max is 0.015 ms (0.37%) above the mean and the
the value of the flag variable and doing essentially nothing if the
min is 0.012 ms (0.31%) below the mean.
flag is still false.
An alternative to using a timer interrupt is to have the code The delayMicroseconds approach is slightly less ac-
pause for exactly the right amount of time at the end of the loop curate than using a timer interrupt but still seems more than ac-
so that the next iteration of the control law calculations starts curate enough for creating real-time control systems. The next
exactly one digital time step after the same point in the previ- section will address whether or not the delayMicroseconds
ous time step. This is done by grabbing the time at the start approach is easier for students to understand that the timer inter-
of the control calculations using the micros function. When rupt approach.

7 Copyright © 2017 ASME

Downloaded From: http://proceedings.asmedigitalcollection.asme.org/ on 01/02/2018 Terms of Use: http://www.asme.org/about-asme/terms-of-use


180 Table 1. Table of student Likert for question 1 of the first survey: How
well do you think you understand the recent labs related to using an Ar-
160
Frequency of Occurence

duino to create a feedback control system?


140
Likert Option Frequency
120
I fully understand everything we have done. 2
100
80 I more or less understand most of what we 8
have done.
60
40 I kind of understand what we have done. 8
20 I am a little confused. 2
0 I am very confused. 0
3.950 3.955 3.960 3.965 3.970 3.975 3.980
Time (ms) Not Applicable 0
Figure 15. Histogram of entire digital period dt using the a timer interrupt

derstanding what they have been doing in lab. Only one student
gives a fully correct answer to question 3; two other students give
Analyzing the Assessment Data answers that are mostly correct. The rest of the answers are ei-
The delayMicroseconds portion of this work was mo- ther vague or simply incorrect. Several of the answers confuse
tivated by interactions with students who seemed confused after the timer interrupt and the encoder interrupt. Here are a couple
several lab activities related to using the DC motor/encoder sys- of examples of responses to question 3: Why have we been using
tem shown Figure 1. Students did not seem to understand the a timer ISR in recent labs?:
role of the timer interrupt and some also seemed confused by the
"ISR timer let’s us use a hard interrupt to flag when specified
fact that there was more than one ISR in the Arduino code (one
variables are at a predetermined definition."
for the encoder and one for the timer).
Three different assessments were conducted in an attempt "To help us change the way the timer works so it can count
to gauge student learning related to real-time control. The first at different rates"
two assessments were anonymous online surveys. The first sur-
"We use an ISR on timer 1 in order to run a routine that will
vey was done after several lab activities using timer interrupts
read in the encoders value to make sure that the Arduino’s
for real-time control but before the delayMicroseconds ap-
loops don’t skip over the values and therefore an adjustment
proach had been introduced.
can be made to the pwm to control the speed of the motor."
The first survey was given right after it became apparent to
the instructor that students were struggling with the timer ISR. The answers to the first survey confirmed the instructor’s
All 20 students responded to the survey. The survey was com- suspicions, based on interactions with the students in lab, that
posed of three questions: the students were not fully understanding the use of timer inter-
rupts for real-time control. The students’ confusion motivated
1. How well do you think you understand the recent labs
the development of the delayMicroseconds approach.
related to using an Arduino to create a feedback control
The second survey was conducted after students had used
system? (Likert scale)
the delayMicroseconds approach for several weeks. The
2. What steps are necessary to create a feedback control
second survey included several topics, so only three of the ques-
system using an Arduino? (essay)
tions are relevant to this paper:
3. Why have we been using a timer ISR in recent labs?
(essay) 3. We used two different approaches for real-time control
in EGR 345 lab: timer ISR and delayMicroseconds.
The results for question 1 are shown in Table 1. After an-
Which approach was clearer to you? (Likert scale)
alyzing the students’ essay response for questions 2 and 3, the
4. How do you use a timer ISR to create a real-time control
students seem to have overestimated how much they understand.
system? Why is the timer necessary? (essay)
Of the 20 responses for question 2, four students mention the
5. Why did we use delayMicroseconds in our real-time
need for a timer or a timer interrupt. Since it is possible to cre-
controllers? Why is the delay necessary? (essay)
ate a feedback control system without real-time execution, this
might not be considered a huge problem. However, the responses The results for question 3 are shown in Table 2. More
to question 3 clearly show that most of the students are not un- than half of the students who took the survey said that the

8 Copyright © 2017 ASME

Downloaded From: http://proceedings.asmedigitalcollection.asme.org/ on 01/02/2018 Terms of Use: http://www.asme.org/about-asme/terms-of-use


delayMicroseconds approach was a little clearer to them proach rather than using a timer interrupt. A third student in this
than the timer ISR approach. While this might not be over- group used the micros function in his pseudo-code answer.
whelming support for the delayMicroseconds approach, it Taken together, these assessment results show that the
seems like the approach was at least somewhat helpful. Note that delayMicroseconds approach might be slightly clearer to
only 11 out of 20 students completed the second survey. students but there is work to be done in teaching students about
real-time feedback control in this course. At most, roughly
six out of twenty students seem to understand what was done
Table 2. Table of student Likert responses for question 3 of the second in using an Arduino to create a real-time control system. The
survey: We used two different approaches for real-time control in EGR delayMicroseconds approach seems to help some students
345 lab: timer ISR and delayMicroseconds. Which approach was clearer at least a little, but it did not have as dramatic an impact on stu-
to you? dent learning as was hoped.
Likert Option Frequency To address these issues, future work will include explicit lec-
turing on controlling real-time execution with an Arduino. Ad-
Timer ISR was much clearer to me 1
ditional introductory lab activities will likely be created that ask
Timer ISR was a little clearer to me 0 students to generate square waves and use the Arduino for data
acquisition.
They are the same to me 4
delayMicroseconds was a little clearer to me 6
delayMicroseconds was much clearer to me 0 Conclusions
This paper has presented several tools related to teaching
Not Applicable 0 students to create real-time control systems using Arduino mi-
crocontrollers. The micros function has been shown to agree
with oscilloscope measurements to within roughly 0.01 millisec-
The number of correct and mostly correct answers for ques- onds (10 microseconds). This means that students can accurately
tion 4 on the second survey was exactly the same as for question 3 measure the execution time of chunks of code without needing
on the first survey. So, exposure to the delayMicroseconds any equipment other than the Arduino itself, which is particu-
approach does not seem to have helped the students better under- larly important for student-owned or take-home experiments.
stand the timer ISR approach. The delayMicroseconds approach was introduced as
Three of the 11 respondents to the second survey got ques- an alternative to using a timer interrupt to control the timing
tion 5 fully correct; no one got it mostly correct. So, the num- of a real-time control law. The approach was able to imple-
ber of fully correct answers went up, but the number of correct ment a 250 Hz real-time controller where the total variation on
and mostly correct responses did not change when switching to the real-time digital period was roughly 0.05 milliseconds. The
the delayMicroseconds approach. It was not clear if these delayMicroseconds approach is easier to implement on an
mediocre survey results truly reflected what students had learned Arduino than a timer interrupt. Assessment results imply that
in the lab. The decision was made to do one final assessment the delayMicroseconds approach might be slightly easier
where the students would be motivated to do their best work. for students to understand than the timer interrupt approach.
The following question was placed on the final exam: Two supplemental findings were also presented. The
sprintf function was shown to significantly reduce the time
"Describe what you need to do to create a PID control sys- to print data to the serial monitor from the Arduino compared
tem for a DC motor using an Arduino. You do not need to to repeatedly calling the Serial.print function. This can
write any code, but describe what the code needs to do, what be important if data is being printed to the serial monitor in real-
steps need to be taken, and any functions you would need to time. The other supplemental finding is that the accuracy of using
write." an Arduino to create a 250 Hz controller was shown to be quite
Much like question 2 on the first survey, this question does good, especially when a timer ISR is used.
not explicitly require students to talk about timing. Of the 20
students taking the final exam, four gave answers that were con-
sidered completely or mostly correct with respect to timing. REFERENCES
Additionally, two more students gave answers that were some- [1] de la Croix, J.-P. and Egerstedt, M., “Flipping the controls
what correct regarding timing. Of the four who gave correct or classroom around a MOOC,” American Control Conference
mostly correct answers, two made comments that seemed to im- (ACC), 2014, IEEE, 2014, pp. 2557–2562.
ply they were thinking about the delayMicroseconds ap- [2] Bernstein, D., “Enhancing undergraduate control educa-

9 Copyright © 2017 ASME

Downloaded From: http://proceedings.asmedigitalcollection.asme.org/ on 01/02/2018 Terms of Use: http://www.asme.org/about-asme/terms-of-use


tion,” Control Systems Magazine, IEEE, Vol. 19, No. 5, oct
1999, pp. 40 –43.
[3] Bernstein, D., “Control experiments and what I learned
from them: a personal journey,” Control Systems Magazine,
IEEE, Vol. 18, No. 2, apr 1998, pp. 81–88.
[4] Hill, R., “Hardware-based activities for flipping the system
dynamics and control curriculum,” American Control Con-
ference (ACC), 2015, IEEE, 2015, pp. 2777–2782.
[5] Shiakolas, P. and Piyabongkarn, D., “Development of a
Real-Time Digital Control System with a Hardware-in-
the-Loop Magnetic Levitation Device for Reinforcement
of Controls Education,” IEEE Transactions on Education,
Vol. 46, No. 1, 2003, pp. 79–87.
[6] Kamis, Z., Topcu, E., and Yuksel, I., “Computer-Aided
Automatic Control Education With a Real-Time Develop-
ment System,” Computer Applications in Engineering Ed-
ucation, Vol. 13, No. 3, 2005, pp. 181–191.
[7] Salzmann, C., Gillet, D., and Huguenin, P., “Introduction
to Real-time Control using LabVIEW with an Application
to Distance Learning,” Int. J. of Engineering Education,
Vol. 16, No. 5, 2000, pp. 372–384.
[8] Reck, R. M. and Sreenivas, R. S., “Developing an Af-
fordable Laboratory Kit for Undergraduate Controls Edu-
cation,” ASME Dynamic Systems and Controls Conference,
2014. Proceedings of the 2014, ASME, Oct. 2014.
[9] Schinstock, D., McGahee, K., and Smith, S., “Engaging
students in control systems using a balancing robot in a
mechatronics course,” 2016 American Control Conference
(ACC), IEEE, 2016, pp. 6658–6663.
[10] Bay, C. J. and Rasmussen, B. P., “Exploring controls ed-
ucation: A re-configurable ball and plate platform kit,”
2016 American Control Conference (ACC), IEEE, 2016,
pp. 6652–6657.
[11] Jouaneh, M. K. and Palm, W. J., “Control system experi-
ments at home,” Frontiers in Education Conference (FIE),
2011, IEEE, 2011, pp. T2G–1.
[12] Jouaneh, M. K. and Palm, W. J., “System dynamics experi-
mentation at home,” ASME 2009 International Mechanical
Engineering Congress and Exposition, American Society
of Mechanical Engineers, 2009, pp. 413–420.
[13] Krauss, R. W., “Evaluation of a Low-Cost Microcontroller
for Real-Time Control Education and Prototyping,” ASME
Dynamic Systems and Controls Conference, 2014. Proceed-
ings of the 2014, ASME, Oct. 2014.
[14] Krauss, R. W., “Sensor Fusion for Vibration Suppression
Implemented on Arduino and Raspberry Pi,” ASME 2015
Dynamic Systems and Control Conference, American So-
ciety of Mechanical Engineers, 2015, pp. V003T52A005–
V003T52A005.

10 Copyright © 2017 ASME

Downloaded From: http://proceedings.asmedigitalcollection.asme.org/ on 01/02/2018 Terms of Use: http://www.asme.org/about-asme/terms-of-use

You might also like