Subversion Repositories svnkaklik

Rev

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