Rev Author Line No. Line
708 cizelu 1 #include "main.h"
700 cizelu 2  
708 cizelu 3 // NEPOUZIVAT PINY B6 A B7, JSOU VYHRAZENY PRO SERIOVOU KOMUNIKACI
789 cizelu 4 // BAUD RATE = 9600
708 cizelu 5  
789 cizelu 6 // univerzalni LED diody
708 cizelu 7 #define LED1 PIN_A4
8 #define LED2 PIN_A5
700 cizelu 9  
789 cizelu 10 // piezo pipak
716 cizelu 11 #DEFINE SOUND_HI PIN_B4
12 #DEFINE SOUND_LO PIN_B5
700 cizelu 13  
789 cizelu 14 // radkovy senzor
745 cizelu 15 #define SDIN PIN_D4 // seriovy vstup
16 #define SDOUT input(PIN_B2) // seriovy vystup
17 #define SCLK PIN_D5 // takt
727 cizelu 18  
789 cizelu 19 // pro komunikaci s OLSA, prvni se posila LSB
20 int main_reset[8]={1,1,0,1,1,0,0,0}; // hlavni reset 0x1B
21 int set_mode_rg[8]={1,1,1,1,1,0,1,0}; // zapis do MODE registru 0x5F
22 int clear_mode_rg[8]={0,0,0,0,0,0,0,0}; // nulovani MODE registru 0x00
730 cizelu 23  
789 cizelu 24 int left_offset[8]={0,0,0,0,0,0,1,0}; // offset leveho segmentu senzoru 0x40
25 int mid_offset[8]={0,1,0,0,0,0,1,0}; // offset prostredniho segmentu senzoru 0x42
26 int right_offset[8]={0,0,1,0,0,0,1,0}; // offset praveho segmentu senzoru 0x44
27 int offset[8]={1,0,0,0,0,0,0,1}; // minus jedna - pouzit pro vsechny segmenty 0x81
741 cizelu 28  
789 cizelu 29 int left_gain[8]={1,0,0,0,0,0,1,0}; // zisk leveho segmentu 0x41
30 int mid_gain[8]={1,1,0,0,0,0,1,0}; // zisk leveho segmentu 0x43
31 int right_gain[8]={1,0,1,0,0,0,1,0}; // zisk leveho segmentu 0x45
32 int gain[8]={1,0,1,0,0,0,0,0}; // zisk = 5 - pouzit pro vsechny segmenty 0x5
771 kaklik 33  
789 cizelu 34 int start_int[8]={0,0,0,1,0,0,0,0}; // zacatek integrace 0x08
35 int stop_int[8]={0,0,0,0,1,0,0,0}; // konec integrace 0x10
36 int readout[8]={0,1,0,0,0,0,0,0}; // cteni senzoru 0x02
37  
38 int olsa_lseg[51]={0}; // leva cast radky (pixely 0 - 50)
790 cizelu 39 int olsa_rseg[51]={0}; // prava cast radky (pixely 51 - 101)
789 cizelu 40 int8 *line_lp=&olsa_lseg; // ukazatel na levou cast radky
41 int8 *line_rp=&olsa_rseg; // ukazatel na pravou cast radky
790 cizelu 42 int8 pixel; // dec hodnota jednoho pixelu
43 int8 pixel_count; // cislo pixelu 0 - 101
789 cizelu 44  
700 cizelu 45 //naraznik
708 cizelu 46 #define BUMPL input(PIN_D6)
47 #define BUMPR input(PIN_D7)
700 cizelu 48  
49 //nouzove senzory
708 cizelu 50 #define LINEL 0
51 #define LINER 1
745 cizelu 52 #define TRESHOLD 200 // rozhodovaci uroven (zmereno 0 - cerna, 255 cerna)
789 cizelu 53 int8 line_l;
54 int8 line_r;
55 int1 line_position;
700 cizelu 56  
57 // motory
708 cizelu 58 #define LMF PIN_D0
59 #define LMB PIN_D1
60 #define RMF PIN_D2
61 #define RMB PIN_D3
700 cizelu 62  
790 cizelu 63 int8 lm_speed;
64 int8 rm_speed;
65  
700 cizelu 66 //PODPROGRAMY
67 //SENZORY
730 cizelu 68 //OLSA01A
745 cizelu 69 void olsa_pulses(int count) // vytvori impulzy pro ridici logiku
730 cizelu 70 {
741 cizelu 71 int8 ct;
72 for(ct=0;ct<=count;ct++)
730 cizelu 73 {
745 cizelu 74 delay_us(1); // doba pro ustaleni
730 cizelu 75 output_high(SCLK);
745 cizelu 76 delay_us(1); // doba pro ustaleni
730 cizelu 77 output_low(SCLK);
78 }
79 }
80  
745 cizelu 81 void olsa_pulse() // vytvori jeden impulz
730 cizelu 82 {
745 cizelu 83 delay_us(1); // doba pro ustaleni
741 cizelu 84 output_high(SCLK);
745 cizelu 85 delay_us(1); // doba pro ustaleni
741 cizelu 86 output_low(SCLK);
87 }
88  
89 void olsa_send(int info[]) // USART komunikace s modulem OLSA01A - poslani zpravy
90 {
790 cizelu 91 int8 *ip=&info; // ukazatel na pole s informaci
92 int i; // pomocna promenna pro nastaveni 0 nebo 1 na SDIN
741 cizelu 93  
94 output_low(SDIN); // start bit
95 olsa_pulse();
96 for(ip=0;ip<8;ip++) // predani informace - 8 bit, LSB prvni > MSB posledni
730 cizelu 97 {
741 cizelu 98 i=info[ip]; // ziskani hodnoty z pole
99 if(i==1) // vyhodnoceni obsahu informace - nastav 1
730 cizelu 100 {
101 output_high(SDIN);
102 }
741 cizelu 103 else // vyhodnoceni obsahu informace - nastav 0
730 cizelu 104 {
105 output_low(SDIN);
106 }
741 cizelu 107 olsa_pulse();
730 cizelu 108 }
741 cizelu 109 output_high(SDIN); // stop bit
110 olsa_pulse();
730 cizelu 111 }
112  
741 cizelu 113 void olsa_reset() // hlavni RESET - provadi se po zapnuti
114 {
115 output_low(SDIN);
116 output_low(SCLK);
117 olsa_pulses(30); // reset radkoveho senzoru
118 output_high(SDIN);
119 olsa_pulses(10); // start bit - synchronizace
120 olsa_send(main_reset);
121 olsa_pulses(5);
122 olsa_send(set_mode_rg);
123 olsa_send(clear_mode_rg);
124 }
789 cizelu 125  
126 void olsa_setup() // kompletni nastaveni, provadi se po resetu
127 {
128 olsa_send(left_offset); // nastaveni leveho segmentu (offset a zisk)
129 olsa_send(offset);
130 olsa_send(left_gain);
131 olsa_send(gain);
132 olsa_send(mid_offset); // nastaveni prostredniho segmentu (offset a zisk)
133 olsa_send(offset);
134 olsa_send(mid_gain);
135 olsa_send(gain);
136 olsa_send(right_offset); // nastaveni praveho segmentu (offset a zisk)
137 olsa_send(offset);
138 olsa_send(right_gain);
139 olsa_send(gain);
140 }
790 cizelu 141  
142 void olsa_integration() // snimani pixelu
143 {
144 olsa_send(start_int); // zacatek integrace senzoru
145 olsa_pulses(22);
146 olsa_send(stop_int); // konec integrace senzoru
147 olsa_pulses(5);
148 }
741 cizelu 149  
150 //ZACHRANNE SENZORY
790 cizelu 151 void read_blue_sensors() // cteni nouzovych senzoru
708 cizelu 152 {
790 cizelu 153 set_adc_channel(LINEL); // cti levy nouzovy senzor
700 cizelu 154 delay_us(10);
745 cizelu 155 line_l=read_adc();
790 cizelu 156 set_adc_channel(LINER); // cti pravy nouzovy senzor
700 cizelu 157 delay_us(10);
158 line_r=read_adc();
708 cizelu 159 }
730 cizelu 160  
700 cizelu 161 //PIPAK
716 cizelu 162 void beep(int16 period,int16 length)
730 cizelu 163 {
790 cizelu 164 int16 bp; //promenna pro nastaveni delky
700 cizelu 165  
166 for(bp=length;bp>0;bp--)
730 cizelu 167 {
168 output_high(SOUND_HI);
169 output_low(SOUND_LO);
170 delay_us(period);
171 output_high(SOUND_LO);
172 output_low(SOUND_HI);
173 delay_us(period);
174 }
175 }
708 cizelu 176 //MOTORY
790 cizelu 177 void l_motor_fwd(int8 speedl) // levy motor dopredu
730 cizelu 178 {
708 cizelu 179 output_high(LMF);
180 output_low(LMB);
181 set_pwm2_duty(speedl);
730 cizelu 182 }
700 cizelu 183  
790 cizelu 184 void l_motor_bwd(int8 speedl) // levy motor dozadu
730 cizelu 185 {
708 cizelu 186 output_high(LMB);
187 output_low(LMF);
188 set_pwm2_duty(speedl);
730 cizelu 189 }
708 cizelu 190  
790 cizelu 191 void r_motor_fwd(int8 speedr) // pravy motor dopredu
730 cizelu 192 {
708 cizelu 193 output_high(RMF);
194 output_low(RMB);
195 set_pwm1_duty(speedr);
730 cizelu 196 }
708 cizelu 197  
790 cizelu 198 void r_motor_bwd(int8 speedr) // pravy motor dozadu
730 cizelu 199 {
708 cizelu 200 output_high(RMB);
201 output_low(RMF);
202 set_pwm1_duty(speedr);
730 cizelu 203 }
708 cizelu 204  
790 cizelu 205 void l_motor_off() // levy motor vypnut
730 cizelu 206 {
708 cizelu 207 output_low(LMF);
208 output_low(LMB);
209 set_pwm2_duty(0);
730 cizelu 210 }
211  
790 cizelu 212 void r_motor_off() // pravy motor vypnut
730 cizelu 213 {
708 cizelu 214 output_low(RMF);
215 output_low(RMB);
216 set_pwm1_duty(0);
730 cizelu 217 }
708 cizelu 218  
790 cizelu 219 void motor_test() // test motoru
730 cizelu 220 {
708 cizelu 221 int8 i;
716 cizelu 222 beep(100,200);
708 cizelu 223 printf("TEST MOTORU\n");
224 delay_ms(1000);
716 cizelu 225 printf("LEVY MOTOR DOPREDU\n");
708 cizelu 226 for(i=0;i<255;i++)
730 cizelu 227 {
708 cizelu 228 l_motor_fwd(i);
229 printf("RYCHLOST: %u\n",i);
716 cizelu 230 delay_ms(5);
730 cizelu 231 }
708 cizelu 232 for(i=255;i>0;i--)
730 cizelu 233 {
708 cizelu 234 l_motor_fwd(i);
235 printf("RYCHLOST: %u\n",i);
716 cizelu 236 delay_ms(5);
730 cizelu 237 }
716 cizelu 238 printf("LEVY MOTOR DOZADU\n");
708 cizelu 239 for(i=0;i<255;i++)
730 cizelu 240 {
708 cizelu 241 l_motor_bwd(i);
242 printf("RYCHLOST: %u\n",i);
716 cizelu 243 delay_ms(5);
730 cizelu 244 }
708 cizelu 245 for(i=255;i>0;i--)
730 cizelu 246 {
708 cizelu 247 l_motor_bwd(i);
248 printf("RYCHLOST: %u\n",i);
716 cizelu 249 delay_ms(5);
730 cizelu 250 }
716 cizelu 251 printf("PRAVY MOTOR DOPREDU\n");
708 cizelu 252 for(i=0;i<255;i++)
730 cizelu 253 {
708 cizelu 254 r_motor_fwd(i);
716 cizelu 255 printf("RYCHLOST: %u\n",i);
256 delay_ms(5);
730 cizelu 257 }
708 cizelu 258 for(i=255;i>0;i--)
730 cizelu 259 {
708 cizelu 260 r_motor_fwd(i);
261 printf("RYCHLOST: %u\n",i);
716 cizelu 262 delay_ms(5);
730 cizelu 263 }
716 cizelu 264 printf("PRAVY MOTOR DOZADU\n");
708 cizelu 265 for(i=0;i<255;i++)
730 cizelu 266 {
708 cizelu 267 r_motor_bwd(i);
268 printf("RYCHLOST: %u\n",i);
716 cizelu 269 delay_ms(5);
730 cizelu 270 }
708 cizelu 271 for(i=255;i>0;i--)
730 cizelu 272 {
708 cizelu 273 r_motor_bwd(i);
274 printf("RYCHLOST: %u\n",i);
716 cizelu 275 delay_ms(5);
730 cizelu 276 }
277 printf("KONEC TESTU MOTORU \N");
708 cizelu 278 }
279  
790 cizelu 280 void diagnostika() // diagnostika - vypis senzoru s moznosti prepnuti na test motoru
708 cizelu 281 {
282 read_blue_sensors();
283 printf("LEVA: %u \t",line_l);
789 cizelu 284 delay_ms(10);
708 cizelu 285 printf("PRAVA: %u \t",line_r);
789 cizelu 286 delay_ms(10);
708 cizelu 287 printf("L_NARAZ: %u \t",BUMPL);
789 cizelu 288 delay_ms(10);
708 cizelu 289 printf("P_NARAZ: %u \n",BUMPR);
789 cizelu 290 delay_ms(10);
790 cizelu 291 if(BUMPL&&BUMPR) // po zmacknuti stran narazniku spusti test motoru
708 cizelu 292 {
293 motor_test();
294 }
295 }
296  
700 cizelu 297 // HLAVNI SMYCKA
298 void main()
730 cizelu 299 {
771 kaklik 300  
708 cizelu 301 printf("POWER ON \n");
700 cizelu 302 // NASTAVENI > provede se pouze pri zapnuti
708 cizelu 303 setup_adc_ports(sAN0-sAN1-sAN2);
789 cizelu 304 setup_adc(ADC_CLOCK_INTERNAL); // interni hodniny pro AD prevodnik
700 cizelu 305 setup_spi(SPI_SS_DISABLED);
306 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
307 setup_timer_1(T1_DISABLED);
789 cizelu 308 setup_timer_2(T2_DIV_BY_16,255,1); // casovac pro PWM
309 setup_ccp1(CCP_PWM); // povoli PWM na pinu RC2
310 setup_ccp2(CCP_PWM); // povolĂ­ PWM na pinu RC1
700 cizelu 311 setup_comparator(NC_NC_NC_NC);
708 cizelu 312 setup_vref(FALSE);
789 cizelu 313 l_motor_off(); // vypne levy motor
314 r_motor_off(); // vypne pravy motor
315 olsa_reset(); // reset logiky radkoveho senzoru
316 olsa_setup(); // nastaveni segmentu radkoveho senzoru (offset a zisk)
317 output_high(LED1); // zhasne LED1
318 output_high(LED2); // zhasne LED2
319 beep(500,200); // pipni pri startu
708 cizelu 320 printf("OK! \n");
716 cizelu 321 delay_ms(500);
708 cizelu 322 printf("VYBRAT MOD... \n");
789 cizelu 323  
700 cizelu 324 while(true)
325 {
326 }
730 cizelu 327 }