Subversion Repositories svnkaklik

Rev

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

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