| Using Wheel Encoders Example Last Modified: 2006-10-31 | | |
| Acroname Robotics | |||
| Introduction A common task for a robot is measuring distance as it travels. For a wheeled robot, this is often accomplished by using encoders that produce a pulse when the wheels move a small distance. This example shows a TEA program that may be used to keep track of two digital encoders. A typical encoding scheme for a small robot is an optoelectronic switch that stares at a pattern of black and white sectors on an encoder wheel. As the wheel turns, the switch turns on and off. A microcontroller can count these pulses and determine the distance traveled. Source Code In the example program below, each transition on an encoder input (low-to-high or high-to-low) increments a counter. The encoder inputs are digital IO pins 0 and 1. When a transition occurs on pin 0, counter 0 is incremented. When a transition occurs on pin 1, counter 1 is incremented. Counters 0 and 1 are two-byte reflex counters. A host computer may use the cmdCTR_SET command to read the current count or set the current count. A concurrent TEA process running within the same module may access the counters via their TEA I/O ports. The rate at which this program can process encoder transitions depends on the processor load. When the processor is busy, it can not count as many pulses. The program was coded by hand using virtual machine (VM) opcodes to keep the code as efficient as possible. In the worst case, the program executes 24 TEA opcodes per iteration. The typical execution rate for VM opcodes is approximately 9000 instructions per second. At this rate, the program can process a maximum of about 375 digital transitions per second for each encoder. If other TEA processes are running, the number of digital transitions that this program can process will be divided by the number of active processes. This program may be used instead of reflexes to monitor incoming pulses. Firmware versions prior to Build 9 may not handle digital transition reflexes properly. /* dualenc.tea */
void main()
{
char j0;
char j1;
asm
{
start:
pushmb 0x0501 /* read pin0 */
pushsb 1 /* copy that value */
pushsb 4 /* push previous value */
xorb /* XOR current and previous */
popb /* discard result and set Z and N flags */
brz save0
pushms 0x03A0 /* increment counter 0 */
pushls 1
adds
popsm 0x03A0
save0:
popbs 2 /* save previous pin0 reading */
pushmb 0x0505 /* read pin1 */
pushsb 1 /* copy that value */
pushsb 3 /* push previous pin1 value */
xorb /* XOR current and previous */
popb /* discard result and set Z and N flags */
brz save1
pushms 0x03A1 /* increment counter 1 */
pushls 1
adds
popsm 0x03A1
save1:
popbs 1 /* save previous pin1 reading */
goto start
}
}
Revision History:
| ||||
| voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy © Copyright 1994-2012 Acroname, Inc., Boulder, Colorado. All rights reserved. |