/*Tourelle.c*/ /* Detector on a ATmega128. {{{ * Copyright (C) 2005 Dalmais Romain * * Robot APB Team/Efrei 2005. * Web: http://assos.efrei.fr/robot/ * Email: robot AT efrei DOT fr * * 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. * }}} */ #include "Tourelle.h" const int8_t delayMax = 125; const int8_t delayMin = 175; const int8_t SEUIL = 180; // 10 cm = 2.25V et 80 cm = 1.75 V //capteur sharp sur PA0 /* etat mode: * 0 = inactif * 1 = continu */ int8_t etat=0; int8_t tableau_longueur[25]; tourelle_init(int8_t prescaler) { continu = 0; delay = 0; SFIOR |= 0x01; // prescaler autorisé crenaux(delay); // on met le capteur à 0° wait_1ms(20); crenaux(delay); wait_1ms(20); crenaux(delay); switch(prescaler) { case 81: registre_prescal |= 0x03;break; case 64 : { registre_prescaler &= 0xFD; registre_prescaler |= 0x01; }break; case 256 : { registre_prescaler &= 0xFD; registre_prescaler |= 0x01; }break; case 1024: registre_prescal |= 0x03;break; } } crenaux(int8_t temps, int8_t port, int8_t pin) { port |= pin; // en admettant que je sois sur le portb et la troisième pin wait_10us(temp); port &= 0xFF - pin; // on remet l'impulsion à 0 } proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) { #define c(cmd, size) (cmd << 8 | size) switch (c (cmd, size)) { case c ('z', 0): reset (); break; /* Commands. */ case c ('c', 0):{ // continu etat = 1; envoie = 1; if(c(0,1)) sendValeur(); /// mettre la bonne fonction }break; case c ('u',0):{ // unique envoie = 0; sendValeur(); etat = 0; }break; case c ('s',0): { // stop etat = 0; envoie = 0; }break; } TournerTourelle() { if(delay=delayMax||delay=delayMin) { sens *= -1; valeur_ancienne1 = valeur_actuelle1; valeur_actuelle1 = 0; valeur_ancienne2 = valeur_actuelle2; valeur_actuelle2 = 0; angle_ancien1 = angle_actuel1; angle_ancien2 = angle_actuel2; nombre_ancien = nombre_actuel; } delay += 2 * sens; crenaux(delay,portc,0x07); } int8_t mesurer() { adc_init(); adc_start(); while(!adc_checkf) {wait_1us(1)} return adc_read(); } void Timer_tourelle() { // on fait la mesure uint8_t i = 0; uint8_t temp = 0; for(i=0;i<(MILIEU*2+1);i++) { if(temp < tableau_longueur [i]) temp = tableau_longueur[i]; } tableau_longueur[0] = mesurer(); if(temp == tableau_longueur[MILIEU+1]) { if(nombre_valeur == 0) { valeur_actuelle1 = (PROUT - distance); angle_actuel1 = 45 + (delay - 6)*90/50; } if(nombre_valeur == 1) { valeur_actuelle2 = (PROUT - distance); angle_actuel2 = 45 + (delay - 6)*90/50; } if(nombre_valeur > 1) { // ALARME // PROBLEME } } cycle(); }