Subversion Repositories svnkaklik

Rev

Rev 251 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log

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