Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
2 kaklik 1
#include "main.h"
2
 
3
int movement;     // smer minuleho pohybu
4
int line;         // na ktere strane byla detekovana cara
5
unsigned int8 dira;     // pocitadlo pro nalezeni preruseni cary
6
unsigned int8 speed;    // rychlost zataceni
7
unsigned int8 straight; // pocitadlo pro zjisteni rovneho useku
8
 
9
// Konstanty pro dynamiku pohybu
10
#define T_DIRA       16       // po jakem case zataceni se detekuje dira
11
#define INC_SPEED    5        // prirustek rychlosti v jednom kroku
12
#define RIGHT_ANGLE  1000     // 90 stupnu
13
#define CIKCAK       20000    // 45 stupnu
14
#define BW_PO_DIRE   200      // zpetny chod po dire
15
#define FW_RYCHLE    200      // cara primo rovne
16
#define FW_POMALU    100      // trochu mimo caru vnitrni pas
17
#define FW_STREDNE   150      // trochu mimo caru vnejsi pas
18
#define TURN_MIN     90       // minimalni rychlost pri zataceni
19
#define TURN_MAX     150      // miximalni rychlost pri zataceni
20
#define BRZDENI      40       // doba zpetneho chodu v ms, aby pas stal
21
#define ROVINKA      8        // doba po kterou se musi jet rovne, aby se brzdilo
22
 
23
//motory            //Napred vypnout potom zapnout!
24
#define FR         output_low(PIN_B5); output_high(PIN_B4)  // Vpred
25
#define FL         output_low(PIN_B7); output_high(PIN_B6)
26
#define BR         output_low(PIN_B4); output_high(PIN_B5)  // Vzad
27
#define BL         output_low(PIN_B6); output_high(PIN_B7)
28
#define STOPR      output_low(PIN_B4);output_low(PIN_B5)    // Zastav
29
#define STOPL      output_low(PIN_B6);output_low(PIN_B7)
30
#define L 0b10  // left
31
#define R 0b01  // right
32
#define S 0b11  // straight
33
 
34
//cidla
35
#define RSENSOR    1       // Senzory na caru
36
#define LSENSOR    0
37
#define BUMPER     PIN_C4   // Senzor na cihlu
38
 
39
#define DIAG_SERVO      PIN_B0   // Propojka pro diagnosticky mod
40
#define DIAG_SENSORS    PIN_B1   // Propojka pro diagnosticky mod
41
 
42
#DEFINE SOUND_HI   PIN_B3     // komplementarni vystupy pro piezo pipak
43
#DEFINE SOUND_LO   PIN_B2
44
 
45
char AXstring[40];   // Buffer pro prenos telemetrie
46
 
47
// makro pro PWM
48
#define GO(motor, direction, power) if(get_timer0()<=power) \
49
                                          {direction##motor;} \
50
                                          else \
51
                                          {stop##motor;}
52
 
53
#int_TIMER1                        // This function is called every time
54
void TIMER1_isr() {                // the RTCC (timer0) overflows (255->0).
55
                                   // For this program this is apx 76 times
56
                                   // per second.
57
   if (speed<TURN_MAX) speed+=INC_SPEED;
58
   if (dira<255) dira++;
59
   if (straight<255) straight++;
60
}
61
 
62
// Primitivni Pipani
63
void beep(unsigned int16 period, unsigned int16 length)
64
{
65
   unsigned int16 nn;
66
 
67
   for(nn=length; nn>0; nn--)
68
   {
69
     output_high(SOUND_HI);output_low(SOUND_LO);
70
     delay_us(period);
71
     output_high(SOUND_LO);output_low(SOUND_HI);
72
     delay_us(period);
73
   }
74
}
75
void diagnostika()
76
{
77
   unsigned int16 n;
78
 
79
   while (input(DIAG_SERVO))   // Propojka, ktera spousti diagnostiku
80
   {
81
      for (n=500; n<800; n+=100)
82
      {
83
         beep(n,n); //beep UP
84
      };
85
      Delay_ms(1000);
86
      //zastav vse
87
      STOPL; STOPR;
88
      //pravy pas
89
      FR; Delay_ms(1000); STOPR; Delay_ms(1000);
90
      BR; Delay_ms(1000); STOPR; Delay_ms(1000);
91
      Beep(880,100); Delay_ms(1000);
92
      //levy pas
93
      FL; Delay_ms(1000); STOPL; Delay_ms(1000);
94
      BL; Delay_ms(1000); STOPL; Delay_ms(1000);
95
      Beep(880,100); Delay_ms(1000);
96
      //oba pasy
97
      FL; FR; Delay_ms(1000); STOPL; STOPR; Delay_ms(1000);
98
      BL; BR; Delay_ms(1000); STOPL; STOPR; Delay_ms(1000);
99
   };
100
 
101
   while (input(DIAG_SENSORS))
102
   {
103
      int ls, rs;
104
		while(!input(BUMPER)){beep(1100,100); Delay_ms(50);}
105
      set_adc_channel(RSENSOR);
106
      Delay_us(20);
107
      rs=read_adc();
108
      set_adc_channel(LSENSOR);
109
      Delay_us(20);
110
      ls=read_adc();
111
   };
112
}
113
void main()
114
{
115
   STOPL; STOPR;     // prepne vystupy na ovladani motoru na output a zastavi
116
 
117
   setup_adc_ports(RA0_RA1_RA3_ANALOG);
118
   setup_adc(ADC_CLOCK_DIV_2);
119
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);  // Casovac pro PWM
120
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);   // Casovac pro regulaci
121
   setup_timer_2(T2_DISABLED,0,1);
122
   setup_ccp1(CCP_OFF);
123
 
124
   diagnostika();
125
 
126
   Beep(1000,200);     //double beep
127
   Delay_ms(50);
128
   Beep(1000,200);
129
   Delay_ms(1000);      // 1s
130
 
131
 
132
   speed=TURN_MIN;      // povoleni rizeni rychlosti zataceni pres preruseni
133
   enable_interrupts(INT_TIMER1);
134
   enable_interrupts(GLOBAL);
135
 
136
// Cik-Cak -------------------------------------------------------------
137
   line=S;
138
   movement=R;
139
 //  cik_cak();     // toc se, abys nasel caru
140
   Delay_ms(500);
141
   Beep(1000,200);
142
   Delay_ms(500);
143
 
144
   dira=0;           // inicializace globalnich promennych
145
   straight=255;
146
 
147
   while(true)       // hlavni smycka (jizda podle cary)
148
   {
149
      set_adc_channel(LSENSOR);								// kdyz cara nebyla pod pravym cidlem, mozna bude pod levym
150
      Delay_us(10);
151
      if(tresholdL > read_adc())
152
		{
153
            movement = L;
154
            if (straight>ROVINKA)
155
             {GO(R, F, FW_STREDNE+40); GO(L, F, FW_POMALU+40)} // pridej
156
            else
157
             {GO(R, F, FW_STREDNE); GO(L, F, FW_POMALU)};
158
            speed=TURN_MIN;
159
            dira=0;
160
				line=L;
161
            continue;
162
		}		
163
      set_adc_channel(RSENSOR);								// podivej se jestli neni cara pod pravym cidlem
164
      Delay_us(10);
165
      if(tresholdR > read_adc())
166
		{
167
            movement = R;
168
            if (straight>ROVINKA)
169
             {GO(L, F, FW_STREDNE+40); GO(R, F, FW_POMALU+40)} // pridej
170
            else
171
             {GO(L, F, FW_STREDNE); GO(R, F, FW_POMALU)};
172
            speed=TURN_MIN;
173
            dira=0;
174
				line=R;
175
            continue;
176
		}
177
		if(line==S)
178
            movement = S;
179
            if (straight>ROVINKA)
180
             {FL; FR;}  // pokud se jede dlouho rovne, tak pridej
181
            else
182
             {GO(R, F, FW_RYCHLE); GO(L, F, FW_RYCHLE)};
183
            speed=TURN_MIN;   // nastav minimalni rychlost pro zataceni
184
            dira=0;           // protoze byla cara, tak nuluj pocitadlo diry
185
            continue;
186
 
187
 
188
      if (straight>ROVINKA)     // pokud byla dlouha rovinka, tak zabrzdi
189
      {
190
            BL; BR;
191
            Delay_ms(BRZDENI);
192
            STOPL;
193
            STOPR;
194
            dira=0;
195
      };
196
      straight=0;    // nuluj pocitadlo rovinky
197
 
198
      if (L==movement)  // kdyz jsou obe cidla mimo caru, zatoc na caru
199
      {
200
         GO(R, F, speed);
201
         STOPL;
202
      }
203
      else
204
      {
205
         GO(L, F, speed);
206
         STOPR;
207
      }
208
 
209
      if (dira>T_DIRA)  // pokud se moc dlouho zataci bez detekce cary, vrat se
210
      {
211
         STOPL;
212
         STOPR;
213
         Beep(1000,200);     //double beep
214
         Delay_ms(30);
215
         Beep(2000,200);
216
         Delay_ms(30);
217
         if (L==movement)  // zpet, podle toho kam se jelo
218
         {
219
            STOPL;
220
            BR;
221
         }
222
         else
223
         {
224
            STOPR;
225
            BL;
226
         };
227
         Delay_ms(BW_PO_DIRE);
228
         STOPL;
229
         STOPR;
230
         cik_cak();        // najdi caru
231
         dira=0;
232
      }
233
 
234
   } // while(true)
235
}
236