Professional Documents
Culture Documents
Christopher Armenio
Table of Contents
1. Introduction................................................................................................................................. 3 1. Goals ................................................................................................................................... 3 1. Motor Control Capabilities ................................................................................................. 3 2. External Connections/Ports................................................................................................. 3 3. Base Sensor Capabilities..................................................................................................... 3 4. Optional Sensor Capabilities............................................................................................... 3 2. Overall Architecture............................................................................................................ 4 2. Component Selection .................................................................................................................. 5 1. Processors: ATMega128..................................................................................................... 5 2. Ethernet Interface: ENC28J60 ............................................................................................ 6 3. Motor Controllers: LMD18200........................................................................................... 6 3. Motor Controller Design............................................................................................................. 8 1. DC Motor Introduction ........................................................................................................... 8 2. Speed Control.......................................................................................................................... 9 1. Voltage/Current Limiting.................................................................................................... 9 2. Pulse Width Modulation ..................................................................................................... 9 3. Mechanics of the LMD18200 ............................................................................................... 10 1. H-Bridge Operation .......................................................................................................... 10 2. Current Sensing................................................................................................................. 11 4. Designing the control circuitry ............................................................................................. 11 1. LMD18200 current sensing circuit ................................................................................... 11 2. Completing the design ...................................................................................................... 12 5. Connecting to the ATMega128............................................................................................. 14 1. PWM Lines ....................................................................................................................... 14 2. Logic Lines ....................................................................................................................... 16 3. Current Sense Inputs ......................................................................................................... 16 4. Quadrature Encoders................................................................................................................. 17 1. Quadrature Encoder Introduction ......................................................................................... 17 2. Connecting to the ATMega128............................................................................................. 18 1. Physical Connection.......................................................................................................... 18 2. Reading Quadrature Encoding.......................................................................................... 18 3. Input Capture .................................................................................................................... 19 4. Processor Connection........................................................................................................ 19 5. PS2 Mouse Connections ........................................................................................................... 20 1. PS2 Protocol.......................................................................................................................... 20 2. Connecting to the ATMega128............................................................................................. 21 6. Ethernet Interface...................................................................................................................... 22 1. Serial Peripheral Interface (SPI) ........................................................................................... 22 2. ENC28J60 Mechanics........................................................................................................... 22 1. Level Shifting Circuitry .................................................................................................... 23 2. Magnetics.......................................................................................................................... 23 3. Connecting to the ATMega128............................................................................................. 23 7. Conclusion ................................................................................................................................ 25
1. Introduction
The purpose of this report is to document the design process used to create an Ethernet-based robotic motion controller. This core of this project will be two Atmel ATMega128 microcontrollers, linked together using dual-port RAM. One microcontroller will strictly handle Ethernet communication, while a second microcontroller will actually interface with the motor control circuitry and sensors.
1. Goals
1. Motor Control Capabilities
2 brushed DC motors, 12-24V supply range, up to 3A continuous current Connections for incremental optical encoders (otherwise known as quadrature encoders) Current sensing on both channels
2. External Connections/Ports
I2C Interface SPI Port Analog to Digital Converters Externally available UART
2. Overall Architecture
Wireless Access
SPI
UART
A/D
PS2
PS2
Optical Mouse
(Odometry)
Optical Mouse
(Odometry)
X/Y-Axis Accelerometer
Tilt/Roll Inclinometer
Figure 1.2.1 Overall Architecture The above diagram illustrates the overall architecture of the project. As mentioned before, the core of the project are the two ATMega128 microcontrollers connected via dual-port RAM. The upper microcontroller (known as the Ethernet Processor) will handle the Ethernet communication, likely being connected directly to some sort of wireless access point; this will provide the platform with a wireless access which may be used for both remote control and debugging purposes. The lower microcontroller (known as the Motor Control Processor) will strictly handle the PS2 communication, quadrature decoding, as well as motor control.
2. Component Selection
Before we can begin to design the board, we first need to determine what components will be used and how they will all talk together. The logical first step will be to identify the major subsystems of the proposed board, and then chose an appropriate part; most decisions made in the section are the product of previous experience.
1. Processors: ATMega128
Figure 2.1.1 ATMega128 pinout Okay, I'm not going to lie here, I absolutely love the AVR series of microcontrollers. They are fast (up to 16MHz), easy and free to program (using the AVR version of the GNU C Compiler), and they have a lot of features; not to mention an amazing support community over at AVR Freaks.net. So why chose the ATMega128? To begin with, it has lots of space for code, 128kB of FLASH to be exact; it has lots of internal RAM (4kB), and an external RAM interface, which is critical for if we are going to use the dual-port RAM architecture. Last but not least, it also has a nice selection of Analog to Digital Converters and timers and UARTS.
Figure 2.2.1 ENC28J60 Block Diagram This chip is really slick. It is manufactured by Microchip and is essentially an integrated MAC and PHY; What this means is that all you need to do is talk to it using a pre-defined protocol, it will handle packaging up the ethernet packets, doing the MAC stuff, and converting the MAC stuff to a physical representation for transmission. The other nice part about this chip is that it uses SPI for communication with our processor. This interface is extremely fast, up to half of the ATMega128s clock speed, and easy to use, not to mention, there are already libraries written to access this chip.
These controllers are your standard H-Bridges. Each one has three control lines we need to worry about, Direction (which way to spin the motor), brake (keeps the motor from spinning freely) and PWM. Essentially, to drive this motor, we are going to generate a PWM signal from our microprocessor whose duty cycle corresponds to our desired output power (more on this later). This particular chip was chosen since it has a wide voltage range (12-55V) and can supply up to 3A continuous current.
Figure 3.1.1 Internals of DC Motor (wikipedia) Since the electromagnet has both a positive and negative end as well, the positive of the electromagnet will try to reach the negative of the permanent magnet and vice versa, cause rotation about the shaft of the motor. Once the electromagnet is about to reach equilibrium, its poles are reversed by a mechanical switch caused the commutator. The momentum of the magnet causes it to keep rotating (instead of spinning back the same way it came), and the process is repeated. So in summary, we have two motor lines that we are concerned about, if we apply voltage one way, the shaft will spin in a given direction; reverse the voltage, and the shaft will reverse as well.
2. Speed Control
Alrighty, now that you have some background on DC motors, lets talk about controlling them. Our goal is to control the speed of the motor, thus we need to limit the power flowing to the motor; there are two common ways of doing this.
1. Voltage/Current Limiting
In this manner, we control the voltage present at the terminals of the motor, or control the amount of current that is available to power the motor. Either of theses methods will work, however, there are significant drawbacks:
Figure 3.2.1.1 Representative current vs. Torque graph We know that the torque produced by a motor is a product of the voltage present and the current consumed (see Figure 4.2.1.1). Thus, if we limit the current or voltage to the motor, we are also losing torque, something that we really need at low speeds. (Remember, one of the disadvantages of DC motors are the low torque available at low speeds).
Figure 3.2.2.1 Average power of pulse width modulation If we average the time spent in an on state over a given period of time, we see that we are essentially controlling the power consumed by the motor while still allowing it to have full voltage and current.
1. H-Bridge Operation
Figure 3.3.1.1 H-Bridge operation (wikipedia) The nice thing about H-Bridges are that they allow the motor to be driven in different directions using only a few control lines. When the top left switch and the bottom right switch are connected (as in the left picture) current will flow in one direction through the motor; when the top right switch and the bottom left switch are thrown (as in the right picture) current will flow in the opposite direction. These switches allow us to essentially control the direction of the motor rather easily. By opening and closing these switches really fast, with a particular ratio of on-time to off-time (as discussed earlier), we can also control the power delivered to the motor, and hence it's speed.
10
2. Current Sensing
In addition to the basic on/off/direction control discussed above, the LM18200 will also provide feedback as to the current consumed of each motor. Since we already know the supply voltage, this makes it easy for us to know how much power the motor is consuming, and coupled with wheel speed sensors, we can tell if the motor happens to stall or become disconnected.
Figure 3.4.1.1 Current sensing resistor We need some way to convert a current into a voltage, Ohm's Law gives us a convenient relation, V=I*R. What this equation is telling us: when we run a specified current through a resistor, we can calculate the voltage drop across said resistor. So by attaching a resistor between
11
our current sense pin and ground, we can relate the current through the resistor (determined by the h-bridge) with the voltage difference across the resistor. Now for some math...from the datasheet, we know that the absolute maximum current these HBridges can source is 6A, we also have our 377uA/A conversion factor; so by multiplying the two, we know what the absolute maximum current that will be sourced by our pin will be: 2.62mA or .002262A. Now, on to Ohm's Law, V=I*R, we have our I, now we need our V and R. We could choose a random R value for our resistor, but we have some more limitations to consider...We need some way to read this voltage, in this case, we will be using the Analog to Digital Converters on the ATMega128. These converters have a maximum range of +5V, so naturally our absolute maximum current output from the H-bridge (2.62mA) should correspond to the maximum input of our Analog to Digital Converter (5V). Now we have 5V = 2.62mA * R...if we solve for R we find that our resistor should be approximately 2.2k Ohms (a common resistor value...those guys at National Semiconductor really though things through)!
12
Figure 3.4.2.1 Truth table of LMD12800 Each of these lines will be connected to a digital port on the ATMega128. The ports on the ATMega128 or bi-directional, meaning they can either read or write a digital value to the port; by default, the ports are configured as inputs, which means that these lines are left "floating" (not being pulled to either a '1' or a '0'). This is generally bad, so we added some pull-down resistors (R1-6) to keep them at a known level ('0') when the ports are configured as inputs, but not interfere with their normal operation. Sensing Lines: The current sensing circuitry from the previous step is also included in our final schematic: we simply have a 2.2k resistor (R8) between our current sense output and our ground. To read our voltage, we will connect the top of the resistor to one of the ATMega128's Analog to digital converters. Power Supply (VCC): Since this pin is the source for all of the current flowing to motors, it has to be connected to our VCC_IN power supply (operating at a battery voltage of ~12V) instead of our regulated +5V supply. Output Connections: Out1 and Out2 are the two pins that get connected to our motor. The two capacitors (c1,3 and c2,4 respectively) are known as bootstrap capacitors. These are included as a requirement for the LM18200, their exact use I'm not sure of, but we need them! Our connectors, J1, J2 will be used to connect to the motor. And now that we have all of our lines connected, below is the final schematic:
13
1. PWM Lines
As I mentioned before, these lines will make use of the ATMega128's timer to produce the PWM signals; doing this reduces the software complexity and will greatly reduce processor load. In order to understand how the PWM signals will be generated, it is important to understand the hardware in the ATMega128's timers. General Timer:
14
The ATMega128 has 4 general purpose timers, two 16-bit and two 8-bit. These timers are linked through a pre-scaler into the main clock of the processor. The core of each timer is a counter (either 8 or 16 bit) that gets incremented every time the output of the pre-scaler toggles. The counter will continue to count up until either it reaches a software configurable limit, or it overflows, both of which will cause the counter to reset to 0 and continue counting. Output Compare:
Figure 3.5.1.2 Output compare match and reset PWM generation To generate our output PWM waveform, we use the output compare units of the timer. In this case, we will be using Timer 1 (16-bit). This particular timer has 3 output compare units: A, B, and C. Each unit is controlled individually and is essentially a fancy comparator. In software, you can program each unit with a 16-bit value. The output compare unit will then continuously compare that value with the value of the counting timer; once they are equal, you can either trigger an interrupt, or in our case, automatically set the value of an output pin. So for our case, we will configure the output compare pin to be a logic '1' until it equals the pre-programmed value, at which point the output compare pin will transition to logic '0'. Once the timer overflows, the output compare pin returns to logic '1'.
15
2. Logic Lines
These lines can be connected to any digital output lines as in Figure 4.5.1.3. Note that in this case, output compare 1A (OC1A) will be disabled and hence act like a regular digital line.
16
4. Quadrature Encoders
For our purposes, being able to determine the speed of each wheel as well as its direction of turn is a pretty vital functionality; it will help us keep the robot traveling in straight lines, executing precision turns, and hopefully even having an idea of where it is. Note that we will be connecting TWO quadrature encoders.
Figure 4.1.1 Quadrature encoder breakdown The detector chip sends out a beam of light from one side of the disk to the other and toggles an output line every time the light beam is interrupted or restored.
17
The detector chip actually has two of these light beams, one placed on the left side of the chip, the other placed on the right. When the wheel spins in one direction, the transition on line A will lead the accompanying transition on line B; when the wheel spins in the opposite direction, the transition on line B will lead the transition on line A. For our purposes, we will disregard the index line.
18
3. Input Capture
Input capture is another timer add-on that is very similar to output compare. Instead of toggling an output line once the timer has reached a certain value, the input capture simply records the current timer value at the instant a transition occurs on the input capture line.
Figure 4.2.3.1 Block diagram of input capture We can set this transition to also trigger an interrupt, at which point we can determine the time since the last transition, and determine the direction.
4. Processor Connection
Now that we know the A channels need to be connected to input captures, we will connect Quad1-A to ICP1 (input capture 1) and Quad2-A to ICP3 (input capture 3). The B channels will simply be connected to random digital input lines (these happened to be free).
19
Mouse 1 y
Figure 5.1 Placement of optical mice In this configuration Mouse 1 should experience movement only on the Y-axis which will correspond to the total forward movement of the robot. Mouse 2 will experience the same Y-axis movement, but it will also experience X-axis movement; this X-axis movement is the rotation of the robot about its axis and can easily be converted to an angle of roation.
1. PS2 Protocol
The PS2 protocol is a simple serial protocol that be spoken fairly easily by any microcontroller. For each mouse there is one clock line and one data line. The protocol is unique in that the mouse generates the clock instead of the host, because of this we will need to be aware when the mouse is generating data, most likely by using an external interrupt.
20
Figure 5.2.1 Connections to the ATMega128 You will notice that due to a pin shortage on the Motor Control processor, we had to fudge and place mouse control on the Ethernet processor, not a big deal. To free up the dedicated external interrupt pins I connected the clock lines to the Input capture of timers 1 and 3 respectively. This method will still give me an interrupt on transition (we just ignore the stored time) and leaves the external interrupt pins free for other uses.
21
6. Ethernet Interface
Compared to other Ethernet interface chips, the ENC28J60 is really simple to talk to. It has an SPI interface which is essentially a hardware controlled serial protocol that treats slave devices as simple shift registers.
Figure 6.1.1 SPI communication schema Also note that multiple slaves can be chained together to give us access to multiple interfaces or devices.
2. ENC28J60 Mechanics
Despite the fact that it is easier to deal with than other Ethernet interface chips, the ENC28J60 still has its own hidden challenges. That being said, I will give you the whole schematic now and explain each part.
22
2. Magnetics
Now the ENC28J60 also contains a PHY which translates Ethernet packets into physical representations, however circuitry must be added that allows it to truly talk in an Ethernet world; this is where the magnetics come in. I managed to find a RJ-45 jack that had the magnetics built right in, as well as two status LED, cant get better than that! All that is need are a few resistors (I do what the datasheet says), and we are set.
Figure 6.3.1 ENC28J60 connections to the ATMega128 Since the EN28J60 talks using SPI, the only required connections are the MISO, MOSI, SCK, and chip enable (Ethernet-Cs). The latter line tells the device that the microcontroller is trying to
23
send data to it and usually transitions low before any data is sent. The last line is the Ethernet-In; this allows the ENC28J60 to get the ATMega128s attention should a packet arrive or other such event.
24
7. Conclusion
In conclusion, once you know the basics of electronics and digital circuit design, creating your own board, no matter how complex it is, is just a demonstration of will power and discipline. As long as you take your time, do some reading, and think things through, you can design anything you put your mind to.
25