Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| learn:courses:unit-6:start [2017/03/20 19:59] – Martha | learn:courses:unit-6:start [2022/10/04 11:38] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Unit 6: Analog I/O and Process Control ====== | ====== Unit 6: Analog I/O and Process Control ====== | ||
| + | [[{}/ | ||
| + | |||
| === Unit 6 Labs === | === Unit 6 Labs === | ||
| - | * [[https:// | + | * [[/ |
| - | * [[https:// | + | * [[/ |
| === Download This Document === | === Download This Document === | ||
| - | * {{ : | + | {{ : |
| + | |||
| + | |||
| + | ===== 1. Introduction ===== | ||
| + | The purpose of this laboratory exercise is to investigate applications of embedded controllers to real time control algorithms that employ analog inputs and analog outputs. You will use a computer algorithm to implement a closed-loop processor fro motor speed control. You will see how feedback can be used to linearize an inherently nonlinear process and result in zero steady state control error. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 2. Objectives ===== | ||
| + | - Generate PWM outputs to implement variable motor supply voltage. | ||
| + | - Implement a tachometer operation using PIC32 Timers. | ||
| + | - Develop MPLAB X projects that implement open-loop and closed-loop motor control. | ||
| + | - Develop the C program code to implement a PI controller and a moving average digital filter. | ||
| + | - Manage multiple background tasks in an interrupt driven system. | ||
| + | - To monitor and display data from a process running in real-time. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 3. Basic Knowledge ===== | ||
| + | - Suggested reading: [[https:// | ||
| + | * Basic circuit theory | ||
| + | * Fundamentals of programming with C | ||
| + | - How to configure I/O pins on a Microchip® PIC32 PPS microprocessor | ||
| + | - How to implement a real-time system using preemptive foreground – background task control | ||
| + | - How to generate a PWM output with the PIC32 processor | ||
| + | - How to configure the Analog Discovery to display logic traces | ||
| + | - How to implement the design process for embedded processor based systems | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 4. Equipment List ===== | ||
| + | ==== 4.1. Hardware ==== | ||
| + | - [[https:// | ||
| + | - Workstation computer running Windows 10 or higher, MAC OS, or Linux | ||
| + | - 2 [[https:// | ||
| + | - [[https:// | ||
| + | - [[https:// | ||
| + | |||
| + | In addition, we suggest the following instruments: | ||
| + | - [[https:// | ||
| + | |||
| + | ==== 4.2. Software ==== | ||
| + | The following programs must be installed on your development workstation: | ||
| + | - [[http:// | ||
| + | - [[http:// | ||
| + | - [[http:// | ||
| + | - [[https:// | ||
| + | - [[http:// | ||
| + | - Spreadsheet application (such as Microsoft Excel) | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 5. Project Takeaways ===== | ||
| + | - How to read analog voltage with a PIC32 processor. | ||
| + | - How to use the PIC32 Output Compare to implement a PWM analog output. | ||
| + | - How to use the PIC32 Input Capture period measurement to implement a tachometer. | ||
| + | - The advantages of using period measurements as opposed to frequency measurements and vice versa. | ||
| + | - Fundamental digital filtering concepts for data smoothing. | ||
| + | - Open-loop and closed-loop process control. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 6. Fundamental Concepts ===== | ||
| + | This unit introduces the concepts of process control of [[https:// | ||
| + | |||
| + | ==== 6.1. Process Control ==== | ||
| + | Automated process control is an engineering discipline that deals with architectures, | ||
| + | |||
| + | The block diagram of Fig. 6.1 has partitioned the automated process into elements of hardware and software that will be described in detail below. The elements shown inside the shaded blue box are implemented using software within the microprocessor. The elements shown inside the shaded green boxes are parts needed for open loop control processing. The elements shown inside the shaded orange boxes are parts that are not required for open loop but provide instrumentation to monitor the performance of the open loop system. The shaded yellow box element is added to the open loop control and instrumentation elements to form a completed closed loop system. The input signals that are continuous in nature must be converted from analog to digital by using an analog-to-digital converter (ADC). Output signals that are used to control mechanical and thermal devices such as motors, heaters, and air conditioners require the digital variable to be converted to an analog signal using a digital-to-analog converter (DAC). Both the ADC and DAC will be discussed in detail to follow. | ||
| + | |||
| + | {{ : | ||
| + | //Figure 6.1. Automated process control block diagram.// | ||
| + | |||
| + | === 6.1.1. Open-loop Control === | ||
| + | [[https:// | ||
| + | |||
| + | === 6.1.2. Closed-loop Control === | ||
| + | [[http:// | ||
| + | |||
| + | ==== 6.2. Signal Processing ==== | ||
| + | [[https:// | ||
| + | |||
| + | === 6.2.1. Analog Signal Processing === | ||
| + | Analog signal processing is any type of signal processing conducted on [[https:// | ||
| + | |||
| + | Electronic [[https:// | ||
| + | |||
| + | === 6.2.2. Digital Signal Processing === | ||
| + | Digital signal processing (DSP) is the use of digital computers to implement [[https:// | ||
| + | |||
| + | Digital computers, particularly in the form of microprocessors, | ||
| + | In the course of this Unit, you will discover there are different approaches to implementing an ADC and DAC. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 7. Background Information ===== | ||
| + | ==== 7.1. Motor Controls and Feedback ==== | ||
| + | The details of the elements of the control system illustrated in Fig. 6.1 will be presented in the following sections, starting with those outside the microprocessor, | ||
| + | |||
| + | Figure 6.1 shows that there are two analog-to-digital converters (ADC) in the closed loop system that we will be implementing. One is used to read a variable reference that sets the motor speed. A second is used to read the motor speed tachometer. When working with time varying signals we will need to resolve the conflict between the measurement of frequency and the frequency of measurement, | ||
| + | |||
| + | ==== 7.2. Variable Speed Control ==== | ||
| + | The //Analog Control Input// on the Basys MX3 processor board will be used as the variable input control. Figure 7.1 shows that the potentiometer is connected to PORT B pin 2 of the PIC32MX370. To use the PIC32 analog input connected to the potentiometer, | ||
| + | |||
| + | {{ : | ||
| + | //Figure 7.1. Analog Input Control schematic diagram.// | ||
| + | |||
| + | |||
| + | Considering that the PIC32 ADC is connected to a potentiometer under manual control, the speed at which the analog voltage can change is relatively low. In Lab 6a and 6b we will run the PIC32 ADC at a high rate of speed so that the most recent ADC conversion will accurately reflect the current potentiometer setting. As Fig. 7.2 illustrates, | ||
| + | |||
| + | $$T_C = T_{CONV} + T_{SAMP} = 4 \mu s = 40 \cdot T_{AD} | ||
| + | |||
| + | Since the conversion time always requires 12 $T_{AD}$ clock cycles, the sample time is 2.8 µs, or 28 clock cycles, as expressed by Eq. 7.2. | ||
| + | |||
| + | $$T_{SAMP} = T_C - T_{CONV} = 2.8 \mu s = 28 \cdot T_{AD} \qquad (Eq. 7.2)$$ | ||
| + | |||
| + | Configuring the PIC32 ADC to start a new conversion as soon as the last one is completed gives us the 250 kHz sample rate. In Lab 6a, the required conversion rate is 4 Hz, which is dictated by the background loop rate. The conversion rate for Lab 6b is 1 kHz. Since both of these rates are far less than the rate at which the ADC is converting the analog inputs to digital values, we can simply read the ADC whenever a new result is needed. | ||
| + | |||
| + | {{ : | ||
| + | //Figure 7.2. ADC10 conversion timing.// | ||
| + | |||
| + | === 7.2.1. Feedback: Digital Tachometers === | ||
| + | [[https:// | ||
| + | |||
| + | The DC motor shown in Fig. 7.3 will be used for Lab 6a and 6b and uses a motor that has a magnet attached to the motor shaft. There are two [[https:// | ||
| + | |||
| + | {{ : | ||
| + | //Figure 7.3. DC Motor with gearhead and bi-phase tachometer.// | ||
| + | |||
| + | Figure 7.4 is a plot of the frequency produced by one of the Hall-effect sensors as a function of the percent duty cycle of a [[https:// | ||
| + | |||
| + | {{ : | ||
| + | //Figure 7.4. Tachometer Frequency vs %PWM for Open Loop control.// | ||
| + | |||
| + | The primary question to be answered is, what is the maximum rate that frequency can be measured? In theory, it is twice the inverse of the period of the signal. The advantages and disadvantages to the common frequency measurement techniques are considered next. | ||
| + | |||
| + | === 7.2.1.1. Cycle Counting === | ||
| + | There are two methods of measuring the frequency of the digital tachometer. The number of pulses can be counted per unit time or the inverse computed of the period between two successive transitions. Measuring frequency by counting cycles or pulses over short intervals is more accurate for signals when the frequency of the signal being measured is much greater than the frequency of measurements. The results should be many hundreds or even thousands of cycles being measured during the measurement period, thus providing a high resolution with a fixed measurement period. | ||
| + | |||
| + | One of the methods to mitigate the low number of cycles per fixed interval for low frequency signals is to employ [[https:// | ||
| + | |||
| + | === 7.2.1.2. Period Measurement === | ||
| + | Using timer counts to measure a signal period is generally more accurate for relatively low frequency signals and is the method that will be implemented in Lab 6a and 6b. The speed of the motor rotor shaft can be measured by computing the period between two successive pulses of the motor tachometer by capturing the time for each transition. In order to achieve the measurement with relatively good accuracy, the period must be measured with a high resolution. This requires that the timer must be counting at a much higher rate than the period of the of the tachometer signal. The accuracy of the frequency is preserved by inverting the period using floating point variables. | ||
| + | |||
| + | Measurement resolution is provided by the number of timer counts between input signal transitions. The measurement period depends on the frequency of the input signal and, for very low frequency signals, may result in excessively long delays between measurement updates. Note that the range of speed is from zero to 300 revolutions per second (RPS). Since the Hall-effect sensor creates a pulse every revolution, the period of revolution is from infinity at zero RPS to 3.33 ms at 300 RPS. Obviously, zero RPS presents a problem for the microprocessor, | ||
| + | |||
| + | The task is to select the timer input clock rate that will provide adequate period accuracy across the range of possible tachometer frequencies. From the derivation shown in Appendix B, for a given measurement period, the granularity of any input frequency can be determined using Eq. 7.3. | ||
| + | |||
| + | $$\Delta = \frac{T3_{CLK\_PD}}{T_{SIGNAL}} \cdot F_{SIGNAL} = T3_{CLK\_PD}F_{SIGNAL}2 \qquad (Eq. 7.3)$$ | ||
| + | |||
| + | Where $F_{SIGNAL} = 1/ | ||
| + | |||
| + | Eq. 7.3 demonstrates that granularity, | ||
| + | |||
| + | {{ : | ||
| + | //Figure 7.5. Plot of frequency measurement resolution.// | ||
| + | |||
| + | === 7.2.1.3. Tachometer Interface === | ||
| + | The DC motor plus encoder shown in Fig. 7.3 has a 6 pin connector, shown in Fig. 7.5 below. The hardware configuration with the Basys MX3 board is shown in Fig. A.1. The wires in Fig. 7.6 attach to the DC motor assembly with pin assignments as shown in Table 7.1. The brown, green and blue wires interface a processor input pin to the Hall Sensor A output, as shown in Fig. 7.7. Hall Sensor B is not used for Lab 6a or 6b. | ||
| + | |||
| + | **Table 7.1. DC motor connector pin assignment.** | ||
| + | ^ Pin Number | ||
| + | | 1 | Black | Motor - | J5 – A1 | | ||
| + | | 2 | Red | Motor + | J5 – A2 | | ||
| + | | 3 | Brown | Hall Sensor VCC (3.3V) | ||
| + | | 4 | Green | Hall Sensor GND | JA – 12 | | ||
| + | | 5 | Blue | Hall Sensor Output A | JA – 10 | | ||
| + | | 6 | Purple | ||
| + | |||
| + | The Hall Sensor outputs have an [[https:// | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ==== Listing 7.1. Configuring I/O pin RG9 for Input Capture ==== | ||
| + | < | ||
| + | | ||
| + | | ||
| + | | ||
| + | IC1R = 0b00000001; | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | {{ : | ||
| + | //Figure 7.6. Motor Connector wiring.// | ||
| + | |||
| + | |||
| + | {{ : | ||
| + | //Figure 7.7. Pmod JA connector wiring.// | ||
| + | |||
| + | === 7.2.2. Analog Output: Pulse-width Modulation === | ||
| + | In [[/ | ||
| + | |||
| + | Lab 5 discussed the details of how to set up a PWM output using the output compare special function of the PIC32 processor. However, Lab 6a and 6b will require that we set up two PWM outputs that all use a common timer if the motor is to be capable of reversing direction. After initializing the timer to be used with the output compare and initializing the two output-compare channels, the PWM duty cycle of each individual channel is easily changed using the instruction // | ||
| + | |||
| + | Demodulation of PWM requires a low-pass filter that can be implemented with a simple RC (resistor-capacitor) filter as shown in Fig. 7.8. Fig. 7.9 shows the input – output relationship of filtering a PWM signal with 60% [[https:// | ||
| + | |||
| + | {{ : | ||
| + | //Figure 7.8. Analog RC Low-pass filter.// | ||
| + | |||
| + | {{ : | ||
| + | //Figure 7.9. PWM Signal with 60% duty cycle and RC filter output.// | ||
| + | |||
| + | For Lab 6a and Lab 6b, the rotor inertia of the DC motor itself provides all the needed low-pass filtering, provided the carrier frequency is sufficiently high. | ||
| + | |||
| + | === 7.2.3. Motor Speed Control === | ||
| + | Figure 7.10 shows the electric model of a permanent magnet brushed DC motor, such as the one shown in Fig. 7.3. The torque of the motor is proportional to the armature current labeled //Ia//. The armature winding resistance, Ra, and winding inductance, //La//, are fixed. The back electromotive force (EMF) is proportional to the speed of the motor. | ||
| + | |||
| + | {{ : | ||
| + | //Figure 7.10. Electric model of a brushed DC motor.// | ||
| + | |||
| + | When the motor voltage, //Vm//, is initially applied, the motor speed will increase until the back EMF plus the voltage drop across Ra due to the armature current, Ia, equals the applied voltage, //Vm//. The most common method of controlling the speed of a permanent magnet brushed DC motor is to vary the motor supply voltage, //Vm//. For a fixed //Vm//, if a mechanical load is applied to the shaft of the motor, the speed of the motor and the back EMF will decrease, resulting in increased armature current to generate the needed motor torque. If the speed is to remain constant for varying applied mechanical loads, the motor applied voltage, //Vm//, must change accordingly. We will vary the applied motor voltage by changing the PWM duty cycle as discussed below. | ||
| + | |||
| + | === 7.2.3.1. Motor Output Interface === | ||
| + | The motor driver IC on the Basys MX3 processor platform is shown in Fig. 7.11. The motor wires used for power input and tachometer output are identified in Fig. 7.12. A 5.0 V external power supply is required to be connected to J11, marked as VBAR on the Basys MX3 processor board. A jumper must be used on JP1 to connect the 5V power, VBAR, to the motor supply, VM. | ||
| + | |||
| + | {{ : | ||
| + | //Figure 7.11. Basys MX3 Motor driver circuit.// | ||
| + | |||
| + | |||
| + | {{ : | ||
| + | //Figure 7.12. Basys MX3 connection to motor power wiring.// | ||
| + | |||
| + | For Labs 6a and 6b, only outputs A1 and A2 are connected to the leads of the DC motor (RED and BLACK). Switching the A1 and A2 connections to the motor causes the motor to rotate in the opposite direction. | ||
| + | |||
| + | **Table 7.2. Motor driver IC7 connection to PIC32 processor.** | ||
| + | ^ Function | ||
| + | | AIN1 | 10 | RB3 | Motor Supply + | | ||
| + | | BIN1 | 8 | RE9 | Not Used - Set to zero | | ||
| + | | AIN2 | 9 | RE8 | Motor Supply - | | ||
| + | | BIN2 | 7 | RB5 | Not used – Set to zero | | ||
| + | | MODE | 11 | RF1 | Set to 0 | | ||
| + | |||
| + | === 7.2.3.2. DC Motor Speed Control === | ||
| + | The percent PWM update rate should minimally match the input sample rate. Maximally, the percent PWM update rate should be less than or equal to the PWM carrier frequency. Attempting to update percent PWM any faster than the PWM carrier frequency will result in some updates being simply overwritten before being transferred to the primary timer compare register. Since two PWM outputs are used to synchronize the two outputs to the motor driver IC, both PWM channels must be updated in rapid succession. The code to do this is included with Lab 6a. | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 8. References ===== | ||
| + | - K.M. Lynch, N. Marchuk, and M.L. Elwin, __Embedded Computing and Mechatronics with the PIC32 Microcontroller__, | ||
| + | - “Open-vs. closed-loop Control”, Vance VanDoren, Control Engineering, | ||
| + | - DRV8835 Data Sheet, [[https:// | ||
| + | - “Brished DC Motor Basics Webinar”, John Moutton, Microchip Technologies Inc., [[http:// | ||
| + | - “AN905 Brushed DC Motor Fundamentals”, | ||
| + | - AN538, “Using PWM to Generate Analog Output”, | ||
| + | - “AB-022: PWM Frequency for Linear Motion Control”, Precision Microdrives™, | ||
| + | - “AN1353 Op Amp Rectifier, Peak Detectors and Clamps”, Dragos Ducu, Microchip Technologies Inc., 2011, [[http:// | ||
| + | - __Scientists and Engineer’s Guide to Digital Signal Processing__, | ||
| + | - “Implementing a PID controller Using a PIC18 MCU”, Chris Valenti, Microchip Technologies Inc., 2005, [[http:// | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Appendix A: DC Motor Connection to Basys MX3 ===== | ||
| + | {{ : | ||
| + | //Figure A.1. Motor and tachometer wiring.// | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Appendix B: Frequency Measurement Resolution ===== | ||
| + | Definition: //The granularity of a sensor measurement is the smallest change it can detect in the quantity that it is measuring. Resolution, which is the inverse of granularity, | ||
| + | |||
| + | Assume that Timer 3 count is recorded on each positive transition. Then for two frequencies, | ||
| + | |||
| + | $$F_{TMR3} = \frac{PBCLK}{T3_{PS}} = \frac{10^7}{1} \qquad (Eq. B.1)$$ | ||
| + | |||
| + | $$T3_{COUNT1} = \frac{F_{TMR3}}{F1} | ||
| + | |||
| + | $$T3_{COUNT2} = \frac{F_{TMR3}}{F2} = \frac{10^7}{1} | ||
| + | |||
| + | To compute the resolution in Hz per count, assume that the count difference is unity which results in Eq. B.4 or Eq. B.5, whichever expresses the desired units. | ||
| + | |||
| + | $$|T3_{COUNT1} - T3_{COUNT2} | = Granularity = (F_{TMR3}) \cdot | (\frac{1}{F2} - \frac{1}{F1} ) | COUNTS/Hz \qquad (Eq. B.4)$$ | ||
| + | |||
| + | $$Resolution = ( \frac{1}{F_{TMR3}} ) \cdot ( \frac{1}{ | \frac{1}{F1} - \frac{1}{F2} | } ) = ( \frac{1}{F_{TMR3}} ) \cdot ( \frac{F1 \cdot F2}{ | F2 = F1 | } ) Hz/COUNT \qquad (Eq. B.5)$$ | ||
| + | |||
| + | $$|F2-F1| = Resolution = ( \frac{F1 \cdot F2}{F_{TMR3}} ) Hz/COUNT \qquad (Eq. B.6)$$ | ||
| + | |||
| + | If F1 approximately equals F2 at 1 Hz, the resolution is 0.0008 Hz/Count. At 200 Hz, the resolution is 1.28 Hz/Count. | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Appendix C: Classical Control Theory ===== | ||
| + | According to Reference 10, [[https:// | ||
| + | |||
| + | $$G(s) = \frac{b_0 + b_1 \cdot s + b_2 \cdot s^2 + b_3 \cdot s^3 + \cdots + b_M \cdot s^M}{a_0 + a_1 \cdot s + a_2 \cdot s^2 + a_3 \cdot s^3 + \cdots + a_N \cdot s^N} \qquad Eq. C.1)$$ | ||
| + | |||
| + | R(s) is the input reference variable, Y(s) is the output variable, E(s) is the error variable computed as the difference between the reference, R(s), and the feedback variable, F(s). F(s) is the feedback variable that is the transformed output representing the dynamics of the instrumentation. | ||
| + | |||
| + | {{ : | ||
| + | //Figure C.1. Simplified block diagram of closed-loop control system.// | ||
| + | |||
| + | $$Y(s) = P(s) \cdot U(s) \qquad (Eq. C.2)$$ | ||
| + | |||
| + | $$U(s) = C(s) \cdot E(s) \qquad (Eq. C.3)$$ | ||
| + | |||
| + | $$E(s) = R(s) - Y(s) \qquad (Eq. C.4)$$ | ||
| + | |||
| + | $$\frac{Y(s)}{R(s)} = \frac{P(s) \cdot C(s)}{F(s) \cdot P(s) \cdot C(s)} = H(s) \qquad (Eq. C.5)$$ | ||
| + | |||
| + | Although PID controllers give optimal control in regards to response time, minimum overshoot, and steady state error, the parameters that govern the controller behavior can be difficult to determine. If proportional gain is used exclusively, | ||
| + | |||
| + | {{ : | ||
| + | //Figure C.2. Block diagram of a PID controller.(([[https:// | ||
| + | |||
| + | In the system shown in Fig. C.2, the term plant generally refers to a dynamic system that we are attempting to control in some specified manner, which for the case of Lab 6a and 6b is the speed of a DC motor. The voltage applied to the motor can be adjusted as needed in order to drive the difference between the desired speed (the reference inputs r(t) and the speed measured using the tachometer, y(t)) to approach zero. The motor cannot instantaneously change because of the laws of physics and electricity that govern the speed of the motor. | ||
| + | |||
| + | When using open loop control, we never really know how fast the motor is turning without monitoring. External influences such as fluctuations in the power, voltage, load, or friction losses will affect the motor speed. Imagine using an open loop control for speed control on your car (equating the speed of the motor with the car wheels). It would work as long as the car is subject to that same road and environmental conditions. But put a hill in the road and watch what happens. First, the car will slow to until the energy set by the position of the accelerator equals the energy needed to climb the hill at a reduced speed. | ||
| + | |||
| + | If the accelerator is controlled by the difference between the desired (or set) speed and the actual speed, we then have a proportional closed-loop system. This is called proportional control and mathematically the control algorithm is expressed as shown in Eq. C.6 where e(t) is the error signal and C(t) is the control output. | ||
| + | |||
| + | $$c(t) = K_p (V_{SET} - V_{TACH}) = K_p \cdot e(t) \qquad (Eq. C.6)$$ | ||
| + | |||
| + | Eq. C.6 works well as long as there is sufficient error and gain (KP) to the control supplying gas to the motor. But as the car approaches the setpoint velocity, the error gets smaller until there is insufficient error such that, when multiplied by the gain, the system will achieve some kind of equilibrium. There will always be a finite error with strictly proportional control. Even though the error can be made smaller by increasing the gain, the error will never go to zero. | ||
| + | |||
| + | //PI// control to the rescue! When we add integration of the error signal to the control algorithm, then we can get zero steady state response errors for step inputs. Such a control algorithm is written using Laplace operators in Eq. C.7. | ||
| + | |||
| + | $$C(s) = (K-p + \frac{K_i}{s}) (V_{SET} - V_{TACH}) = (K_p + \frac{K_i}{s}) \cdot E(s) \qquad (Eq. C.7)$$ | ||
| + | |||
| + | Thus, the control signal is proportional to the integral of the error signal, and in part, directly to the magnitude of the error signal itself. Because of this, this approach to control is referred to as PI (proportional-integral) control. A distinct advantage of PI control is that using it results in zero steady state error for a first order plant (G1(s) = k/s). Now computers can’t solve equations in the continuous domain, therefore we must transform to a discrete domain that allows us to use samples of continuous signals. Eq. C.8 is the transformation from continuous to discrete time domain that is based upon rectangular integration. Ts is the interval between samples of the input. | ||
| + | |||
| + | $$s = \bigg( \frac{1-z^{-1}}{T_s} \bigg) \qquad (Eq. C.8)$$ | ||
| + | |||
| + | Completing the appropriate computations after substituting Eq. C.8 into Eq. C.7, we end up with a discrete time equation shown in Eq. C.9. | ||
| + | |||
| + | $$C(z) = (K_p + K_i \cdot (1-z^{-1}) \cdot E(z) \qquad (Eq. C.9)$$ | ||
| + | |||
| + | A more popular conversion is based upon the trapezoidal rule integration and is called the bilinear transformation and is expressed in Eq. C.10. This transformation is actually a better approximation of the exact transformation $s = e^{-jwTs}$ than is Eq. C.8. Substituting Eq. C.10 into Eq. C.7, we get the result shown in Eq. C.11. After some simplification we arrive at Eq. C.12. | ||
| + | |||
| + | $$s = \bigg( \frac{2}{T_s} \bigg) \bigg( \frac{1-z^{-1}}{1+z^{-1}} \bigg) \qquad (Eq. C.10)$$ | ||
| + | |||
| + | $$C(z) = (E(z)) \bigg[ K_p + \frac{T_sK_i}{2} \bigg( \frac{1+z^{-1}}{1-z^{-1}} \bigg) \bigg] = \bigg( \frac{E(z)}{1-z^{-1}} \bigg) \cdot \bigg[ \bigg( \frac{T_sK_i}{2} + K_p \bigg) + \bigg( \frac{T_sK_i}{2} - K_p \bigg) \cdot z^{-1} \qquad (Eq. C.11)$$ | ||
| + | |||
| + | Or | ||
| + | |||
| + | $$C(z) \cdot (1-z^{-1}) = \bigg[ \bigg( \frac{T_sK_i}{2} + K_p \bigg) \bigg] \cdot E(z) + \bigg( \frac{T_sK_i}{2} - K_p \bigg) \cdot E(z)z^{-1} \qquad (Eq. C.12)$$ | ||
| + | |||
| + | So the question that is begged to be answered is what is z-1? Well for our purposes, it is the delay operator and any variable multiplied by the delay operator is simply that variable at the previous time interval. So Eq. C.12 can now be written as Eq. C.13, which is a difference equation that can be solved by any computer, microprocessor, | ||
| + | |||
| + | $$c_n = \bigg( \frac{T_sK_i}{2} + K_p \bigg) \cdot e_n + \bigg( \frac{T_sK_i}{2} - K_p \bigg) \cdot e_{n-1} + c_{n-1} \qquad (Eq. C.13)$$ | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ==== Listing C.1. Pseudo code for Digital PI controller ==== | ||
| + | < | ||
| + | #define GetPeripheralClock() 10000000 // | ||
| + | #define PWM_MAX (GetPeripheralClock()/ | ||
| + | #define PWM_MIN 0 | ||
| + | |||
| + | static int ERROR = 0; // Initial value for en-1 | ||
| + | static int CTRL = 0; // Initial value for cnn-1 | ||
| + | |||
| + | TACH = Read motor speed (Hz); // Global variable set by InputCapture ISR | ||
| + | // The SET_SPEED reference input is scales using a first order polynomial in the form y = ax + b to set the slope, a, with | ||
| + | // units rps/ADC bit and ”b” being the minimum speed when the ADC input is zero | ||
| + | SET_SPEED = Scaled potentiometer setting // Determine speed set point from ADC | ||
| + | |||
| + | ERROR_LAST = ERROR; | ||
| + | CTRL_LAST = CTRL; | ||
| + | ERROR = SET_SPEED - TACH; | ||
| + | |||
| + | CTRL = (KP + KI)*ERROR - (KP - KI)*ERROR_LAST + CTRL_LAST; // Compute new control output value | ||
| + | |||
| + | if(CTRL > PWM_MAX) then PWM_CONTROL = PWM_MAX; else // Limit output range to prevent windup | ||
| + | { | ||
| + | If (CTRL < PWM_MIN) | ||
| + | PWM_CONTROL = CTRL; // Output PWM value | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | In the above code listing, the integral gain $KI$ is equal to $(T/2)K_i$. An alternative approach for controlling motor speed can be found in any control theory text book that includes derivative control, but is not required for this assignment. Other approaches may be found in various control theory text books. | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | [[{}/ | ||
| + | [[{}/ | ||
| + | [[{}/ | ||
| + | [[{}/ | ||
| + | |||
| + | {{tag> | ||

