AME 30315 Pendulum Project: Difference between revisions
| Line 184: | Line 184: | ||
* The function init_ports() enables all the necessary pins that are used on PORTA of the processor that communicate with the PWM (pin 5), encoder (pin 7) and direction (pin 1). | * The function init_ports() enables all the necessary pins that are used on PORTA of the processor that communicate with the PWM (pin 5), encoder (pin 7) and direction (pin 1). | ||
* The function set_zero() is called near the beginning of StudentCode.c. When the program starts running, the controller has no idea what position the pendulum is in. This function instructs the user to move the pendulum through the zero position by hand (just move it from one end to the other somewhat slowly). When it detects the index, it sets the value of the position to zero. | * The function set_zero() is called near the beginning of StudentCode.c. When the program starts running, the controller has no idea what position the pendulum is in. This function instructs the user to move the pendulum through the zero position by hand (just move it from one end to the other somewhat slowly). When it detects the index, it sets the value of the position to zero. | ||
* welcome() should be obvious. | |||
* __premain(). DO NOT EDIT THIS. | |||
* | |||
* | |||
Revision as of 19:23, 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.
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 -Os -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
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.
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:
- outstring() which can print a sring to to the serial port and
- out_unsigned_dec() which can print a number.
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 main(). 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.
- It has an 880Hz PWM written already. That is the OC3 interrupt and the interrupt handler is the first function after main().
- The second function after main is the interrupt handler for the pulse accumulator (the encoder).
- The next function is the default interrupt handler, which will catch if any other interrupts happen (which should not happen).
- The function init_interrupts() configures the hc11 so that only the interrupts we need are enabled.
- As you might guess, the function set_torque() is a useful one. It takes one argument, a long integer. This must be a long integer in order to be able to do the calculations with sufficient precision. However, ultimately the largest magnitude you ever want to send to set_torque() is 400. In other words, do not send anything greater than 400 or less than -400 or else the PWM (at least as we've supplied it) will not behave as you expect. I think that if you send it, for example, 600, it will NOT just max out at 400, but actually overflow and do unexpected things. Once you start programming torques, you should probably always have a check before set_torque() that make sure the computed value is between -400 and 400, and if it's out of that range, just set it to -400 or +400, whichever is appropriate.
- The function check_encoder_direction() checks if the pendulum is moving clockwise or counter-clockwise.
- check_encoder_top() checks the pin on PORTA that the index channel is connected to and returns a 1 if the pendulum is at the index position. This should be somewhat close to the top (within 20 degrees seems typical).
- pause() pauses for a while. It takes an unsigned int, so the maximum it can delay is however long it takes to count from 0 to 65535. Counting up to that seems to take about a second.
- The function init_ports() enables all the necessary pins that are used on PORTA of the processor that communicate with the PWM (pin 5), encoder (pin 7) and direction (pin 1).
- The function set_zero() is called near the beginning of StudentCode.c. When the program starts running, the controller has no idea what position the pendulum is in. This function instructs the user to move the pendulum through the zero position by hand (just move it from one end to the other somewhat slowly). When it detects the index, it sets the value of the position to zero.
- welcome() should be obvious.
- __premain(). DO NOT EDIT THIS.
