Subversion Repositories svnkaklik

Rev

Rev 251 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
203 kakl 1
// Program pro MiniSumo na R-Day 2006
206 kakl 2
//"$Id: cholerik.c 361 2007-09-09 15:09:55Z kakl $"
205 kakl 3
 
203 kakl 4
#include "cholerik.h"
5
 
243 kakl 6
#define THRESHOLD     15     // rozhodovaci uroven pro okraj areny
361 kakl 7
                             // 0-15 (od 0.25 do 0.75 Vdd)
8
                             // 0V cerna; 5V bila
203 kakl 9
 
361 kakl 10
#define KOLIKRAT     3//10
11
 
203 kakl 12
//motory            //Napred vypnout potom zapnout!
13
#define FR         output_low(PIN_B5); output_high(PIN_B4)  // Vpred
14
#define FL         output_low(PIN_B7); output_high(PIN_B6)
15
#define BR         output_low(PIN_B4); output_high(PIN_B5)  // Vzad
16
#define BL         output_low(PIN_B6); output_high(PIN_B7)
17
#define STOPR      output_low(PIN_B4);output_low(PIN_B5)    // Zastav
18
#define STOPL      output_low(PIN_B6);output_low(PIN_B7)
19
 
20
//cidla
21
#define SIDE_R    !input(PIN_A7)     // Sensory na soupere
22
#define SIDE_L    !input(PIN_A4)
23
#define FRONT     !input(PIN_A6)
24
#define BACK      !input(PIN_B3)
25
#define GRAVITY   !input(PIN_A1)
240 kakl 26
// PIN_A3   Prave cidlo na okraj areny (C1OUT)
27
// PIN_A2   Leve cidlo na okraj areny (C2OUT)
203 kakl 28
 
29
#DEFINE SOUND_HI   PIN_B1     // komplementarni vystupy pro piezo pipak
30
#DEFINE SOUND_LO   PIN_B2
31
 
32
// makro pro PWM
33
#define GO(motor, direction, power) if(get_timer0()<=power) \
34
{direction##motor;} else {stop##motor;}
35
 
36
unsigned int8 majak=0;
37
unsigned int8 sl=0;
38
unsigned int8 sr=0;
39
unsigned int8 b=0;
40
unsigned int8 f=0;
41
unsigned int8 g=0;
240 kakl 42
int1  arena_l=FALSE;
43
int1  arena_r=FALSE;
203 kakl 44
int1 diag=FALSE;
45
 
46
#int_TIMER0
47
TIMER0_isr()
48
{
49
   int1 stav;
50
 
51
   stav = ((majak & 0b1) == 0b1);
52
   if (((SIDE_R && stav) || (!SIDE_R && !stav))) {if (sr<255) sr++;} else {sr=0;};
53
   if (((SIDE_L && stav) || (!SIDE_L && !stav))) {if (sl<255) sl++;} else {sl=0;};
54
   if (((BACK && stav) || (!BACK && !stav))) {if (b<255) b++;} else {b=0;};
55
   if (((FRONT && stav) || (!FRONT && !stav))) {if (f<255) f++;} else {f=0;};
56
   majak++;
57
   stav = ((majak & 0b1) == 0b1);
240 kakl 58
 
242 kakl 59
   if (!C1OUT) arena_r=TRUE; else arena_r=FALSE;
60
   if (!C2OUT) arena_l=TRUE; else arena_l=FALSE;
61
 
203 kakl 62
   if (stav)
63
   {
64
      set_pwm1_duty(27);      // 1:1
65
   }
66
   else
67
   {
68
      set_pwm1_duty(55);      // 1:0
69
   };
242 kakl 70
   setup_comparator(NC_NC_NC_NC);   // inicializace komparatoru
203 kakl 71
   if (GRAVITY) {if (g<255) g++;} else g=0;
242 kakl 72
   setup_comparator(A3_VR_A2_VR);   // inicializace komparatoru
203 kakl 73
   if (g>3 && !diag) {FL; FR; while(TRUE);}; // kdyz nas preklopi, nedej se
74
}
75
 
76
// Primitivni Pipani
77
void beep(unsigned int16 period, unsigned int16 length)
78
{
79
   unsigned int16 nn;
80
 
81
   disable_interrupts(GLOBAL);
82
   for(nn=length; nn>0; nn--)
83
   {
84
     output_high(SOUND_HI);output_low(SOUND_LO);
85
     delay_us(period);
86
     output_high(SOUND_LO);output_low(SOUND_HI);
87
     delay_us(period);
88
   }
89
   enable_interrupts(GLOBAL);
90
}
91
 
240 kakl 92
/**** DIAG ********************************************************************/
203 kakl 93
inline void diagnostika()
94
{
95
   unsigned int16 n;
96
 
242 kakl 97
   setup_comparator(NC_NC_NC_NC);   // inicializace komparatoru
209 kakl 98
   if (GRAVITY)
203 kakl 99
   {
242 kakl 100
      setup_comparator(A3_VR_A2_VR);   // inicializace komparatoru
209 kakl 101
      diag=TRUE;
102
      enable_interrupts(INT_TIMER0);
103
      enable_interrupts(GLOBAL);
104
      while (true)         // Diagnostika cidel
105
      {
106
         if (g>100) beep(800,100);
107
         Delay_ms(50);
108
         if (arena_l) {beep(1000,200); delay_ms(10);beep(1000,200);};
109
         Delay_ms(50);
110
         if (arena_r) {beep(2000,300); delay_ms(10);beep(2000,300);};
111
         Delay_ms(50);
112
 
113
         if (sr>10) beep(3000,400);
114
         Delay_ms(50);
115
         if (f>10) beep(4000,500);
116
         Delay_ms(50);
117
         if (sl>10) beep(5000,500);
118
         Delay_ms(50);
119
         if (b>10) beep(6000,600);
120
         Delay_ms(50);
240 kakl 121
         if((g>100) && arena_l && arena_r) break;  // Preklopen na zada a bily papir na obou cidlech na okraj areny
122
      };
123
 
242 kakl 124
      while(TRUE)   // Diagnostika podvozku
240 kakl 125
      {
126
         for (n=500; n<800; n+=100)
127
         {
128
            beep(n,n); //beep UP
129
         };
130
         Delay_ms(1000);
131
         //zastav vse
132
         STOPL; STOPR;
133
         //pravy pas
134
         FR; Delay_ms(1000); STOPR; Delay_ms(1000);
135
         BR; Delay_ms(1000); STOPR; Delay_ms(1000);
136
         Beep(880,100); Delay_ms(1000);
137
         //levy pas
138
         FL; Delay_ms(1000); STOPL; Delay_ms(1000);
139
         BL; Delay_ms(1000); STOPL; Delay_ms(1000);
140
         Beep(880,100); Delay_ms(1000);
141
         //oba pasy
142
         FL; FR; Delay_ms(1000); STOPL; STOPR; Delay_ms(1000);
143
         BL; BR; Delay_ms(1000); STOPL; STOPR; Delay_ms(1000);
144
      };
209 kakl 145
   };
203 kakl 146
}
147
 
240 kakl 148
/**** MAIN ********************************************************************/
203 kakl 149
void main()
150
{
151
   unsigned int16 n; // for FOR
152
 
153
   STOPL; STOPR;     // zastavi motory
154
 
155
   setup_oscillator(OSC_8MHZ|OSC_INTRC);     // CPU clock 8MHz
240 kakl 156
   setup_adc_ports(NO_ANALOGS);   // komparatory vypnuty
209 kakl 157
   setup_adc(ADC_OFF);
203 kakl 158
   setup_spi(FALSE);
240 kakl 159
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);  // Casovac pro SW PWM a cteni cidel
203 kakl 160
   setup_timer_1(T1_DISABLED);
161
   setup_timer_2(T2_DIV_BY_1,54,1); // Casovac pro PWM pro IR sensory cca 36kHz
162
   setup_ccp1(CCP_PWM);    // HW PWM ON
163
   set_pwm1_duty(27);      // 1:1
240 kakl 164
   setup_comparator(A3_VR_A2_VR);   // inicializace komparatoru
242 kakl 165
   setup_comparator(NC_NC_NC_NC);   // inicializace komparatoru
240 kakl 166
   setup_vref(VREF_HIGH|THRESHOLD);        // 16 kroku od 0.25 do 0.75 Vdd
203 kakl 167
 
168
   Beep(1000,200);     //double beep
169
   Delay_ms(50);
170
   Beep(1000,200);
171
   diagnostika();
172
 
173
   enable_interrupts(INT_TIMER0);
174
   enable_interrupts(GLOBAL);
240 kakl 175
 
209 kakl 176
   for (n=1;n<=5;n++)   // 5s do zacatku souboje
203 kakl 177
   {
244 kakl 178
      Delay_ms(670); // 5,25s
203 kakl 179
      Beep(1000,200);
180
   }
181
 
240 kakl 182
/*----- Main Loop ------------------------------------------------------------*/
203 kakl 183
   while(true)       // hlavni smycka
184
   {
185
LOOP:
186
 
187
      GO(L, F, 150); GO(R, F, 150);
188
 
189
      if (arena_r)
190
      {
191
         BL; BR;
251 kaklik 192
         delay_ms(180);
203 kakl 193
         STOPL; BR;
194
         for(n=0; n<5000; n++)
195
         {
196
            if (!arena_r || arena_l) {BL; BR;};
197
         };
198
         FL; BR;
199
         delay_ms(100);
200
         STOPL; STOPR;
201
      }
202
 
203
      if (arena_l)
204
      {
205
         BL; BR;
251 kaklik 206
         delay_ms(180);
203 kakl 207
         BL; STOPR;
208
         for(n=0; n<5000; n++)
209
         {
210
            if (!arena_l || arena_r) {BL; BR;};
211
         };
212
         BL; FR;
213
         delay_ms(100);
214
         STOPL; STOPR;
215
      }
216
 
361 kakl 217
      if (sr>KOLIKRAT)     // Nepritel vpravo
203 kakl 218
      {
219
         FL; FR;                 // popojed rovne
220
         for(n=0; n<5000; n++)
221
         {
222
            if (arena_l || arena_r) {BL; BR; delay_ms(100); goto LOOP;};
223
         };
224
         FL; BR;                     // otoc se na nej
225
         for(n=0; n<10000; n++)
226
         {
227
            if (arena_l || arena_r) {BL; BR; delay_ms(100); goto LOOP;};
228
            if (f>5)
229
            {
230
               FL; FR;               // vytlac ho
231
            };
232
            if (sl>5) {BL; FR;};
233
            if (sr>5) {FL; BR;};
234
         };
235
      }
236
 
361 kakl 237
      if (sl>KOLIKRAT)     // Nepritel vlevo
203 kakl 238
      {
239
         FL; FR;                 // popojed rovne
240
         for(n=0; n<5000; n++)
241
         {
242
            if (arena_l || arena_r) {BL; BR; delay_ms(100); goto LOOP;};
243
         };
244
         BL; FR;                    // otoc se na nej
245
         for(n=0; n<10000; n++)
246
         {
247
            if (arena_l || arena_r) {BL; BR; delay_ms(100); goto LOOP;};
248
            if (f>5)
249
            {
250
               FL; FR;              // vytlac ho
251
            };
252
            if (sl>5) {BL; FR;};
253
            if (sr>5) {FL; BR;};
254
         };
255
      }
256
 
361 kakl 257
      if (f>KOLIKRAT)      // Nepritel vpredu
203 kakl 258
      {
259
         BL; FR;
260
         delay_ms(110);
261
         FL; BR;
262
         delay_ms(50);
263
         STOPL; STOPR;
264
      }
265
 
361 kakl 266
      if (b>KOLIKRAT)      // Nepritel vzadu
203 kakl 267
      {
268
         BL; FR;
269
         delay_ms(110);
270
         FL; BR;
271
         delay_ms(50);
272
         STOPL; STOPR;
273
      }
274
 
275
   } // while(true)
276
}