AME 30315 Pendulum Project
- Hopefully you already know your group assignment.
- The Pendulum platforms are stored in room 214 Stinson-Remick. You all should have swipe-card access to that room. It is not very big, so finding the pendulums is easy. Finding your assigned pendulum for the first time may take a bit of searching, but after that, as long as you put it back in the same spot, it will be easy to find. Keep using the same one. If it is damaged or broken, let me know ASAP. We have some backups.
- There are four TAs for the project, and they have agreed that one of them will be available in 212-213 S-R on Tuesday and Wednesday evenings from 8:00pm to midnight. They were in this class last year and spent the first part of this semester working out the bugs in the project, so they will be an excellent resource and I encourage you to consult with them.
- You may reserve the pendulum for up to two hours on the google calendar created for it and shared with you.
- Your group may only have one pending reservation at any given time. In other words, your group can only make one reservation and only after that reservation time is over may you make another reservation.
- The project is due at 5:00pm on the last day of classes. There will be no extensions. If all the pendulum platforms are reserved the evening before it is due, that will not be accepted as a valid excuse for not completing the project. You will HAVE to work ahead of time and in an organized manner because it will basically be impossible to start this a couple days before, even with unlimited access to one of the platforms, and finish.
- It is expected that greater than 80% of the time spent on this project will be spent working together with all the group members present in the same room at the same time on the same thing. If this is not happening, you must let me know. If you have a delinquent group member, you must let me know. Also you must let me know these things when it is happening, not when the project is due.
In this project you will design a feedback controller to stabilize an inverted pendulum. The basic steps are:
- system identification,
- controller design,
- implementation and verification.
The first and third steps require that you be able to program a microcontroller. All three steps, but particularly the second step, requires that you understand the course material.
This web page provides a description of the system and instructions to download a program. Read everything before the "Getting Started" section, but do not worry if you do not understand it all. You will probably have to re-read it several times.
One student in each group, probably the ME major, should read these sections. None of it is absolutely necessary to get the project to work. However, it describes the system, and that will make it easier to complete the project.
|The pendulum and controller are shown in the picture below. It is comprised of
The microcontroller is very inexpensive and has limited capability, as is realistic in industry where component costs are of significant importance.
The following elements are illustrated in the picture.
You will need to use one of the desktop computers in 212 or 213 Stinson-Remick. Those computers should have the following software installed:
There are 30 pendulum platforms and each group has been assigned to a specific one. They are stored in 214 S-R and must be returned to that room when you are finished.
All the software can be found in the start menu, except the compiler. To run that you need to open a command prompt (under Accessories) and type "m6811-elf-gcc". If it responds with "no input files" then it's installed. If it responds with "no recognized as an internal or external command" then it's not installed or is not in your path.
|There are two interrupts in the program. One is an output compare interrupt and the other is a pulse accumulator interrupt.
This section outlines the steps to write, compile, download and execute a program on the microcontroller. Until you have a controller designed that is supposed to stabilize the pendulum, make sure the pendulum is pointing down.
The file format that can be downloaded to the hc11 is called an "S-record" and has a filename that has a ".s19" suffex. Here is an example of an S-record that commands a square-wave torque to the pendulum with a period of approximately ten seconds. It is a text file, so it is something you can look at and open in an editor, but it is not really decipherable. Save this program on your computer.
The steps required to download the run this example square-wave program are as follows.
- The rest of these instructions assume you made a folder on the desktop of one of the computers in 212 or 213 Stinson-Remick.
- Save the S-record in the link above to that folder on the desktop. Also save the files msload9.bin and pms91.bat in the same directory as the S-record.
- You need to use a command prompt and be in the correct directory.
- Open a command prompt (type "cmd" in the search in the windows start menu.
- Type "N:" to switch to the network drive.
- Type "cd Private"
- Type "cd "Desktop"
- Type "cd #######" to get into the folder you made.
- It is best to usually have the pendulum on the floor near the computer. Ensure that the pendulum is hanging down.
- Plug in the power plug for the microcontroller board, but NOT the motor. Some LEDs light up on the board when you plug in the right one. On most of the pendulums, the smaller plug is the microcontroller one.
- Connect the serial cable to the serial port on the computer and make sure it's connected to the serial port on the hc11 board. Be careful with both ends of the serial cable. The serial port on is on the back of the computer, so you may have to slide it out of the enclosure that holds it a bit. Also, the attachment point for the serial cable on the pendulum is delicate. If you accidentally kick the cable or power cords, something will likely break.
- Set the two switches on the hc11 board to be "together" that is the top one should be down and the bottom one should be up. Push the reset button. This puts the board in "bootstrap mode" to download the program to the EEPROM.
- The pms91.bat file is a batch file that copies stuff to the serial port. First it sends msload9.bin to the hc11, and with this program, the hc11 knows to copy the other stuff sent to it into the EEPROM.
- The program needs access to the COM port. If another program is using it, it will be blocked. The first time you do this, it probably won't be a problem, but you definitely will be debugging stuff via the serial port later. If you leave putty running, it will block it, in which case you will get a warning saying basically that.
- If it's working, you will see a message like:
TECHNOLOGICAL ARTS S-record File Downloader for 9MHz MicroStamp11 ============================================== For expanded-mode 68HC11Dx systems with external EEPROM/RAM . NOTE: For proper operation, do not apply or remove board power while it is Write-Enabled . USAGE: to download a file called myfile.s19 via COM1, type pms91 myfile . 1) Make sure Docking Module is connected to COM1 2) Make sure MicroStamp11 is properly inserted in Docking Module 3) Apply power to Docking Module (+5VDC to +12VDC is acceptable) 4) Place both switches in LOAD position 5) Press target board RESET button. . Press C to abort, or... Press any key to continue . . . Installing bootloader in RAM now... (to abort, disconnect serial cable) Status for device COM1: ----------------------- Baud: 9600 Parity: None Data Bits: 8 Stop Bits: 1 Timeout: OFF XON/XOFF: OFF CTS handshaking: OFF DSR handshaking: OFF DSR sensitivity: OFF DTR circuit: ON RTS circuit: ON 1 file(s) copied. Programming EEPROM now... (takes approximately 20 sec/Kbyte) Status for device COM1: ----------------------- Baud: 1200 Parity: None Data Bits: 8 Stop Bits: 1 Timeout: OFF XON/XOFF: OFF CTS handshaking: OFF DSR handshaking: OFF DSR sensitivity: OFF DTR circuit: ON RTS circuit: ON 1 file(s) copied. EEPROM programming complete. WRITE PROTECT MicroStamp11. Place MicroStamp11 in RUN mode. Press RESET button. Your program is now running...
Be patient. This can take a minute or two. It's not done until you see the "programming complete" line. Move the two switches into the opposite positions (top one up and bottom one down), or in other words, push them apart. When you push the reset button, your program will run!
Things may be a little underwhelming at this point because it may seem like nothing is happening. To make things happen you have to establish communication with the hc11. To do this, start putty. In the opening window, click on "serial" for the connection type, and then click ok. If you click the reset button again, you should see a welcome message and instructions to move the pendulum through the zero position. If you move it all the way left and right, you should see an indication that it found the zero position. If you plug in the motor, you should hear the PWM and also see the pendulum alternate between a left and right position with a period of a couple seconds.
The program StudentCode.s19 basically commands a square-wave torque to the pendulum with a period of several two seconds. So it should move one way and oscillate some, then after several second or so, move the other way and oscillate some, etc.
When you are done, CLOSE putty! If you forget, it will block you the next time you try do download an S-record.
Compiling and Downloading a Program
Ultimately your job is to edit the program to implement a controller that you design. In this section you will compile the source code to make an S-record that does ths same thing as above. You need to download and save the following files in the same directory
What each of these files do will be described later. In this section the point is to just successfully compile them. You must have them all in the same director or it won't work.
To compile the program and create the S-record type
m6811-elf-gcc -Wall -N -mshort -Wl,-m,m68hc11elfb -msoft-reg-count=0 -o filename.elf filename.c
You need to replace "filename.c" with whatever program you are compiling. It will create a file called "filename.elf". You should probably replace "filename.elf" with the same prefix as your C program, i.e., StudentCode.elf. An elf file is a file in Executable and Linkable Format.
To translate the elf into an S-record, type
m6811-elf-objcopy --output-target=srec filename.elf filename.s19
Again, replace "filename" with whatever. The file "filename.s19" is the S-record that you can download to the hc11 as outlined above. You may want to use a different name for the S-record so you can be sure that when you use it, it doesn't happen to be the same one that you downloaded before.
Do these steps for StudentCode.c to make sure you can compile, make the S-record and download it to the hc11.
Notes on Editing the Program to Control the Pendulum
To stabilize the pendulum in the upright position, you really only need to change two things in the code:
- "OFFSET" at around line 24
- "ulong" at around line 105.
If you want to stabilize about non-zero positions, then modify "pos_desired" at around line 70.
In the computation for ulong, wherever you put a number, you must end it with an "L" (lower-case works too). That tells the compiler to treat that literal as a long integer (4 bytes), so it multiplies the correct way with the errors, etc. If you don't have the "L" it might not work at all! This may seem pedantic, but microcontroller processors often have compilers that make almost no assumptions about how you want to combine things, which makes sense.
|More Programming Details|
Do NOT edit either vectors.c or memory.x. The former defines the interrupt vector so that when an interrupt occurs, the processor knows where to go to handle it. The second is the memory map for the processor. You can certainly look at each one. I don't think it would hurt or corrupt you.
StudentCode.c set set to print out the time, angle and control input every 1/20 of a second using the "print_data()" function at the end of the if() statement that happens at that rate. If you want to modify what's printed, that's cool, and read the following.
The file serial.c provides functions to communicate stuff through the serial port. In particular, for your purposes, it provides ways to print out information that may be useful for you to debug your program. Note: serial communication is very slow. If you try to print too much stuff, it will slow down your program and affect its performance. As will be described later, the main control loop operates at about 20Hz. The code seems to be able to handle printing up to the order of 30 characters in that loop. If you try much more, it may take longer than 0.05 seconds.
You can figure out all the wonders of serial.c by reading the comments in it. The two main functions you will use are:
If you want to print positive or negative numbers, you have to go through the labor of checking whether it is positive or negative and printing the "-" sign manually!
The file StudentCode.c has most of the functionality you need to control the pendulum. You should be able to complete the project by only modifying the main() function in StudentCode.c. However, in order to be able to do that, you may have to figure out how many of the other functions in the file work.
There is not a function that gives you the pendulum position. The variable pos does this and is incremented by the pulse accumulator interrupt. The value of pos at any time is the position of the pendulum in encoder ticks. If you have a variable that is equal to pos*18, it will be the position of the pendulum in degrees times 100 relative to the index position.
Be very careful writing your code. The compiler option '-Wall' tells the compiler to give all possible warnings. In the development of this project it became VERY obvious that strict C code must be written. If it gives you a warning, fix it. Just because it compiles and even runs, doesn't mean it's doing what you expect. Things like 'a=b' where a is a long and b is an int generates a warning, as it should, and fixing all of this in your code is necessary.
I would suggest breaking it down in the following steps before you try to implement the real controller.
- First modify the StudentCode.c program so that it pauses for a longer or shorter time between switching the torque. Also perhaps increase or decrease the torque.
- Write your own calibration code that sets an offset variable that makes the zero angle not equal to the index, but rather equal to the real zero, which corresponds to when the pendulum is hanging straight down. You can do this by writing 'pos to the serial port and just checking the value when it hangs straight down. The negative of that will be the offset. Each platform will have a different offset and over time it may even change for individual platforms.
- Implement proportional control. This will not give satisfactory performance, but is a good first step.
To determine the transfer function describing the response of the system, do the following.
- Enable logging in putty so that everything that gets printed to the screen gets saved.
- With the pendulum hanging down, command a step input for the torque.
- In the while(1) loop in main() there is an if() statement that checks if it's time for the 20Hz control loop to execute. Inside that if() statement, get the position and print it to the serial port. You may also want to create a variable that is 100*time, that increments by 5 each time that if() is true. If you print that right before the position, what gets printed on the putty terminal should be the time and position every time the control loop executes.
- After it settles down, quit putty. Edit the log file to cut out any extra junk and you should have two columns of numbers, where one is time and the other is position.
- Plot it in matlab and, hopefully, do that problem correctly to find the natural frequency, damping ratio and scale factor in the numerator.
- Good engineering practice, obviously, requires you to verify stuff you determine by repeating the experiment several times and checking that what you computed for the first one gives a good match for the others. This should be true for different directions as well as different magnitudes of inputs.
- Your report should contain a graph of the step response you used to compute the system parameters as well as enough comparisons with other responses to give confidence that your numbers are good.
Implementing a Controller
In our class, we will only consider continuous-time systems. However, the microcontroller only works in discrete time steps. Hence, you must convert your controller from continuous-time to discrete-time. In the frequency domain, continuous-time systems are represented by the Laplace transform, with s as the independent variable. In the frequency domain, multiplication by s represents differentiation, i.e.,
In discrete-time, the analogous transformation to the Laplace transform is called the z-Transform, with, not surprisingly, the independent variable being z instead of s. In discrete time instead of a function depending continuously on time, a function is only defined for a sequence of times, i.e., instead of x(t) we have x(T), x(2T), x(3T), etc., where T is some time step. In discrete time, under the z-transform, the analog to multiplication by s being differentiation is that multiplication by z is a time shift. Specifically, z X(nT) = X((n+1)T), it shifts the function ahead by one time step. Dividing by z shifts it back. Multiplication by z twice shifts it ahead by two time steps, etc.
The approach we will take in this class, which is relatively common, is to do all of our work in the continuous frequency domain and at the very end convert our continuous controller to a discrete-time one. For very simple operations you would naturally do this, for example, replacing the derivative of a function, f'(t), by [f((n+1)t)-f(nT)]/T. However, for more complicated things, this could get confusing. It turns out that the usual trapezoidal rule for integration gives a nice relationship between s and z given by
This is called Tustin's method. Now, say you have some controller that you designed given by, for example
where F(s) is a control force and E(s) is the error signal. What you do is substitute the equation relating s and z into C(s) to get it in terms of z, e.g., for this problem (if I did the algebra correctly...)
for the right hand side. Because this is the z-transform for the error to the force, we have
Because multiplication by z is a shift forward in time, in discrete-time this corresponds to
which is our computation for f(t) at the new time step. It depends on both f and e at the previous time step as well as e at the current time step (t=(n+1)T). For a lead-lag compensator, the numerator and denominator will both be second-order in z, so the computation for the new control input will depend on the current error as well as the error and control at the previous two time steps.
You should definitely do the computation by hand. You should know too that Matlab has a function called c2d() which stands for "continuous-to-discrete" which does this conversion computation. You need to give it the time step as well as the method. For this example with a time step of 1/20 and using Tustin's method,
>> c2d(tf([1 2],[1 15]),1/20,'tustin') Transfer function: 0.7636 z - 0.6909 ----------------- z - 0.4545 Sampling time: 0.05 >>
Assuming this is correct, let's proceed to implement it on the hc11. That processor is CHEAP ($3.50), so it's not going to be able to do too much math. In fact, floating point arithmetic requires an additional library is is way too slow. It can, however, very quickly do fixed point (integer) arithmetic. Assuming the matlab controller for the pendulum (it won't work, by the way, this is just an example...) with u representing the pwm level and err the error, we would be tempted to write
- u = 0.4545*u_old + 0.7636*err - 0.6909*err_old;
where u_old and err_old were copied from u and err at the bottom of the 20 Hz loop. But it can't do the 0.4545*u_old computation. What it CAN do is
- u = 4545*u_old + 7636*err - 6909*err_old;
- u = u/10000;
This will allow it to retain necessary precision but still do the computations in fixed-point.
- For the hc11 an integer is 16 bits, which has a maximum value approximately 64,000 (half that if it is signed). When I implemented the controller, the computation for u in the first line (before it was divided by 10000) exceeded 32k. Hence, the variable type for u in the code had to be a long integer. This made u 32 bits, which was large enough for the computations (but slower).
- You must be very careful with the variable types in the code. Embedded systems are very pick about them and passing an int to a function that wants an unsigned long, for example, may compile but exhibit flaky behavior because the function isn't getting the intedned value from the variable.
- Also, be careful printing stuff to the serial port. It can only put out so much stuff. One mistake I made was to compute some stuff, print it, use it in another computation, print that, and then set the torque to the computed value. It turns out that even though the loop was still running at 20Hz, there was enough delay in the printing that it didn't work too well. I would suggest that if you print stuff, do so judiciously and also to do all the computations and torque_set commands at the top, and then print out debugging things. If the printing slows it down to less than 20Hz, it won't complain or otherwise indicate anything, but effectively the time step will be longer than what you used in your computations, and things won't work!
- Be very careful with units. You will definitely save time if you are careful with all the units whenever you write something. Are you using encoder counts for the position, or degrees, or degreesx100? I would have saved a few hours if I were more disciplined about this.
- I think a positive PWM signal creates a negative angle. Watch the signs for this! This too cost me a few hours.
Project and Report Content
You must write this report as if I were your boss and people trusted their lives to the product we were developing (medical device, airplane, etc.). What I am looking for in this project are the following:
- A controller design that works. It should stabilize the pendulum for any desired angle between +/- 30 degrees of vertical.
- The report should contain representative step responses, for example having it stabilized at zero until 10 seconds, then stabilizing it at 20 degrees. If it seems to work perfectly for all angles in that range, then say so and give representative responses. If it has any sort of steady-state error, then you should report it. I do NOT want voluminous data unless it's necessary. For example, if the error at zero is zero, at +/- 10 degrees is 1 degree, at +/- 20 degrees is 3 degrees and at +/- 30 degrees is 5 degrees, then just say that and report what the trend seems to be. If it's not so simple, then a table giving error as a function of, say, 5 degree increments would be good. A good report will effectively communicate the nature of the error in a concise way. (Putting data in an appendix is ok, but your boss won't read that; a colleague following-up on your work might find it useful). Having one or two plots showing it works for zero and one other angle with nothing more is not an acceptably professional report. It is not acceptable here, and not acceptable in the real world. Imagine showing your boss at Boeing that the aileron actually deflects 10 degrees when that's what you want it to do. He or she will likely actually care about some other angles too. In other words, you must thoroughly evaluate the performance of your design.
- You must also justify any decisions you make. For example, in the system identification section above, you determined the transfer function for the pendulum when it's hanging down. You must show in your report how you determined the transfer function for the inverted pendulum from that. If I'm your boss and people's lives depend on it, I'm not going to accept something like "when the pendulum is hanging down, we determined zeta and omega_n to be xxxxxxx. Thus when it's inverted the transfer function is yyyyyyy." There are multiple steps to this. For the downward-hanging pendulum, just showing a plot of a step response and saying the damping ratio and natural frequency are such-and-such is not sufficient. You must compare the step theoretical step response with the parameters you determined with the actual response. You must also do it for step inputs of different magnitudes and signs than the one you used for your computations. You should show such comparisons on the same plot (having two plots, with different units but arguably qualitatively similar responses is not sufficient). If the units you use are the PWM signal level and encoder counts (what I used), then on a plot of a step response of the real system, I should see the theoretical response with the same units compared with it. For going from downward to upward, you need to do some analysis (mechanics). This is important, because it will give the transfer function that you will use to design your controller. If I'm responsible for people's lives, I want to know you did each step correctly.
- After you have a good model, you must implement a lead-lag controller that gives satisfactory transient and steady-state performance. I'll leave it to you to decide what good performance is, but you must predict the performance (overshoot, settling time, etc) based on theory and correlate it with the actual system response. E.g., "for the system we designed, the theoretical step response for an input of 10 degrees is illustrated in Figure YYZ, which also shows the actual response of the system." The design of your controller must be supported by analysis, specifically root locus plots and other analyses. You must predict the transient and steady-state performance and compare the actual performance to that. You must include in your report root locus plots for the transfer function and the means by which you used them to design the controller.
- Since you did your analysis in continuous time, you must include in your report the details of the computations to convert it to discrete time.
- Your report must contain a comparison of the predicted response of the system versus how it actually responds. You must show this for stabilizing to the zero position (the real zero position, straight up, with the appropriate offset determined) as well as stabilizing to several other positions, say 20 degrees, -30 degrees, etc. Any significant differences between the predicted and actual responses must be explained.
- Your report must contain a link to a movie showing the system in action.
- Your report must show any warnings that the compiling command gives. In other words, cut and paste from the terminal whatever is printed out when 'm68hc11-elf-gcc' is run. It must contain the '-Wall' option. The only allowable warning is the one that it says about the eeprom memory definition.
- Your report must contain your code. You do not need to include vectors.c, memory.x and any other files you used but that were unmodified from what was supplied.
- Every group must validate their design and determine the best controller/gain combination. However, in your report you must include computations for the discrete controller based on the parameters in the following table. This of the assigned values in the following table as "homework" in that each group must show they can do all the necessary computations for the indicated parameter values. You may use those as a logical starting point, but it's not necessary. You should validate the assigned values in matlab. It is not required to actually implement the controller in the real pendulum with the assigned values.
- In all cases, the designed rise time for the system should be less than 1/2 second.
- Words to keep in mind are "justification," "evaluation" and "validation." I will be looking for you to justify everything you did. I will look for you to look for ways to evaluate your decisions. If you found a damping ratio and natural frequency, are there other tests you can do to check if they were good choices? Does your controller performance match the prediction? If not, how does it seem to deviate? Is the deviation systematic and repeatable? A report that shows it works for one or maybe two points, only ties the variables you use to one step input, etc., is not going to be enough to give me confidence that your design is a good one.
- Having said all that, I don't want a bunch of extra junk in the report. I will have to read 70 of them. A great report will convince me that you did a correct and thorough job. A bunch of extra stuff will waste my time (or your boss' time, which is much worse) and make me think you are trying to hide crappy results with a bunch of vacuous verbiage.
Groups with two students may do any of the following for extra credit. Groups with three members must do one of these. Additional ones are extra credit.
- In addition to the modifications to main() and specifically the while(1) loop therein, modify any other selected parts of the code to significantly improve the performance of the system. Check how fast you can make the PWM work with your baseline code and controller before it gets flaky or how much faster you can make the 20Hz control loop. Then find something in the code that you can modify to make it faster so that it can run at a faster PWM and/or control loop before it starts running into problems.
- Use simulink to model the real system. The plant will be continuous, but the sensing and control will be discrete.
- Use this model to predict the steady-state error when there is no lag compensation.
- Use this model to predict the effect of faster or slower control loops. What seems to be the slowest it can work and still be effective?
- Use this model to predict the effect of having less resolution on the PWM, i.e., instead of a resolution from -400 to +440, check the effect of -40 to +40, etc.
- Use simulink or other matlab tools to automatically generate the C code for the controller. Automatic code generation is becoming commonplace. Nothing is more dangerous than blindly trusting what comes out of a computer, so you have to know how the controller should work. Now that you have done that, check what simulink gives to you for code. Disclaimer: I have not done this myself and am not sure if the hc11 is a recognized target, how to do it, etc.
- You may propose an additional component to the project if you wish. Be sure to get the instructor's approval prior to doing it if you want to receive credit for it.