/* 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: * }}} */ /* * Brochange : * Xout <--> (ICP1)PB0 * Yout <--> (AIN0)PD6 * */ #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 proto_send1('E',6);// calcul trop long // 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_send1('E',3); break; // Ce cas ne doit pas arriver } } SIGNAL(SIG_OVERFLOW1) { proto_send1('E',1); //erreur 1 Overflow timer/compteur1 } void test_callback (uint8_t c, uint8_t argc, proto_arg_t argv[]) // fonction de gestion des communications // {{{ { //proto_send (c, argc, argv); if (argc == 0) { switch (c) { case 'c' : proto_send0('C'); calibration(); break; case 'x' : proto_send2('X',Tx >> 8, Tx); break; case 'y' : proto_send2('Y',Ty >> 8, Tx); break; case 'r' : proto_send2('z',T2x >> 8,T2x); proto_send2('e',T2y >> 8, T2y); proto_send2('R',T2 >> 8, T2); break; case 't' : proto_send2('T',Timer1 >> 8, Timer1); break; case 'g' : proto_send2('x',Gx >> 8, Gx); proto_send2('y',Gy >> 8, Gy); break; case 'k' : proto_send2('K',T2 >> 8, T2); break; case 'v' : rs232_putc('V'); proto_send2('x',Vx >> 8,Vx); rs232_putc('V'); proto_send2('y',Vy >> 8,Vy); break; case 'z' : reset(); default : proto_send1('E',2); //erreur 2 Command incorrect } } else { if (argc == 1) { if (c == 'i') command = argv[0]; else proto_send1('E',4); //erreur 4 command non reconnu } else proto_send1('E',5); //Trop d'argument } } //}}} int main (void) { //initialisation rs232_init(); proto_init(test_callback,rs232_putc); proto_send0('Z'); //initialisation du timer prescaler 256 TCCR1B = _BV(ICES1) | _BV(CS12); // 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) { //proto_send0('b'); 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_send2('P',Tx >> 8,Tx); } if (command & 0x02) { proto_send2('Q', Ty >> 8, Ty); } if (command & 0x04) { proto_send2('R', Gx >> 8,Gx); } if (command & 0x08) { proto_send2('S',Gy >> 8,Gy); } if (command & 0x10) { proto_send2('T',Vx >> 8,Vx); } if (command & 0x20) { proto_send2('U',Vy >> 8,Vy); } if (command & 0x40) { proto_send2('V', Dx >> 8,Dx); } if (command & 0x80) { proto_send2('W',Dy >> 8, Dy); } } } } return 0; }