Subversion Repositories svnkaklik

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2 kaklik 1
#include "tank.h"
2
 
3
#define DEBUG
4
 
5
#define  TXo PIN_A3           // To the transmitter modulator
6
#include "AX25.c"             // podprogram pro prenos telemetrie
7
 
8
unsigned int8 sensors;        // pomocna promenna pro cteni cidel na caru
9
unsigned int8 line;           // na ktere strane byla detekovana cara
10
unsigned int8 speed;          // rychlost zataceni
11
unsigned int8 rovinka;        // pocitadlo pro zjisteni rovneho useku
12
unsigned int8 last;           // kde byla cara, kdyz byly minule cidla mimo
13
unsigned int8 movement;       // obsahuje aktualni smer zataceni
14
unsigned int8 dira;           // pocita dobu po kterou je ztracena cara
15
unsigned int8 cihla;          // urcuje za jak dlouho muze byt znova detekovana cihla
16
 
17
// Konstanty pro dynamiku pohybu
18
#define T_DIRA       87      // po jakem case zataceni se detekuje dira
19
#define INC_SPEED    1        // prirustek rychlosti v jednom kroku
20
#define FW_POMALU    230      // trochu mimo caru vnitrni pas
21
#define FW_ZATACKA   200      // rychlost vnejsiho kola pri zataceni
22
#define FW_STREDNE   240      // trochu mimo caru vnejsi pas
23
#define COUVANI      600      // couvnuti zpet na caru, po detekci diry
24
#define PRES_DIRU    250
25
#define MAX_ROVINKA  (255-FW_STREDNE)
26
#define TRESHOLD     10        // rozhodovaci uroven komparatoru, 0xF = 0.75*Vdd
27
#define BUMPER_TRESHOLD 128
28
#define CIK_CAK      30000
29
#define T_CIHLA      100      // perioda detekce cihly
30
 
31
//motory            //Napred vypnout potom zapnout!
32
#define FR         output_low(PIN_B5); output_high(PIN_B4)  // Vpred
33
#define FL         output_low(PIN_B7); output_high(PIN_B6)
34
#define BR         output_low(PIN_B4); output_high(PIN_B5)  // Vzad
35
#define BL         output_low(PIN_B6); output_high(PIN_B7)
36
#define STOPR      output_low(PIN_B4);output_low(PIN_B5)    // Zastav
37
#define STOPL      output_low(PIN_B6);output_low(PIN_B7)
38
 
39
#define L 0b10  // left
40
#define R 0b01  // right
41
#define S 0b11  // straight
42
 
43
//cidla
44
#define RSENSOR    C2OUT      // Senzory na caru
45
#define LSENSOR    C1OUT
46
#define BUMPER     PIN_A4     // Senzor na cihlu
47
 
48
#define DIAG_SERVO      PIN_B3   // Propojka pro diagnosticky mod
49
#define DIAG_SENSORS    PIN_B2   // Propojka pro diagnosticky mod
50
 
51
#DEFINE SOUND_HI   PIN_A6     // komplementarni vystupy pro piezo pipak
52
#DEFINE SOUND_LO   PIN_A7
53
 
54
char AXstring[40];   // Buffer pro prenos telemetrie
55
 
56
// makro pro PWM
57
#define GO(motor, direction, power) if(get_timer0()<=power) \
58
{direction##motor;} else {stop##motor;}
59
 
60
#int_TIMER2
61
void TIMER2_isr()
62
{
63
   if (speed<255) speed+=INC_SPEED;
64
   if (rovinka<MAX_ROVINKA) rovinka++;
65
   if (dira<=T_DIRA) dira++;
66
   if (cihla>0) cihla--;
67
}
68
// Primitivni Pipani
69
void beep(unsigned int16 period, unsigned int16 length)
70
{
71
   unsigned int16 nn;
72
 
73
   for(nn=length; nn>0; nn--)
74
   {
75
     output_high(SOUND_HI);output_low(SOUND_LO);
76
     delay_us(period);
77
     output_high(SOUND_LO);output_low(SOUND_HI);
78
     delay_us(period);
79
   }
80
}
81
/******************************************************************************/
82
void diagnostika()
83
{
84
   unsigned int16 n;
85
 
86
   while (input(DIAG_SERVO))   // Propojka, ktera spousti diagnostiku
87
   {
88
      for (n=500; n<800; n+=100)
89
      {
90
         beep(n,n); //beep UP
91
      };
92
      Delay_ms(1000);
93
      //zastav vse
94
      STOPL; STOPR;
95
      //pravy pas
96
      FR; Delay_ms(1000); STOPR; Delay_ms(1000);
97
      BR; Delay_ms(1000); STOPR; Delay_ms(1000);
98
      Beep(880,100); Delay_ms(1000);
99
      //levy pas
100
      FL; Delay_ms(1000); STOPL; Delay_ms(1000);
101
      BL; Delay_ms(1000); STOPL; Delay_ms(1000);
102
      Beep(880,100); Delay_ms(1000);
103
      //oba pasy
104
      FL; FR; Delay_ms(1000); STOPL; STOPR; Delay_ms(1000);
105
      BL; BR; Delay_ms(1000); STOPL; STOPR; Delay_ms(1000);
106
   };
107
   while (input(DIAG_SENSORS))
108
   {
109
      int ls, rs;
110
		while(!input(BUMPER)){beep(1100,100); Delay_ms(50);}
111
      set_adc_channel(RSENSOR);
112
      Delay_us(20);
113
      rs=read_adc();
114
      set_adc_channel(LSENSOR);
115
      Delay_us(20);
116
      ls=read_adc();
117
      sprintf(AXstring,"L: %U  R: %U\0", ls, rs);  // Convert DATA to String.
118
      SendPacket(&AXstring[0]);
119
      delay_ms(1000);
120
   };
121
}
122
///////////////////////////////////////////////////////////////////////////////
123
void cikcak()
124
{
125
unsigned int16 n;
126
sem1:
127
   n=CIK_CAK;
128
	while (0==RSENSOR)       // zkontroluj caru
129
	{
130
		if (n==CIK_CAK)							// zmen smer zataceni
131
		{
132
			n=0;
133
			switch(movement)
134
			{
135
			case L:
136
						FL;BR;
137
						movement=R;
138
					break;
139
			case R:
140
						FR;BL;
141
						movement=L;
142
					break;
143
         case S:
144
				     FL;BR;
145
				     movement=R;
146
                 n=CIK_CAK/2;
147
			      break;
148
			}
149
		}
150
		n++;
151
	}
152
	STOPL;STOPR;
153
   line = RSENSOR;         // cteni senzoru na caru
154
   line |= LSENSOR << 1;
155
   if (line==0) goto sem1;
156
   												// nasli jsme caru
157
	line=S;
158
}
159
///////////////////////////////////////////////////////////////////////////////
160
void objizdka()                     // objede cihlu
161
{
162
unsigned int16 n;
163
 
164
	BL;BR;Delay_ms(200);
165
	STOPR;STOPL;
166
	beep(900,1000);
167
  // movement=S;
168
  // cikcak();
169
 
170
   BR; FL; Delay_ms(215);           // otoc se 70° do prava
171
 
172
   FR; FL; Delay_ms(600);           // popojed rovne
173
 
174
   BL; Delay_ms(30);               // otoc se 90° do leva
175
   STOPL; FR; Delay_ms(600);
176
 
177
   FR; FL; Delay_ms(100);           // popojed rovne na slepo
178
   for(n=600;n>0;n--)               // popojed rovne ale kontroluj caru
179
   {
180
      line = RSENSOR;         // cteni senzoru na caru
181
      line |= LSENSOR << 1;
182
      if (line!=0) {Delay_ms(50); break;}
183
      Delay_ms(1);
184
   }
185
 
186
   BR;                              // otoc se 60° do prava
187
   for(n=600;n>0;n--)
188
   {
189
      line = RSENSOR;         // cteni senzoru na caru
190
      line |= LSENSOR << 1;
191
      if (line!=0) break;
192
      Delay_ms(1);
193
   }
194
   STOPR; STOPL;
195
 
196
   movement=R;
197
   cikcak();
198
   cihla=T_CIHLA;
199
   dira=0;
200
}
201
///////////////////////////////////////////////////////////////////////////////
202
void prejeddiru()                    // vyresi diru
203
{
204
unsigned int16 n;
205
unsigned int8 speed_dira;
206
 
207
   STOPL;STOPR;
208
   speed_dira=speed;
209
   beep(1000,500);
210
   switch (movement)                            //vrat se zpet na caru
211
   {
212
   case L:
213
         for (n=COUVANI;n>0;n--) {GO(R,B,speed_dira); Delay_ms(1);}
214
         STOPL;STOPR;
215
      break;
216
   case R:
217
         for (n=COUVANI;n>0;n--) {GO(L,B,speed_dira); Delay_ms(1);}
218
         STOPL;STOPR;
219
      break;
220
   case S:
221
         goto sem;
222
      break;
223
   }
224
   beep(800,500);
225
 
226
   line=0;
227
   FR; BL; Delay_ms(300);                 // otoc se na caru
228
   while(line==0)
229
   {
230
      line = RSENSOR;         // cteni senzoru na caru
231
      line |= LSENSOR << 1;
232
   }
233
   FL;BR; Delay_ms(60);
234
   STOPL; STOPR;
235
 
236
   FL; BR; Delay_ms(500);
237
   STOPL; STOPR;
238
 
239
   Delay_ms(1000);
240
 
241
   FR;FL;                     //popojed rovne
242
   for(n=PRES_DIRU;n>0;n--)
243
   {
244
      line = RSENSOR;         // cteni senzoru na caru
245
      line |= LSENSOR << 1;
246
      if (line!=0) break;
247
      Delay_ms(1);
248
   }
249
sem:
250
   STOPL; STOPR;
251
   movement=S;
252
   cikcak();                                    // najdi caru
253
   cihla=T_CIHLA;
254
   dira=0;
255
}
256
///////////////////////////////////////////////////////////////////////////////
257
void main()
258
{
259
   unsigned int16 n; // pro FOR
260
 
261
   STOPL; STOPR;     // prepne vystupy na ovladani motoru na output a zastavi
262
 
263
   setup_oscillator(OSC_4MHZ|OSC_INTRC);     // 4 MHz interni RC oscilator
264
 
265
   port_b_pullups(TRUE);      // pullups pro piano na diagnostiku
266
   setup_spi(FALSE);
267
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);  // Casovac pro PWM
268
 
269
   setup_timer_2(T2_DIV_BY_4,255,10);    // Casovac pro regulaci
270
                                         // preruseni kazdych 10ms
271
   setup_adc_ports(sAN2|VSS_VDD);      // nastaveni A/D prevodniku pro naraznik
272
   setup_adc(ADC_CLOCK_INTERNAL);
273
   set_adc_channel(2);
274
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);   // Casovac pro naraznik
275
   setup_ccp1(CCP_COMPARE_RESET_TIMER);
276
   CCP_1=(2^10)-1;                        // prevod kazdou 1ms
277
 
278
   setup_comparator(A0_VR_A1_VR);   // inicializace komparatoru pro cidla cary
279
   setup_vref(VREF_HIGH|TRESHOLD);        // 32 kroku od 0.25 do 0.75 Vdd
280
 
281
   Beep(1000,200);     //double beep
282
   Delay_ms(50);
283
   Beep(1000,200);
284
   Delay_ms(1000);      // 1s
285
 
286
         // povoleni rizeni rychlosti zataceni pres preruseni
287
   enable_interrupts(INT_TIMER2);
288
   enable_interrupts(GLOBAL);
289
 
290
/*---------------------------------------------------------------------------*/
291
   sensors=S;
292
   line=S;
293
   last=S;
294
   movement=S;
295
   speed=FW_POMALU;
296
 
297
   diagnostika();
298
 //  cikcak();     // toc se, abys nasel caru
299
   Delay_ms(500);
300
   Beep(1000,200);
301
   Delay_ms(500);
302
 
303
   while(true)       // hlavni smycka (jizda podle cary)
304
   {
305
      sensors = RSENSOR;         // cteni senzoru na caru
306
      sensors |= LSENSOR << 1;
307
 
308
      if ((read_adc(ADC_READ_ONLY)<BUMPER_TRESHOLD) && (cihla==0)) objizdka();
309
 
310
      switch (sensors)  // zatacej podle toho, kde vidis caru
311
      {
312
         case S:                          // rovne
313
            FL; FR;  // pokud se jede dlouho rovne, tak pridej
314
            dira=0;
315
            movement=S;
316
            continue;
317
         case L:                          // trochu vlevo
318
            GO(L, F, FW_POMALU+rovinka); GO(R, F, FW_STREDNE+rovinka);
319
            line=L;
320
            dira=0;
321
            movement=L;
322
            continue;
323
         case R:                          // trochu vpravo
324
            GO(R, F, FW_POMALU+rovinka); GO(L, F, FW_STREDNE+rovinka);
325
            line=R;
326
            dira=0;
327
            movement=R;
328
            continue;
329
         default:       // kdyz jsou obe cidla mimo caru, tak pokracuj dal
330
      }
331
   rovinka=0;
332
      if (dira>=T_DIRA) prejeddiru();
333
      if (last!=line)     // pokud si prejel caru z jedne strany na druhou stranu, tak zabrzdi
334
      {
335
         last=line;
336
         speed=FW_ZATACKA;
337
      }
338
      if (L==line)  // kdyz jsou obe cidla mimo caru, zatoc na caru
339
      {
340
         STOPL;
341
         GO(R, F, speed);
342
         movement=L;
343
      }
344
      else
345
      {
346
         STOPR;
347
         GO(L, F, speed);
348
         movement=R;
349
      }
350
   } // while(true)
351
}
352