Subversion Repositories svnkaklik

Rev

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

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