encoder.c
Go to the documentation of this file.
00001 /***************************************************************************
00002 ***  Fraunhofer IPA 
00003 ***  Robotersysteme 
00004 ***  Projekt: 3D cartography demonstrator
00005 ****************************************************************************
00006 ****************************************************************************
00007 ***  Autor: Julio Sagardoy (goa-js)
00008 ***************************************************************************/
00009 
00017 
00018 #include <stdlib.h>
00019 
00021 #include <avr/io.h>
00022 #include <avr/interrupt.h>
00023 #include <util/delay.h>
00024 
00026 #include "encoder.h"
00027 #include "utils.h"
00028 
00029 #define PHASE_A BITREAD(PIND,PD7)
00030 #define PHASE_B BITREAD(PINE,PE6)
00031 
00032 volatile int8_t enc_delta;      // Contains the difference between the last sample and the current sample in binary, if any
00033 volatile int16_t enc_accum;     // Contains the absolute count of steps
00034 static int16_t last;            // Contains last sampling binary values
00035 
00038 void encoder_reset()
00039 {
00040         enc_accum = 0;
00041         enc_delta = 0;
00042         last = 0;
00043 }
00044 
00047 int16_t encoder_read()
00048 {
00049         return ~enc_accum;
00050 }
00051 
00052 ISR( TIMER3_COMPA_vect )
00053 {
00054         int8_t new, diff;
00055         
00056         // convert from gray to binary, code by Peter Dannegger
00057         new = 0;
00058         if( PHASE_A )
00059                 new = 3;
00060         if( PHASE_B )
00061                 new ^= 1;
00062 
00063         diff = last - new;
00064         
00065         // See if there is difference with last
00066         if(diff & 1)    //      Bit 0 = value
00067         {
00068                 last = new;                     //Store new as next last
00069                 enc_delta += (diff & 2) - 1;    //BIt 1 = direction. enc_delta: 1/-1
00070                 enc_accum += enc_delta;
00071                 enc_delta = 0;
00072         }
00073 }
00074 
00077 void encoder_init()
00078 {
00079         int8_t new;
00080         
00081         new=0;
00082         //Encoder output is gray-coded. At each sampling of the encoder input, we convert gray to binary, so value can be easily added or substracted. Gray code is:
00083         //____^^^^____^^^^____ PHASE_A
00084         //  ____^^^^____^^^^____        PHASE_B
00085         //  0 1 1 0 0 1 1 0 0 
00086         //  0 0 1 1 0 0 1 1 0
00087 
00088         if( PHASE_A )
00089                 new = 3;
00090         if( PHASE_B )
00091                 new ^= 1;
00092         last = new;
00093         
00094         enc_delta = 0;
00095         
00096         TCCR3A = 0x03;  //PWM, TOP=OCR3A, BOTTOM=TOV
00097         TCCR3B = 0x11;  //no preescaling
00098         OCR3A = 3500;// Sampling rate for loseless sampling calculation: stepper steps at 3ms/step max. 1 step of stepper are 5 encoder steps. This is 0.6ms/e_step. Nyquist with factor 2.5 = 0.24ms/e_sambple --> around OCR3A = 3500
00099         BITSET(TIMSK3,OCIE3A);  //enable interrupt at OC3A
00100 
00101         // Index signal is not sampled, instead, it triggers an external interrupt.
00102         BITSET(PCICR,PCIE0);
00103         BITSET(PCMSK0,PCINT0);
00104 }


cob_3d_mapping_demonstrator
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:03:46