Program Design

Cornell University EE 476 Final Project

Fertilizer Feed Rate Controller

Warren Scott



There are several objectives that the program achieves: Measuring the frequency of the square wave signal from the radar unit, measuring the frequency of the square wave signal from the proximity sensor to measure the shaft rotation speed, generate a PWM signal for motor speed control, as well as control a display for the tractor ground speed, and fertilizer delivery rate (during the prototype stage the display will show control parameters). The logic behind the operation of each of these items is discussed in detail in the following sections.



Timing the radar unit frequency:

The radar unit sends out a microwave frequency and analyzes the echo signal to determine at what speed the radar unit is travelling in relation to the object it is pointed at. The radar unit then outputs a square wave signal (0-12V) at 17Hz/MPH (10Hz/KPH). To determine the speed of the tractor, the ATMEL chip accurately measures the frequency of this output. As the Atmel is inherently 5V, and the radar unit sends a 12V signal, the signal goes through a series of transistors (see schematic) which drops the square wave signal down to a 0-5V waveform. This waveform is then fed into the Atmel through the ICP port (Pin 31) which causes the Atmel to perform a hardware capture of the contents of timer1 into the ICR register and cause an interrupt (TIM1_CAPT) on a rising edge. This way one can design a program that is based around having an interrupt occur once per period of the square wave. By performing a difference between the most recent and the previous timer1 captures, the number of clock cycles between the two rising edges of the speed signal can be calculated, and then converted into a time, then a frequency, and finally the ground speed of the tractor. However, there is no guarantee that timer1 will not overflow between the two captures (timer1 is actually guaranteed to overflow between two captures at one point or another) as timer1 increments without being reset, and measures a wide range of velocities (anywhere from 0 to ~10MPH). This means that we must consider the case where timer1 overflows in-between the two captures. This is accomplished by having a variable that is dedicated to counting the number of times that timer1 overflows between the two ICP interrupts, incremented by the timer1 overflow interrupt. The clock cycles between the two captures can then be calculated through the formula (clock cycles = (endcount + 65535*overflows)-startcount). This finally becomes a speed through the formula (MPH = (4000000/clock cycles)/(17 Hz/mph).



Timing the shaft rotation speed:

The rotation of the fertilizer feed shaft will be accomplished by putting a gear with a fixed number of teeth on the end of the drive shaft, and mounting a proximity sensor such that it measures the motion of the gear. The proximity sensor works on the principal that the inductance of a coil changes as it approaches a magnetic material. This way it will output a square wave signal as a tooth of the (iron) gear passes close to it, with the same number of pulses per revolution as there are teeth on the gear. For simulation before mounting on the tractor and fertilizer feed unit, a PC cooling fan was used to simulate the electric drive motor. A set of magnets was mounted on the fan, and a Hall effect sensor was used to generate a square wave pulse as magnets of opposite polarity spin by.

The measurement of the shaft speed is now similar to measuring the signal from the radar unit (measuring the frequency of a square wave signal), except that the Atmel only has one ICP pin, so we need to implement the same function differently. Therefore, the signal (0-5V square wave) enters the Atmel through the INT0 pin (external interrupt 0, pin 12, PORTD.2). The program is configured such that a rising edge on INT0 pin will cause an interrupt to happen (EXT_INT0). The interrupt then causes a manual read of the timer1 value, and performs the same timer1 overflow checking as done in the timing of the radar unit signal. The disadvantage here is that there is a delay between when the INT0 pin goes high, and when the value of timer1 is actually read (the program must enter the interrupt routine and execute the read timer1 command, instead of the read being done in hardware and stored in a temporary register to be accessed at leisure once the interrupt routine got around to reading it). This means that the timing of the shaft rotation speed will not be as accurate, however the time to enter the interrupt will be very close to the same each time the interrupt executes, meaning the delay times will cancel themselves out. Furthermore, the design can accept a slightly higher random error in the speed of the shaft rotation than the speed of the tractor. As there are two external interrupt pins (INT0 and INT1 - PORTD.2 and PORTD.3) two independent shaft speeds can be measured. At this point there is only need for measuring one shaft speed, however this allows for expandability in the design for later use.



PWM generation for speed control:

The basic premise of this project is to be able to drive an electric motor at a speed that varies depending on an input signal (the pulse train of the radar gun that encodes the ground speed of the tractor). Therefore we must come up with a method of controlling the speed of the electric motor. One of the simpler methods for controlling the speed is to send the motor a PWM signal instead of a constant voltage. In theory, this causes the motor to be turned on and off very quickly, while in practice the inertia of the motor and the system it is driving smoothes out the turning on and off, causing the appearance of a constant but controllable motor speed. Therefore, by sending a square wave with varying duty cycle, the speed of the electric motor can be changed: we generate a PWM signal. The Atmel microcontroller has a built in PWM generation circuit in hardware that runs off of timer1. However, if this hardware setup is activated, timer1 cannot be used for anything else. Since we require timer1 for measuring the input from the radar unit and the shaft rotation, we cannot use the built-in PWM hardware. However, it is not difficult to use some of the other features of timer1 to write our own PWM in software by using the timer1 overflow and compare match interrupts. When timer1 overflows, the program sets the PWM output pin(s) high. These pin(s) stay high until the timer1 compare match interrupt executes, which sets the pin low again. The pin will stay low until timer1 overflows again setting the pin high. By changing the value in the timer1 compare match register, the width of the pulse (or duty cycle) can be changed. Since there are two compare match registers for timer1, it is possible to control two PWM outputs (meaning two separate electric motors) at once. Since the project only requires controlling one motor at this time, the other will allow for future expansion.



LCD operation:

The CodeVision AVR C compiler makes it very easy to implement a LCD: a full library is included so one only needs to call an initialize statement and then use one line of code to send a string to the LCD. For more information on how CodeVision handles LCD's, see this document.



Keypad Operation:

A standard 16 button matrix-style keypad is used for user input of control parameters and later the ratio between ground speed and fertilizer delivery rate. The keypad is read by first clearing the lower nibble as output 0, and the upper nibble as input with pull-ups on. We then wait until the pull-up resistors can charge up the stray capacitance in the system (by using a series of no-op's to prevent using a timer or a delay function with unknown implementation), and then read the port to get the upper nibble. The input and output nibbles are then switched and we read the port again to get the lower nibble. The upper and lower nibble are then combined. This number is then compared to a lookup table and the element number of the match is stored and used to look up the character value of the key pressed in another lookup table. This lookup table has 18 characters (16 for the keys, a none pressed flag and a multiple pressed flag). The character of the key is then returned as the key pressed.


Go Home