ELEC2540-ELEC2540代写
时间:2023-04-27
School of Electronic &
Electrical Engineering
FACULTY OF ENGINEERING
ELEC2540
Control Systems
Laboratory: Closed-Loop Control Analysis for a Proportional/Integral System
Revision: 2.0
Created by Roger Berry
ELEC2540 Page 2 of 46 2022-23
Control Systems
Notes:
The instructions for the laboratory experiments should hopefully be clearly explained
below. However, if you are not sure about anything, please ask. Laboratory work is not
solely about doing practical experiments; it is also an opportunity to reflect on theoretical
material and consider how it relates to the practical applications. Discussing these things
with each other or with the instructors is encouraged, as this will help you get the most
from the learning opportunity.
Where you see the  symbol, this means that there is something to measure, observe
or action and therefore make appropriate notes in your logbook. If you are unable to
extract and print screen captures from instrumentation and insert them into your logbook
then make sure your drawings are accurate, neat and represent what you see.
If you find any errors in this documentation, then please inform one of the instructors.
Introduction:
A compass is a device to determine a specific direction. Its’ most common application is to
determine a direction relative to the cardinal directions North, South, East and West.
Compasses are widely used for orientation and navigation. The first use of compasses for
navigation dates to the 11th century in China and to the 13th century in Europe and Persia.
The most common type of compasses is the magnetic compass. It consists of a
ferromagnetic pointer mounted on a low-friction pivot point, see Figure 1. The pointer then
orients itself towards the Earth’s magnetic field. As the field lines of the Earth’s magnetic
field are oriented approximately in a geographical North-South orientation, a magnetic
compass allows to obtain a relatively accurate estimate of the ”geographic North”, also
called ”true North”. Depending on where the compass is located on the Earth surface, the
deviation between the ”magnetic North” provided by a magnetic compass and the
“geographic North” may vary. Likewise, if the compass is placed close to another magnetic
field, e.g., a machine, this will also affect the accuracy of the obtained “magnetic North”.
In recent years, solid-state compasses have become more and more popular in mobile
devices, such as smartphones. Solid-state compasses usually consist of a magnetometer,
which provides information of the Earth’s magnetic field to a microprocessor. In a
smartphone, the actual direction can then be displayed by an app, see Figure 1 below.
Figure 1 Mechanical compass and modern compass App
ELEC2540 Page 3 of 46
Control Systems
In this lab, you will use the positional data from an electronic compass, the ‘CMPS12’
which is attached to a continuous servo via an extension arm, and use this data within a
closed loop control algorithm running Proportional (P) and Proportional/Integral (PI) control
software, in an attempt to maintain the compass at the ‘North’ position regardless of the
relative position of the servo or the initial starting point of the compass pointer.
In this lab you will:
• Interconnect all of the component parts and link the system to a virtual terminal
session on your Lab PC running ‘Mttty’ software
• Via a Human Machine Interface (HMI), calibrate, investigate and evaluate the
functionality and performance of the hardware
• Via the HMI you will be able to investigate the effects of the control system
constants such as the gain ‘K’ and the integration time ‘Ti’ (refer to Figure 2), in real
time, on P and PI closed loop control systems based on the Ziegler-Nichols method
for determining the initial values for these system constants
• Via pulley belt ratio options you will investigate the effects of the speed of response
of a drive system i.e, the continuous servo, and evaluate how the values of ‘K’ and
‘Ti’ are affected by these changes
• You will investigate the effects of saturation of a closed loop system and how ‘Anti-
Windup’ affects the performance by the application of ‘Back-Calculation’ feedback
via a control system constant ‘KA’
• You will be able to observe the maths outputs of the control equations at different
points in the control algorithm. This is performed by passing the absolute signed
maths values, at specific points in the algorithm, through Digital to Analogue
Converters (DACs) which make the maths observable on an oscilloscope in real
time. This facilitates critical analysis of the performance of both the complete control
system and the algorithm when K, KA and Ti are changed
There are many ways in which to implement PI and PID control systems but the method
described in this lab is founded on the Ziegler-Nichols method, which uses the Integration
Time as a critical control value, rather than just the gain constants signified by ‘K’ values.
2022-23
ELEC2540 Page 4 of 46
Control Systems
Section 1: Proportional-Integral (PI) Control:
PI controllers are a type of feedback controllers which are very often used in industrial
control systems. In many cases, the control performance can be improved by adding a
derivative (D) part to the PI controller, yielding a PID controller. But for the purpose of this
lab, a well-tuned PI controller provides a good enough control performance.
The Standard PI Controller in the Time- and Laplace-Domain
In the time-domain, the standard form of a PI controller is where:
As shown by the control scheme below:
Figure 2 PI control system in the Laplace domain
(1.1)
2022-23
ELEC2540 Page 5 of 46
Control Systems
Digital Implementation of a PI Controller
The aim of this lab is to tune a PI controller to operate an electronic compass. Therefore,
the PI controller (1.1) should be implemented on a microcontroller, in the present case on
a PIC16F1779. Yet, the controller (1.1) is represented in what is called continuous time. In
a continuous time representation, time is viewed as a continuous variable. Another way of
looking at this is to view a period of time being split into infinitesimally short sections.
However, a microcontroller is not capable of performing calculations in infinitesimally short
time intervals. But the microcontroller can perform calculations every certain time interval,
typically called sampling time. Therefore, the implementation of a PI controller on a
microcontroller requires the continuous-time PI algorithm in (1.1) to be discretised. There
are several methods to do this, but discussing these here would be out of the scope of this
lab. Instead, one particular common form of a discretised PI controller is discussed. To this
end, the sampling interval is denoted by Δt. Now, assume we are at a point in time tk >= 0:
Then, the control error at tk is given by:
The next point in time at which the microcontroller can provide an updated actuator signal
‘u’ is
Note that for calculating u(tk+1); the microcontroller can use the current control error e(tk)
(this is proportional control), but also all previous control errors at all previous sampling
times tk, tk-2 …..t1; where t1 = 0 (this is integral control). Hence, the discrete-time version of
the PI controller (1.1) is given by:
which can be implemented on a microcontroller.
Some General Design Rules for PI Controllers
As you certainly can imagine, the implementation of the PI algorithm (1.2) is only one step
in designing a controller for a specific task. But in order to achieve a satisfactory closed-
loop performance, one also needs to select appropriate numerical values for the feedback
gain K and the integration time constant Ti: It is obvious that, though many physical and
industrial systems can be controlled using PI controllers, in most cases the parameters K
(1.2)
2022-23
ELEC2540 Page 6 of 46
Control Systems
and Ti will have different numerical values depending on the physical properties of the
system to be controlled. The process of determining these values is called controller
design or controller tuning. Hence, controller tuning means to adjust the parameters K and
Ti such that the closed-loop system consisting of the plant in feedback with the PI
controller, as shown below in Figure 3, satisfies certain performance requirements.
Figure 3 Basic control loop in the Laplace domain
Two examples for performance requirements are speed and accuracy of the control
system. Here are some general guidelines for tuning of PI controllers taken from [1].
• Increasing proportional gain K decreases stability.
• Error decays more rapidly if integration time Ti is decreased.
• Decreasing integration time Ti decreases stability.
The Ziegler-Nichols Method
Proper tuning of PI controllers can be a very challenging task. Therefore, variety of
methods have been developed which provide a structured and systematic way for tuning.
One of the most prominent PI design methods is the one developed by Ziegler and Nichols
in 1942 [1]. Even today, the Ziegler-Nichols method or modifications of it are widely used
in practice. The main reason for this may be their simplicity.
The Ziegler-Nichols method based on the frequency response of the plant dynamics is
introduced as follows [1, Chapter 6.2]1. This method relies on determining the so-called
ultimate gain Ku and ultimate period Tu of the system. The specific design steps are as
follows.
1. Run the system with the proportional algorithm first
2. Set the feedback gain K to a low value, e.g., K = 0:01
1 It is said to be based on the frequency response, because the design uses the information of the
point, where the Nyquist curve of the systems’ open-loop transfer function intersects the negative
real axis.
2022-23
ELEC2540 Page 7 of 46
Control Systems
3. Increase K slowly until the closed-loop system starts to oscillate consistently. The
gain at which this occurs is the ultimate gain Ku
4. The period of this consistent oscillation is the ultimate period Tu
5. Ziegler and Nichols now provided some formulas to determine the controller
parameters K and Ti from the ultimate gain Ku and ultimate period Tu: These
formulas are listed in Figure 4 below
6. Run the PI algorithm and apply the values of K and Ti and observe the system
performance and adjust the values in an attempt to ‘tune’ the control system
Figure 4 Zeigler Nichols formulas
Attention! The Ziegler-Nichols method is a heuristic PI design method, which uses a
simple characterisation of the system dynamics. Therefore, it may not necessarily give
control parameters yielding an excellent control performance. In fact, it often only provides
a moderately good control performance. It has, however, the advantage of providing
simple tuning formulas based on direct experiments. It also may provide a good starting
point for a more extensive control design procedure.
2022-23
ELEC2540 Page 8 of 46
Control Systems
Section 2 - Task1: Getting to know your hardware and HMI:
The hardware consists of the following:
• A PC running ‘Mttty’ virtual terminal at 9600 baud rate, 8 data bits, 1 stop bit
• USB to RS232 TTL 5V communications lead
• Power cables for +12 V and 0 V and -12 V and 0 V
• The Compass Processing Unit which performs the following functions:
o Regulates the incoming ±12 V to ±5 V for system power
o Indicate the power status for ±12 V via two red LEDs
o Power monitoring via two Analogue to Digital Converters (ADCs)
o Four analogue output channels test points, CH1 to CH4, with a voltage range
of ±1.25V and a 10 bit resolution. These are used to display real time maths
values within the control algorithm and also show absolute values from the
CMPS12 compass unit
o Precision reference and voltage offset for accurate, signed, DAC conversions
o Trigger test point output. This output is used initially for calibration of the
microcontroller R/C oscillator, but once initialised it is used as the scope
input trigger signal (positive pulse with a width of 4 μs and an amplitude of 0
to 5 V, sourced via a 1KΩ resistor). This function allows for accurate, relative
measurement of the control algorithm maths from each of the DAC outputs
CH1 to CH4 test points
o Common ‘GND’ test point output for the scope reference
o Interconnectivity to the compass drive unit:
▪ I2C communications for extracting compass data (running at 50 KHz)
▪ PWM output for servo control, based around a 1.5 ms pulse in a 20
ms period for no servo motion of the continuous servo
o RS232 TTL 5V communications port
o Runs the HMI and control algorithms
o Push button (unused)
• Compass Drive Unit:
o Continuous servo controlled via PWM
o CMPS12 controlled via I2C and connected via a 6 way slip ring for
continuous rotation
o Power indicator for +5 V
o Manually selectable pulley system
▪ 2:1
▪ 1:1
▪ 1:2
• Compass Control Unit to Compass Drive Unit interface cable
2022-23
ELEC2540 Page 9 of 46
Control Systems
Section 2 - Task 1A: Understanding the pulley/belt ratiometric
drive
In order for you to investigate the effects of changing the system response time due to
physical properties, such as the speed of the drive mechanism, a ratiometric pulley/belt
drive system has been implemented providing 1:1, 1: 2 and 2:1 drive connectivity between
the servo and the compass pointer.
The drive connectivity is via a rubber ‘O’ ring (the belt), which has a degree of elasticity.
This elasticity helps you to easily change the drive ratio for a given experiment. When
changing the ratio of the belt drive system always remove the ‘O’ ring from the smallest
pulley first to release it from the mechanism, then move the belt to the largest pulley of
your chosen ratio and then re-attach the belt to the parallel pulley. By doing this you will
avoid stretching the ‘O’ ring excessively and reduce the stress on the compass pointer slip
ring.
The ratios are as follows:
Figure 5 Ration set for 1:1 i.e. one full turn of the servo produces one full turn of the compass pointer. This is
the base setting
Figure 6 Ratio set for 1:2 i.e. one full turn of the servo produces two full turns of the compass pointer i.e. the
response time is faster
2022-23
ELEC2540 Page 10 of 46
Control Systems
Figure 7 Ratio set for 2:1 i.e. two full turns of the servo produces one full turn of the compass pointer i.e. the
response time is much slower
During this lab you will be asked to repeat your experiments by adjusting this ratio and
observing, documenting and analysing the effects of these changes.
Section 2 - Task 1B: Setting up the virtual terminal
 Action the following steps:
• Download the program ‘Mttty.exe’ from Minerva onto your PCs. Do not run this
software yet.
• Connect your USB to RS232 cable assemblies to any available USB port (Note that
it may take up to two minutes for the PC to find the drivers for the USB device).
• Run the ‘Mttty’ software and you will get a screen similar to that shown below
Figure 8 Mttty software application
• Ensure that the ‘Baud’ is set to 9600, ‘Parity’ is set to ‘None’ and the ‘Stop Bits’ is
set to 1
• Select the ‘Font’ button and select ‘Courier new’ and font size 10
Scroll bar
2022-23
ELEC2540 Page 11 of 46
Control Systems
• In the ‘Port’ window you will see a list of ‘COMx’ devices that are connected. The
number that is allocated is not consistent between PCs but it will be a number larger
than ‘COM3’. Select your device
• Go to the ‘File’ tab and select ‘Connect’
• You should now see a black flashing square in the bottom left hand corner of the
comms display window. Make sure that the right hand ‘Scroll’ bar is at the bottom of
its travel (see red circle in Figure 8 above)
• Your USB lead has now been recognised and connected for a virtual terminal
session
Section 2 - Task 1C: Connecting the compass components
Figure 9 Complete compass configuration
Action the following steps:
• Connect the system together as shown above. NOTE that the jack plug on the end
of the USB communications cable must not be connected to the system at this
point. WARNING: If the USB cable is connected to both the Compass Processing
Unit and to the PC then the compass will not power up correctly due to power
availability through the USB comms RX line, even when the main power supply is
off, potentially resulting in an undefined ‘NORTH’ position
• Set the two channels of the power supply unit to +12 V and set the current limit of
the left hand unit to 80 mA and the right hand unit to 1.3 A (the reason for setting
the right hand current much higher is because the positive supply drives the servo)
• Ensure that the power outputs are off and then connect the power supply leads
between the supply and the Compass Processing Unit as shown in the image
below:
CMPS12
Compass
Drive
Unit
Interconnectivity
cable
Compass
Processing Unit
Power
cables
USB to RS232
TTL 5V
2022-23
ELEC2540 Page 12 of 46
Control Systems
Figure 10 Power connections
• Switch both power supply outputs on and observe the following:
o The two red power LEDs on the Compass Processing Unit should illuminate
o The red power LED in the Compass Drive Unit should illuminate
o The current on the left hand power supply should read approximately 12 mA and
the right hand current should read anything between 50 mA to 120 mA
depending on the activity of the servo
o The compass will rotate clockwise for 2 seconds and then anticlockwise for 2
seconds and should then stop. However, if the servo needs an offset adjustment
then the pointer will continue to rotate in an unspecified direction. This will be
addressed in the following instructions
o Connect the communications cable jack into the Compass Processing Unit jack
socket
o Press the ‘enter’ key on your PC keyboard and you should see the following
screen
2022-23
ELEC2540 Page 13 of 46
Control Systems
Figure 11 HMI start up screen
Note that, on this ‘HOME’ screen, the ‘System Status’ gives you information about the
state of the hardware, the present compass position is also displayed, however, this will
not be valid if the compass pointer is moving since it is the last recorded position when the
screen was refreshed.
Section 2 - Task 1D: Calibrating the oscillator
Action the following:
If you look at the ‘System Status’ message you will see that the ‘Oscillation Calibration
Status’ indicates ‘Oscillator not calibrated’. In this application, the PIC16F1779
microcontroller uses the internal R/C oscillator rather than an external crystal oscillator and
the operational frequency of the microcontroller may require minor adjustment to ensure
precise operation. The internal frequency can be tuned by selecting option number 1,
which will result in the following message being displayed:
Figure 12 HMI message displayed for option number 1
2022-23
ELEC2540 Page 14 of 46
Control Systems
Connect an oscilloscope to the ‘TRIGGER’ test point output of the Compass Processing
Unit and the ‘GND’ lead of the scope to the ‘0V’ test point output. Observe the square
wave and set the scope to measure ‘frequency’ on your selected channel:
Figure 13 Frequency calibration
Adjust the frequency to get as close to 500 Hz as possible by selecting the ‘+’ key or the ‘-‘
key on your PC terminal. When you are happy that the frequency is as close to 500 Hz as
possible then select ether ‘x’ or ‘X’ to exit option 1.
Your virtual terminal will refresh the screen and you will now observe that the status of the
‘Oscillation Calibration Status’ now indicates ‘Oscillator calibrated’.
Note that every time you power down the control system you must go through this
procedure. If you see a square wave output on the ‘TRIGGER’ terminal it means that you
have not calibrated the oscillator
Section 2 - Task 1E: Setting the servo null point
Action the following:
Continuous servos have a manual adjust which may drift in time. It is possible that once
you have calibrated the frequency of the microcontroller that the compass pointer is still
rotating or has started to rotate. If this is happening then the compass ‘Null’ point needs to
be adjusted. This can be performed by option 2 ‘Adjust servo null point’ from the ‘HOME’
HMI.
Select this option and you will see the following text line:
Figure 14 HMI message displayed for option number 2
2022-23
ELEC2540 Page 15 of 46
Control Systems
If your compass pointer is rotating anticlockwise then press the ‘+’ key several times and
you will observe that the compass pointer starts to slow down and eventually stops. Keep
on pressing the ‘+’ key and eventually the compass will start rotating clockwise. This
means that you have passed through the ‘Null’ zone.
Similarly, if your compass pointer is rotating clockwise, then press the ‘-’ key several times
and you will observe that the compass pointer starts to slow down and eventually stops.
Keep on pressing the ‘-’ key and eventually the compass will start rotating anticlockwise.
This means that you have passed through the ‘Null’ zone.
When you are setting the ‘Null’ point observe how many ‘-‘ or ‘+’ are required to cross the
null zone and then divide that number by 2 and press either the ‘-‘ or the ‘+’ keys to enter
the centre of the ‘Null’ zone.
To exit option 2, enter either ‘x’ or ‘X’ and the virtual terminal screen will refresh.
Section 2 - Task 1F: Testing the DACs
Action the following:
To ensure that your hardware DACs are functioning correctly there is option 3 ‘Test DACs’.
When this option is selected you will observe ‘triangle’ waveforms on the CH1 through to
CH 4 test point outputs. These wave forms have an amplitude of ±1.25 V and a frequency
of approximately 2 Hz, see Figure 15 below. Check all of the DACs are outputting this
signal. Enter ‘x’ or ‘X’ to exit this option.
Note that a 2 Hz signal has a slow time base and therefore it can take up to 2.5 seconds
for the scope to trigger and capture the waveform.
Figure 15 Option 3, Test DACs. CH1 and CH2 test points
2022-23
ELEC2540 Page 16 of 46
Control Systems
Section 2 - Task 1G: Setting the compass pointer to
approximate North
Action the following:
Select option number 4 and the compass will automatically move as close to the ‘North’
position as possible (note that in a lab environment there may be electrical influences that
affect the compass so you may find that your compass ‘North’ position does not
necessarily point to exactly the same position as others in the lab).
When the option is selected you will note that the HMI displays the start compass position
point in degrees and then, when it gets to North, it refreshes the screen and you will see
the present compass position in the status section of the screen, which should read
something close to 0° ±1°.
Section 2 - Task 1H: Setting the compass pointer to
approximate South
Action the following:
Select option number 5 and the compass will automatically move as close to the ‘South’
position as possible (note that in a lab environment there may be electrical influences that
affect the compass so you may find that your compass ‘South’ position does not
necessarily point to exactly the same position as others in the lab).
When the option is selected you will note that the HMI displays the start compass position
point in degrees and then, when it gets to South, it refreshes the screen and you will see
the present compass position in the status section of the screen, which should read
something close to ±180° ±1°.
The purpose of being able to set the ‘South’ position is so that you can start your compass
at the worst position possible prior to starting your closed-loop control algorithms. This
allows you to assess the performance of the system from a consistent and repeatable
position, making system analysis much simpler. The compass positioning is available in all
of the control algorithm options i.e. options 8, 9 and 10.
Section 2 - Task 1J: Rotate the compass clockwise
Action the following:
Select option number 6 and the HMI will display the following text:
Figure 16 HMI message displayed for option number 6
2022-23
ELEC2540 Page 17 of 46
Control Systems
The servo PWM signal has a central pulse value of 1.5 ms for a condition of ‘no rotation’
i.e. the ‘Null’ point. However, if that pulse deviates by ± X, where X is in μs and does not
exceed 500 μs then the servo will rotate at a speed defined by the variance from the ‘Null’
point. Option 6 allows you to characterise the performance of the servo in a clockwise
direction.
• With your pulley belt system set for a 1:1 ratio, enter the value of 30 into the HMI.
The compass pointer will now start rotating in a clockwise direction, at a constant
speed.
• Connect the scope probe to the CH4 test point output and observe the saw-tooth
waveform similar to that shown below:
Figure 17 Option 6 with 30µs selected
This signal is the raw compass data from the CMPS12 ie. 0 to 359.9°, that has then been
scaled and offset such that 0 V = 0°, -179.9°= -1.25 V and 180° = +1.25 V using the
following software (note that due to analogue buffer inversion on the PCB the signal is
inverted in software):
2022-23
ELEC2540 Page 18 of 46
Control Systems
Figure 18 CompassToCH4’ function to convert compass angular information into an analogue signal for DAC6
(CH4 output)
Note that the frequency of the signal in Figure 18Figure 17 is approximately 240 mHz i.e. a
period of 4.17 s. This implies that if the PWM offset is set to 30 then the rotational speed of
the servo is 0.24 revolutions per second, or 14.4 rpm. By changing the value of the PWM
offset in option 6 it is possible to determine the complete transfer characteristics of the
servo i.e. how its speed changes with changes in the PWM offset value. This is an
important feature that allows for critical evaluation of the performance of the closed loop
hardware.
To exit option 6, enter a zero value for the PWM offset. The HMI screen with then refresh.
Section 2 - Task 1K: Rotate the compass anticlockwise
Action the following:
Repeat the same test above but use option 7 i.e. the compass will move in an
anticlockwise direction.
2022-23
ELEC2540 Page 19 of 46
Control Systems
Section 3 - Task 1 Understanding the relationship between the
maths and the DAC analogue outputs:
In order for you to truly understand what you will be observing on the oscilloscope it is
important to understand how the DACs for CH1 through to CH4 are defined. These
channels are used for different maths points in the equations depending on which control
algorithm you have selected however, the maths/DAC relationship is common to all of
them. The CHx designation is as follows:
• CH1 : ‘Error’ signal, the difference between the present compass position and
‘North’ i.e. 0°
• CH2 : ‘ProportionalResult’ i.e. ‘Error x K’ signal, the error signal times the
proportional gain value
• CH3 : ‘IntegrationResult’, the value of the summation of the Error x Δt multiplied by
the integration constant defined by K/Ti. Δt is the algorithm processing loop time in
ms.
• CH4 : In the ‘HOME’ screen this output is the absolute compass position as
described earlier, however, in both the ‘Run proportional/Integral Control’ option 9
and ‘Run proportional/Integral with anti-windup Control’ option 10, CH4 outputs the
‘ProportionalIntegralResult’, the total result from the calculated algorithm prior to the
servo offset limits being applied.
Let us consider CH1 in more detail. CH1 will always show you the absolute error between
the present compass position and the ‘North’ position i.e. the target point for the algorithm.
Since the algorithm performs the error maths in degrees i.e. your error signal within the
maths is the present compass position defined by a positive limit of 180.0 through to a
negative limit of -179.9 relative to the target position ‘North’ which is 0.
The following section of code (Figure 19 below) is taken from the ‘Proportional’ control
algorithm, available within option 8, and shows the calculation of the error:
= ℎ − _ (Line 1604)
Where ℎ = 0 and _ is the present compass position
Figure 19 Section from the 'Proportional' control algorithm, calculating the error value and then loading the
absolute maths value into the DAC1 i.e. CH1
2022-23
ELEC2540 Page 20 of 46
Control Systems
There are a few points to note about this code and its output. Firstly, the error can never
be greater than +180.0 or less than -179.9. Secondly, the voltage output that is sent to the
DAC is the absolute signed binary value of the error. This means that the voltage seen on
the output is the scaled range of the DAC based on a range of ±1.25 V and a DAC
resolution of 1023 e.g. if the instantaneous error value was 165.3 then the actual voltage
output from the DAC for that error value would be approximately:
=
2.5
1023
Where 2.5 is the voltage range of the DAC and 1023 is its resolution (10 bits)
= 165.3 2.4438 10−3
= +0.40396
Due to the resolution of the DAC this value will be quantised to the nearest actual value of
165 i.e.
= 0.403
This means that when you observe any of the analogue values displayed through CH1 to
CH4* you can take an instantaneous value such as 0.403 V, from the example above, and
convert it into the absolute maths error value, within the limits of the resolution of the
system i.e.
ℎ =
1023
2.5
ℎ = +0.403 409.2
ℎ = +164.9
As you can see, this value has a percentage error of 0.24% against the original maths
value of 165.3. However, this is not considered to be of significance in relation to the
analysis performed in these lab experiments.
It is possible to achieve a higher degree of observational, instantaneous accuracy by
applying a gain/attenuation value to the channel. This is an option within the HMI for a
given control algorithm. The application of this gain/attenuation can be seen in lines 1257
of the code shown in Figure 20 below.
2022-23
ELEC2540 Page 21 of 46
Control Systems
Figure 20 'LoadDAC_CH1' function
This theory is applied to all the maths calculations that are displayed through CH1 to CH4.
If you observe a flattening of your signal at ±1.25V then it suggests that the maths has
exceeded the approximate value ±512 and therefore you should consider applying and
attenuation factor of 0.5 or less to allow you to see the true maths response. If the maths
results are two small then consider applying a gain of 2 or more to see the values in more
detail.
It is possible to use the scope channel V/div to perform the latter task but you will see an
increase in noise and therefore the visual results are not as good.
Note: If you apply a gain/attenuation factor then remember to ensure that your calculations
are corrected accordingly
Section 3 - Task 2: Characterising the servo drive
Ensure that you truly understand the hardware and the HMI before you undertake this
task.
 Action the following:
Referring to Section 2 - Task 1J and with your pulley system set for a ratio of 1:1, perform
the following task:
1. Using option 6 from the ‘HOME’ screen of the HMI, and starting with a PWM offset
of 50, observe and record, on the CH4 test point output, the speed of rotation of the
2022-23
ELEC2540 Page 22 of 46
Control Systems
compass. Increment the PWM offset by 50 and repeat the process. The last
measurement should be taken when the PWM offset equals 500
2. Exit option 6 and draw a graph of your results, where PWM offset is on the X axis
and the speed is on the Y axis
3. Repeat steps 1 and 2 with the pulley ratio set for 2:1
4. Repeat steps 1 and 2 with the pulley ratio set for 1:2
5. Critically analyse, comment on and discuss your results
2022-23
ELEC2540 Page 23 of 46
Control Systems
Section 4 - Task 1: A practical example of system set up and
analysis based on Proportional Control
It is not possible to critically evaluate the following tasks unless you clearly understand the
information set out in Section 3 – Task 1 and also understand the software described in
Appendix A. If you are in any doubt, then ask an instructor to help assist you in your
understanding.
 Action the following:
• Set your pulley ratio to 1:1 and select option 8 for proportional control. You will see
the following screen:
Figure 21 Screen shot for Option 8, Proportional control
• Set the GAIN (K) to 1.0 using option 2 and ensue that the CH1 and CH2
gain/attenuation values are set to 1.0 (check the status values). If they are not, then
edit them via the options 3 and 4
• Always check the status section of the HMI after each option action
• Set your compass to ‘South’ using option 6
• Take your BNC to crock clip lead and connect the BNC end to the back of the
oscilloscope (EXT TRIG), connect the red read to the ‘TRIGGER’ terminal on the
Compass Processing Unit and the black lead to the ‘0V’ terminal on the Compass
Processing Unit (This references the trigger circuit to the scope).
• Set the scope for ‘Trigger Type Edge’, a source of ‘External’ and a slope of ‘Rising’
with a threshold of 1.0 V
• Set the time base of the scope to 500ms/div and connect the CH1 test point output
of the Compass Processing Unit to CH1 of the scope with 1 V/div (connect the
ground lead of the scope to the 0V terminal on the Compass Processing Unit
• Press the ‘Single’ button (top right of the scope)
2022-23
ELEC2540 Page 24 of 46
Control Systems
• Select option 1 and the following should occur:
o The compass pointer will now move to the ‘North’ position
o The scope should display an image similar to the following (note that it may
be negative rather than positive since it depends where the ‘South’ point
starts from i.e. either > +170° or < -170°)
Figure 22 Proportional control with K = 1.0 and CH1 gain/attenuation = 1.0
• This image shows the following:
o The trigger point is at the centre of the scope
o The maths error is at its maximum i.e. the pointer was in the ‘South’ position.
The maximum error in the maths is 180 which equates to 0.439 V (which is
not dissimilar to what you see in the image above)
o The Proportional algorithm, shown in Appendix A, is attempting to move the
compass pointer back to ‘North’
o It has taken approximately 1 second to return to North
• The detail is not well defined so let us maximise the performance of the signal and
the image
• Press ‘x’ to exit option 1
• Apply a gain of 2 to CH1 by using option 3 (check updated status for new values)
• Increase the time base to 200 ms/div
• Move the compass pointer back to ‘South’ via option 6
• Press the ‘Single’ button (top right of the scope)
• Select option 1 and the following should occur:
o The compass pointer will now move to the ‘North’ position
o The scope should display an image similar to the following (note that it may
be negative rather than positive since it depends where the ‘South’ point
starts from i.e. either > +170° or < -170°)
2022-23
ELEC2540 Page 25 of 46
Control Systems
Figure 23 CH1 gain/attenuation increased to 2.0 and time base increased to 200 ms/div
• This image shows the following:
o The trigger point is at the centre of the scope
o The maths error is at its maximum at the trigger point i.e. the pointer was in
the ‘South’ position. The maximum error in the maths is 180 which now
equates to 0.88 V due to the increased gain/attenuation factor of 2.0
o The Proportional algorithm, shown in Appendix A, is attempting to move the
compass pointer back to ‘North’
o The detail now shows that the compass is still trying to get to North since the
trace is not at zero on the right hand edge of the screen
• Press ‘x’ to exit option 1
• Adjust the horizontal trigger point to 800 ms using the knob to the left of the
‘RUN/STOP’ button on the scope
• Move the compass pointer back to ‘South’ via option 6
• Press the ‘Single’ button (top right of the scope)
• Select option 1 and the following should occur:
o The compass pointer will now move to the ‘North’ position
o The scope should display an image similar to the following (note that it may
be negative rather than positive since it depends where the ‘South’ point
starts from i.e. either > +170° or < -170°)
2022-23
ELEC2540 Page 26 of 46
Control Systems
Figure 24 Time shifted display by 800 ms to maximise screen width
• This image shows the following:
o The trigger point is now offset by 800 ms
o The maths error is at its maximum at the trigger point i.e. the pointer was in
the ‘South’ position. The maximum error in the maths is 180 which now
equates to 0.88 V due to the increased gain/attenuation factor of 2.0
o The Proportional algorithm, shown in Appendix A, is attempting to move the
compass pointer back to ‘North’
o The detail now shows that the compass is close to ‘North’ i.e. zero error after
1.2 seconds
• Press ‘x’ to exit option 1
• Change the K value to 5 via option 2
• Move the compass pointer back to ‘South’ via option 6
• Press the ‘Single’ button (top right of the scope)
• Select option 1 and the following should occur:
o The compass pointer will now move to the ‘North’ position
o The scope should display an image similar to the following (note that it may
be negative rather than positive since it depends where the ‘South’ point
starts from i.e. either > +170° or < -170°)
2022-23
ELEC2540 Page 27 of 46
Control Systems
Figure 25 Proportional control with K = 5, CH1 gain = 2
• This image shows the following:
o The trigger point is offset by 800 ms
o The maths error is at its maximum at the trigger point i.e. the pointer was in
the ‘South’ position. The maximum error in the maths is 180 which now
equates to 0.88 V due to the increased gain/attenuation factor of 2.0
o The Proportional algorithm, shown in Appendix A, is attempting to move the
compass pointer back to ‘North’
o The detail now shows that the compass has now gone into oscillation due to
the higher ‘K’ value and cannot reach a stable position
o The period of oscillation can now be carefully measured
• Press ‘x’ to exit option 1
• Connect channel 2 of the scope (Set to 1 V/div) to output CH2 on the Compass
Processing Unit. In the Proportional control algorithm CH2 represents the result of

• Move the compass pointer back to ‘South’ via option 6
• Press the ‘Single’ button (top right of the scope)
• Select option 1 and the following should occur:
o The compass pointer will now move to the ‘North’ position
o The scope should display an image similar to the following (note that it may
be negative rather than positive since it depends where the ‘South’ point
starts from i.e. either > +170° or < -170°)
2022-23
ELEC2540 Page 28 of 46
Control Systems
Figure 26 CH1 = Absolute maths value for the error signal with a channel gain/attenuation of 2.0 and CH2 =
Absolute maths value for K x Error signal with a channel gain/attenuation of 1.0
• This image shows the following:
o The trigger point is offset by 800 ms
o CH1 is the maths at its maximum at the trigger point i.e. the pointer
was in the ‘South’ position. The maximum error in the maths is 180 which
now equates to 0.88 V due to the increased gain/attenuation factor of 2.0
o CH2 is the absolute maths value from , which shows that the maths
has exceeded the limiting value of +512 (flat line), which makes sense since
180 x 5 = 900 i.e. > 512. (refer to the software in appendix A). This value is
also above the maximum PWM offset of 500 i.e. the algorithm has saturated
the servo drive and the control system is unable to move any faster than the
limits of the drive system i.e. the servo cannot change its speed for the first
400 ms since it is already at its maximum speed
o The Proportional algorithm, shown in Appendix A, is attempting to move the
compass pointer back to ‘North’
o The detail, as before, shows that the compass has now gone into oscillation
due to the ‘K’ value and cannot reach a stable position
• Press ‘x’ to exit option 1
• Since CH2 is saturated then you can apply an attenuation to the maths from CH2,
such as 0.5, using option 4. If you then repeat the steps above you will see an
image similar to the following (note that it may be negative rather than positive since
it depends where the ‘South’ point starts from i.e. either > +170° or < -170°):
2022-23
ELEC2540 Page 29 of 46
Control Systems
Figure 27 CH1 = Absolute maths value for the error signal with a channel gain/attenuation of 2.0 and CH2 =
Absolute maths value for K x Error signal with a channel gain/attenuation of 0.5
The maths output from CH2 on the Compass Processing Unit is now no longer saturated
and shows that the is approximately 1.1 V at the point of triggering, which
equates to about 900, which is what is expected. You can now observe that the algorithm
maintains the value of 900 for approximately 120 ms.
Why do you think this might happen?
You now know how to:
• Set up the scope
• Use the HMI for algorithm control
• Read the absolute maths values from the scope
• Measure and critically evaluate the closed-loop control system performance
2022-23
ELEC2540 Page 30 of 46
Control Systems
Section 5 - Task 1: Performing Proportional Control
 Action the following:
• Using the worked example in Section 4 – Task 1, characterise the Proportional
control system by changing the values of ‘K’ and measuring and recording the
effects of ‘K’ on the speed of response and the points at which oscillation occurs, as
required by the Ziegler-Nichols method as discussed in the earlier part of this
document.
• Determine the KU and TU values from the experimental procedures above
• Note that the CHx allocation is as follows:
o CH1 =
o CH2 =
o CH3 = (not used)
o CH4 = Absolute compass position
• Relate your measurements and results to a Proportional closed-loop control
system, as suggested by the theory that you have learned in your lectures
• Repeat this process for a pulley ratio of 2:1 and 1:2 and critically evaluate the
comparative response times and oscillation frequencies to that of the 1:1 system
• When you have completed this task, exit the Proportional Control HMI via option 7
and return to the main HMI menu
Section 5 – Task 2 Performing Proportional/Integral Control
 Action the following:
• Examine the software in Appendix B and repeat the experimental procedures and
analysis, as in Section 5 Task 1, but using the Proportional/Integral Closed-loop
control algorithm via option 9 of the home menu.
• Apply the KU and TU values to the algorithm and then experiment, document and
evaluate the effects of changing these values in an attempt to achieve the ‘best’
performance
• Before you start, ensure that K is set to 1.0 and Ti to 1000 (ms). Note that all
gain/attenuation values automatically return to 1.0 when you exit the control
algorithm selected
• In the Proportional/Integral control algorithm, the maths outputs from CH1 to CH4
test point outputs from the Compass Processing Unit are as follows:
o CH1 =
o CH2 =
o CH3 = )
o CH4 =
• Always start your test using a ratio of 1:1 and then, when all of your tests are
complete, repeat the process with ratios 2:1 and 1:2 and critically evaluate your
2022-23
ELEC2540 Page 31 of 46
Control Systems
results in relation to the control theory discussed at the start of this assignment and
also from your lectures
• When you have completed this task, exit the Proportional Control HMI via option 10
and return to the main HMI menu
Section 5 - Task 3 Performing Proportional/Integral Control
with Anti-Windup
 Action the following:
• Examine the software in Appendix C and repeat the experimental procedures and
analysis, as in Section 5 Task 2, but using the Proportional/Integral with Anti-
Windup Closed-loop control algorithm via option 10 of the home menu
• Having determined the ‘best’ performance, enable the anti-windup by changing the
value of KA from 0 to some value, but critically observe the output from CH4 and
determine where saturation has occurred, if at all. Remember that Anti-windup does
not have any effect unless saturation has occurred
• Note that when KA = 0 there is no Anti-Windup being performed and therefore you
can perform an immediate comparison between the two types of control
• Note that KA may need to be a high value to have an effect since saturation may
only be for a short period of time. Also note that if KA is too high the algorithm no
longer performs (investigate why this might be the case)
• In the Proportional/Integral with Anti-Windup control algorithm, the maths outputs
from the CH1 to CH4 test point outputs from the Compass Processing Unit are as
follows:
o CH1 =
o CH2 =
o CH3 = )
o CH4 =
• Always start your test using a ratio of 1:1 and then, when all of your tests are
complete, repeat the process with ratios 2:1 and 1:2 and critically evaluate your
results in relation to the control theory discussed at the start of this assignment and
also from your lectures
• When you have completed this task, exit the Proportional Control HMI via option 11
and return to the main HMI menu
2022-23
ELEC2540 Page 32 of 46
Control Systems
Report Tasks
1. The compass system: Identify the parameters in the control system given in Figure 3
i.e. which references are hardware components and which references are software
components and how they relate to the control algorithm.
2. System calibration: Discuss the critical evaluation and analysis of the hardware.
3. Characterising the Servo Drive: Section 3 - Task 2.
4. Proportional Control: Section 5 - Task 1.
5. Proportional/Integral Control: Section 5 - Task 2.
6. Conclusions
7. Anti-windup: Section 5 - Task 3.
Refer to the accompanying report template for specific guidance on report
requirements.
Created by RB, revised by WD 2022-23
ELEC2540 Page 33 of 46 RMB 2020-21
Control Systems
APPENDIX A
//*********************************************
//run the proportional control algorithm
void RunProportionalControl(void)
{
unsigned int Status = 0;
unsigned int Character;
unsigned int CompassStatus;
int Result;
int Delay;
unsigned int PWM_PulseWidth;
SendMessage(RunProportionalControlMessage);
//run control software until any character received
//initialise scope trigger pulse signal
TRIGGER_OUT = 1;
//extend pulse width for triggering scope
for(Delay =0; Delay >= 4; Delay++);
TRIGGER_OUT = 0;
while(Status == 0)
{
//test for exit request
Character = GetChar();
if(Character != 0xFFFF)
{
switch(Character)
{
case 'x': //exit
Status = 1;
break;
case 'X': //exit
Status = 1;
break;
//no default
}
}
ELEC2540 Page 34 of 46 RMB 2020-21
Control Systems
//run proportional algorithm
//get compass position
//initialise master timeout for IIC communication errors
GLOBAL_MasterTimeOutCounter = IIC_TIMEOUT;
GLOBAL_MasterTimeOutFlag = 0;
//get compass data and display raw data on CH4
CompassStatus = GetCompassData(1);
//disable master timeout system
GLOBAL_MasterTimeOutCounter = 0;
GLOBAL_MasterTimeOutFlag = 0;
//test for IIC error
switch(CompassStatus)
{
case IIC_START_FAIL:
SendChar('D');
break;
case IIC_STOP_FAIL:
SendChar('E');
break;
case IIC_WRITE_FAIL:
SendChar('F');
break;
case IIC_READ_FAIL:
SendChar('G');
break;
case IIC_TIMEDOUT:
SendChar('H');
break;
//default condition
default:
//data value is valid therefore perform control calculation
Result = ProportionalCalculation();
//calculate the new PWM pulse time
PWM_PulseWidth = (unsigned int)((int)GLOBAL_ServoNullPoint - Result);
//test for limits
if(PWM_PulseWidth > SERVO_MAX)
ELEC2540 Page 35 of 46 RMB 2020-21
Control Systems
{
PWM_PulseWidth = SERVO_MAX;
}
if(PWM_PulseWidth < SERVO_MIN)
{
PWM_PulseWidth = SERVO_MIN;
}
//get new servo PWM value
GLOBAL_PWM5_PulseTime = PWM_PulseWidth;
}
}
GLOBAL_PWM5_PulseTime = GLOBAL_ServoNullPoint;
//reset all DACs to 0V output i.e. set to 512
DAC1REF = 512;
DAC2REF = 512;
DAC5REF = 512;
DAC6REF = 512;
//synchronise the DAC latching
DACLDbits.DAC1LD = 0b1;
DACLDbits.DAC2LD = 0b1;
DACLDbits.DAC5LD = 0b1;
DACLDbits.DAC6LD = 0b1;
}
//******************************************************
//perform the control algorithm for proportional control
//return the calculated value of: Product = position error x k
//display the position error on the CH1 output, corrected for any gain/attenuation applied
//display the Product on the CH2 output, corrected for any gain/attenuation applied
int ProportionalCalculation(void)
{
float Error = 0;
float ProportionalResult = 0;
//get the error value i.e. the angular distance from North
Error = -GLOBAL_CompassValue;
ELEC2540 Page 36 of 46 RMB 2020-21
Control Systems
//display the error value on CH1
LoadDAC_CH1(Error);
//perform proportional control
ProportionalResult = Error * GLOBAL_K_Value;
//display the result value on CH2
LoadDAC_CH2(ProportionalResult);
//synch DAC latches
DACLDbits.DAC1LD = 0b1;
DACLDbits.DAC2LD = 0b1;
//test for integer limits to stop errors in float to integer conversion
ProportionalResult = TestMathsLimits(ProportionalResult);
//return the calculated value as an integer
return (int)ProportionalResult;
}
//*********************************************************************************
//Test maths limits to avoid problems when the float is converted into an integer
//the limits are set at +/- 10000
float TestMathsLimits(float Value)
{
if (Value > 10000)
{
//set max limit
Value = 10000;
}
else if (Value < -10000)
{
//set min limit
Value = -10000;
}
return Value;
}
ELEC2540 Page 37 of 46 RMB 2020-21
Control Systems
APPENDIX B
//*********************************************
//run the proportional integral control algorithm
void RunProportionalIntegralControl(void)
{
unsigned int Status = 0;
unsigned int Character;
unsigned int CompassStatus;
int Result;
int Delay;
unsigned int PWM_PulseWidth;
unsigned int FirstCycleCompleteFlag = 0;
//ensure DAC6 CH4 is loaded with 0V i.e.512
DAC6REF = 512;
DACLDbits.DAC6LD = 0b1;
SendMessage(RunProportionalIntegralControlMessage);
//clear integral error summation value
GLOBAL_ErrorIntegralSummation = 0;
//calculate integration constant
GLOBAL_IntegrationConstant = GLOBAL_K_Value / GLOBAL_Ti_Value;
//enable delta T timer 1 counter
//reset counter
TMR1 = 0;
//enable timer
T1CONbits.ON = 0b1;
//initialise scope trigger pulse signal
TRIGGER_OUT = 1;
//extend pulse width for triggering scope
for(Delay =0; Delay >= 4; Delay++);
TRIGGER_OUT = 0;
while(Status == 0)
{
//test for exit request
Character = GetChar();
if(Character != 0xFFFF)
{
ELEC2540 Page 38 of 46 RMB 2020-21
Control Systems
switch(Character)
{
case 'x': //exit
Status = 1;
break;
case 'X': //exit
Status = 1;
break;
//no default
}
}
//run proportional algorithm
//get compass position
//initialise master timeout for IIC communication errors
GLOBAL_MasterTimeOutCounter = IIC_TIMEOUT;
GLOBAL_MasterTimeOutFlag = 0;
//get compass data but do not display the raw data on CH4
CompassStatus = GetCompassData(0);
//disable master timeout system
GLOBAL_MasterTimeOutCounter = 0;
GLOBAL_MasterTimeOutFlag = 0;
//test for IIC error
switch(CompassStatus)
{
case IIC_START_FAIL:
SendChar('D');
break;
case IIC_STOP_FAIL:
SendChar('E');
break;
case IIC_WRITE_FAIL:
SendChar('F');
break;
case IIC_READ_FAIL:
SendChar('G');
break;
ELEC2540 Page 39 of 46 RMB 2020-21
Control Systems
case IIC_TIMEDOUT:
SendChar('H');
break;
//default condition
default:
//data value is valid therefore perform control calculation
//test to see if this is the first time around i.e. is delta T valid
if(FirstCycleCompleteFlag == 1)
{
Result = ProportionalIntegralCalculation();
//calculate the new PWM pulse time
PWM_PulseWidth = (unsigned int)((int)GLOBAL_ServoNullPoint - Result);
//test for limits
if(PWM_PulseWidth > SERVO_MAX)
{
PWM_PulseWidth = SERVO_MAX;
}
if(PWM_PulseWidth < SERVO_MIN)
{
PWM_PulseWidth = SERVO_MIN;
}
//get new servo PWM value
GLOBAL_PWM5_PulseTime = PWM_PulseWidth;
}
//get delta T value
GetDeltaT();
//set first cycle complete flag
FirstCycleCompleteFlag = 1;
}
}
//disable delta T timer 1
T1CONbits.ON = 0b0;
GLOBAL_PWM5_PulseTime = GLOBAL_ServoNullPoint;
//reset all DACs to 0V output i.e. set to 512
DAC1REF = 512;
DAC2REF = 512;
DAC5REF = 512;
ELEC2540 Page 40 of 46 RMB 2020-21
Control Systems
DAC6REF = 512;
//synchronise the DAC latching
DACLDbits.DAC1LD = 0b1;
DACLDbits.DAC2LD = 0b1;
DACLDbits.DAC5LD = 0b1;
DACLDbits.DAC6LD = 0b1;
}
//******************************************************
//perform the control algorithm for proportional integral control
int ProportionalIntegralCalculation(void)
{
float Error = 0;
float ProportionalResult = 0;
float ProportionalIntegralResult = 0;
float IntegrationResult = 0;
//get the error value i.e. the angular distance from North
Error = -GLOBAL_CompassValue;
//display the error result on CH1
LoadDAC_CH1(Error);
//perform proportional part of control control
ProportionalResult = Error * GLOBAL_K_Value;
//display the proportional result on CH2
LoadDAC_CH2(ProportionalResult);
//calculate the integral error i.e the signed accumulation of the product of the error and the sample time
GLOBAL_ErrorIntegralSummation = GLOBAL_ErrorIntegralSummation + (Error * GLOBAL_DeltaT_Value);
//apply the correction factor to the integral summation
IntegrationResult = GLOBAL_ErrorIntegralSummation * GLOBAL_IntegrationConstant;
//display the Integral result on CH3
LoadDAC_CH3(IntegrationResult);
//add the result of the proportional calculation and the integral calculation
ProportionalIntegralResult = ProportionalResult + IntegrationResult;
//display the proportional/Integral result on CH4
LoadDAC_CH4(ProportionalIntegralResult);
//synchronise the DAC latching
DACLDbits.DAC1LD = 0b1;
ELEC2540 Page 41 of 46 RMB 2020-21
Control Systems
DACLDbits.DAC2LD = 0b1;
DACLDbits.DAC5LD = 0b1;
DACLDbits.DAC6LD = 0b1;
//test for integer limits to stop errors in float to integer conversion
ProportionalIntegralResult = TestMathsLimits(ProportionalIntegralResult);
//return the calculated value as an integer
return (int)ProportionalIntegralResult;
}
//*****************************************************
//get the delta T value for integration and display purposes in ms
void GetDeltaT(void)
{
//stop the timer
T1CONbits.ON = 0b0;
//convert into seconds
GLOBAL_DeltaT_Value = (float)((double)TMR1 / 1000);
//reset counter
TMR1 = 0;
//start the timer
T1CONbits.ON = 0b1;
}
ELEC2540 Page 42 of 46 RMB 2020-21
Control Systems
APPENDIX C
//*********************************************
//run the proportional integral with anti-windup control algorithm
void RunProportionalIntegralWithAntiWindupControl(void)
{
unsigned int Status = 0;
unsigned int Character;
unsigned int CompassStatus;
int Result;
int Delay;
unsigned int PWM_PulseWidth;
unsigned int FirstCycleCompleteFlag = 0;
//ensure DAC6 CH4 is loaded with 0V i.e.512
DAC6REF = 512;
DACLDbits.DAC6LD = 0b1;
SendMessage(RunProportionalIntegralWithAntiWindupControlMessage);
//clear global integral values
GLOBAL_ErrorIntegralSummation = 0;
GLOBAL_AntiWindupSaturationValue = 0;
GLOBAL_LastProportionalIntegralResult = 0;
//calculate integration constant
GLOBAL_IntegrationConstant = GLOBAL_K_Value / GLOBAL_Ti_Value;
//enable delta T timer 1 counter
//reset counter
TMR1 = 0;
//enable timer
T1CONbits.ON = 0b1;
//initialise scope trigger pulse signal
TRIGGER_OUT = 1;
//extend pulse width for triggering scope
for(Delay =0; Delay >= 4; Delay++);
TRIGGER_OUT = 0;
while(Status == 0)
{
//test for exit request
Character = GetChar();
ELEC2540 Page 43 of 46 RMB 2020-21
Control Systems
if(Character != 0xFFFF)
{
switch(Character)
{
case 'x': //exit
Status = 1;
break;
case 'X': //exit
Status = 1;
break;
//no default
}
}
//run proportional algorithm
//get compass position
//initialise master timeout for IIC communication errors
GLOBAL_MasterTimeOutCounter = IIC_TIMEOUT;
GLOBAL_MasterTimeOutFlag = 0;
//get compass data but do not display on CH4
CompassStatus = GetCompassData(0);
//disable master timeout system
GLOBAL_MasterTimeOutCounter = 0;
GLOBAL_MasterTimeOutFlag = 0;
//test for IIC error
switch(CompassStatus)
{
case IIC_START_FAIL:
SendChar('D');
break;
case IIC_STOP_FAIL:
SendChar('E');
break;
case IIC_WRITE_FAIL:
SendChar('F');
break;
case IIC_READ_FAIL:
ELEC2540 Page 44 of 46 RMB 2020-21
Control Systems
SendChar('G');
break;
case IIC_TIMEDOUT:
SendChar('H');
break;
//default condition
default:
//data value is valid therefore perform control calculation
//test to see if this is the first time around i.e. is delta T valid
if(FirstCycleCompleteFlag == 1)
{
Result = ProportionalIntegralWithAntiWindupCalculation();
//calculate the new PWM pulse time
PWM_PulseWidth = (unsigned int)((int)GLOBAL_ServoNullPoint - Result);
//test for limits
if(PWM_PulseWidth > SERVO_MAX)
{
PWM_PulseWidth = SERVO_MAX;
}
if(PWM_PulseWidth < SERVO_MIN)
{
PWM_PulseWidth = SERVO_MIN;
}
//get new servo PWM value
GLOBAL_PWM5_PulseTime = PWM_PulseWidth;
}
//get delta T value
GetDeltaT();
//set first cycle complete flag
FirstCycleCompleteFlag = 1;
}
}
//disable delta T timer 1
T1CONbits.ON = 0b0;
GLOBAL_PWM5_PulseTime = GLOBAL_ServoNullPoint;
//reset all DACs to 0V output i.e. set to 512
DAC1REF = 512;
ELEC2540 Page 45 of 46 RMB 2020-21
Control Systems
DAC2REF = 512;
DAC5REF = 512;
DAC6REF = 512;
//synchronise the DAC latching
DACLDbits.DAC1LD = 0b1;
DACLDbits.DAC2LD = 0b1;
DACLDbits.DAC5LD = 0b1;
DACLDbits.DAC6LD = 0b1;
}
//******************************************************
//perform the control algorithm for proportional integral with anti-windup control
int ProportionalIntegralWithAntiWindupCalculation(void)
{
float Error = 0;
float ProportionalResult = 0;
float ProportionalIntegralResult = 0;
float IntegrationResult = 0;
//get the error value i.e. the angular distance from North
Error = -GLOBAL_CompassValue;
//display the error result on CH1
LoadDAC_CH1(Error);
//perform proportional part of control control
ProportionalResult = Error * GLOBAL_K_Value;
//display the proportional result on CH2
LoadDAC_CH2(ProportionalResult);
//calculate the integral error i.e the signed accumulation of the product of the error and the sample time
//but also subtract any Anti-windup saturation that has occurred
GLOBAL_ErrorIntegralSummation = GLOBAL_ErrorIntegralSummation + ((Error - GLOBAL_AntiWindupSaturationValue)
* GLOBAL_DeltaT_Value);
//apply the correction factor to the integral summation
IntegrationResult = GLOBAL_ErrorIntegralSummation * GLOBAL_IntegrationConstant;
//display the integral result on CH3
LoadDAC_CH3(IntegrationResult);
//add the result of the proportional and integral calculations
ProportionalIntegralResult = ProportionalResult + IntegrationResult;
ELEC2540 Page 46 of 46 RMB 2020-21
Control Systems
//display the proportional/Integral result on CH4
LoadDAC_CH4(ProportionalIntegralResult);
//perform anti-windup calculation for next sample
//if the result of the calculations is saturated then the integration accumulation is large
//and therefore it takes a long time for the integrator to get rid of the accumulated value
//This overshoot can be reduced by applying 'back-calculation' anti windup by adjusting
//the Error integral accumulation by subtracting the saturation value x a constant Ka
//test for saturation of the result against the maximum PWM offset i.e +/- 500
if(ProportionalIntegralResult > 500)
{
GLOBAL_AntiWindupSaturationValue = (ProportionalIntegralResult - 500) * GLOBAL_Ka_Value;
}
else if(ProportionalIntegralResult < -500)
{
GLOBAL_AntiWindupSaturationValue = (ProportionalIntegralResult + 500) * GLOBAL_Ka_Value;
}
else
{
GLOBAL_AntiWindupSaturationValue = 0;
}
//latch the DAC values
DACLDbits.DAC1LD = 0b1;
DACLDbits.DAC2LD = 0b1;
DACLDbits.DAC5LD = 0b1;
DACLDbits.DAC6LD = 0b1;
//test for integer limits to stop errors in float to integer conversion
ProportionalIntegralResult = TestMathsLimits(ProportionalIntegralResult);
//return the calculated value as an integer
return (int)ProportionalIntegralResult;
}


essay、essay代写