Caddy
A 2005 Roborodentia entry with vision and path planning capability
 All Data Structures Files Functions Variables Typedefs Macros Pages
encoder.h
Go to the documentation of this file.
1 /*
2  * This file is part of Caddy.
3  *
4  * Caddy is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Caddy is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Caddy. If not, see <http://www.gnu.org/licenses/>.
16  */
18 //*****************************************************************************
19 //
20 // File Name : 'encoder.h'
21 // Title : Quadrature Encoder reader/driver
22 // Author : Pascal Stang - Copyright (C) 2003-2004
23 // Created : 2003.01.26
24 // Revised : 2004.06.25
25 // Version : 0.3
26 // Target MCU : Atmel AVR Series
27 // Editor Tabs : 4
28 //
29 // Description : This library allows easy interfacing of quadrature encoders
30 // to the Atmel AVR-series processors.
31 //
32 // Quadrature encoders have two digital outputs usually called PhaseA and
33 // PhaseB. When the encoder rotates, PhaseA and PhaseB produce square wave
34 // pulses where each pulse represents a fraction of a turn of the encoder
35 // shaft. Encoders are rated for a certain number of pulses (or counts) per
36 // complete revolution of the shaft. Common counts/revolution specs are 50,
37 // 100,128,200,250,256,500,etc. By counting the number of pulses output on
38 // one of the phases starting from time0, you can calculate the total
39 // rotational distance the encoder has traveled.
40 //
41 // Often, however, we want current position not just total distance traveled.
42 // For this it is necessary to know not only how far the encoder has traveled,
43 // but also which direction it was going at each step of the way. To do this
44 // we need to use both outputs (or phases) of the quadrature encoder.
45 //
46 // The pulses from PhaseA and PhaseB on quadrature encoders are always aligned
47 // 90 degrees out-of-phase (otherwise said: 1/4 wavelength apart). This
48 // special phase relationship lets us extract both the distance and direction
49 // the encoder has rotated from the outputs.
50 //
51 // To do this, consider Phase A to be the distance counter. On each rising
52 // edge of PhaseA we will count 1 "tic" of distance, but we need to know the
53 // direction. Look at the quadrature waveform plot below. Notice that when
54 // we travel forward in time (left->right), PhaseB is always low (logic 0) at
55 // the rising edge of PhaseA. When we travel backwards in time (right->left),
56 // PhaseB is always high (logic 1) at the rising edge of PhaseA. Note that
57 // traveling forward or backwards in time is the same thing as rotating
58 // forwards or bardwards. Thus, if PhaseA is our counter, PhaseB indicates
59 // direction.
60 //
61 // Here is an example waveform from a quadrature encoder:
62 /*
63 // /---\ /---\ /---\ /---\ /---\ /---\
64 // Phase A: | | | | | | | | | | | |
65 // ---/ \---/ \---/ \---/ \---/ \---/ \-
66 // -\ /---\ /---\ /---\ /---\ /---\ /---
67 // Phase B: | | | | | | | | | | | |
68 // \---/ \---/ \---/ \---/ \---/ \---/
69 // Time: <--------------------------------------------------->
70 // Rotate FWD: >---------------------------------------------->
71 // Rotate REV: <----------------------------------------------<
72 */
73 // To keep track of the encoder position in software, we connect PhaseA to an
74 // external processor interrupt line, and PhaseB to any I/O pin. We set up
75 // the external interrupt to trigger whenever PhaseA produces a rising edge.
76 // When a rising edge is detected, our interrupt handler function is executed.
77 // Inside the handler function, we quickly check the PhaseB line to see if it
78 // is high or low. If it is high, we increment the encoder's position
79 // counter, otherwise we decrement it. The encoder position counter can be
80 // read at any time to find out the current position.
81 //
82 //
83 // NOTE: This code is currently below version 1.0, and therefore is considered
84 // to be lacking in some functionality or documentation, or may not be fully
85 // tested. Nonetheless, you can expect most functions to work.
86 //
87 // This code is distributed under the GNU Public License
88 // which can be found at http://www.gnu.org/licenses/gpl.txt
89 //
90 //*****************************************************************************
91 
92 #ifndef ENCODER_H
93 #define ENCODER_H
94 
95 #include "global.h"
96 
97 // include encoder configuration file
98 #include "encoderconf.h"
99 
100 #include <stdint.h>
101 
102 // constants/macros/typdefs
103 
104 // defines for processor compatibility
105 // chose proper Interrupt Mask (IMSK)
106 #ifdef EIMSK
107  #define IMSK EIMSK // for processors mega128, mega64
108 #else
109  #define IMSK GIMSK // for other processors 90s8515, mega163, etc
110 #endif
111 
112 
114 // stores the position and other information from each encoder
115 typedef struct struct_EncoderState
116 {
117  uint16_t position;
118 // s32 velocity; ///< velocity
120 
121 
122 // functions
123 
125 // Run this init routine once before using any other encoder function.
126 inline void encoderInit(void);
127 
129 uint16_t encoderGetPosition(uint8_t encoderNum);
130 
132 void encoderSetPosition(uint8_t encoderNum, uint16_t position);
133 
134 #endif