Dynamic Torque Control of Brushed DC Motors For Hardware-in-the-Loop Integration
Dynamic Torque Control of Brushed DC Motors For Hardware-in-the-Loop Integration
Dynamic Torque Control of Brushed DC Motors For Hardware-in-the-Loop Integration
of Brushed DC Motors
for Hardware-in-the-Loop Integration
Eric Schyllert
The main goal with the thesis was to design, construct and evaluate a system able
to drive and brake a mechanical axis to simulate loads and stored rotational energy
- a hardware force simulator. The force simulator was then to be integrated in a
existing hardware-in-the-loop testing rig to enhance the mechanical dynamics of
door simulations. The force simulator was to be realised using a brushed DC motor
and by controlling the motor torque. Controlling the motor torque of a brushed DC
motor is achieved by controlling the motor current - and this is mainly what this
thesis is about. The thesis explains the foundations of an embedded system capable
to control and log the current of a brushed DC motor.
The force simulator consisted of a microcontroller, a motor card, a circuit to
measure the inline motor current and a brushed DC motor. The circuit to measure the
current was designed and implemented, consisting of a shunt resistor and a current
sense amplifier. The shunt was placed in series (inline) with the motor. A software
strategy was developed and implemented to deal with noise due to common-mode
voltage transients (caused by motor control using PWM). This strategy came with
a cost of introducing a measurement delay in the system.
A discrete PI-controller to control the motor current was researched and imple-
mented. An expression to optimal tune the controller based on motor parameters and
the sampling period was researched. Current control experiments were conducted
but due to a calculation error the PI-controller was very untuned, this compared to
the tuned expression. This error was discovered very late in the work and there was
no time to redo the experiments. The experiments with the implemented untuned PI-
controller were conducted and a current reference was successfully tracked, how-
ever with noise which had an amplitude of approximately 200 mA.
A strategy to record current measurements used in the current control, without
missing any samples was successfully developed. It worked by using a double buffer
system, filling the buffers utilizing a DMA and then written to a SD card once a
buffer was filled. With the main objective to develop the capability to be able to
analyze the current control algorithm.
3
Acknowledgements
5
Contents
1. Introduction 9
1.1 Problem formulation . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2 Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3 Thesis outline . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2. Background 12
2.1 Measuring current using a current shunt . . . . . . . . . . . . . 12
2.2 Torque control - brushed DC motor . . . . . . . . . . . . . . . . 12
2.3 Sampled current control - brushed DC motor . . . . . . . . . . . 13
2.4 Simplified model of the mechanics of a brushed DC motor . . . . 15
3. System overview 16
3.1 Motor setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.2 Motor driver card . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.3 Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4 Micro SD card . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.5 Operating the system . . . . . . . . . . . . . . . . . . . . . . . 18
4. Current Control 21
4.1 Current shunt placement . . . . . . . . . . . . . . . . . . . . . . 21
4.2 Analog-to-digital convert with the Arduino MKR Zero . . . . . . 22
4.3 Current sense amplifier . . . . . . . . . . . . . . . . . . . . . . 23
4.4 Current sense dead-zones and PWM dependency . . . . . . . . . 25
4.5 Current control overview . . . . . . . . . . . . . . . . . . . . . 27
4.6 Challenges in current control of unloaded DC motors . . . . . . 27
5. Motor Control 29
5.1 Motor control strategy . . . . . . . . . . . . . . . . . . . . . . . 29
5.2 Encoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6. Software design 33
6.1 Software overview . . . . . . . . . . . . . . . . . . . . . . . . . 33
6.2 Direct memory access controller . . . . . . . . . . . . . . . . . 33
6.3 PWM generation . . . . . . . . . . . . . . . . . . . . . . . . . . 33
7
Contents
8
1
Introduction
ASSA ABLOY Entrance Systems has a wide range of automatic door systems,
each product with its own unique mechanical dynamics. Today at Assa Abloy their
physical testing rig does not take into account these mechanical difference between
different door systems. The motor in the testing rig is not loaded with any physical
load. The testing rigs are small and fairly mobile and enables developers/testers to
do verification and testing of new implementations without having to access a real
door system.
A more realistic testing rig would make it easier to motivate the use of it in a
third party certification process of the door system. One such step in the certification
process is to ensure that the control system and motors are operable in temperatures
of -30 °C to 50 °C. Placing a fully installed door system in a climate chamber is a
hard task - if the testing rig was used instead it would make it a lot easier and speed
up the process, reduce the cost and open up the use of smaller climate chambers.
A more accurate testing rig in which a range of different door types and config-
urations can be hardware simulated opens up for a more dynamic and agile verifi-
cation and testing process in which the control system can be tested for edge cases
such as really heavy doors or a door system installed with a floor material with a
really high frictional coefficient. This could speed up development time due to de-
creasing time from implementation to verification, make testing more accurate and
increase the trust in the testing process from a third party certification point of view.
The main goal with the thesis was to design, construct and evaluate a prototype
system able to drive and brake a mechanical axis to simulate loads and rotational
energy stored in the system. This load simulator would be used to simulate the
dynamics of doors and to be integrated in a existing hardware-in-the-loop testing
rig - to capture a more realistic behaviour.
The scope was narrowed due to time constraint to create a system able to mea-
sure motor current and to log the data in real-time, and to control the current of a
brushed dc motor. Due to the physics of a brushed dc motor, to control the motor
current is equivalent with being able to control the motor torque which would be
used to drive a mechanical axis to simulate stored rotational energy. The logging of
data would be used to be able to validate the current control.
9
Chapter 1. Introduction
.
Figure 1.1 Overview of what a control system to simulate rotational energy as-
sumed could be composed of. Five key components were identified: mathematical
model/recorded current measurements, CAN bus, system motor together with the
control system under test, physical mechanical axis and a simulator motor with a
control system.
The idea was that the error between the mathematical model/recorded values
and the actual values of the system motor was to be used as a reference to the
simulator motor which would be controlled to minimize the error between the cur-
rent/speed of the system motor and the model. It was limited to only compensate for
10
1.2 Methodology
the error when it meant that the simulator motor would apply a driving force that
would increase the speed of the system motor - simulating stored rotational energy.
1.2 Methodology
The work methodology during the thesis work was applied practical work with an
iterative approach with each iterative phase consisting of research, prototyping and
verification. Both hardware and software verification was often done with an oscil-
loscope by capturing signals and evaluating the result. A signal generator, a power
supply unit and a multi-meter was also used in the verification process.
11
2
Background
To be able to control the torque generated by the brushed DC motor a model of the
torque is required. Equation 2.2 represents a simplified model of a brushed DC mo-
tor - a model that explains the dynamics of the motor current [Alaküla and Karlsson,
2011].
di
Udc = RI + L − ωψ (2.2)
dt
where Udc represents the DC input voltage, R is the electrical resistance of the mo-
tor’s armature, I is the electrical current flowing through the armature, L is the in-
di
ductance of the motor’s armature, dt is the rate of change of current with respect to
time, ω is the angular velocity of the motor’s armature and ψ is the magnetic flux
linkage in the motor’s armature.
12
2.3 Sampled current control - brushed DC motor
Equation 2.3 expresses the relationship between the motor current and the motor
torque [Alaküla and Karlsson, 2011] - if the goal is to control the motor torque it
requires to be able to control the motor current.
τ = Iψ (2.3)
where τ represents the generated torque, I is the electrical current flowing through
the motor’s armature, and ψ is the magnetic flux linkage in the motor’s armature.
where k indicates the beginning of the sample interval, ū(k, k + 1) is the average
voltage during sample k, u∗ (k) is the reference voltage, i(k) is the measured current,
i∗ (k) is the reference current, ī(k, k + 1) is the average current during sample k,
ē(k, k + 1) is the average back-emf during sample k and e(k) is the measured back-
emf. Assumption 2.4 means that the reference voltage for the coming period should
be the average voltage needed to achieve the desired current change. Assumption 2.5
is an assumption of "dead-beat" current control - meaning that the current error is
eliminated in one sampling interval. Assumption 2.6 is argued to be true in average.
Assumption 2.7 is usually true due to that the sampling frequency of the current
control is magnitudes faster than the dynamics of the back-emf. Assumption 2.8
13
Chapter 2. Background
!
L n=k−1
∗ R ∗ Ts ∗
u (k) = + · i (k) − i(k) + L Ts · ∑ i (n) − i(n) + e(k)
Ts 2 | {z } |R + 2 n=0 {z |{z}
} Feed forward
Proportional
Integral
(2.9)
where u∗ (k) represents the voltage reference, R is the electrical resistance of the
motor’s armature, L is the inductance of the motor’s armature, Ts is the sampling
period, i∗ (k) is the current reference, i(k)is the measured
current, e(k) is the back-
emf added as a feed-forward and ∑n=k−1
n=0 i∗ (n) − i(n) represents the accumulated
current errors.
Equation 2.10 shows the basic equation for a discrete PI controller derived using
Euler transformation [Bengtsson, 2020].
Ts n=k
u(k) = K p e(k) + ∑ e(n) (2.10)
TI n=0
where u(k) is the discrete control signal, e(k) is the error between the reference
and the actual value of the sampled process, ∑n=k n=0 e(n) is the accumulated errors,
K p is the amplification, TI is the integration time and Ts is the sample time.
By comparing Equation 2.9 with Equation 2.10 and setting e(k) = i∗ (k) − i(k),
e(n) = i∗ (n) − i(n) we see that, if we disregard the feed-forward in Equation 2.9,
that these two equations are almost the same. The boundary of the summation differs
with one sample. We identify that K p = TLs + R2 and TI = L 1 Ts .
R+ 2
14
2.4 Simplified model of the mechanics of a brushed DC motor
If the rotor of a brushed DC motor is simplified as single axis lumped inertia drive
then the motion is described by Equation 2.11 [Leonhard, 2012]
dω
J = mM (ω,t) − mL (ω,t) = ma (ω,t) (2.11)
dt
where J represents the moment of inertia of the rotor, ω represents the angular
velocity of the rotor, t represents time, mM (ω,t) represents the torque due to the
motor current, mL (ω,t) represents the torque due to the load and ma (ω,t) represents
the resulting torque on the system. Such a simplified single axis lumped inertia can
be seen in Figure 2.2.
Figure 2.2 This figure illustrates a simplified model of the mechanics of the rotor
when simplified as a single axis lumped inertia drive - inspired by [Leonhard, 2012]
15
3
System overview
The system marked with dashed lines in Figure 1.1 is the system that will be used
to simulate stored rotational energy. This system consist of a microcontroller, a mo-
tor setup with encoder, an evaluation module (DRV8704EVM) based on the motor
driver DRV8704, a micro SD card, a current measuring circuit and a circuit to sup-
ply pull-up resistors for the signals from the motor encoder. This is the system that
was worked on in this thesis and an overview can be seen in Figure 3.1.
16
3.1 Motor setup
To simplify the process of the laboratory work a command prompt was made
which enabled the user to send input through the serial port to the microcontroller
to carry out commands and to display reply messages. All time critical operations
are interrupt driven which means that the command prompt is not able to steal valu-
able processor time from important operations. To get a better understanding of the
system a list with information about all interrupts can be seen in Table 6.1. Writing
data to the SD card can be seen as a time critical operation - it is not interrupt driven
- and can thus get CPU time hijacked from interrupts. The interrupts are designed
to be executed as fast as possible and the importance of being quick increases with
increasing calling frequency.
Figure 3.2 Existing setup with two brushed DC motors connected on same axis.
Mechanical axis marked in black and motors color-coded - red defined as simulator
motor and blue as system motor.
17
Chapter 3. System overview
Figure 3.3 Figure showing the DRV8704EVM - a platform built to be able to drive
two different brushed DC motors with variable current limiting using a dual H-bridge
3.3 Microcontroller
The Arduino MKR Zero was selected and used in this project to act as the micro-
controller with the purpose to orchestrate and carry out necessary calculations and
operations. The processor of the Arduino MKR Zero is the Arm Cortex-M0 32-bit
SAMD21 processor. The processor contains peripherals with key functions such as
timers, signal generators and analog-to-digital converters that were utilized during
the prototype building. An overview of the microcontroller and how it is connected
can be seen in Figure 3.4.
18
3.5 Operating the system
Figure 3.4 Overview of Arduino pins and what they are connected to. *Only one
of the PWM signals goes to D5 due to lack of OR circuit.
19
Chapter 3. System overview
Table 3.1 Table with information about all commands implemented in the system
to use in the command line used to operate the system
20
4
Current Control
To be able to control the motor current it is necessary to be able to measure the actual
motor current. A system to measure the motor current - with sufficient amplitude
resolution and sample frequency was necessary to be designed and implemented.
The current control in the real door system served as a requirements baseline - with
an current amplitude resolution of roughly estimated 400 mA and a PWM frequency
of 25 KHz - in which the duty cycle is updated at a frequency of 5 KHz.
The main goal was to be able to measure the inline motor current with precision
of around 400 mA, bidirectional currents of maximum 16.5 A in both direction and
be designed to be able to handle higher currents up to 32 A without breaking. The
ambition was to create a prototype fast that could be evaluated and later improved
upon. The current measurement had to be able to measure bidirectional currents due
to the real door system being able to rotate in both directions.
The shunt size was chosen to be able to handle twice the maximum current of the
maximum current of the motor in the real door system with Equation 2.1 in mind.
The goal was to be able to use the same measuring circuit for the real system and
a motor operated at twice the maximum current (twice as strong) without breaking.
The motivation was to prototype faster and not having to design more than one
measurement circuit.
21
Chapter 4. Current Control
the problem that if using a current sense amplifier it has to support high common-
mode rejection [Bridgmon and Andrews, 2016].
It was observed that the voltage measured with the Arduino ADC had a constant
gain error, this was verified by measuring the voltage with a voltmeter and then
comparing it with the voltage measured with the Arduino - for different voltages.
The real voltage was that of 87.5% of what the Arduino measured, Equation 4.1
compensate for this gain error:
For an n bit ADC the number of discrete digital levels that it can produce is given
by:
The resolution of the smallest voltage that can be quantified is then given by:
22
4.3 Current sense amplifier
The ADC is only able to measure non-negative voltages and to be able to measure
negative voltages the Current Sense Amplifier has to be biased such that one part of
the voltage range is used to represent negative currents and the other part positive
currents. The Current Sense Amplifier was biased at half the supply voltage due to
the currents being symmetrical. The region of no output saturation is then described
by:
Vsupply
0≤ + Ishunt Rshunt · gain100 ≤ Vsupply (4.5)
2
From 4.5 the maximum current without output saturation is described by:
Vsupply
Imax ≤ (4.6)
2Rshunt gain100
3.3 V
Imax = = 16.5 A =⇒ Imin = −16.5 A (4.7)
2 · 1 · 10−3 Ω · 100 Equation 4.4
23
Chapter 4. Current Control
Using Equation 4.3 for a 12 bit ADC and a reference voltage of 3.3 V, the voltage
level resolution for the ADC is calculated as:
3.3 V
= 0.806 mV (4.8)
212
The smallest input voltage to the Current Sense Amplifier that the ADC is then
able to quantify and thus also the smallest current this corresponds to when using
Rshunt = 1 mΩ is given by:
0.806 mV 8.06 µV
= 8.06 µV =⇒ I = A = 8.06 mA (4.9)
gain100 1mΩ
This means that the smallest current the system theoretically can measure with-
out regarding any errors is 8.06 mA.
24
4.4 Current sense dead-zones and PWM dependency
Figure 4.1 Print-screen from oscilloscope: Green signal shows the PWM-signal,
olive green signal shows the output from the current sense amplifier. The area inside
the red cursors enclose the time span with very large output error from the current
sense amplifier caused by a change in the state (high to low) of the PWM-signal.
25
Chapter 4. Current Control
Figure 4.2 Print-screen from oscilloscope: Green signal shows the PWM-signal,
olive green signal shows the output from the current sense amplifier. The area inside
the red cursors enclose the time span with very large output error from the current
sense amplifier caused by a change in the state (low to high) of the PWM-signal.
26
4.5 Current control overview
Figure 4.3 Overview of the closed current control loop in the case of "fast com-
puter"
Using Equation 2.11 as a model for the rotor and inserting the force produced by
the motor we get Equation 4.10 which describes the acceleration of the rotor.
dω τmotor − τload
= (4.10)
dt J
where τmotor is the force produced by the DC-motor, τload is the force counter-
acting the motor - such as friction, J the moment of inertia of the rotor and dω
dt is the
rate of change of the motor speed.
27
Chapter 4. Current Control
Rewriting Equation 2.2 to get the expression of the rate of change of the motor
current one obtains:
di Udc − RI − ωψ
= (4.11)
dt L
When a DC motor is unloaded, there is very little mechanical load on the motor
shaft, and the motor can spin at a very high speed with very little current flow-
ing through the electrical resistance of the motor’s armature. This can be seen by
combining Equation 4.11 and Equation 4.10 - if a small current accelerate the rotor
speed to a high speed the resulting back-emf will be large and oppose the flow of
current through the motor’s armature. The back-emf will limit how big the current
can grow.
28
5
Motor Control
To supply the voltage and current required by the motors, a motor driver circuit is
necessary. This circuit also serves to separate and protect the microcontroller from
the high currents and voltages used with DC motors. The DRV8704 used in the
project can handle a supply voltage of 8 V to 52 V. The chosen motor card can
be configured via SPI, making it easy to modify its behavior without needing to
replace hardware. Another highly beneficial feature of the motor driver card is its
capability to report errors. This information can be accessed through the SPI inter-
face, enabling easy detection and resolution of issues without the need for hardware
replacement.
Figure 5.1 Table of how to operate the motor using the motor driver DRV8704
and two PWM-signals xIN1 and xIN2. The red zones marks the modes used in the
project.
29
Chapter 5. Motor Control
To operate the motor, two PWM signals (xIN1 and xIN2) and two GPIO signals
(X and Y) were utilized. The motor output was controlled with the PWM-signals
according to Table shown in Figure 5.1. The GPIO signals X and Y were used to
control each of the half-bridges which can be seen in Figure 5.2
30
5.1 Motor control strategy
u∗ (k)
∗
VV M , 0 ≤ u (k) ≤ VV M
D1 (u∗ (k)) = 1, u∗ (k) > VV M (5.1)
0, u∗ (k) < 0
u∗ (k)
∗
− VV M , 0 > u (k) ≥ −VV M
D2 (u∗ (k)) = 1, u∗ (k) < −VV M (5.2)
0, u∗ (k) ≥ 0
(
∗ 1, u∗ (k) ≥ 0
X(u (k)) = (5.3)
0, u∗ (k) < 0
(
0, u∗ (k) ≥ 0
Y (u∗ (k)) = (5.4)
1, u∗ (k) < 0
where VV M is the DC supply voltage, D1 (u∗ (k)) controls the duty cycle of PWM-
signal xIN1 and D2 (u∗ (k)) controls the duty cycle of PWM-signal xIN2.
31
Chapter 5. Motor Control
5.2 Encoder
An incremental encoder attached on the motor rotor was used - producing two
square-wave signals phased at 90 degrees which are defined as the alpha and beta
signals and that can be used to determine the rotational direction of the motor and
motor position. Each motor revolution produces 500 alpha and beta impulses.
The encoder is using a logical level of 5 V which is to high too be used directly to
the Arduino MKR zero which uses 3.3 V logic. The encoder is using an open-drain
output that can only sink current, meaning we need an external source to supply
current when the signal is high. Figure 5.3 shows the final iteration of the encoder
circuit with a voltage dividers to transform the 5 V logic down to approximately 3.3
V and with pull-up resistors to supply current when the signal is high.
Figure 5.3 Shows the encoder circuit with voltage dividers and pull-up resistors -
with the encoder output marked as Alpha and Beta
32
6
Software design
33
Chapter 6. Software design
Table 6.1 Showing information about all interrupts implemented in the system
higher than the register CCx the output is low otherwise the output is high. The
counter will count up to the value set by the register PER and then restart the count
from zero.
Equation 6.1 express the relationship between the counters clock frequency,
prescaler and the maximum count value (PER) and the frequency of the generated
PWM signal [SAM D21 Family Data Sheet 2018]. The counters were configured to
use a 48 MHz clock frequency, prescaler=1 and maximum count value PER=2000
which using Equation 6.1 results in a PWM frequency of 24 KHz.
fclock 48MHz
fPW M = = = 24KHz (6.1)
Prescaler · PER 1 · 2000
34
6.4 Strategy to go from the case "slow computer" to "fast computer"
Equation 6.2 expresses the relationship between the compare value CCx and the
maximum count value PER that the counter is configured to use and the duty cycle
of the generated PWM signal.
CCx
Duty cycle = (6.2)
PER
One of the counters (TCC1) was configured to generate two of the three PWM
signals - these two signals share the same count value but their output are respec-
tively controlled by different compare registers (CC0 and CC1). The two PWM
signals are defined as xIN1, xIN2 and are fed to the motor driver - thus the signals
controlling the DC motor. The signals can be seen in overview Figure 3.1 and in
Figure 5.1 in which they show how their state relate to the motor output. As the
count value reaches the maximum count value PER an interrupt is called - in the
case of the counter TCC1, TCC1_handler() is called, more information about the
interrupt can be seen in Table 6.1.
By updating the duty cycle every fourth PWM period the resulting sample period of
the duty cycle is described by Equation 6.3.
Ts = 4 · TPW M (6.3)
Using the proposed strategy with synchronized counters to time the calculation of
the new duty cycle - the time between starting to calculate the new duty cycle and
when the new duty cycle is put in use is described by Equation 6.4.
OFFSET
dt = · TPW M , where OFFSET ≤ PER (6.4)
PER
35
Chapter 6. Software design
Figure 6.1 Strategy using synchronized counters to time the calculation of the next
duty cycle closer in time to its usage
where dt is the time to calculate the new duty cycle, OFFSET is the added
count value to offset the second counter TCC0, PER is the maximum count value
and TPW M is the period of the PWM-signal.
How the sample period of the duty cycle relates to the calculation time and the
period of the PWM signal can be seen in Figure 6.2. By comparing the time to
calculate the duty cycle expressed in Equation 6.4 in relation to the sample time of
the duty cycle which is expressed in Equation 6.3 - we get the relative duty cycle
calculation delay which is described in Equation 6.5
OFFSET
dt PER · TPW M OFFSET
= = , where OFFSET ≤ PER (6.5)
Ts 4 · TPW M 4 · PER
where dt
Ts is the relative calculation delay. A relative calculation delay of one
would be the case "slow computer" discussed in the background chapter and a rela-
tive delay of approximately zero would be the case of "fast computer". This under
the assumption that the current measurement used in the duty cycle calculation is
taken in the beginning of the calculation.
36
6.4 Strategy to go from the case "slow computer" to "fast computer"
Figure 6.2 Overview of how the sample period Ts of the duty cycle is related to the
period of the PWM signal TPW M . The figure shows the time interval dt that represents
the delay from when the duty cycle is calculated until it is put in use.
37
Chapter 6. Software design
Values from the ADC were continuously updating a global voltage average. A bit-
shift averaging algorithm was used to calculate the average voltage and can be seen
in Equation 6.6. A fast algorithm was assumed to be needed due to having voltage
measurement taken periodically every 4 µs.
(
acc = (voltage_avg(k) << n) − voltage_avg(k))
(6.6)
voltage_avg(k + 1) = (acc + measurement) >> n
acc denotes the accumulator variable, measurement denotes the newest input sam-
ple from the ADC and n is a factor determining how much the new measurement
will impact the average. This algorithm is commonly used to perform digital filter-
ing of data samples in real-time applications, where the goal is to smooth out the
signal while minimizing processing overhead. The resulting filter introduces a delay
in the system.
The interrupt is called every 6 KHz and the data to be stored is 4 bytes in size, the
generated data rate is then calculated as:
KB
6·4 = 24 KB/s (6.7)
S
The rate of the data generated in Equation 6.7 is a slower than the write speed to the
SD card seen in Table 7.1, which is required to not miss generated data. Designing
for the worse case scenario the maximum writing latency from Table 7.1 is 12.368
ms but this is in a benchmark environment and not tested in the load simulator
environment - in which CPU time is taken by interrupts. Assuming a maximum
latency of 250 ms, a minimum array size required to be able to store all samples if
each sample is taken with a frequency of 6 KHz is calculated as:
variables
Bu f f ermin = Latencymax · = 250 ms · 6 KHz = 1500 (6.8)
time
38
6.6 Strategy to write to SD card in real-time
A double buffer system was designed and implemented to be able to save data with
the DMA while writing to the SD card simultaneously. The buffer size was chosen
to 2560 with Calculation 6.8 in mind. The time to fill the buffer with the DMA
saving one value with a frequency of 6 KHz is calculated as:
2560
= 426.66 ms (6.9)
6KHz
During this time the SD card with a latency of 250 ms and write speed of 480 KB/s
can write an amount of bytes calculated as:
The amount of bytes that an array of datatype Long and size 2560 takes up is cal-
culated as:
To structure the data such that it can be understood when exported from the SD card
extra symbols are required to be written, if an extra 4 bytes is added with every data
point then the total data to be written to the SD card from an array of size 2560 is
calculated as:
The time to fill one buffer of 2560 samples when samples are stored with a
frequency of 6 KHz is calculated according to Equation 6.9 to 426.66 ms. In theory,
the double buffer system should work because the amount of data required to empty
a buffer of 2560 samples, including extra characters, is determined to be 20480
bytes according to Equation 6.12. This quantity is smaller than the data that can be
written to the SD card in 426.66 ms according to Equation 6.10. Therefore, it is
feasible to implement the double buffer system, which allows for continuous data
filling in one buffer while the other buffer is being written to the SD card.
From the command prompt the user starts the data logging, this activates the
DMA, the DMA is configured such that it is triggered by the interrupt that calcu-
lates the duty cycle - when triggered it saves the voltage used for the duty cycle
calculation. A flowchart of the cyclic process of the DMA filling two buffers can
be seen in Figure 6.3. The DMA interrupt is called when a buffer is filled, a flag is
set indicating that it is ready to be written to the SD card. The interrupt checks if
the SD card is done writing to the next buffer - if it is not done an error is incre-
mented, indicating that samples might have been missed due to the DMA filling and
SD writing is overlapping - the flowchart of this erroneous behaviour can be seen in
Figure 6.5 and Figure 6.4 shows the desired behaviour.
39
Chapter 6. Software design
Figure 6.3 Flowchart of DMA configured in cyclic mode and set to suspend the
DMA and trigger a DMA interrupt when a buffer is filled - the interrupt executes
some operations and then reactivates the DMA and the process of filling the next
buffer is started.
Figure 6.4 Time chart of how the DMA and writing to SD card operates in time
when working as expected.
40
6.6 Strategy to write to SD card in real-time
Figure 6.5 Time chart of how the DMA and writing to SD card operates when a
buffer takes to long to write to the SD card and an error is generated. The SD card is
writing from the buffer that the DMA has started to fill with new data - this result in
the risk overwriting data not yet recorded by the SD card.
41
7
Results
Figure 7.1 Schematic of the current measuring circuit - ADC+ and ADC- goes to
the ADC differential inputs of the Arduino.
42
7.2 Dead-zone strategies
Figure 7.2 Print Screen from the Arduino IDE, no implemented method to deal
with dead-zones. Note: the y-axis is the current measured in mA, the X-axis is dis-
continuous and can be broken up in two continuous parts one part (x values from
1554 to 1620) showing continuous samples with duty set to zero and the other part
(x values from 1621 to 2054) continuous samples with duty set to 75%. The blue
graph shows the measured current.
43
Chapter 7. Results
Figure 7.3 Print Screen from the Arduino IDE, Stationary dead-zone skip method
implemented. Note: the y-axis is the current measured in mA, the X-axis is discon-
tinuous and can be broken up in two continuous parts one part (x values from 778 to
910) showing continuous samples with duty set to zero and the other part (x values
from 911 to 1270) continuous samples with duty set to 75%. The blue graph shows
the measured current.
Figure 7.4 Print Screen from the Arduino IDE, Stationary and varying dead-zone
skip methods implemented. Note: the y-axis is the current measured in mA, the X-
axis is discontinuous and can be broken up in two continuous parts one part (x values
from 716 to 920) showing continuous samples with duty set to zero and the other part
(x values from 921 to 1216) continuous samples with duty set to 75%. The blue graph
shows the measured current.
44
7.4 Writing to SD card in real-time using DMA
A moving averaging filter was used to smooth out the motor current measurements.
The filter used is presented in Equation 6.6 and the speed of the filter used was set
to n=1. The resulting filter is derived in Equation 7.1:
(
acc = (voltage_avg(k) << 1) − voltage_avg(k))
(7.1)
voltage_avg(k + 1) = (acc + measurement) >> 1
Using this filter results in that new average is the average between the old average
and the new measurement. This is a filter using recursive feedback and thus cate-
gorized as an Infinite Impulse Response filter (IIR-filter). A difference equation for
the filter is presented in Equation 7.2:
where y(n) is the new average, y(n-1) is the old average and x(n) is the measure-
ment.
45
Chapter 7. Results
Using a PWM frequency of 6 KHz and calculating the duty cycle every fourth PWM
period the resulting sample time of duty cycle calculation is calculated by inserting
the values in Equation 6.3:
1
Ts = 4 · TPW M = 4 · = 166.67µs (7.3)
6 · 103 Hz
The time needed to calculated the duty cycle was experimentally determined and
set to 20 % of the maximum count value PER - using Equation 6.4 the time delay is
calculated as:
The relative duty cycle calculation delay is then calculated using the Equation 6.5
and inserting the values from calculation 7.3 and 7.4 as:
0.2 · TPW M
= 0.05 (7.5)
4 · TPW M
In the background section we compared Equation 2.9 with Equation 2.10 and setting
e(k) = i∗ (k) − i(k), e(n) = i∗ (n) − i(n) we see that, if we disregard the feed-forward
in Equation 2.9 that these two equations are almost the same. The boundary of the
summation differs with one sample. We identify that K p = TLs + R2 and Ti = L 1 Ts .
R+ 2
Inserting the motor parameters and sampling time with the motor resistance of 1
1
Ohm, motor inductance of 6.9 mH and a sample period of 6KHz = 166.67µs we
get:
L R 6.9 · 10−3 1
Kp = + = −6
+ = 41.9 (7.6)
Ts 2 166.67 · 10 2
1 1
Ti = L Ts = −6 = 143.2 (7.7)
6.9·10−3
R+ 2 1 + 166.67·10
2
46
7.7 Current control with blocked rotor
Using a supply voltage of 30 V and inserting the calculated voltage reference from
Calculation 7.8 into the algorithm to calculate the duty cycle found in Equation 5.1
and 5.2 we get:
u∗ (k)
∗
30 , 0 ≤ u (k) ≤ 30
∗ ∗
D1 (u (k)) = 1, u (k) > 30 (7.9)
u∗ (k) < 0
0,
u∗ (k)
∗
− 30 , 0 > u (k) ≥ −30
D2 (u∗ (k)) = 1, u∗ (k) < −30 (7.10)
u∗ (k) ≥ 0
0,
If the calculated duty cycle is a value between 0 and 1 then to generate a matching
PWM signal we rewrite Equation 6.2 to get an expression for how to calculate the
compare register based on a given duty cycle - this is shown in Calculation 7.11
The final proposed current control algorithm with a discrete PI-controller, tuned
with the motor parameters, able to control the current in both directions can be
found in Appendix A.1. It is based upon combining calculations 7.8, 7.9, 7.10 and
7.11.
Due to time constraint the entire control algorithm covering both PWM signals
was not implemented, this because a solution to handle both varying dead-zones
was not implemented in time. Instead the algorithm was limited to only PWM-
signal, meaning that the supply voltage could only be applied in one direction and
thus only able to control torque in one direction. A calculation error that was not
discovered until the writing of this thesis meant that current control experiments that
had been carried out - had been done without a tuned PI-controller. The untuned PI-
controller had a K p = 126 · 103 and Ti = 7. Figures 7.5 and 7.6 shows the current
control with the simplified untuned PI-controller and a blocked rotor. The data is
logged using the double buffer strategy using a DMA to write to a SD-card, which
is presented in Section 6.6.
47
Chapter 7. Results
Figure 7.5 The Figure shows current measurements captured with the DMA and
saved to the SD-card - with the blue dots representing measured current, red dot
indicating the current reference - with the red line indicating a change in the current
reference from -200 mA to -400 mA.
48
7.7 Current control with blocked rotor
Figure 7.6 The Figure shows current measurements captured with the DMA and
saved to the SD-card - with the blue dots representing the current reference and the
red lines indicating the measured current.
49
8
Discussion
50
8.3 Current averaging calculations
negative impact of having to skip a fixed amount of samples. This imposes an upper
limit on how fast the PWM frequency can be when using this strategy.
Skipping samples introduces an varying delay in the current measurements that
are used to calculate the next duty cycle. The effect of this potential delay has not
been fully investigated. The implemented control system used an ADC sampling
frequency of 250 KHz, resulting in one sample being taken every 4 µs. In contrast,
the period of a single PWM signal is approximately 41.7 µs, allowing for potentially
10-11 measurements being taken during one PWM period. In the the worst-case
scenario, 6 measurements are skipped during one PWM period when implementing
the strategy to skip both the varying and stationary dead-zones.
51
Chapter 8. Discussion
52
8.7 Current control with blocked rotor
reference the motor rotor was blocked. This to prevent the motor from accelerating
and changing the dynamics of the motor current by the induced back-emf. It can be
argued that this will be somewhat the case when the control system (the red box) is
used to simulate a load in the system presented in Figure 1.1. This due to the system
motor (blue box) trying to control the connected physical axis to achieve a speed
reference - and thus applying a counter-force to achieve a fixed speed. However this
has not been investigated or experimented with and will require a more in depth
analysis of how it could work.
A PI-controller tuned based on motor parameters and the sample period of the
duty cycle was calculated. However due to not having implemented a way to deal
with the varying dead-zone strategy for both PWM signals a simplified control al-
gorithm was used instead. This algorithm only utilizes one of the half-bridges in
the H-bridge and can thus only apply current (torque) in one direction. Due to a
calculation error the current control experiments had been done with an untuned PI-
controller. The tuned PI-controller has a K p = 41.9 and a Ti = 143.2, the untuned
PI-controller that was used in the experiments has a K p = 126·103 and a Ti = 7. This
is a K p that is approximately 3000 times bigger than the tuned controller, meaning
a very small current error will most likely result in outputting the maximum duty
cycle and completely overtake the control algorithm. Figures 7.5 and 7.6 shows the
experiment of the current control with the untuned PI-controller. Even though the
PI-controller is very badly tuned, it is possible to see that the controller is able to
track the current reference. It would have been very interesting to see the how the
tuned algorithm would perform - however due to time constraints there were no
time to conduct such experiments.
53
9
Conclusion
The project was not a further development on an already existing system and was
designed and built from scratch. A time consuming process, but benefited from
not being locked to certain solutions or components. A lot of work was mainly
conducted to research, to understand and implement basic functionality - such as
how to operate the peripherals of the Arduino MKR Zero.
The goal to create a system that could hardware simulate the stored rotational
energy in a door has not been achieved. This due to no such experiments has been
conducted. Current control of a brushed DC motor was partially achieved which
was assumed to be the foundation to generate a driving force that could be used to
simulate stored rotational energy. Current control was only partially achieved due
to being able to control the current in one direction and not both. A very untuned
PI-controller was used in current control experiments and could follow a current
reference but with heavy oscillation. The oscillations had an amplitude of approxi-
mately 200 mA. It was not established if these oscillations were due to the control
algorithm being untuned or due to measuring noise in the system. A strategy with
double buffers and utilizing a DMA was implemented and tested and was success-
fully able to record the current used in the current control algorithm. The captured
current was stored to a SD-card, so that it later could be analysed.
A current measuring circuit was designed and implemented with the purpose
to be able to measure the inline motor current. The measuring circuit worked on
the principle of measuring the voltage drop across a shunt resistor. The circuit was
designed to be able to handle bidirectional currents of 16.5 A due to this the shunt
resistance had to be small. A current sense amplifier was utilized to amplify the volt-
age drop across the shunt to be able to utilized the full voltage range of the ADC.
The current sense amplifier was also selected due to its ability to reject common-
mode voltage transients. Common-mode voltage transients are created due to the
switching of the H-bridge. The current sense terminals either experience the supply
voltage or ground and the switching of the H-bridge causes both of these terminals
to rapidly change potential. The common-mode transients caused a large distur-
bance in the output of the current sense amplifier and is referred to as a measuring
dead-zone in this thesis. It was analysed that up to two dead-zones could appear
54
9.1 Limitations
during a PWM-period. The first dead-zone could only occur in the beginning of
the PWM period and due to this was named the stationary dead-zone. The second
dead-zone could occur at any time during the PWM period and was thus named
the varying dead-zone. The time a dead-zone lasted was measured and a strategy to
deal with them was implemented. The strategy was based upon skipping measuring
samples. The stationary dead-zone was dealt with by always skipping the first sam-
ples at the start of a PWM period. The varying dead-zone was dealt with by having
the PWM signal as an input to the microcontroller. A transition in the PWM signal
triggered an interrupt that caused the system to skip samples. This method dealt
with the disturbances caused by the common-mode voltage transients effectively
but came at the cost of introducing a delay in the current measurements.
9.1 Limitations
Due to time constraint the current control was limited to the scenario with blocked
rotor and only using one direction of the H-bridge.
55
A
Code
if (dutyCycle => 0)
{
REG_TCC1_CCB0 = 0;
REG_TCC1_CCB1 = min(dutyCycle, 1) * 2000;
X = 1;
Y = 0;
}
else
{
REG_TCC1_CCB0 = -max(dutyCycle, -1) * 2000;
REG_TCC1_CCB1 = 0;
X = 0;
Y = 1;
}
56
A.2 Simplified and untuned control algorithm
if (dutyCycle => 0)
{
REG_TCC1_CCB0 = 0;
REG_TCC1_CCB1 = min(dutyCycle, 1) * 2000;
X = 1;
Y = 0;
}
else
{
REG_TCC1_CCB0 = 0;
REG_TCC1_CCB1 = 0;
X = 0;
Y = 0;
}
57
Bibliography
58
Bibliography
SAM D21 Family Data Sheet (2018). ds40001882d. Low-Power, 32-bit Cortex-
M0+ MCU with Advanced Analog and PWM. Microchip Technology Inc.
URL: https : / / content . arduino . cc / assets / mkr - microchip _
samd21 _ family _ full _ datasheet - ds40001882d . pdf ? _gl = 1 *
12ox7kk * _ga * MTQ4NTMyNzM2Ni4xNjQyNTgxMzU1 * _ga _ NEXN8H46L5 *
MTY1MzQ5ODE4MC4xNTQuMS4xNjUzNDk4MTgyLjU4. (Accessed: 2022-05-25).
59
Document name
Lund University
MASTER’S THESIS
Department of Automatic Control Date of issue
Box 118 October 2023
SE-221 00 Lund Sweden Document Number
TFRT-6220
Author(s) Supervisor
Eric Schyllert EricWarnquist, ASSA ABLOY Entrance Systems AB
Per Byrhult, ASSA ABLOY Entrance Systems AB
Anton Cervin, Dept. of Automatic Control, Lund
University, Sweden
Karl-Erik Årzén, Dept. of Automatic Control, Lund
University, Sweden (examiner)
The main goal with the thesis was to design, construct and evaluate a system able to drive and brake a
mechanical axis to simulate loads and stored rotational energy - a hardware force simulator. The force
simulator was then to be integrated in a existing hardware-in-the-loop testing rig to enhance the
mechanical dynamics of door simulations. The force simulator was to be realised using a brushed DC
motor and by controlling the motor torque. Controlling the motor torque of a brushed DC motor is
achieved by controlling the motor current - and this is mainly what this thesis is about. The thesis
explains the foundations of an embedded system capable to control and log the current of a brushed
DC motor.
The force simulator consisted of a microcontroller, a motor card, a circuit to measure the inline
motor current and a brushed DC motor. The circuit to measure the current was designed and
implemented, consisting of a shunt resistor and a current sense amplifier. The shunt was placed in
series (inline) with the motor. A software strategy was developed and implemented to deal with noise
due to common-mode voltage transients (caused by motor control using PWM). This strategy came
with a cost of introducing a measurement delay in the system.
A discrete PI-controller to control the motor current was researched and implemented. An expression
to optimal tune the controller based on motor parameters and the sampling period was researched.
Current control experiments were conducted but due to a calculation error the PI-controller was very
untuned, this compared to the tuned expression. This error was discovered very late in the work and
there was no time to redo the experiments. The experiments with the implemented untuned
PIcontroller were conducted and a current reference was successfully tracked, however with noise
which had an amplitude of approximately 200 mA.
A strategy to record current measurements used in the current control, without missing any samples
was successfully developed. It worked by using a double buffer system, filling the buffers utilizing a
DMA and then written to a SD card once a buffer was filled. With the main objective to develop the
capability to be able to analyze the current control algorithm.
Keywords
http://www.control.lth.se/publications/