Professional Documents
Culture Documents
DSCC2017
October 11-13, 2017, Tysons, Virginia, USA
DSCC2017-5140
Ryan W. Krauss
Associate Professor
Product Design and Manufactoring Engineering
Grand Valley State University
Grand Rapids, MI 49504
Email: kraussry@gvsu.edu
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
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-
Encoder
300
θ from
Frequency of Occurence
80 200
100
60 0
0.280 200 400 600 800 1000
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
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.
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.
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