17
Microcontrollers (Embedded Systems) Lab 7 Closed-Loop Motor Control Authors: Cuauhtémoc Carbajal Francisco Rojas Learning Objectives In this lab, you will learn how to construct a closed-loop angular velocity controller for a DC motor. The motor velocity will be determined by measuring the tachometer voltage using an ADC channel. The reference angular velocity will be provided to the microcontroller by software and should be calibrated in revolutions per second. The motor driving signal will be provided by the STM32F3 DAC. The value for the controller’s constants should be determined. Introduction Closed-loop speed control of DC motors is used in many robotics applications. A DC motor can be driven from a microcontroller using a Pulse Width Modulated (PWM) signal. The inherent low-pass characteristic of the DC motor leads to the averaging of the PWM signal over time; this is in effect equivalent to driving the motor with a DC voltage. Where available, a programmable analog voltage can be used instead, via a DAC for example. This laboratory will address the design of a closed-loop speed control system for a DC motor that is part of the Sensors and

homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

  • Upload
    doque

  • View
    214

  • Download
    1

Embed Size (px)

Citation preview

Page 1: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

Microcontrollers (Embedded Systems) Lab 7

Closed-Loop Motor Control

Authors:Cuauhtémoc CarbajalFrancisco Rojas

Learning Objectives

In this lab, you will learn how to construct a closed-loop angular velocity controller for a DC motor. The motor velocity will be determined by measuring the tachometer voltage using an ADC channel. The reference angular velocity will be provided to the microcontroller by software and should be calibrated in revolutions per second. The motor driving signal will be provided by the STM32F3 DAC. The value for the controller’s constants should be determined.

Introduction

Closed-loop speed control of DC motors is used in many robotics applications. A DC motor can be driven from a microcontroller using a Pulse Width Modulated (PWM) signal. The inherent low-pass characteristic of the DC motor leads to the averaging of the PWM signal over time; this is in effect equivalent to driving the motor with a DC voltage. Where available, a programmable analog voltage can be used instead, via a DAC for example.

This laboratory will address the design of a closed-loop speed control system for a DC motor that is part of the Sensors and Instrumentation System made by TecQuipment. Figure 1 shows the set-up to be used for this experiment: A small DC motor can be driven using an analog control signal. A tachometer is mechanically coupled to the driving motor, generating an output voltage that is proportional to the shaft velocity of the driving motor. By monitoring this voltage, a simple closed-loop speed control system can be implemented.

Page 2: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

Fig 1

Because both the microcontroller’s ADC and DAC must work with voltages in the 0-3 V range, the voltage coming from the tachometer is divided by 2 before being applied to the ADC. By the same token, the voltage generated by the DAC is multiplied by 2 before being applied to the motor, allowing more room for control.

Your program should use one of the on-board DAC channels to generate an analog control signal vC (0 ... 3 V). This signal –multiplied by 2– is to be applied to the motor. On the other hand, the feedback signal from the tachometer vFB (0 ... 6 V) –divided by 2– is to be read by the microcontroller using a channel of an ADC unit. The set-point signal vSP should be given as a constant by software. A proportional control law is to be implemented, i. e. control signal

vC = KP ⋅ (vSP - vFB),

where KP is the proportional control gain.

In a first iteration, the whole control cycle – from data acquisition (vFB), via the calculation of control signal vC through to the output of this signal on the DAC – should be implemented within an endless loop. The controller then runs as quickly as possible with the next control cycle directly following the previous one.

However, digital control systems commonly execute at a fixed sample rate. This can be achieved by running the control cycle from within a timer Interrupt Service Routine (ISR), where the timer has been configured to elapse at the end of the desired sample interval. This means that all control signals are only updated at these fixed pre-programmed sample instances. Extend the design of your digital control system to execute at a sample rate appropriate to the motor dynamics.

Page 3: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

Fig 2

Required Material

Hardware SoftwareSensors and Instrumentation System

STM32F3Discovery BoardKeil uVision version 4.7

Practical Experiments

For all experiments, switch on the Sensors and Instrumentation System for a minimum of 5 minutes prior to any settings being made or any results take. This will ensure that any drift in the circuits due to thermal effects will be kept to a minimum.

Part I: Adjustment of the differential amplifier for a gain = 0.5, 0ffset = 0

Consider figure 3a for this part of the lab.

1. Connect the power eliminator to the power input of the Sensor and Instrumentation System.

2. Wait 5 minutes.3. Connect Ref1 to the V input of the panel meter.4. Select V with the panel meter switch (up position).5. Adjust Ref1 voltage to 0 volts.

Consider figure 3b for this part of the lab.

6. Connect Ref1 to the positive input of the differential amplifier. 7. Connect the negative input of this block to 0V.8. Connect the output of this block to the V input of the panel meter. 9. Select V with the panel meter switch (up position)10. In the differential amplifier, select k1 = 1.11. In the differential amplifier, adjust the zero control for getting 0 volts in the panel meter.

vC

MicrocontrollervFB

vSP

ω

Page 4: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

Consider figure 3a for this part of the lab.

12. Connect Ref1 to the V input of the Panel meter.13. Adjust Ref1 voltage to 3 volts.

Consider figure 3b for this part of the lab.

14. Connect Ref1 to positive input of the differential amplifier. 15. Connect the negative input of this block to 0V.16. Connect the output of this block to the V input of the panel meter. 17. Select V with the panel meter switch (up position).18. In the differential amplifier, adjust k2 until the voltage at the output of this block = 1.5

volts. The result is that the gain of this block is 0.5.

Fig 3a Fig 3b

Part II: Adjustment of the summing amplifier for a gain = 2.

Consider figure 4a for this part of the lab.

1. Connect Ref1 to the V input of the panel meter.2. Select V with the panel meter switch (up position).3. Adjust Ref1 voltage to 1.5 volts.

Consider figure 4b for this part of the lab.

4. Connect Ref1 to positive input of the summing amplifier. 5. Connect the negative input of this block to 0V.6. Connect the output of the summing amplifier to the low-pass filter input.7. Connect the output of the low-pass filter to the V input of the panel meter. 8. Select V with the panel meter switch (up position).

Powerinput Zero

Control

Page 5: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

9. In the summing amplifier, adjust k until the reading in the panel meter is equal to 3V. This knob is very sensitive, so it is difficult to adjust. When correctly adjusted, the gain for the summing amplifier is 2.

Fig 4a Fig 4b

Part III: Getting the motor voltage – velocity relationship

Consider figure 5a for this part of the lab.

1. Connect Ref1 to the V input of the panel meter.2. Select V with the panel meter switch (up position).3. Adjust Ref1 voltage to 1 volt.

Consider figure 5b for this part of the lab.

4. Connect everything as shown in figure 5b. 5. Select Hz with the panel meter switch (down position).6. Turn on the motor changing the motor’s switch to the position indicated by 1.7. Wait 10 seconds.8. Get the value shown in the panel meter. It corresponds to the motor’s velocity in Hz (rps).9. Turn off the motor changing the motor’s switch to the position indicated by 0.10. Repeat step 1 to 6 for the following Ref1 voltages: 1.5, 2.0, and 2.5.11. Fill the table shown below.

Table 1Ref1 voltage (Volts) Motor

velocityHz (rps)

1.01.52.02.5

Page 6: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

12. Use linear interpolation to calculate the constant that relates Ref1 voltage to the motor velocity. What is the value of this constant? ______________ (rps/volt)

Fig 5a Fig 5b

Part IV: Getting the motor voltage – tachometer voltage relationship

Consider figure 6a for this part of the lab.

1. Connect Ref1 to the V input of the panel meter.2. Select V with the panel meter switch (up position).3. Adjust Ref1 voltage to 1 volt.

Consider figure 6b for this part of the lab.

4. Connect everything as shown in figure 6b. 5. Turn on the motor changing the motor’s switch to the position indicated by 1.6. Wait 10 seconds.7. Get the value shown in the panel meter. It corresponds to the output voltage of the

tachometer (divided by 2).8. Observe in particular the voltage shown in the panel meter. The voltage should never

exceed 3V for avoiding damaging the STM32F3 Discovery board.9. Turn off the motor changing the motor’s switch to the position indicated by 0.10. Repeat step 1 to 6 for Ref1 voltages: 1.5, 2.0, and 2.5.11. Fill the table shown below.

Table 2Ref1 voltage (Volts)vC

Motor velocity (got in part III)Hz (rps)ω

Tachometer voltage /2(Volts)vFB

1.01.52.02.5

Page 7: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

12. Use linear interpolation to calculate the constant that relates the motor angular velocity (rps) to the tachometer’s output voltage. What is the value of this constant? _______ (volts/rps).

Note: The value of the tachometer voltage should not exceed 3V under any circumstance.

Fig 6a Fig 6b

Part V: Getting the motor’s time constant

Consider figure 6a for this part of the lab.

1. Connect Ref1 to the V input of the panel meter.2. Select V with the panel meter switch (up position).3. Adjust Ref1 voltage to 1.5 volt (DAC output = 2048).

Consider figure 6b for this part of the lab.

4. Connect everything as shown in figure 6b. 5. Connect the oscilloscope channel 1 probe input to the low-pass filter output.6. Set the time base to 500 ms/div. Set the voltage scale to 1 volt/div.7. Turn on the motor changing the motor’s switch to the position indicated by 1.8. Get the open-loop time response of the system using the oscilloscope. Use the hold button

to freeze the response. 9. Assuming a first order system (see Figure 7) calculate the time constant (T1) of the system

__________. Calculate also K __________. Finally calculate Td __________.10. Convert K —observed in the oscilloscope screen in volts— to ADC/DAC units (4095 is

equivalent to 4095 * 3 / 4096) _____________.

Page 8: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

Fig 7

Part VI: Generating a 1-Hz sinusoidal frequency on DAC channel 1

1. Elaborate a program to generate a 1-Hz sinusoidal frequency on DAC channel 1 with a voltage range equal to 3 volts.

2. Download the code to the STM32F3 discovery board. 3. Connect the oscilloscope probe to PA4 for verifying the correct behavior of the DAC.4. Run the program. Does everything work as expected?5. Connect everything as shown in figure 8. 6. Run your program again.7. Turn on the motor changing the motor’s switch to the position indicated by 1.8. Does everything work as expected?9. Observe in particular the voltage shown in the panel meter. The voltage should never

exceed 3V for avoiding damaging the board.10. Turn off the motor changing the motor’s switch to the position indicated by 0.

Fig 8.

Page 9: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

Part VII: On-off closed-loop motor control

1. Connect everything as shown in figure 9.2. Elaborate a program to:

a. Generate a timer interrupt with a time interval equal to the motor’s time constant divided by 20.

b. When the interrupt is generated, i. Measure the voltage coming from the tachometer (via PC1).

ii. Convert this voltage to rps and send it to the LCD display.iii. If the voltage delivered by the ADC is less or equal than the setpoint (1.5V)

set the DAC output value to 3V; else set the DAC output to 0V.3. Download the code to the STM32F3 discovery board. 4. Connect the oscilloscope to the ADC input. 5. Turn on the motor changing the motor’s switch to the position indicated by 1.6. Run the program. 7. Get the time response of the system using the oscilloscope. Does everything work as

expected?8. Turn off the motor changing the motor’s switch to the position indicated by 0.

Part VIII: Proportional closed-loop motor control

1. Connect everything as shown in figure 9.2. Calculate KP using Table 3. 1 3. Elaborate a program to:

a. Generate a timer interrupt with a time interval equal to the motor’s time constant divided by 20.

b. When the interrupt is generated, i. measure the voltage coming from the tachometer (via PC1)

ii. Convert this voltage to rps and send it to the LCD display.iii. Calculate the error : setpoint – voltage measured. The setpoint —set by

software— is 1.5V (equivalent to ADC value = 2048).iv. Calculate m as follows: m = m + KP*e.v. If m ≥ 4095 then m = 4095; else if m<0 then m=0.

vi. Output m value using the DAC.4. Download the code to the STM32F3 discovery board. 5. Connect the oscilloscope to the ADC input. 6. Turn on the motor changing the motor’s switch to the position indicated by 1.7. Run the program. Does everything work as expected?8. Turn off the motor changing the motor’s switch to the position indicated by 0.9. Adjust KP. Repeat steps 4 to 8 until getting a satisfactory system time response (low rising

time, low overshoot).1 As an alternative, you can use the ZNFD PID tuning procedure explained in the Annex.

Page 10: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

Table 3

Controller KP TI TD

ProportionalT 1KT d

Proportional + Integral0.9T 1K Td

3.3T d

Proportional + Integral + Derivative1.2T 1KT d

2Td 0.5T d

Fig 9

Reflections Please be specific.

1. Are the instructions for doing this lab clear enough?2. Is the lab too complicated or too easy? 3. For which aspects of the lab did you need help from your colleagues? 4. For which aspects of the lab did you need help from your professor? 5. What did you learn?

Page 11: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

Annex

Digital PID Controllers

Proportional-Integral-Derivative (PID) control is still widely used in industries because of its simplicity, good control performance and excellent robustness to uncertainties. No need for a plant model. No design to be performed. The user just installs a controller and adjusts 3 gains to get the best achievable performance. Most PID controllers nowadays are digital. In this document we discuss digital PID implementation on an embedded system. We assume the reader has some basic understanding of linear controllers.

Different forms of PID

A standard "textbook" equation of PID controller is

where the error e(t), the difference between command and plant output, is the controller input, and the control variable u(t) is the controller output. The 3 parameters are K (the proportional gain), Ti (integral time), and Td (derivative time).

Performing Laplace transform on (1), we get

Another form of PID that will be discussed further in this document is sometimes called a parallel form.

With its Laplace transform

We can easily convert the parameters from one form to another by noting that

(1)

(2)

(3)

(4)

Page 12: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

Discrete-time PID Algorithm

For digital implementation, we are more interested in a Z-transform of (3)

Rearranging gives

Define

(8) can then be rewritten as

which then converted back to difference equation as

a form suitable for implementation. Listing 1 shows how to code this algorithm in C. We assume that the plant output is returned from a function readADC( ), and the control variable u is outputted using writeDAC( ). Note that u must be bounded above and below depending on the DAC resolution. For instance, UMAX = 4095 and UMIN = 0 for 12-bit DAC.

double e, e1, e2, u, delta_u;k1= kp + ki + kd;k2=-kp – 2*kd;k3= kd;

void pid( ){

(5)

(6)

(7)

(8)

(9)

(10)

Page 13: homepage.cem.itesm.mxhomepage.cem.itesm.mx/.../ASSIGNMENTS/labs/LAB7c.docx · Web viewLearning Objectives In this lab, you will learn how to construct a closed-loop angular velocity

e2 = e1; // update error variables e1 = e; y = readADC( ); // read variable from sensor e = setpoint – y; // compute new error delta_u = k1*e + k2*e1 + k3*e2; // PID algorithm u = u + delta_u; if (u > UMAX) u = UMAX; // limit to DAC range if(u < umin) u = UMIN; writeDAC(u); // send to DAC hardware}

Listing 1: C code for the PID algorithm

PID Tuning

Adjusting the PID gains to achieve a good response could be problematic, especially for an inexperienced user. As a result, most commercial PID controllers have functions to tune the 3 parameters automatically. This is normally called "autotuning" feature. There are some variants of autotuning methods suggested in the literature. On the other hand, there are several manual tuning schemes that can be used, such as the Ziegler-Nichols Frequency Domain (ZNFD) method that we will explain now.

First we have to caution that, to conform to the derivation from [1], our ZNFD discussion refers to the "textbook" PID equation (1), not the parallel form (3). This does not pose any problem since the two forms are closely related by equation (5).

To tune a PID controller manually by ZNFD method, we start by turning off both the integral and derivative terms. From (1) we see this can be done by lettingT i→∞ ,T d=0. So now the PID is left only with the proportional gain K. We crank K up to the point that the closed-loop system starts to oscillate. At this point, the plant output will swing in a constant sinusoid motion, not growing and not dying out. Write this value down on a paper as Ku. Then find a way to measure the period of oscillation. Note this period as Tu. That’s all. Suggested values of the 3 parameters can be found from Table a1.

Table a1: Suggested PID parameters from ZNFD methodController Form K Ti Td

P 0.5 Ku - -PI 0.4 Ku 0.8 Tu -

PID 0.6 Ku 0.5 Tu 0.125 Tu

Reference

[1] K.J. Astrom and T.Hagglund. PID Controllers, 2nd ed., Instrument Society of America, 1995.