summaryrefslogtreecommitdiff
path: root/n/es/src/tourelle.c
blob: b186cef77c16a7131d61838ad8d66f05a4bd70de (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
/*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();
}