2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
4 * This program 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 2 of the License, or
7 * (at your option) any later version.
9 * This program 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: encoders_microb.c,v 1.6.4.3 2008-01-09 22:27:19 zer0 Exp $
22 /** \file encoders_microb.c
23 * \brief Implementation for getting motor position
25 * \todo test the module
29 * This module provides functions for getting position from
30 * coders. Its implementation depends on hardware, so that's why there
31 * is (or will be) multiple implementation of this module.
33 * The harware is quite simple here :
48 * | SEL |----> (1 bit)
59 #include <aversive/wait.h>
61 #include <encoders_microb.h>
62 #include <encoders_microb_config.h>
65 /******************************************/
66 int32_t g_encoders_microb_values[ENCODERS_MICROB_NUMBER];
67 int8_t g_encoders_microb_previous[ENCODERS_MICROB_NUMBER];
69 /******************************************/
71 #define SEL_A() cbi(ENCODERS_MICROB_SEL_PORT,ENCODERS_MICROB_SEL_BIT)
72 #define SEL_B() sbi(ENCODERS_MICROB_SEL_PORT,ENCODERS_MICROB_SEL_BIT)
74 /******************************************/
78 val = ENCODERS_MICROB##n##_PIN; \
80 if (val != ENCODERS_MICROB##n##_PIN) \
81 val = ENCODERS_MICROB##n##_PIN; \
83 res = (val - g_encoders_microb_previous[n]); \
84 g_encoders_microb_previous[n] = val; \
86 g_encoders_microb_values[n] += (int32_t)res ; \
89 /******************************************/
91 #define INIT(n) do { \
92 g_encoders_microb_values[n] = 0; \
95 /******************************************/
102 * Initialisation of encoders, variables
104 void encoders_microb_init(void)
108 sbi(DDR(ENCODERS_MICROB_SEL_PORT),ENCODERS_MICROB_SEL_BIT);
110 // wait at least 10us
112 encoders_microb_manage(NULL);
114 encoders_microb_manage(NULL);
117 #ifdef ENCODERS_MICROB0_ENABLED
121 #ifdef ENCODERS_MICROB2_ENABLED
125 #ifdef ENCODERS_MICROB4_ENABLED
129 #ifdef ENCODERS_MICROB6_ENABLED
135 // wait at least 10us
138 #ifdef ENCODERS_MICROB1_ENABLED
142 #ifdef ENCODERS_MICROB3_ENABLED
146 #ifdef ENCODERS_MICROB5_ENABLED
150 #ifdef ENCODERS_MICROB7_ENABLED
159 * Update encoders values, need to be done quite often
160 * (Fmax_encoders/64). First, encoders 0 2 4 6 are read, and at next call
163 * \param dummy : a (void *) pointer that is not used. It is here according
164 * to the encoders interface.
166 void encoders_microb_manage(void * dummy)
168 static uint8_t sel=0;
176 if ( ! sel) /* here SEL_A() is set since last call */
178 #ifdef ENCODERS_MICROB0_ENABLED
182 #ifdef ENCODERS_MICROB2_ENABLED
186 #ifdef ENCODERS_MICROB4_ENABLED
190 #ifdef ENCODERS_MICROB6_ENABLED
196 else /* here SEL_B() is set since last call */
198 #ifdef ENCODERS_MICROB1_ENABLED
202 #ifdef ENCODERS_MICROB3_ENABLED
206 #ifdef ENCODERS_MICROB5_ENABLED
210 #ifdef ENCODERS_MICROB7_ENABLED
225 /** Extract encoder value.
227 * \param number : a (void *) that is casted in (int) containing the number
228 * of the encoder to be read.
230 int32_t encoders_microb_get_value(void * number)
236 value = g_encoders_microb_values[(int)number];
242 /** Set an encoder value
244 * \param number : a (void *) that is casted in (int) containing the number
245 * of the encoder to be read.
246 * \param v : the value
248 void encoders_microb_set_value(void * number, int32_t v)
253 g_encoders_microb_values[(int)number] = v;