You are on page 1of 25

Design of an Ethernet-based Robotic Motion Controller Final Project Report Computer Engineering Robotics

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

3. Base Sensor Capabilities


2 PS2 connections for optical mice for odometry (to be covered in another instructable)

4. Optional Sensor Capabilities


Tilt Yaw Roll X-axis acceleration Y-axis acceleration Note: The ICs to perform these measurements are somewhat expensive, so I'm leaving room for them on the board, but won't be populating them just yet.

2. Overall Architecture
Wireless Access

I2C Ethernet Microcontroller


ATMega128

SPI

UART

A/D

Dual Port RAM


Cypress CY7C006

PS2

Motor Control Microcontroller


ATMega128

PS2

Optical Mouse
(Odometry)

Optical Mouse
(Odometry)

x2 DC Motors x2 Quadrature Encoders


(3A @ 55V Max)

X/Y-Axis Accelerometer

Tilt/Roll Inclinometer

Z-Axis (yaw) Accelerometer

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.

2. Ethernet Interface: ENC28J60

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.

3. Motor Controllers: LMD18200

Figure 2.3.1 LMD18200 pinout

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.

3. Motor Controller Design


1. DC Motor Introduction
Before we can understand how we are going to control the DC motors specified in our design goals, we must understand how they work. To begin with, DC motors are perhaps the most popular actuators on the market; this is due to a number of reasons: Cheap Easy to manufacture Fairly straightforward control mechanisms Oldest and simplest Unfortunately, they also have a few drawbacks: Lack of torque compared to other actuators Most efficient at high speed No holding torque So now that we understand the advantages and weaknesses of the DC motor, now we need to understand how they work. Essentially, the DC motor is a giant loop of wire that creates an electromagnet; this electromagnet is placed inside of two permanent magnets with opposite charge.

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).

2. Pulse Width Modulation


Pulse Width Modulation is the answer to our torque problems. Instead of limiting the current or voltage applied to the motor, we are going to either give the motor full voltage and full current, or nothing at all. If we do this really fast, the on/off switches won't even be noticeable. Now to control the speed of the motor, we simply change the ratio of time spent on to the time spent off.

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.

3. Mechanics of the LMD18200


The LM18200 is pretty much your standard H-Bridge. H-Bridges are essentially a group of four beefy transistors that control the current flow into the motor.

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.

4. Designing the control circuitry


1. LMD18200 current sensing circuit
The purpose of this circuit is to let the microprocessor know how much current each motor controller is consuming. This information is only useful for pretty much one reason: detecting a stalled motor. Should one of your motors get stuck, you will immediately see a spike in current consumption (which will let you know that it is time for your robot to ask for some help instead of burning out your motors). The LM18200 H-Bridges provide a current sense output on pin 8. According to the datasheet "This pin provides the sourcing current sensing output signal, which is typically 377uA/A". In english, this simply means that for every Amp of current that the motor consumes, this output pin will present 377uA of current (.00000377 Amps). This may seem a little strange to some people, especially beginners: why wouldn't they just provide a voltage output that changes with the current used by the motors? Well, there are a number of reasons for this, but that would stretch a little beyond the scope of this instructable, so for now, we'll leave this be. Now, on to how to convert this signal into something useful, say a voltage that corresponds to the current being consumed. Well, there are two general ways to do this, the complex way that involves Op-Amps, resistors and capacitors, or the simple way that will get you results, I prefer the later.

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)!

2. Completing the design


Now that we have designed the current sensing circuitry for the LM18200 H-Bridge, it's time to put it together with the rest of the H-Bridge design. Control Lines As I mentioned before, we will only be concerned about controlling three lines per H-Bridge: Direction, Brake, and PWM. All three of these lines are considered to be digital lines, meaning they only expect to see a '1' (+5v) or a '0' (0V). Again, lets look that the functioning of each of these lines: Direction: If this line is a '1', current will flow through motor in one direction, change it to a '0' and the current reverses itself. Brake: If this line is a '1', the motors will be prevented from spinning freely, change it to a '0' and the brakes are released. Note that this isn't a true brake, the motors still can and will turn, but it will be more difficult with the brake on than off. PWM: This line expects to see a PWM signal; the duty cycle of the signal determines how much power will be supplied to the motor. A useful diagram from the datasheet is presented below, it details how each one of these lines affects the functioning of the H-Bridge. (H is a '1', L is a '0', X means it could be a '1' or a '0').

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

Figure 3.4.2.2 Final motor control schematic

5. Connecting to the ATMega128


We now have six motor control lines to worry about: Right-PWM Left-PWM Right-Dir Right-Brake Left-Dir Left-Brake The first group is special in that we will need to provide some sort of PWM signal to them, meaning that we need to take advantage of the built-in timers on the ATMega128. The last four are just simple digital outputs, so nothing special is really needed.

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.1 Diagram of timer/output compare structure

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'.

Figure 3.5.1.3 Final connections for PWM and logic lines

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.

3. Current Sense Inputs


Since we are interested in measure the voltage on these lines (they are analog, not digital), we must connect them to an analog to digital converter. The ATMega128 has 8 of such inputs, and I simply chose two of them.

Figure 3.5.3.1 Final connections for current sensing

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.

1. Quadrature Encoder Introduction


Quadrature encoders are relatively simple devices, so it shouldnt take to long to explain how they work. To begin with, a disk is attached to the output shaft of the motor or gearbox, preferably the same shaft as the wheel (to minimize error); this disk has a number of black and white (or black and clear) lines on it.

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.

Figure 4.1.2 Output of a quadrature encoder

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.

2. Connecting to the ATMega128


1. Physical Connection
Since the detector chip we will be using (manufactured by US Digital) outputs TTL compatible signals (+5V = 1, 0V = 0), we will not need any interface circuitry and hence can connect the chip directly to the ATMega128 (though a suitable connector). Note that we also provide +5V and ground for the detector chip as well.

Figure 4.2.1.1 Quadrature Connectors

2. Reading Quadrature Encoding


So now if we look carefully at sequence of 1 and 0 that will be produced by the detector chip, we can see a pattern that will greatly reduce our processing time: Clockwise A 0 1 1 0 B 0 0 1 1 Counter-Clockwise A 0 0 1 1 B 0 1 1 0 Figure 4.2.2.1 output of detector chip (bold marks transistions on A) If we make our microcontroller sensitive to transitions on channel A (both transitions from 0 to 1 and 1 to 0), then we can actually use B to judge our direction. Notice that in clockwise rotation, the transition of A from 0 to 1 lists B as 0, and the transition of A from 1 to 0 lists B as a 1, hence A and B are different. Also notice that in counter-clockwise rotation, the transition of A from 0 to 1 lists B as 1 and the transition of A from 1 to 0 lists B as 0, hence A and B are equal. In summary, in order to determine our speed, all we need to do is record the time between transitions on A, and to determine our direction, just compare A and B. Now, how do we record the time between transitions on A? Simple, use an input capture.

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).

Figure 4.2.4.1 Connection to the ATMega128

19

5. PS2 Mouse Connections


Wheel speeds are great in that we can use them to generally keep the robot in a straight line, or making proper turns, however, it doesnt actually tell us about the physical movement of the robot. In one case, a wheel could be lifted off the ground and spinning freely, but the quadrature encoders are still going to say we are moving; in another case the wheel may slip and we may lose quite a bit of accuracy. By placing two optical PS2 mice on the robot, we can determine not only the X/Y movement of the robot, but the rotation as well.
Center of rotation y Mouse 1

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.1.1 PS protocol

2. Connecting to the ATMega128


Since we will need to monitor two separate clock lines, we will need the clocks of the two mice connected to pins that support external interrupt. The data line will be read during the interrupt service routine, and hence we just need to connect it to a digital input line.

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.

1. Serial Peripheral Interface (SPI)


The host generates the clock and shifts out one bit of data on the MOSI (master output, slave input) line; for each bit shifted in by the master, the slave will produce one bit on the MISO (master input, slave output) line. (Note MOSI = SDI, MISO = SDO)

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

Figure 6.2.1 Overall ENC28J60 Schematic

1. Level Shifting Circuitry


One of the first difficulties in dealing with the ENC28J60 is the fact that it runs on 3.3V instead of 5V; since the rest of our circuitry runs on 5V we need to work some magic so the chips can talk to each other; this magic comes in the form a 74HCT125 buffer chip. This chip is a simple buffer, it takes whatever is on the input, and copies it to the output, but the T in the name specifies that it can accept unusually low logic levels on its input. This low input tolerance means that the buffer chip sees a 3V 1 as a 1 instead of an undetermined logic value; this when it recreates the 1 at the output using 5V levels, a true 5V 1 comes out. As far as inputs to the ENC28J60 are concerned, the chip is fabricated using CMOS technology, this means that its input are 5V tolerant, even powered off a 3.3V supply.

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.

3. Connecting to the ATMega128

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

You might also like