AME 30315 Pendulum Project: Difference between revisions
| Line 136: | Line 136: | ||
</pre> | </pre> | ||
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! | 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! | ||
The program StudentCode.s19 basically commands a square-wave torque to the pendulum with a frequency of approximately two seconds. So it should move one way and oscillate some, then after a second or so, move the other way and oscillate some, etc. | |||
Revision as of 18:40, 18 March 2012
Introduction
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. 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.
The System
The pendulum and controller are shown in the figure below. It is comprised of
- the pendulum,
- a d.c. motor with optical encoder,
- an H-bridge current controller,
- a Freescale 68hc11 8-bit microcontroller with peripherals, and
- a usb-port logic analyzer.
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.
- The smaller green printed circuit board, on the lower left, with the serial port on it, has the 68hc11 microprocessor on it. This will be called the "hc11 board" or "microcontroller board." The actual hc11 is the square chip on the vertical board sticking out from the base. The chip on the base board is the serial interface. The round silver component next to the hc11 is the clock. The larger rectangular chip on the other side is the EEPROM.
- The larger printed circuit board, on the upper left, provides the interface between the microcontroller and the H-bridge and, the encoder, the index encoder and some limit sensors. It will be called the "interface board." It also has a chip on it that is a decoder for the encoder. Whenever the encoder encounters an edge, the decoder generates a pulse that produces an interrupt on the hc11. The hc11 then can query the decoder to determine which direction the pendulum is moving.
- The H-Bridge is in the center of the picture and has the heat sinks on it. It takes the PWM signal from the hc11 and produces a current proportional to the duty cycle.
- The motor is on the right in the picture.
- On the right edge of the interface board, you can see three colored wires going to the upper edge of the picture. These wires are connected to a USBee SX logical analyzer, which can be connected to the USB port on a PC. If you run the USBee tools on the PC, you can see when interrupts are occurring, etc.
You will need to use one of the desktop computers in 212 or 213 Stinson-Remick. Those computers should have the following software installed:
- the 68hc11 port of the gcc compiler,
- putty, to be able to communicate with the microcontroller over the serial port,
- notepadd++, for editing your programs, and
- the logic analyzer software.
There are 10 pendulum platforms and five USB logic analyzers on the window sill in 212. Return them when you are finished. I have been told there are many more logic analyzers available in S-R.
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.
Interrupts
There are two interrupts in the program. One is an output compare interrupt and the other is a pulse accumulator interrupt.
- The code that is provided has the output compare interrupt running at 880 Hz and is for the pulse width modulation control of the motor. It is output compare 3, OC3. If the motor power is on, you can hear this one in operation because you can hear an 880 Hz sound from the H-bridge.
- The pulse accumulator interrupt happens whenever the optical encoder senses an edge. There is a decoder chip on the board that the hc11 can query to determine which direction the pendulum is moving. On the interface board, there is a green LED that will be on when the pendulum is moving to the right and off when it is moving to the left. It has RIGHT printed next to it. The red LED next to it that has LEFT printed next to it does not come on when it is moving to the left. It is on when the pendulum is at either limit position.
- The encoder also has an index channel, which senses when the encoder passes through the nominal zero position. However, because it was assembled by hand and also because the motor shaft may slip in the collar on the pendulum, the index will never exactly be at 0 degrees. Part of the calibration process will be to determine the offset between the index and the zero position. The index channel on the encoder does not generate an interrupt. However, it does make the white/blue LED flash that is next to the grey ribbon cable on the interface board.
Important Parameters
- There are approximately 5 or 6 interrupts per angular degree of motion of the pendulum. More specifically, there is 0.18 degree per interrupt generated by the encoder.
- There is an index on the encoder that happens only once per revolution. It should be aligned so that the pendulum is near vertical when the motor goes through this position. There is a whitish/blue LED near the grey ribbon cable that flashes when it goes through this position. The calibration steps outlined below provide for an offset if this is not aligned with the vertical position. However, it must be the case that this position is within the range of motion of the pendulum. If the shaft slips too much and it's outside the range of motion, then the code will not work.
- All the I/O for the pendulum control is through PORT A on the hc11.
- pin 0 is connected to the limit sensors for the hard stops.
- pin 1 is the direction.
- pin 2 is connected to the encoder index (close to the top).
- pin 3 is not used.
- pin 4 has been used for various debugging things. It is ok to leave it unused.
- pin 5 is the PWM.
- pin 6 is connected to the red LED on the hc11 board. The code is written so it flashes at 20Hz.
- pin 7 is connected to the decoder which pulses whenever the decoder receives an edge from the encoder.
Switches
- On the smaller, hc11 board, there are two switches and one button. The two switches are facing the back of the system (if the actual pendulum is on the "front") on the vertical board. They must be together to download a program and apart to run. The button near the switches is the reset button. Be careful toggling the switches because the vertical hc11 board can be pulled out of its socket fairly easily. If it comes out, just put it back in. Be sure that the switches are facing the back because it may be possible to put it in backwards.
- The four white buttons on the interface board are wired to PORT B on the processor. You do not have to use these, but they can be accessed by your program. You may want to use them, for example, to change a gain value without having to recompile and download your program. It is acceptable to leave these as unused for this project. The rightmost button is wired to the reset button on the hc11 board and does exactly the same thing.
Getting Started
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 two 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.
- Save the S-record in the link above to your computer. Also save the files msload9.bin and pms91.bat in the same directory as the S-record.
- Ensure that the pendulum is hanging down.
- Plug in the power plug for the microcontroller board, but NOT the motor (the smaller plug is for the boards and the larger is for the H-bridge and motor). Some LEDs light up on the board when you plug in the right 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.
- Set the two switches on the hc11 board to be "in" that is the top one should be down and the bottom one should be up. In other words, they should be pushed together. Push the reset button. This puts the board in "bootstrap mode" to download the program to the EEPROM.
- Type
pms91 StudentCodeThe 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!
The program StudentCode.s19 basically commands a square-wave torque to the pendulum with a frequency of approximately two seconds. So it should move one way and oscillate some, then after a second or so, move the other way and oscillate some, etc.
m6811-elf-gcc -g -Os -N -mshort -Wl,-m,m68hc11elfb -fomit-frame-pointer -msoft-reg-count=0 -o pendulum.elf PendulumControl.c
m6811-elf-objcopy --only-section=.text --only-section=.rodata --only-section=.vectors --only-section=.data --output-target=srec pendulum.elf pendulum.s19
pms91 pendulum
Notes on the C Program
Ultimately your job is to edit the program to implement a controller that you design.
Files:
- PendulumControl.c: this is the main C program.
- mc.h: header file that defines what the pins on PORT A are used for. This can't be changed because they are wired in hardware for that functionality.
- hc11.h: header file that defines the register block, where PORT A is, etc.
- vectors.c: C file that defines the interrupt vector. T
Important variables:
- pos: this is encoder counts
- pos_deg: is pos*scale which comes out to be degrees times 100
- u: is the torque with a range of -400 to +400. Negative values are a CCW torque.
- dir: specifies the direction, 0 is CW, 1 is CCW. This should not have to be changed because the provided code interprets the sign of u and sets the direction accordingly.
Functions you will probably use:
Functions you probably won't use:
