Subversion Repositories svnkaklik

Rev

Rev 209 | 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 240 2007-04-19 06:14:36Z 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
 
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
   };
66
   if (GRAVITY) {if (g<255) g++;} else g=0;
67
   if (g>3 && !diag) {FL; FR; while(TRUE);}; // kdyz nas preklopi, nedej se
68
}
69
 
70
// Primitivni Pipani
71
void beep(unsigned int16 period, unsigned int16 length)
72
{
73
   unsigned int16 nn;
74
 
75
   disable_interrupts(GLOBAL);
76
   for(nn=length; nn>0; nn--)
77
   {
78
     output_high(SOUND_HI);output_low(SOUND_LO);
79
     delay_us(period);
80
     output_high(SOUND_LO);output_low(SOUND_HI);
81
     delay_us(period);
82
   }
83
   enable_interrupts(GLOBAL);
84
}
85
 
240 kakl 86
/**** DIAG ********************************************************************/
203 kakl 87
inline void diagnostika()
88
{
89
   unsigned int16 n;
90
 
209 kakl 91
   if (GRAVITY)
203 kakl 92
   {
209 kakl 93
      diag=TRUE;
94
      enable_interrupts(INT_TIMER0);
95
      enable_interrupts(GLOBAL);
96
      while (true)         // Diagnostika cidel
97
      {
98
         if (g>100) beep(800,100);
99
         Delay_ms(50);
100
         if (arena_l) {beep(1000,200); delay_ms(10);beep(1000,200);};
101
         Delay_ms(50);
102
         if (arena_r) {beep(2000,300); delay_ms(10);beep(2000,300);};
103
         Delay_ms(50);
104
 
105
         if (sr>10) beep(3000,400);
106
         Delay_ms(50);
107
         if (f>10) beep(4000,500);
108
         Delay_ms(50);
109
         if (sl>10) beep(5000,500);
110
         Delay_ms(50);
111
         if (b>10) beep(6000,600);
112
         Delay_ms(50);
240 kakl 113
         if((g>100) && arena_l && arena_r) break;  // Preklopen na zada a bily papir na obou cidlech na okraj areny
114
      };
115
 
116
      while (TRUE)   // Diagnostika podvozku
117
      {
118
         for (n=500; n<800; n+=100)
119
         {
120
            beep(n,n); //beep UP
121
         };
122
         Delay_ms(1000);
123
         //zastav vse
124
         STOPL; STOPR;
125
         //pravy pas
126
         FR; Delay_ms(1000); STOPR; Delay_ms(1000);
127
         BR; Delay_ms(1000); STOPR; Delay_ms(1000);
128
         Beep(880,100); Delay_ms(1000);
129
         //levy pas
130
         FL; Delay_ms(1000); STOPL; Delay_ms(1000);
131
         BL; Delay_ms(1000); STOPL; Delay_ms(1000);
132
         Beep(880,100); Delay_ms(1000);
133
         //oba pasy
134
         FL; FR; Delay_ms(1000); STOPL; STOPR; Delay_ms(1000);
135
         BL; BR; Delay_ms(1000); STOPL; STOPR; Delay_ms(1000);
136
      };
209 kakl 137
   };
203 kakl 138
}
139
 
240 kakl 140
/**** MAIN ********************************************************************/
203 kakl 141
void main()
142
{
143
   unsigned int16 n; // for FOR
144
 
145
   STOPL; STOPR;     // zastavi motory
146
 
147
   setup_oscillator(OSC_8MHZ|OSC_INTRC);     // CPU clock 8MHz
240 kakl 148
   setup_adc_ports(NO_ANALOGS);   // komparatory vypnuty
209 kakl 149
   setup_adc(ADC_OFF);
203 kakl 150
   setup_spi(FALSE);
240 kakl 151
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);  // Casovac pro SW PWM a cteni cidel
203 kakl 152
   setup_timer_1(T1_DISABLED);
153
   setup_timer_2(T2_DIV_BY_1,54,1); // Casovac pro PWM pro IR sensory cca 36kHz
154
   setup_ccp1(CCP_PWM);    // HW PWM ON
155
   set_pwm1_duty(27);      // 1:1
240 kakl 156
   setup_comparator(A3_VR_A2_VR);   // inicializace komparatoru
157
   setup_vref(VREF_HIGH|THRESHOLD);        // 16 kroku od 0.25 do 0.75 Vdd
203 kakl 158
 
159
   Beep(1000,200);     //double beep
160
   Delay_ms(50);
161
   Beep(1000,200);
162
   diagnostika();
163
 
164
   enable_interrupts(INT_TIMER0);
165
   enable_interrupts(GLOBAL);
240 kakl 166
 
209 kakl 167
   for (n=1;n<=5;n++)   // 5s do zacatku souboje
203 kakl 168
   {
209 kakl 169
      Delay_ms(720);
203 kakl 170
      Beep(1000,200);
171
   }
172
 
240 kakl 173
/*----- Main Loop ------------------------------------------------------------*/
203 kakl 174
   while(true)       // hlavni smycka
175
   {
176
LOOP:
177
 
178
      GO(L, F, 150); GO(R, F, 150);
179
 
180
      if (arena_r)
181
      {
182
         BL; BR;
183
         delay_ms(100);
184
         STOPL; BR;
185
         for(n=0; n<5000; n++)
186
         {
187
            if (!arena_r || arena_l) {BL; BR;};
188
         };
189
         FL; BR;
190
         delay_ms(100);
191
         STOPL; STOPR;
192
      }
193
 
194
      if (arena_l)
195
      {
196
         BL; BR;
197
         delay_ms(100);
198
         BL; STOPR;
199
         for(n=0; n<5000; n++)
200
         {
201
            if (!arena_l || arena_r) {BL; BR;};
202
         };
203
         BL; FR;
204
         delay_ms(100);
205
         STOPL; STOPR;
206
      }
207
 
208
      if (sr>10)     // Nepritel vpravo
209
      {
210
         FL; FR;                 // popojed rovne
211
         for(n=0; n<5000; n++)
212
         {
213
            if (arena_l || arena_r) {BL; BR; delay_ms(100); goto LOOP;};
214
         };
215
         FL; BR;                     // otoc se na nej
216
         for(n=0; n<10000; n++)
217
         {
218
            if (arena_l || arena_r) {BL; BR; delay_ms(100); goto LOOP;};
219
            if (f>5)
220
            {
221
               FL; FR;               // vytlac ho
222
            };
223
            if (sl>5) {BL; FR;};
224
            if (sr>5) {FL; BR;};
225
         };
226
      }
227
 
228
      if (sl>10)     // Nepritel vlevo
229
      {
230
         FL; FR;                 // popojed rovne
231
         for(n=0; n<5000; n++)
232
         {
233
            if (arena_l || arena_r) {BL; BR; delay_ms(100); goto LOOP;};
234
         };
235
         BL; FR;                    // otoc se na nej
236
         for(n=0; n<10000; n++)
237
         {
238
            if (arena_l || arena_r) {BL; BR; delay_ms(100); goto LOOP;};
239
            if (f>5)
240
            {
241
               FL; FR;              // vytlac ho
242
            };
243
            if (sl>5) {BL; FR;};
244
            if (sr>5) {FL; BR;};
245
         };
246
      }
247
 
248
      if (f>10)      // Nepritel vpredu
249
      {
250
         BL; FR;
251
         delay_ms(110);
252
         FL; BR;
253
         delay_ms(50);
254
         STOPL; STOPR;
255
      }
256
 
257
      if (b>10)      // Nepritel vzadu
258
      {
259
         BL; FR;
260
         delay_ms(110);
261
         FL; BR;
262
         delay_ms(50);
263
         STOPL; STOPR;
264
      }
265
 
266
   } // while(true)
267
}