$Id: README,v 1.12 2003/10/08 08:39:16 dwalters Exp $
-- Open Automaton Project: http://oap.sourceforge.net -- 

MOTOR CONTROL MODULE INTRODUCTION
---------------------------------

This README covers the source code and circuit schematics for the Open 
Automaton Project Motor Control Module.

The Motor Control Module is a hardware device that controls the two DC drive
motors of the robot using PWM (Pulse Width Modulation), and also keeps track of
two odometric counters driven by the robot's quadrature encoders. 

The module is currently designed for a differential-drive robot with two drive
motors. It outputs two PWM signals, one for each motor driver.

The Motor Control Module outputs the motor PWM signals in direct proportion to 
drive command values issued by the host, by open loop control (no feedback).

All communication between the module and the host robot's mainboard is via the 
I2C interface.

The circuit schematic (motor_control_module.sch) was developed in gschem, part
of the gEDA package, version 20030525.

Circuit schematic symbols that are not in the symbol library bundled with gEDA
are included as separate sym files (these .sym files can be copied into your
local symbol directory, typically /usr/local/share/gEDA/sym/local/).


LICENSING
---------

All source code and electronic circuit designs are copyright (C) 2003, Dafydd 
Walters.

The Open Automaton Project software is distributed under the terms of the GNU
General Public License. See the file COPYING.SOFTWARE for details.

Please see the file COPYING.ELECTRONICS to see the terms under which the
electronic circuit designs of the Open Automaton Project are distributed.


CIRCUIT NOTES
-------------

The two PWM outputs of the Motor Control Module are Locked Antiphase signals.
This means that at a 50% duty cycle, the motor should be stationary. At
100%, the motor should be turning at full speed in one direction, and at 0%
duty cycle, the motor should be turning at full speed in the opposite
direction. The PWM drive signals have a frequency of 19.53125 kHz.

The Motor Control Module decodes the quadrature encoder pulse train in
software, rather than using hardware, so there's a limit to how fast the
pulses can be received before pulse counts are lost. Some experimentation
is needed to establish exactly what that limit is, but this design ought to
be able to cope with at least 80,000 counts per second (one count being
a transition of either of the A or B channel signals). This estimate is
based on 20,000 rising edges of the A channel per second from both
wheels, since the module performs 1x decoding (which only updates the counters
at each rising edge of the A channel).

So for example, using a quadrature encoder which outputs 1024 pulses per 
revolution, the maximum shaft speed that the Motor Control Module could handle
is probably around 4,600 r.p.m.


SMBUS INTERFACE
---------------

This module is a slave on the I2C SMBus, with the fixed address 89
(the address can be changed by modifying the source code and re-compiling).
The device responds to the following host (master) commands:

Block Read, Command = 20:       Read the two odometry counters (driven from the
                                quadrature encoders). The block size is
                                5 bytes.  The first two bytes contain the
                                16-bit value of the odometry counter for the
                                Left wheel, and the next two bytes contain
                                the 16-bit value of the Right wheel odometry 
                                counter. The 16-bit numbers are sent low-byte
                                first. The last byte contains the value
                                of the free running 8-bit up counter, which
                                ticks with a period of 13.1072ms. The counter
                                therefore wraps around approximately every 
                                3.4 seconds. The Motor Control Module performs
                                a 1x decode of the quadrature encoder pulses.
                                This means that the counters are incremented or
                                decremented by 1 for each rising edge of an
                                A-channel signal.

Read Byte Data, Command = 21 :  Query the output state. 0=PWM off (initial
                                default - low power mode), 1=on.

Read Word Data, Command = 22 :  Report the current PWM values last set by
                                command 120. The first (low) byte is the Left 
                                motor drive command value, and the second 
                                (high) byte is the Right motor drive command 
                                value. The initial default value is 128 
                                (i.e. before command 120 is issued).

Read Byte Data, Command = 23:   Read the current value of the 8-bit timer
                                counter.

Read Byte Data, Command = 24:   Read the encoder XOR mask byte. Only bits
                                5 and 6 of this byte are relevant. Bit 5
                                corresponds to the Right encoder, and bit 6
                                to the Left encoder. When one of these bits is
                                set to zero, counts from the corresponding
                                quadrature encoder will count upwards when the
                                B channel lags the A channel, and downwards
                                when the B channel leads the A channel. This
                                is the conventional counting direction. If one
                                of these bits is 1, the counting direction is
                                reversed.

Write Word Data, Command = 120: Set the direct PWM output of the motors. The
                                first (low) byte is the Left motor drive 
                                command, and the second (high) byte is the 
                                Right motor drive command. 
                                128=stop, 
                                255=full speed forward,
                                0=full speed reverse.

Send Byte, Command = 121:       Activate PWM output.

Send Byte, Command = 122:       Turn off PWM output (low power mode). This is
                                the initial default state.

Send Byte, Command = 123:       Zero the free running 8-bit timer count.

Write Byte Data, Command = 124: Set encoder XOR mask byte. Only bits
                                5 and 6 of this byte are relevant. Bit 5
                                corresponds to the Right encoder, and bit 6
                                to the Left encoder. When one of these bits is
                                set to zero, counts from the corresponding
                                quadrature encoder will count upwards when the
                                B channel lags the A channel, and downwards
                                when the B channel leads the A channel. This
                                is the conventional counting direction. If one
                                of these bits is 1, the counting direction is
                                reversed. This setting is stored in 
                                nonvolatile EEPROM.

PEC (Packet Error Checking) is disabled for all SMBus commands.


FIRMWARE NOTES
--------------

The software for the PICMicro is written in assembly language, and compiles 
using the GNU PIC Assembler, gpasm. To compile the source code into a hex file 
suitable for programming a PIC16F876 device, type

    gpasm -n motor_control_module.asm

The resulting hex file will be called motor_control_module.hex  The -n option 
puts DOS-style CR-LF newline sequences in the .hex file, required by most 
device programmers. 

It also compiles with the MPASM compiler for Microsoft Windows that is a part
of the MPLAB suite from Microchip. However, you must make sure the assembly
(.asm) file is in Windows/DOS format (i.e. with CR-LF sequences at the end of
each line) before it will successfully compile in MPASM for Windows.

Some device programmers require the configuration bits to be set manually. It
is very important that these are set correctly. The Sonar Array Module
requires the following configuration bit settings:

 - Flash program memory code protection: Off
 - In-circuit debugger mode: disabled (RB6 and RB7 are I/O)
 - Flash program memory write enable: Off
 - Data EE memory code protection: Off
 - Low voltage programming: Off (RB3 is I/O)
 - Brown-out reset enable bit: Enabled
 - Power-up timer enabled: On
 - Watchdog timer: Off
 - Oscillator: HS (high speed)

The above configuration settings are represented by the configuration word
3D72 in hexadecimal.