<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Procyon AVRlib: encoder.h Source File</title>
<link href="dox.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.2 -->
<div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div>
<h1>encoder.h</h1><a href="encoder_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file encoder.h \brief Quadrature Encoder reader/driver. */</span>
00002 <span class="comment">//*****************************************************************************</span>
00003 <span class="comment">//</span>
00004 <span class="comment">// File Name : 'encoder.h'</span>
00005 <span class="comment">// Title : Quadrature Encoder reader/driver</span>
00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2003-2004</span>
00007 <span class="comment">// Created : 2003.01.26</span>
00008 <span class="comment">// Revised : 2004.06.25</span>
00009 <span class="comment">// Version : 0.3</span>
00010 <span class="comment">// Target MCU : Atmel AVR Series</span>
00011 <span class="comment">// Editor Tabs : 4</span>
00012 <span class="comment">//</span><span class="comment"></span>
00013 <span class="comment">/// \ingroup driver_hw</span>
00014 <span class="comment">/// \defgroup encoder Quadrature Encoder Driver (encoder.c)</span>
00015 <span class="comment">/// \code #include "encoder.h" \endcode</span>
00016 <span class="comment">/// \par Overview</span>
00017 <span class="comment">/// This library allows easy interfacing of standard quadrature encoders</span>
00018 <span class="comment">/// (used for sensing shaft rotational position and speed) to the Atmel</span>
00019 <span class="comment">/// AVR-series processors. The library uses external interrupts to sense</span>
00020 <span class="comment">/// and keep track of the encoder's movements. The library is extendable</span>
00021 <span class="comment">/// with the maximum number of encoders equal to the total number of</span>
00022 <span class="comment">/// external interrupts available on the target AVR processor.</span>
00023 <span class="comment">/// </span>
00024 <span class="comment">/// \note Due to the wide range of external interrupt capability on</span>
00025 <span class="comment">/// AVR processors, it is difficult for this library to automatically</span>
00026 <span class="comment">/// adapt to different processors. For this reason, much of the</span>
00027 <span class="comment">/// configuration responsibility has been left with the user. See</span>
00028 <span class="comment">/// the encoderconf.h configuration file.</span>
00029 <span class="comment"></span><span class="comment">//</span><span class="comment"></span>
00030 <span class="comment">/// \par Operation:</span>
00031 <span class="comment">/// Quadrature encoders have two digital outputs usually called PhaseA and</span>
00032 <span class="comment">/// PhaseB. When the encoder rotates, PhaseA and PhaseB produce square wave</span>
00033 <span class="comment">/// pulses where each pulse represents a fraction of a turn of the encoder</span>
00034 <span class="comment">/// shaft. Encoders are rated for a certain number of pulses (or counts) per</span>
00035 <span class="comment">/// complete revolution of the shaft. Common counts/revolution specs are 50,</span>
00036 <span class="comment">/// 100,128,200,250,256,500,etc. By counting the number of pulses output on</span>
00037 <span class="comment">/// one of the phases starting from time0, you can calculate the total</span>
00038 <span class="comment">/// rotational distance the encoder has traveled.</span>
00039 <span class="comment">/// </span>
00040 <span class="comment">/// Often, however, we want current position not just total distance traveled.</span>
00041 <span class="comment">/// For this it is necessary to know not only how far the encoder has traveled,</span>
00042 <span class="comment">/// but also which direction it was going at each step of the way. To do this</span>
00043 <span class="comment">/// we need to use both outputs (or phases) of the quadrature encoder.</span>
00044 <span class="comment">///</span>
00045 <span class="comment">/// The pulses from PhaseA and PhaseB on quadrature encoders are always aligned</span>
00046 <span class="comment">/// 90 degrees out-of-phase (otherwise said: 1/4 wavelength apart). This</span>
00047 <span class="comment">/// special phase relationship lets us extract both the distance and direction</span>
00048 <span class="comment">/// the encoder has rotated from the outputs.</span>
00049 <span class="comment">///</span>
00050 <span class="comment">/// To do this, consider Phase A to be the distance counter. On each rising</span>
00051 <span class="comment">/// edge of PhaseA we will count 1 "tic" of distance, but we need to know the</span>
00052 <span class="comment">/// direction. Look at the quadrature waveform plot below. Notice that when</span>
00053 <span class="comment">/// we travel forward in time (left->right), PhaseB is always low (logic 0) at</span>
00054 <span class="comment">/// the rising edge of PhaseA. When we travel backwards in time (right->left),</span>
00055 <span class="comment">/// PhaseB is always high (logic 1) at the rising edge of PhaseA. Note that</span>
00056 <span class="comment">/// traveling forward or backwards in time is the same thing as rotating</span>
00057 <span class="comment">/// forwards or bardwards. Thus, if PhaseA is our counter, PhaseB indicates</span>
00058 <span class="comment">/// direction.</span>
00059 <span class="comment">///</span>
00060 <span class="comment">/// Here is an example waveform from a quadrature encoder:</span>
00061 <span class="comment"></span><span class="comment">/*</span><span class="comment"></span>
00062 <span class="comment">/// /---\ /---\ /---\ /---\ /---\ /---\</span>
00063 <span class="comment">/// Phase A: | | | | | | | | | | | |</span>
00064 <span class="comment">/// ---/ \---/ \---/ \---/ \---/ \---/ \-</span>
00065 <span class="comment">/// -\ /---\ /---\ /---\ /---\ /---\ /---</span>
00066 <span class="comment">/// Phase B: | | | | | | | | | | | |</span>
00067 <span class="comment">/// \---/ \---/ \---/ \---/ \---/ \---/</span>
00068 <span class="comment">/// Time: <---------------------------------------------------></span>
00069 <span class="comment">/// Rotate FWD: >----------------------------------------------> </span>
00070 <span class="comment">/// Rotate REV: <----------------------------------------------<</span>
00071 <span class="comment"></span>*/<span class="comment"></span>
00072 <span class="comment">/// To keep track of the encoder position in software, we connect PhaseA to an</span>
00073 <span class="comment">/// external processor interrupt line, and PhaseB to any I/O pin. We set up</span>
00074 <span class="comment">/// the external interrupt to trigger whenever PhaseA produces a rising edge.</span>
00075 <span class="comment">/// When a rising edge is detected, our interrupt handler function is executed.</span>
00076 <span class="comment">/// Inside the handler function, we quickly check the PhaseB line to see if it</span>
00077 <span class="comment">/// is high or low. If it is high, we increment the encoder's position</span>
00078 <span class="comment">/// counter, otherwise we decrement it. The encoder position counter can be</span>
00079 <span class="comment">/// read at any time to find out the current position.</span>
00080 <span class="comment"></span><span class="comment">//</span>
00081 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span>
00082 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>
00083 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span>
00084 <span class="comment">//</span>
00085 <span class="comment">// This code is distributed under the GNU Public License</span>
00086 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>
00087 <span class="comment">//</span>
00088 <span class="comment">//*****************************************************************************</span>
00089
00090 <span class="preprocessor">#ifndef ENCODER_H</span>
00091 <span class="preprocessor"></span><span class="preprocessor">#define ENCODER_H</span>
00092 <span class="preprocessor"></span>
00093 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>
00094
00095 <span class="comment">// include encoder configuration file</span>
00096 <span class="preprocessor">#include "<a class="code" href="encoderconf_8h.html">encoderconf.h</a>"</span>
00097
00098 <span class="comment">// constants/macros/typdefs</span>
00099
00100 <span class="comment">// defines for processor compatibility</span>
00101 <span class="comment">// chose proper Interrupt Mask (IMSK)</span>
00102 <span class="preprocessor">#ifdef EIMSK</span>
00103 <span class="preprocessor"></span><span class="preprocessor"> #define IMSK EIMSK // for processors mega128, mega64</span>
00104 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
00105 <span class="preprocessor"></span><span class="preprocessor">#ifdef GICR</span>
00106 <span class="preprocessor"></span><span class="preprocessor"> #define IMSK GICR // for mega16,32,etc</span>
00107 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
00108 <span class="preprocessor"></span><span class="comment">// default</span>
00109 <span class="preprocessor">#ifndef IMSK</span>
<a name="l00110"></a><a class="code" href="encoder_8h.html#a0">00110</a> <span class="preprocessor"></span><span class="preprocessor"> #define IMSK GIMSK // for other processors 90s8515, mega163, etc</span>
00111 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
00112 <span class="preprocessor"></span>
00113 <span class="comment"></span>
00114 <span class="comment">//! Encoder state structure</span>
00115 <span class="comment"></span><span class="comment">// stores the position and other information from each encoder</span>
<a name="l00116"></a><a class="code" href="structstruct__EncoderState.html">00116</a> <span class="keyword">typedef</span> <span class="keyword">struct </span><a class="code" href="structstruct__EncoderState.html">struct_EncoderState</a>
00117 {
<a name="l00118"></a><a class="code" href="structstruct__EncoderState.html#o0">00118</a> s32 <a class="code" href="structstruct__EncoderState.html#o0">position</a>; <span class="comment">///< position</span>
00119 <span class="comment"></span>} <a class="code" href="structstruct__EncoderState.html">EncoderStateType</a>;
00120
00121
00122 <span class="comment">// functions</span>
00123 <span class="comment"></span>
00124 <span class="comment">//! encoderInit() initializes hardware and encoder position readings</span>
00125 <span class="comment"></span><span class="comment">// Run this init routine once before using any other encoder function.</span>
00126 <span class="keywordtype">void</span> <a class="code" href="encoder_8c.html#a1">encoderInit</a>(<span class="keywordtype">void</span>);
00127 <span class="comment"></span>
00128 <span class="comment">//! encoderOff() disables hardware and stops encoder position updates</span>
00129 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="encoder_8c.html#a2">encoderOff</a>(<span class="keywordtype">void</span>);
00130 <span class="comment"></span>
00131 <span class="comment">//! encoderGetPosition() reads the current position of the encoder </span>
00132 <span class="comment"></span>s32 <a class="code" href="encoder_8c.html#a3">encoderGetPosition</a>(u08 encoderNum);
00133 <span class="comment"></span>
00134 <span class="comment">//! encoderSetPosition() sets the current position of the encoder</span>
00135 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="encoder_8c.html#a4">encoderSetPosition</a>(u08 encoderNum, s32 position);
00136
00137 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:06 2006 for Procyon AVRlib by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
</body>
</html>
|