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 }
793 cizelu 149  
150 void olsa_read() // precist senzor
151 {
152 olsa_send(readout); // prikaz pro cteni ze senzoru
153 }
741 cizelu 154  
155 //ZACHRANNE SENZORY
790 cizelu 156 void read_blue_sensors() // cteni nouzovych senzoru
708 cizelu 157 {
790 cizelu 158 set_adc_channel(LINEL); // cti levy nouzovy senzor
700 cizelu 159 delay_us(10);
745 cizelu 160 line_l=read_adc();
790 cizelu 161 set_adc_channel(LINER); // cti pravy nouzovy senzor
700 cizelu 162 delay_us(10);
163 line_r=read_adc();
708 cizelu 164 }
730 cizelu 165  
700 cizelu 166 //PIPAK
716 cizelu 167 void beep(int16 period,int16 length)
730 cizelu 168 {
790 cizelu 169 int16 bp; //promenna pro nastaveni delky
700 cizelu 170  
171 for(bp=length;bp>0;bp--)
730 cizelu 172 {
173 output_high(SOUND_HI);
174 output_low(SOUND_LO);
175 delay_us(period);
176 output_high(SOUND_LO);
177 output_low(SOUND_HI);
178 delay_us(period);
179 }
180 }
708 cizelu 181 //MOTORY
790 cizelu 182 void l_motor_fwd(int8 speedl) // levy motor dopredu
730 cizelu 183 {
708 cizelu 184 output_high(LMF);
185 output_low(LMB);
186 set_pwm2_duty(speedl);
730 cizelu 187 }
700 cizelu 188  
790 cizelu 189 void l_motor_bwd(int8 speedl) // levy motor dozadu
730 cizelu 190 {
708 cizelu 191 output_high(LMB);
192 output_low(LMF);
193 set_pwm2_duty(speedl);
730 cizelu 194 }
708 cizelu 195  
790 cizelu 196 void r_motor_fwd(int8 speedr) // pravy motor dopredu
730 cizelu 197 {
708 cizelu 198 output_high(RMF);
199 output_low(RMB);
200 set_pwm1_duty(speedr);
730 cizelu 201 }
708 cizelu 202  
790 cizelu 203 void r_motor_bwd(int8 speedr) // pravy motor dozadu
730 cizelu 204 {
708 cizelu 205 output_high(RMB);
206 output_low(RMF);
207 set_pwm1_duty(speedr);
730 cizelu 208 }
708 cizelu 209  
790 cizelu 210 void l_motor_off() // levy motor vypnut
730 cizelu 211 {
708 cizelu 212 output_low(LMF);
213 output_low(LMB);
214 set_pwm2_duty(0);
730 cizelu 215 }
216  
790 cizelu 217 void r_motor_off() // pravy motor vypnut
730 cizelu 218 {
708 cizelu 219 output_low(RMF);
220 output_low(RMB);
221 set_pwm1_duty(0);
730 cizelu 222 }
708 cizelu 223  
790 cizelu 224 void motor_test() // test motoru
730 cizelu 225 {
708 cizelu 226 int8 i;
716 cizelu 227 beep(100,200);
708 cizelu 228 printf("TEST MOTORU\n");
229 delay_ms(1000);
716 cizelu 230 printf("LEVY MOTOR DOPREDU\n");
708 cizelu 231 for(i=0;i<255;i++)
730 cizelu 232 {
708 cizelu 233 l_motor_fwd(i);
234 printf("RYCHLOST: %u\n",i);
716 cizelu 235 delay_ms(5);
730 cizelu 236 }
708 cizelu 237 for(i=255;i>0;i--)
730 cizelu 238 {
708 cizelu 239 l_motor_fwd(i);
240 printf("RYCHLOST: %u\n",i);
716 cizelu 241 delay_ms(5);
730 cizelu 242 }
716 cizelu 243 printf("LEVY MOTOR DOZADU\n");
708 cizelu 244 for(i=0;i<255;i++)
730 cizelu 245 {
708 cizelu 246 l_motor_bwd(i);
247 printf("RYCHLOST: %u\n",i);
716 cizelu 248 delay_ms(5);
730 cizelu 249 }
708 cizelu 250 for(i=255;i>0;i--)
730 cizelu 251 {
708 cizelu 252 l_motor_bwd(i);
253 printf("RYCHLOST: %u\n",i);
716 cizelu 254 delay_ms(5);
730 cizelu 255 }
716 cizelu 256 printf("PRAVY MOTOR DOPREDU\n");
708 cizelu 257 for(i=0;i<255;i++)
730 cizelu 258 {
708 cizelu 259 r_motor_fwd(i);
716 cizelu 260 printf("RYCHLOST: %u\n",i);
261 delay_ms(5);
730 cizelu 262 }
708 cizelu 263 for(i=255;i>0;i--)
730 cizelu 264 {
708 cizelu 265 r_motor_fwd(i);
266 printf("RYCHLOST: %u\n",i);
716 cizelu 267 delay_ms(5);
730 cizelu 268 }
716 cizelu 269 printf("PRAVY MOTOR DOZADU\n");
708 cizelu 270 for(i=0;i<255;i++)
730 cizelu 271 {
708 cizelu 272 r_motor_bwd(i);
273 printf("RYCHLOST: %u\n",i);
716 cizelu 274 delay_ms(5);
730 cizelu 275 }
708 cizelu 276 for(i=255;i>0;i--)
730 cizelu 277 {
708 cizelu 278 r_motor_bwd(i);
279 printf("RYCHLOST: %u\n",i);
716 cizelu 280 delay_ms(5);
730 cizelu 281 }
282 printf("KONEC TESTU MOTORU \N");
708 cizelu 283 }
284  
790 cizelu 285 void diagnostika() // diagnostika - vypis senzoru s moznosti prepnuti na test motoru
708 cizelu 286 {
287 read_blue_sensors();
288 printf("LEVA: %u \t",line_l);
789 cizelu 289 delay_ms(10);
708 cizelu 290 printf("PRAVA: %u \t",line_r);
789 cizelu 291 delay_ms(10);
708 cizelu 292 printf("L_NARAZ: %u \t",BUMPL);
789 cizelu 293 delay_ms(10);
708 cizelu 294 printf("P_NARAZ: %u \n",BUMPR);
789 cizelu 295 delay_ms(10);
790 cizelu 296 if(BUMPL&&BUMPR) // po zmacknuti stran narazniku spusti test motoru
708 cizelu 297 {
298 motor_test();
299 }
300 }
301  
700 cizelu 302 // HLAVNI SMYCKA
303 void main()
730 cizelu 304 {
771 kaklik 305  
708 cizelu 306 printf("POWER ON \n");
700 cizelu 307 // NASTAVENI > provede se pouze pri zapnuti
708 cizelu 308 setup_adc_ports(sAN0-sAN1-sAN2);
789 cizelu 309 setup_adc(ADC_CLOCK_INTERNAL); // interni hodniny pro AD prevodnik
700 cizelu 310 setup_spi(SPI_SS_DISABLED);
311 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
312 setup_timer_1(T1_DISABLED);
789 cizelu 313 setup_timer_2(T2_DIV_BY_16,255,1); // casovac pro PWM
314 setup_ccp1(CCP_PWM); // povoli PWM na pinu RC2
315 setup_ccp2(CCP_PWM); // povolĂ­ PWM na pinu RC1
700 cizelu 316 setup_comparator(NC_NC_NC_NC);
708 cizelu 317 setup_vref(FALSE);
789 cizelu 318 l_motor_off(); // vypne levy motor
319 r_motor_off(); // vypne pravy motor
320 olsa_reset(); // reset logiky radkoveho senzoru
321 olsa_setup(); // nastaveni segmentu radkoveho senzoru (offset a zisk)
322 output_high(LED1); // zhasne LED1
323 output_high(LED2); // zhasne LED2
324 beep(500,200); // pipni pri startu
708 cizelu 325 printf("OK! \n");
716 cizelu 326 delay_ms(500);
708 cizelu 327 printf("VYBRAT MOD... \n");
789 cizelu 328  
700 cizelu 329 while(true)
330 {
331 }
730 cizelu 332 }