summaryrefslogtreecommitdiff
path: root/n/accel/accel.c
blob: baca9e013526030411c4b6396920d96cd5f09b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* accel.c */
/* accel. {{{
 *
 * Copyright (C) 2004 Thomas Burg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Contact :
 *      Email: <burg@efrei.fr>
 * }}} */

/*
 * Brochange :
 * Xout <--> (ICP1)PB0
 * Yout <--> (AIN0)PD6
 *
 */

// Define

#include "accel.h"

//#define DEBUG

#ifdef DEBUG
#define debug_putc(x) rs232_putc (x)
#else
#define debug_putc(x) 
#endif

/* definition de nouveau type necessaire � l'acquisition 
 * des donn�es
 * */



volatile enum T_etat etat;

volatile uint16_t Tb,Tc,Td;
volatile uint8_t calcul_rdy;
/*
 * calcul_rdy = 0 Un calcul a �t� fait, on peut �craser Tx et Ty
 * calcul_rdy = 1 Le calcul n'est pas encore terminer interdiction
 * d'�craser Tx et Ty
 */
volatile uint8_t mode;
/* mode = 0 Mode normal;
 * Mode = 1 Mode Mesure de T2 sur la voix x
 * mode = 2 Mode Mesure de T2 sur la voix y
 */

volatile uint16_t Tx,Ty,T2;
volatile uint16_t T2x=0;
volatile uint16_t T2y=0;

int16_t Gx,Gy;
uint16_t Timer1;
int16_t Vx,Vy;
int16_t Dx,Dy;
uint8_t command;

/* Variable Globale
 */

/* Interruption input_capture */

SIGNAL(SIG_INPUT_CAPTURE1)
{
        //code de l'interruption
	switch (etat)
	{
	case strt_at_Ta :
		debug_putc('A');
		TCNT1 = 0; // remise de TCNT1 (counter 16bits)
		if (mode !=0) 
		  {
		    //va vers le mode config
		    if(mode == 1 )
		      {
			etat = rd_at_T2_x;
		      }
		    else
		      {
			etat = rd_at_T2_y;
		      }
		  }
		else
		  {
		    //v vers le mode normal
		    etat = rd_at_Tb;
		    TCCR1B &= ~_BV(ICES1); // trigge sur le front descendant
		  }
		break;
	case rd_at_Tb :
		debug_putc('B');
		Tb = ICR1; // sauvegarde du registre Input Capture
		etat = rd_at_Tc;
		ACSR |= 0x04; //change d'entr�e : analogue comparator
		TCCR1B |= _BV(ICES1); //trigge sur le front montant
		break;
	case rd_at_Tc :
		debug_putc('C');
		Tc = ICR1; // sauvegarde du registre Input Capture
		TCCR1B &= ~_BV(ICES1); // trigge sur le front descendant
		etat = rd_at_Td;
		break;
	case rd_at_Td :
		debug_putc('D');
		Td = ICR1; // sauvegarde du registre Input Capture
		etat = record;
		ACSR &= ~0x04; //change d'entr�e : IPC
		TCCR1B |= _BV(ICES1); //trigge sur le front montant
		break;
	case record :
		if (calcul_rdy == 0 )
		  {
		    Tx = Tb;	   // Calcul des Ton
		    Ty = Td - Tc;
		    // Todo T2 ne doit pas �tre calculer tout le temps
		    //T2 = (Td + Tc - Tb) /2;
		    calcul_rdy = 1; // Autorise le calcul
		  }
		else
		  {
		    // calcul trop long //
		    proto_send1b('E',6);
		  }
		etat = strt_at_Ta;
		break;
	case rd_at_T2_x :
		T2x = ICR1; // sauvegard du registre input capture
		etat = record_T2_x;
		break;
	case record_T2_x:
		etat = strt_at_Ta;
		break;
	case rd_at_T2_y :
		T2y = ICR1; // sauvegard du registre input capture
		ACSR &= ~0x04; //change d'entr�e : IPC
		etat = record_T2_y;
		break;
	case record_T2_y:
		etat = strt_at_Ta;
		break;
	default :proto_send1b('E',3);
		 break; // Ce cas ne doit pas arriver
	}
}

SIGNAL(SIG_OVERFLOW1)
{
	//erreur 1 Overflow timer/compteur1
	proto_send1b('E',1);
}

void
proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
// fonction de gestion des communications
// {{{
{
    //proto_send (c, argc, argv);
    if (size == 0)
      {
	switch (cmd)
	  {
	  case 'c' : proto_send0('C');
		     calibration();
		     break;
	  case 'x' : proto_send1w('X',Tx); 
		     break;
	  case 'y' : proto_send1w('Y',Ty);
		     break;
	  case 'r' : proto_send1w('z',T2x);
		     proto_send1w('e',T2y);
		     proto_send1w('R',T2);
		     break;
	  case 't' : proto_send1w('T',Timer1);
		     break;
	  case 'g' : proto_send1w('x',Gx);
		     proto_send1w('y',Gy);
		     break;
	  case 'k' : proto_send1w('K',T2);
		     break;
	  case 'v' : rs232_putc('V');
		     proto_send1w('x',Vx);
		     rs232_putc('V');
		     proto_send1w('y',Vy);
		     break;
	  case 'z' : reset();
	  default :  proto_send1b('E',2); //erreur 2 Command incorrect
	  }
      }
    else 
      {
	if (size == 1)
	  {
	    if (cmd == 'i') command = args[0];
	    else 
	      {
		//erreur 4 command non reconnu
		proto_send1b('E',4);
	      }
	  }
	else 
	  {
	    //Trop d'argument
	    proto_send1b('E',5);
	  }
      }
}
//}}}

int
main (void)
{
	//initialisation
    	rs232_init();
	//proto_init(test_callback,rs232_putc);
		
	proto_send0('Z');
	
	//initialisation du timer prescaler 256
	TCCR1B = _BV(ICES1) | _BV(CS11);
	// Input Capture Interrupt enable & Timer 1 overflow
	TIMSK = _BV(TICIE1) | _BV(TOIE1); 	
	TCCR1B |= _BV(ICES1); //trigge sur le front montant
	etat = strt_at_Ta;
	sei ();
	//Boucle
	while(1)
	{
	  if ( rs232_poll() )
	    {
	      /* polling sur la reception serie
	       * */
	      proto_accept(rs232_getc());
	    }
	  // calcul si l'on peut
	  if (calcul_rdy == 1)
	    {
	      //proto_send0('c');	
	      // remise de TCNT1 pour mesurer le temps
	      TCNT1 = 0;
	      Gx = calculG(Tx,T1x_cal);
	      Gy = calculG(Ty,T1y_cal);
	      
	      // Tx et Ty ont �t� utilis�, on peut les �cras�s
	      calcul_rdy = 0 ;

	      Vx +=Gx;
	      Vy +=Gy;
	      Timer1 = TCNT1;

		    if (command !=0)
		      {
			if (command & 0x01)
			  {
			    proto_send1w('P',Tx);
			  }
			if (command & 0x02)
			  {
			    proto_send1w('Q',Ty);
			  }
			if (command & 0x04)
			  {
			    proto_send1w('R',Gx);
			  }
			if (command & 0x08)
			  {
			    proto_send1w('S',Gy);
			  }
			if (command & 0x10)
			  {
			    proto_send1w('T',Vx);
			  }
			if (command & 0x20)
			  {
			    proto_send1w('U',Vy);
			  }
			if (command & 0x40)
			  {
			    proto_send1w('V',Dx);
			  }
			if (command & 0x80)
			  {
			    proto_send1w('W',Dy);
			  }
		      }

		  }
	}
	return 0;
}