Subversion Repositories svnkaklik

Rev

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