summaryrefslogtreecommitdiff
path: root/n/es/src/tourelle.c
blob: 62f037fba4f29daa643162eaf682519754e04683 (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
/* 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"
#include "avr/signal.h"

const uint8_t delayMax = 125;
const uint8_t delayMin = 175;
const uint8_t pin_sharp = 3 ;  
uint8_t delay = 0;
const uint8_t SEUIL = 280;	// 10 cm = 2.25V et 80 cm = 1.75 V
const uint8_t MILIEU = 2;
// const int8_t PROUT = 12;  A VERIFIER !!

//capteur sharp sur PA3
//servo sur PB

/* etat mode:
 * 0 = inactif
 * 1 = continu */

uint16_t tableau_longueur[25];

extern uint8_t continu;

extern uint8_t etat;

extern uint8_t envoie;

extern uint8_t sens;

uint8_t valeur_actuelle1 = 0; 
uint8_t valeur_actuelle2 = 0; 
uint8_t angle_actuel1 = 0; 
uint8_t angle_actuel2 = 0; 
uint8_t valeur_ancienne1 = 0; 
uint8_t valeur_ancienne2 = 0; 
uint8_t angle_ancien1 = 0; 
uint8_t angle_ancien2 = 0; 
uint8_t nombre_actuel = 0;
uint8_t nombre_ancien = 0;

void tourelle_init (uint16_t prescaler)
{
	delay = 135;
	
	SFIOR |= 0x01;		// prescaler autoris�

	rs232_putc('T');
	crenaux(delay,PORTC,7); 	// on met le capteur � 0�
	delay_ms(20L);
	
	crenaux(delay,PORTC,7);
	delay_ms(20L);
	
	crenaux(delay,PORTC,7); 	// on met le capteur � 0�
	delay_ms(20L);
	DDRD = 0x80;	
	crenaux(delay,PORTD,7);
	delay_ms(20L);
	crenaux(delay,PORTD,7);
	rs232_putc('C');

	crenaux(delay,PORTC,7); 	// on met le capteur � 0�
	delay_ms(20L);
	
	crenaux(delay,PORTC,7);
	delay_ms(20L);

	rs232_putc('F');
	
	switch(prescaler)
	{
		
		case 8  :
			{
				TCCR1B |= 0x02;
				TCCR1B &= 0xFA;
						
				
			}break;
		
		case 64 : 
			{
				TCCR1B &= 0xFB;
				TCCR1B |= 0x03;
			}break;
			
		case 256 : 
			{
				TCCR1B &= 0xFC;
				TCCR1B |= 0x04;
			}break;
			
		case 1024: 
			{
				TCCR1B |= 0x05;
				TCCR1B &= 0xFD;
			}break;
	
	}
}

void crenaux(uint8_t temps, uint8_t port, uint8_t pin)
{
	uint8_t delay_crenaux = 0;
	PORTD |= _BV (7);   			// en admettant que je sois sur le portb et la troisi�me pin
	for(delay_crenaux=0;delay_crenaux<temps;delay_crenaux++)
	{
		delay_us(10L);	
	}
	PORTD &= ~_BV (7);		// on remet l'impulsion � 0
	rs232_putc('C');
}

void rs232_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();			/// METTRE LA BONNE FONCTION
		etat = 0;
	}break;

	case c ('s',0): {			// stop
		etat = 0;	
		envoie = 0;
	}break;
	}
}


void TournerTourelle(void)
{
 	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);
}

int16_t mesurer(void)
{
	adc_init();
	adc_start(pin_sharp);
	while(!adc_checkf()){}
	return adc_read();
}


SIGNAL (SIG_OVERFLOW1)
{
	// on fait la mesure
	uint8_t i = 0;
	uint8_t temp = 0;
	
	TIMSK &= ~(0x04);

	rs232_putc('3');
	
	for(i=0;i<(MILIEU*2+1);i++)
	{
		if(temp < tableau_longueur [i]) temp = tableau_longueur[i];
	}
	tableau_longueur[0] = mesurer();
	proto_send1w ('S',tableau_longueur[0]);	
	/*	
	if(temp == tableau_longueur[MILIEU+1] && temp > SEUIL )
	{
		if(nombre_actuel == 0)
		{
			valeur_actuelle1 =  tableau_longueur[MILIEU+1];
			angle_actuel1 = 45 + (delay - 6)*90/50;
		}
		
		if(nombre_actuel == 1)
		{
			valeur_actuelle2 = tableau_longueur[MILIEU +1];
			angle_actuel2 = 45 + (delay - 6)*90/50;
		}
		
		if(nombre_actuel > 1)
		{
//			ALARME // PROBLEME // ALLUMER LED
		}
		
	}
	*/
	
	TournerTourelle ();
	etat = 1;
	rs232_putc('4');
}