2nd Year Microprocessors Laboratory by cometjunkie49

VIEWS: 21 PAGES: 20

									2nd Year Microprocessors Laboratory
2009-2010 Dr Rachel Edwards, P554

INTRODUCTION Computer control of experiments is becoming the norm. It’s used for experiments in the MPhys laboratory and research work within the department, and in industrial applications, with computers able to analyse data while it is being recorded. One program which is becoming increasingly common for such work is LabVIEW, a graphical programming language. This module is intended to give you a grounding in computer control and analysis for experiments, and in using LabVIEW for these tasks. Preparatory Tasks Have a look through the script! This will give you an idea of what to expect. There are sections where you will be asked questions, in particular section 3.1 (theory), and you will need to work on this outside the laboratory time. You may also find the very useful assessment checklist hidden somewhere in the script. The online version of the script has colour figures in it. Some useful instructions; USE A LAB BOOK. You are required to keep a lab book in the microprocessors laboratory, as in any other lab, and this should be a record of your work. SAVE your programs to your H: file space and/or a USB flash disc, and ensure both of you have access to the programs (it shouldn’t need pointing out that you should keep backups and change the version number of the file you are working on between revisions, e.g. LED1, LED2 etc; sometimes you will need to refer to an older version). The lab outline is given below;

1. Introduction to LabVIEW; you will learn to write simple LabVIEW routines to do
mathematical calculations and work with different loops.

2. Introduction to computer control; this will introduce you to the data acquisition board for
control of experiments and reading of data. You should aim to complete these two sections by early in session 2. 3. Main experiment; control of a compound pendulum. Using LabVIEW and the data acquisition card you will control a moving arm, calibrating the angle and testing several control mechanisms for setting the arm position. You must attempt the theory work before you start this section. 4. Assessment; once your final routine is completed you must show it to a demonstrator. Approximately one third of the marks will be given to the final program, split between how well it works, program layout (including annotation) and correct use of code. The rest of the marks will be based on your lab book, taking into account results, equation derivations

1

and discussion. The lab book should be handed in after your laboratory work is complete, and no later than 12 noon on the Thursday following your final session. This should be posted into the appropriate cupboard in the UG lab. 5. Suggestions for further work; this section gives suggestions for things to do for extra marks if you complete the lab before the end of the fourth session. Learning outcomes For successful completion of the laboratory you will be able to • Demonstrate knowledge of LabVIEW by o performing simple mathematical calculations o plotting, analysing and saving data o controlling experiments using a data acquisition board • Show understanding of theoretical models of control and feedback by comparing these with the behaviour of a real system • Use feedback to monitor and control a compound pendulum. IMPORTANT SAFETY INFORMATION 1. The box labelled Servo Controller Interface is the interface between the computer and the experiments. DO NOT plug in or unplug the cables between this box and the computer. 2. When using a computer, it is important to take regular breaks to rest the eyes and stretch. 3. Be careful of the nodding donkey – it may bite!

1. INTRODUCTION TO LABVIEW LabVIEW is a graphical programming language developed by National Instruments. It is a neat way of building “virtual instrumentation” (VIs), like a PC-based oscilloscope. For some people, programming graphically seems a bit odd. For others, it’s a lot more intuitive than using a written language. LabVIEW has many built-in training programs, and further examples are available on their website and in the books in the lab (see References). The first part of this laboratory is designed to give you an introduction to LabVIEW. Attempt all the exercises even if you know LabVIEW already; these will be useful for later parts of the laboratory, and may be marked for credit. 1.1 Starting LabVIEW Turn on your computer and log-in using your ph**** username. To run LabVIEW, from the Start menu choose Programs → National Instruments (NI) LabVIEW 8.5.1. A window will open and offer you the option of opening an existing routine or starting a new one. 1.2 “My first routine” – a simple calculator Start by opening a blank VI (virtual instrument). Figures 1.1 & 1.2 show the windows which you can open, with keyboard shortcuts and menu options. Note: LabVIEW has a built-in automatic tool selection feature on the Tools Palette (top button) that changes the behaviour of the mouse pointer, depending on what type of object you are pointing at. You should have open a blank grey Front Panel – this is where you make the user interface for the instrument – and a Block Diagram, which is where the programming happens. It can be useful to open Context Help.

2

Context Help will give you (mostly!) useful information about the thing your mouse is over. Context Help Help → Show Context Help

Front Panel - Ctrl-E This is what the end user sees – it has the controls and graphical / numerical outputs.

Controls Palette View → Controls Palette

Tools palette View → Tools Palette

The tools palette decides what the mouse does, e.g. wiring tool, moving things tool, writing tool… If the top part is highlighted, it chooses the most sensible option.

The controls palette gives you a choice of buttons, graphs, numerical outputs etc to put on your front panel. It can also be summoned with a right click.

Figure 1.1; Front Panel. Control of the windows is described in the black boxes.

Block Diagram –Ctrl-E This is the “behind the scenes” programming, and contains everything to make the controls and outputs work.

The functions palette gives you all the things you’ll need to write the program, including lots of pre-written functions and programs. It can also be summoned with a right click. Functions Palette View → Functions Palette

Figure 1.2; Block diagram.

3

Your first challenge will be to build a simple calculator. This is intended to show you how to start programming in LabVIEW, not to demonstrate its power! 1.2 A. This calculator will add two numbers. Start by building your user interface. This should have two numeric inputs for the numbers, and a numeric output to display the result. • Open the controls palette by right-clicking on the front panel and choose Modern. To see icons and words as in figure 1.1, click View » view palette as » Category (icons & text). • Add some numerical controls for inputting the numbers. In figure 1.3 I chose Modern » Numeric » Numeric Control but there are other ways to input a number in LabVIEW. Click on your chosen control, then on the Front Panel where you want to place it. Add a second control for the second number. • Choose an output for displaying the results, such as Modern » Numeric » Numeric Indicator, place on the Front Panel, and arrange the icons. To add writing, either doubleclick on the Front Panel or choose the “A” tool from the Tools Palette.

Figure 1.3; Calculator A; Front panel (left) and block diagram (right).

Now go to the block diagram. The two numerical inputs and one output will be placed at random, so rearrange them. Note: LabVIEW does not execute from left to right or from top to bottom. The functions execute when the data they need reaches them, so it is good to arrange your program like a flow diagram. • Open the functions palette by right-clicking on the block diagram. Choose Programming » Numeric » Add (the triangle shown in figure 1.3) and place on the block diagram. • In LabVIEW data flows along wires, so you need to wire everything up – hover the mouse over the little output arrow on the right-hand side of a numerical input, and the pointer should change to a spool of wire. Left-click once on the arrow, and again on one of the inputs for the Add (this will become clearer when you try it). • Wire the second numerical input to the Add. Wire the output of the Add to the numerical indicator, as shown in figure 1.3. • Don’t forget to save your program. Now go back to the front panel and input some numbers. To run your program, press the arrow underneath “Edit” – if the arrow is broken, there are errors in your program. Check the answer.

4

Challenges; Display your answer pictorially, using e.g. a tank or a meter, as well as a number. You may need to alter its properties to give the right range of numbers – right click on the display and choose Properties, Data Entry. • Right-click a numerical input control, and choose Properties. What does changing the data range, the format and precision do? (You can also change its label in the “Appearance” tab). • Right click on the Add icon. One of the choices is “Replace”. Try replacing it with some other mathematical operation. • Open Context Help and hover the mouse over the orange wire. What does it tell you about the data type? See also Appendix B.
•

1.2 B. A calculator that only does one thing is pretty useless! The Case Structure works as an “if… then do… else…” does in text-based languages and allows you to choose the action. • Open the functions palette. Choose Structures » Case Structure. To place this on the block diagram, click towards the top left of the Add button and drag the mouse to the bottom right of it. This may break the wires – press Ctrl-B to remove all broken wires. • If needed, rewire the inputs to Add, and the result to the output (Case 1, figure 1.4). • A standard case structure has two possible actions for an input of True or False. Click the right pointing arrow at the top of the Case Structure to open the case for an input of False. Into this window place your second mathematical function, and wire it up. • This structure needs a true/false, i.e. Boolean control. Right-click on the green ? on the left hand side of the structure and choose Create Control. This will give you a Boolean control on the Front Panel. Move this to a sensible place and play with its properties (right click the control for its menu); in the Operation tab it should switch when pressed. In figure 1.4 a Toggle switch is used. • Save your program with a different name (“Substitute copy for original”). Now test your program, changing between the functions before running.

Figure 1.4; Calculator B; Front panel (left) and block diagram (right), showing two different cases.

The Case Structure has one or more sub-diagrams, or cases, exactly one of which executes when the program runs. The value wired to the green ? determines which case to execute and

5

can be Boolean, string, integer, ring or enumerated type. It is possible to use this to choose between +, -, x and / for a more complicated calculator (try this if you want a challenge – a Ring menu control may be useful, and you can add cases by right-clicking on the structure border). 1.2 C. LabVIEW has many built in mathematical operations. Go back to your first version of the calculator. Right-click on the Add button on the block diagram and choose Replace. If you want to add more than two numbers use Compound Arithmetic (shown right). To add more inputs make it larger. For a more complicated function you can create the function yourself using several operations, use the Formula structure if you like using written code, or use the built in Formula VI; Functions palette, Express » Arithmetic & Comparison » Formula. This will take as many inputs as you need and you can program in your chosen formula. Choose a formula or expression and test the express VI for several inputs. Note the formula and results in your lab book. 1.3 While Loops, For Loops and Shift Registers When a program is running it will continue to run until either it has finished, or it is stopped. In the previous examples there was a definite stopping point. If this isn’t reached by a sensible time you can use the red Abort Execution button to the right of the run arrow to stop a routine; this is not recommended as it can cause problems (e.g. when saving a file, LabVIEW does “open file”; “write”; “close file” and aborting before the file is closed can corrupt it). Instead, use the While Loop – the program will keep running until a certain condition is met, in most cases until a stop button is pressed, doing exactly the same thing each time it runs (each iteration).

Figure 1.5; Gauge mechanism

1.3 A. By now you should be getting to know your way around LabVIEW. • Open a new blank VI and place a numeric input and output (Numeric Indicator » Gauge) on the Front Panel (see figure 1.5). You may need to play with their limits in Properties. • On the block diagram, put a While Loop (Structures » While Loop) around the number and slide in the same way as adding a Case Structure. You may need to add a Stop button – right click on the red stop sign in the bottom right hand corner of the loop, and choose Create » Control. Wire these together if needed. • Add some timing control (Timing » Wait until next ms multiple, and create a numeric constant); a small delay on each iteration of the program will free the processor to do any other essential tasks. Figure 1.5 will run the program (read numeric input, plot to gauge) and then wait until the time taken to do this plus the delay equals 10 ms.

6

Run the program and try changing the input number. The program will continue to run until you get bored and hit the stop button on the Front Panel. As in text-based programming languages, you can run a program for a chosen number of iterations using a For Loop. On your program right-click on the While Loop and choose Replace with For Loop. Now you don’t need the stop button – but you do need to tell it how many iterations by wiring a control or a constant to the N (right-click, Create Constant or Control). Choose the number of iterations and run the program. This will run very quickly! To give yourself time to change the input increase the delay (figure 1.6).

Figure 1.6; Gauge mechanism with For Loop and delay

1.3 B. It is sometimes essential to carry data from one iteration to the next, for example when doing feedback and control. In a new blank VI, program figure 1.7(a) on the block diagram. This is effectively a counter.

Figure 1.7; Simple addition routine with shift registers, using (a) While or (b) For Loops

Start with a While Loop and stop button. Place an Add, then give the constant to be added (right-click, Create » Constant; 1 in this figure, choose your own number). • Wire from the output of the Add to the edge of the While Loop. An orange box will appear on the Loop – right-click on this and choose Replace with Shift Register, and the up-arrow box will appear. A corresponding box may appear on the opposite side, or the mouse pointer will change and you will need to click on that side. This will feed the output back into the While Loop. • Now wire the rest! The constant (2, to the left of the loop) is the initial input. Run the program; it will continue adding until you press stop. The output will be huge but you can increase the delay to slow it down. Figure 1.7(b) shows a similar routine but with a For Loop, with the user controlling the number of iterations (Numeric 3) and the number to start with (Numeric 4).
•

Program this, and work out what it does differently. Up until now you have worked with single or double accuracy real data, and Boolean data. The other types of data, including arrays and dynamic data, are shown in Appendix B. Congratulations – now you know some LabVIEW!

7

2. INTRODUCTION TO COMPUTER CONTROL When you have completed the Introduction to LabVIEW exercises you should be ready to work on control of experiments using LabVIEW and the data acquisition (DAQ) board (the analogue to digital, A-D system). More details about the board are available in Appendix A. The box labelled Servo Controller Interface (the interface between the DAQ board and the experiments) should now be turned on. The following exercises will familiarise you with passing digital and analogue information through ports to and from the equipment on the bench. 2.1 Measurement & Automation; checking the system Plugged into the Interface box should be an LED board, a pair of crocodile clips, and the “Nodding Donkey”, a moving arm with a sliding mass for exploring control and feedback mechanisms. The sliding mass is controlled by a motor using a digital output from the computer, and a potentiometer gives an analogue input related to the arm position. A set of weights for adding to the arm is available in the lab. You should also have a resistor ladder for use with the crocodile clips. It is important to check equipment before doing experiments. Open Measurement & Automation from the desktop. Choose My System » Devices & Interfaces » NI-DAQmx Devices » NI PCI-6221: “Dev 2”. This is the National Instruments DAQ board. Start by performing a self test – click on Self-Test, this should open a window reading “Okay”. If not, see a demonstrator. 2.1.1 Test digital control • Open Test Panels. First check the LED board. Open the Digital I/O tab. The LED board is on port1, so set this in section 1 of the Digital IO tab (figure 2.1, left) • Set all to output in section 2. • Press Start – this starts the computer talking to the port. In part 3 of this window, try changing some of the switches (above the all-high and all-low buttons, see figure 2.1). • If changing the switches turns on and off the LEDs on the board, this is set up correctly. Choose “All Low” and press Stop. • Now check the digital control of the moving arm. This is on port 0, again as all output. • Press start, and turn on the switches, starting on the side marked 0. The sliding mass, and hence the arm, should move – this movement will become very noticeable by the time the output is at 00011111. Reselect all-low. • Press Stop. 2.1.2 Test analogue input • Now open the Analogue Input tab. • Firstly, check the crocodile clips. These are on Dev2/ai1, with an Input Configuration of RSE and input limits of ± 10 (figure 2.1, right). • Press Start and check that the graph changes when you connect the clips to different pins on the resistor ladder. Remember to connect its battery! • Press Stop. • The arm position reading is on Dev2/ai0 (RSE, ± 10). Check the settings, press Start, and gently nudge the arm. The graph should update as in figure 2.1, right. • Press Stop and close the Measurement & Automation window. This has confirmed that all components are working correctly. You have achieved computer control! However, it is much better to have this as part of a program which can control things in exactly the way you want and analyse and save the data. This is when LabVIEW is useful…

8

Figure 2.1; Digital IO and Analogue Input in Measurement & Automation program

2.2 Digital output; control of LEDs The first exercise is to control the LED board using LabVIEW. Initially, you will control just the first LED. • Open a new blank VI in LabVIEW. Place a button / switch on the Front Panel – this will be the on-off switch for the LED. To make it show “on” or “off” you can right click the switch and in Properties choose Show Boolean Text, and add text in Multiple Strings. In the Operation tab, choose Switch when pressed. • Place an LED on the Front Panel. If you don’t know where in the Functions Palette these are you can Search, look through the options, or as a last resort ask a demonstrator. • On the Block Diagram place a While Loop around the button and the LED and add a Stop button. Wire between the button and LED. • Run the program – pressing the button should turn the LED on screen on or off. Press the Stop button. Now to talk to the LED board. LabVIEW has a series of Express VIs, sub-programs written by NI, which do common tasks. To talk to equipment you need Express » Output » DAQ Assist, so place this on the Block Diagram. A configuration window will open. Choose Generate Signals, with a Digital Output, and a Line Output. The LED board is, as in section 2.1, port1, and we will talk to one LED on line0. Press Finish. • In the next window choose 1 Sample, On Demand, and press Okay. This will send a signal to the LED board whenever LabVIEW executes an iteration. • If you wire the button directly to the DAQ Assistant you will get an error, as the DAQ expects an Array input. Instead; on the Block Diagram place a Build Array (Programming

9

» Array » Build Array). The output will be an array containing the required data. Wire as shown in figure 2.2, including a small delay. Run the program. Pressing the button should control both the screen and the board LEDs. Don’t forget to save your program.

Figure 2.2 Control of a single LED on board and on screen

The second exercise is to control all 8 LEDs on the LED board and to use this to translate a number from a decimal input to a binary representation on the board. Save your program as a new version. • On the front panel replace the button and LED with a numerical control. Right-click this, choose Representation, and pick U8 (8-bit, unsigned integer). This will send the expected number of bits to the DAQ assistant. In Properties, under Data Entry, set the steps to be integer steps. What is the range of numbers you can represent with 8 bit binary? Explain your calculation in your lab book and set the limits for the numerical control.
•

•

On the block diagram you will need to convert from a number to a Boolean array to send the correct output to the LED board. Fortunately, LabVIEW has a tool for this in Mathematics » Numeric » Conversion » Number to B… Wire the number to the conversion, and the conversion to the DAQ Assistant (figure 2.3).

Figure 2.3 Numeric control of all LEDs on board

The DAQ expects only one line input, but is now going to get 8 bits of information. Doubleclick the DAQ Assistant. Click Add Channels, and add Dev2 / port1 / line1 (you should already have line0). Repeat this until all 8 lines on port1 are included in the right order. Press Okay. It will think for a while, then your program should be ready to run.
•

Test that your program works and correctly converts from a numeric to binary. Give the conversion of several numbers in your lab book.

10

Finally, you can also control the LEDs using an array of Boolean switches. In a new version of the program delete the Numeric Control and Conversion. • On the Front Panel, open the Controls Palette, choose Modern » Array, Matrix & Cluster » Array, and place on the Front Panel. • Again from Controls, choose Modern » Boolean » Push Button and place this inside the array. Stretch the array towards the right until you can see eight buttons (figure 2.4). • On the Block Diagram, wire the array to the DAQ Assistant.

Figure 2.4 Boolean control of all LEDs

Go back to the Front Panel. All the buttons will be grey, and the program won’t run until you press the far right button to initialise the array – LabVIEW always needs this with new arrays! Run the program, have fun turning LEDs on and off… then turn off all LEDs and press Stop. By using a Case Structure within the While Loop it is possible to switch between Numeric control and switch control. If you have time, attempt this. 2.3 Analogue input; voltmeter This section is concerned with reading in analogue signals which can then be processed and/or plotted in LabVIEW and saved to a file for future analysis. For this you will use the resistor ladder consisting of a PP9 battery and a chain of 10 nominally equal resistors. Using the crocodile clips you can measure the voltage across different numbers of resistors on the chain. In section 2.1 you used Measurement & Automation to plot the analogue input from the crocodile clips. To recreate this you need a graphical display. LabVIEW offers several options, the most useful being Waveform Graph (this updates with a set number of points each time) and Waveform Chart (updates 1 point at a time). Open a new Blank VI and place a waveform chart (Express » Graphs » Waveform Chart) on the Front Panel. On the Block Diagram, • Choose Express » Output » DAQ Assist and place on the Block Diagram. • Set up the DAQ; Acquire signals » Analogue Input » Voltage » Dev2, ai1 and press Finish. Set-up with a signal input range of -10 to +10 V, RSE terminal configuration and 1 sample (on demand). Press Okay. • Wire between the DAQ Assist and the chart, add a While Loop and Stop button so you can stop the program acquiring data, and a short delay. Run the program and test by checking the voltage across one or more resistors. You can change the properties of the chart, for example turning off auto-scaling. Then to change the axis range double-click on a number and change it to what you want to show. This program gives a graphical output, but in order to get a reliable measure of the voltage at different points on the chain you will need to take an average. This can be done manually by saving a set number of points and using a data analysis program, or by using stacked feedback loops. Figure 2.5 shows a program to save the voltage drop over a resistor a chosen number of times. Figure 2.5 is based on your previous program, replacing the While Loop with a For Loop with a control for the number of iterations. You can input the resistor number and use Programming » Array » Build Array, extending so it has two inputs, to create an array containing resistor

11

number and voltage. The data is output to a file using an Express VI, found in Programming » File I/O » Write Meas File. When you place this on the block diagram a window will open for you to change the settings.

Figure 2.5 Analogue input and data saving routine for resistor ladder measurements

Figure 2.6; Write Meas File

Set this up as in Figure 2.6, choosing a sensible name for the file on your H: drive. Using headers will give information about the experiment but can make the file harder to read. To save multiple data to one file choose “Append to file”. Context Help will show you the wiring options for this VI; one is called Enable and allows you to turn on and off saving the data using a switch. When you wire from Build Array to the signal input an Array to Dynamic Data converter appears. Double-click this and check it is converting “2D array of scalars, columns are channels”. Run the program and check what the output file

12

looks like – you should be able to read it using notepad. Run the program and plot the results in Origin (or Excel, if you must) and comment on them. What are the values of V and R? Can you calculate any properties of the circuit? You should now be ready to proceed to the main control experiment. 3. MAIN EXPERIMENT; FEEDBACK AND CONTROL Feedback and control mechanisms are used in a wide variety of applications, from room temperature control to precision servo controllers on an aircraft. Taking as an example the temperature in a room – we don’t want the temperature to overshoot what it’s set to or oscillate, but likewise we don’t want it to take too long to reach the desired temperature. Feedback of the temperature measured by a sensor can be used to control the temperature by turning on and off the heaters / air conditioning. In this experiment you will look at stabilisation of a relatively simple system – the position of an arm which is free to rotate and is controlled through moving a weight along the arm.

Figure 3.1 Moving arm experiment and controllers

Figure 3.1 shows a schematic of the equipment used for this experiment. An irregularly shaped object with an arm extending out to one side is pivoted on an axle about a horizontal axis (point P in Figure 3.2). The angle θ is shown by the pointer and the scale on the stand, or can be measured by calibrating a potentiometer attached to the axle. The voltage of this potentiometer is approximately proportional to the angle of the arm and can be read by the computer through the A-D card. On the arm is a small mass, m, which can be moved by the servo motor and controlled through the digital output of the A-D card; the system should come to rest with the arm approximately horizontal when this mass sits at the centre of its range. Additional masses are available to perturb the system. If the system is displaced (for example, by nudging the arm) the centre of mass is no longer below the axis of support and gravity provides a restoring torque. Moving the sliding mass adds an additional torque and the system will come to rest at a different angle. This mass can be moved very quickly, leading to an almost instantaneous control of this additional torque. A requirement may be to bring the system to a series of different states quickly and with minimal overshoot, or to return quickly to a state despite outside influences such as nudging of the arm or the addition of extra weights. 3.1 Theory Much of the theory behind this system has been covered in PX145 (Physics Foundations) and PX132 (Mechanics A). The system can be modelled as a damped compound pendulum

13

with moment of inertia I. The system is free to move through an angle θ, and with the sliding mass m at x0 and no outside perturbation will settle at θ0. Figure 3.2 (a) shows the system after it has been moved from equilibrium. A restoring torque,

τ = − Mgr sin(θ − θ 0 ) ,

(1)

where M is the total mass and r is the distance between the pivot (P) and centre of mass (C), will cause it to return it to equilibrium (3.2(b)). θ is defined to be increasing as the arm moves anticlockwise; hence the sign of the torque, as positive torque must rotate the arm in the anticlockwise direction. In practice, x0 is chosen so that θ0 is zero.

Figure 3.2 Angles and positions in moving arm experiment

When the weight is displaced a distance ∆x(t) (fig. 3.2 (c)) an extra torque occurs,

τ (t ) = mg ( x0 − ∆x(t )) cos(θ − θ 0 ) − mgx0 cos(θ − θ 0 )
= − mg∆x(t ) cos(θ − θ 0 ) .
(2) Again, to get the right sign for the torque ∆x(t) is defined to be positive for the sliding mass moving away from the axle, and ∆x(t) can be varied in order to control the angle of the arm. For small angles the total torque for a system with moment of inertia I can be approximated as,

d 2θ τ T = Iα = I 2 = − Mgrθ − mg∆x(t ) . dt
Derive (1)-(3) in your lab book. Diagrams may be useful. 3.2 Feedback and control

(3)

There are several mechanisms for performing feedback and control, each with its own advantages and disadvantages. To understand the behaviour of the system we can solve its equation when the total torque is considered. The control mechanisms you will use are known collectively as PID control (proportional – integral – derivative), and determine the output to the controller by measuring the error e(t) between the system state and the desired set-point;

output (t ) = k P ⋅ e(t ) + k I ⋅ ∫ e(t )dt + k D ⋅
0

t1

de(t ) , dt

(4)

where kP, kI and kD are constants for each mechanism. • No control; with no control the arm will act like a damped harmonic oscillator centred at θ0 when the mass is at x0 and the system will slowly return to its equilibrium point after a perturbation. Damping is due to dissipative effects, with an equation of motion

& θ& + 2 µθ& + ω 02 (θ − θ 0 ) = 0 ,

(5)

where 2µ is the natural damping coefficient and ω0 is the undamped natural frequency given Derive the solutions to equation (5); considering the different damping regimes.

14

2 by ω 0 = Mgr I .

•

Damping (derivative control); this aims to increase the natural damping of the system to bring it to rest as quickly as possible. Equation (5) only applies when the mass is at x0, i.e. ∆x(t) = 0. When the mass is moved to damp the motion the extra torque must be considered, and for small angles

& θ& + 2 µθ& + ω 02 (θ − θ 0 ) = −

mg ∆x D . I

(6)

We can apply a torque where the displacement of the sliding mass ∆xD is proportional to the & & instantaneous angular velocity of the pendulum θ , i.e. ( mg I ) ∆x = k Dθ . (6) becomes

& θ& + (2 µ + k D )θ& + ω 02 (θ − θ 0 ) = 0
which is again damped harmonic motion.

(7)

& & Find, by putting θ& = θ = 0 , the equilibrium value (i.e. where it is stable). Derive the general solution for this motion.
This allows the motion of the system to be damped, but gives no control over θ0.
•

Proportional control; in a real system we wish to set a target, e.g. an angle for the arm to move to, which requires feedback. By moving the mass we can apply an extra torque which is proportional to the error between the desired and actual states, i.e.

mg ∆x P = k P (θ − θ T ) I
where θT is the target angle, changing equation (7) to

(8)

& θ& + (2 µ + k D )θ& + ω 02 (θ − θ 0 ) + k P (θ − θ T ) = 0 .

(9)

Find the equilibrium value and the general solution. You will see from your solution that this method of control allows you to approach the target angle but not reach it, unless θ T = θ 0 .
•

Integral control; the torque applied in the previous situations will not reliably move the system to the desired state under all conditions. Integral control will sense that the angle at which the system is settling is different from the target angle and adjust the applied torque until the desired state is reached. This applies a torque τ i at a time t by setting
t1 mg ∆x I = k I ⋅ ∫ (θ (t ) − θ T )dt . 0 I

(10)

If kI has the right sign this will tend to push the system towards the target angle. This, in principal, should work on its own, but can have problems when used in isolation.
•

PID control; To overcome all the problems with each individual control mechanism, PID control is used. The equation describing this is

& θ& + (2µ + k D )θ& + ω 02 (θ − θ 0 ) + k P (θ − θ T ) + k I ⋅ ∫ (θ (t ) − θ T )dt = 0 .
0

t1

(11)

Zeigler & Nichols (see references) developed a method for tuning kP, kI and kD. For a rapid response it is tempting to set them to be large, leading to a large control output. This will amplify every error and the system will tend towards chaos. For small values of the constants the system is slow to respond and may not eliminate an error before the next one appears.

15

3.3 THE EXPERIMENT Your program to talk to the moving arm experiment needs to read the arm position from the potentiometer analogue input and control the sliding mass through the digital output. You should create a new VI, and on the front panel place an indicator to display the angle or analogue input (is a chart or numerical output better?), and a control for the digital output, remembering the limits on the digital output you calculated in section 2.2. On the block diagram place a DAQ Assistant VI to read the analogue input, As before, the settings are: Analogue input / voltage; Dev2/ai0; 1 Sample (On Demand); Settings: -10 to 10 V, RSE Terminal Configuration, no scaling. Click Run/Stop on the Assistant window to test and nudge the arm to check that it is reading okay. Use a second DAQ Assistant VI to send an output to the sliding mass motor, with the following settings: Digital output; Dev2/port0; 1 Sample (On Demand). Click Run/Stop to test – the digital outputs that are active are 0 to 7. Wire up your system (a suggested program is shown in figure 3.3); remember that the DAQ Assistant VI for the digital output expects an array input, so you will need to use Build Array. In the following experiments the time delay you choose will affect your control constant values.

Figure 3.3 Block diagram for the initial analogue input / digital output routine for moving arm

Guidance Some guidance for how to run your experiment is given in the steps below. At the end of the laboratory you should have one program capable of using all three control mechanisms. The graphs you produce for your lab book should have labelled and enumerated axes. When programming you can add notes to describe what’s going on by using the “A” tool from the Tools Palette; this is essential for good layout of the program. A good student will be able to correctly implement each control mechanism. A very good student will demonstrate the significance of the different parameters, and present the ranges of good values of kP, kI and kD. They will connect together theory and experiment qualitatively. An excellent student will correctly relate the results and theory quantitatively. NEXT STEPS; Keep notes on ALL your work.

1. Calibrate the system to find the relationship between potentiometer reading and arm pointer angle. Find “Offset”, the value for the digital output such that when the mass is at x0 the angle is θ0; Offset may vary depending on the direction of movement of the sliding mass.
Update the VI to display the angle of the pointer rather than potentiometer reading (you can use numeric functions, or program a formula using the express VI). Remember to keep each version of the VI you write; save each as a different file e.g. Arm1.vi, Arm2.vi etc.

16

2. Investigate the behaviour of the system with no feedback control. You will need to output
the time and angle data to a file. Figure 3.4 shows a suggested block diagram using Dynamic Data to save angle and timing information to a file. The delay value should be tested; your system may work best with a very small delay. Explore the settling motion of the arm after an initial displacement and calculate 2µ and ω0 (see theory); figures and analysis should be included in your lab book.

Figure 3.4 Calibrated routine for moving arm experiment, with output for time-angle data

3. The first control mechanism to program is derivative (damping) control. As in section 3.2 we & assume an extra torque of the form ( mg I ) ∆x = k Dθ due to moving the mass. On each iteration the angle changes and the extra torque can be approximated as k D (θ i − θ i −1 ) ∆t .
Figure 3.5 (next page) shows a suggested routine implementing this equation; note that it is simpler to use “Convert from dynamic data” to get the angle before doing calculations. The output to the motor should include your offset (to set x0) and the damping control. What sign should kD have? Should there be any constraint on the number sent to the motor (“coerce” in LabVIEW)? Investigate the behaviour of the system with kD (e.g. what is the optimum value of kD for bringing the system to rest; for what values is the system stable / unstable; what happens when you add a perturbing mass…) To what extent are the predictions of the simple model valid?

4. This has (hopefully!) damped the system. Next add Proportional control, bearing in mind the equations in section 3.2. You will need to create a control to set the chosen angle, θT. The
output to the motor should consider Offset, derivative, and proportional control. How does the behaviour depend on kP, and is it always stable (for a good value for kD)? Do you expect it to reach θT (see theory)? For your optimal values of kP and kD produce a plot of final angle achieved against target angle. How good is it if there is a perturbing mass?

17

Figure 3.5 Moving arm routine with Derivative control.

5. Add integral control. Investigate its effect by itself and with the other forms of control.
Produce a plot of target vs. achieved angle and discuss the results. If you are stuck, consider how the differentiation was done for derivative control.

6. Use your PID control program to set the system to a chosen target angle. Produce figures
showing this works even when the system is perturbed by knocking the arm and/or adding extra weights. You may need to alter kP, kI and kD when all are used. When your program is completed show it working to a demonstrator – successful implementation will also be demonstrated by the results presented in your lab book. The equations discussed earlier are for an ideal system, so may not precisely describe this experiment, and you may need to consider the relationship between angle, position and the format the motor expects. You will need to discuss where the differences arise and their likely effect.

4 ASSESSMENT; A CHECKLIST
This is the end of the core-work. A good program, completion of all the tasks and a wellpresented lab book will give you a good mark. You should reach this stage at some point during the final session. Suggestions for further work are below. Even though the laboratory work is done in pairs you should prepare your own lab book for assessment. Approximately one third of the marks will be given to the final program, split between how well it works, program layout (including annotation) and correct use of code. The rest of the marks will be based on your lab book, taking into account results, equation derivations and discussion. You may need to print certain programs for inclusion in your lab book. To do this choose File » Print » choose file » Icon, description, panel and diagram. Printing as a rich text file will give you a Word-readable format including a snapshot of both parts of the program. The report of your work should include (at least!) the following;

18

• • • • • • • • • •

your name, student number and kit number brief notes and results as requested in sections 1 & 2 calculations as requested in sections 3.1 and 3.2 details of calibration of the position of the moving arm, and measurement of Offset figures and calculations for finding 2µ and ω0 the optimal ranges for kP, kI and/or kD in each case; plots of angle vs. time may help explain your choices target angle versus measured angle for proportional control a figure showing the system behaviour with optimal PID control with and/or without a perturbing mass the front panel and block diagram for the final PID routine answers to the following questions; 1. The simple theory presented here predicts that derivative control should be able to get critical damping. Did you get this? If not, why not? 2. Integral control is just the integral of proportional control. Why then does integral control work? Why do we always include proportional control?

plus any other information you feel is relevant.

5 SUGGESTIONS FOR FURTHER WORK
If you finish the core-work before the end of the final session you should investigate an area of the lab in more depth. There may be something you have noticed which you are interested in, or you may like to follow one of these suggestions. Feel free to discuss the work with a demonstrator. Attempt only one or two things!
•

•

•

Programming: • the program can be altered so that it will start/stop recording or perform some other action when the arm is fully settled. Consider carefully your definition of settled! • in a real application it may be useful to have different set-points for different times (e.g. required temperature at different times of day). Set the system to sit at a series of angles for set times (hint – read more about different structures in LabVIEW) • LabVIEW has a built in Fourier transform VI (in spectral analysis) and other analysis routines. Would this help in finding the natural damping (Next Step 2)? Experiment: 2 • what is the total mass M? (hint – what is ω 0 , and will a different mass affect your measurements in Next Step 2?) When damping is added, does changing the mass of the experiment affect the optimal value of kD? Theory: • it may be possible to relate the k-values you’ve found to the theory, considering the angle, timing details and motor output. • is there a better way to set your k-values than by trial and error? • PID control used to be done using analogue electronics or pneumatics before microprocessors were common. How might this be done?

REFERENCES There are several LabVIEW 8 textbooks in the cupboard in the lab. The library has several older books based on earlier versions. Other useful resources may include books or papers on control mechanisms; several are given here.

19

• • • • • •

LabVIEW 8 Student Edition, Robert Bishop, Prentice Hall, London (2006) LabVIEW for Everyone, Jeffrey Travis and Jim Kring, Prentice Hall, London, 3rd edition (2006) University Physics, Young & Freedman, Addison Wesley (any year) Introduction to Control System Technology, R.N. Bateson, Prentice Hall, London (1996) Control Engineering, C.C. Bissel, Chapman & Hall, London (1994) Control Engineering, W. Bolton, Longman (1998)

It can be useful to get information from websites, but be aware that not everything is official or even right! http://www.ni.com is the official National Instruments site http://www.controleng.com – a search for PID brings up a lot of useful articles. APPENDIX A: NI PCI-6221 DAQ data acquisition board Data acquisition (DAQ) techniques sample information about the real world and put it into a form that can be understood and manipulated by a computer. A typical system has an experiment connected through cables to a DAQ board (in this case via an interface box) which then passes data to and from the computer. The DAQ hardware digitizes signals, performs digital to analogue conversions to generate analogue output signals, and measures and controls digital I/O signals. Each computer in this lab has an interface board fitted in a PCI slot. The data acquisition board, with connections through the Servo Controller Interface, is a National Instruments M Series DAQ, PCI-6221 (68 pin version). It has 16 analogue inputs, 24 digital I/O and 2 analogue outputs, and can be controlled via the NI-DAQmx software. The manual is available on the National Instruments website; pin inputs and outputs are shown in the figure below. This board provides the connections (ports) with the real world. APPENDIX B: LabVIEW Data Types

20


								
To top