| Line 1... |
Line 1... |
| 1 |
#include "main.h" |
1 |
#include "main.h" |
| 2 |
|
2 |
|
| 3 |
// NEPOUZIVAT PINY B6 A B7, JSOU VYHRAZENY PRO SERIOVOU KOMUNIKACI |
3 |
// NEPOUZIVAT PINY B6 A B7, JSOU VYHRAZENY PRO SERIOVOU KOMUNIKACI |
| 4 |
// BAUD RATE = 4800 |
4 |
// BAUD RATE = 9600 |
| 5 |
|
5 |
|
| 6 |
|
- |
|
| 7 |
|
- |
|
| 8 |
//univerzalni LED diody |
6 |
// univerzalni LED diody |
| 9 |
#define LED1 PIN_A4 |
7 |
#define LED1 PIN_A4 |
| 10 |
#define LED2 PIN_A5 |
8 |
#define LED2 PIN_A5 |
| 11 |
|
9 |
|
| 12 |
//piezo pipak |
10 |
// piezo pipak |
| 13 |
#DEFINE SOUND_HI PIN_B4 |
11 |
#DEFINE SOUND_HI PIN_B4 |
| 14 |
#DEFINE SOUND_LO PIN_B5 |
12 |
#DEFINE SOUND_LO PIN_B5 |
| 15 |
|
13 |
|
| 16 |
//radkovy senzor |
14 |
// radkovy senzor |
| 17 |
#define SDIN PIN_D4 // seriovy vstup |
15 |
#define SDIN PIN_D4 // seriovy vstup |
| 18 |
#define SDOUT input(PIN_B2) // seriovy vystup |
16 |
#define SDOUT input(PIN_B2) // seriovy vystup |
| 19 |
#define SCLK PIN_D5 // takt |
17 |
#define SCLK PIN_D5 // takt |
| 20 |
|
18 |
|
| - |
|
19 |
// pro komunikaci s OLSA, prvni se posila LSB |
| 21 |
int1 main_reset[8]={1,1,0,1,1,0,0,0}; // hlavni reset |
20 |
int main_reset[8]={1,1,0,1,1,0,0,0}; // hlavni reset 0x1B |
| 22 |
int1 set_mode_rg[8]={1,1,1,1,1,0,1,0}; // zapis do MODE registru |
21 |
int set_mode_rg[8]={1,1,1,1,1,0,1,0}; // zapis do MODE registru 0x5F |
| 23 |
int1 clear_mode_rg[8]={0,0,0,0,0,0,0,0}; // nulovani MODE registru |
22 |
int clear_mode_rg[8]={0,0,0,0,0,0,0,0}; // nulovani MODE registru 0x00 |
| - |
|
23 |
|
| - |
|
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 |
| - |
|
28 |
|
| - |
|
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 |
| - |
|
33 |
|
| - |
|
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 |
| 24 |
|
37 |
|
| 25 |
unsigned int8 olsa_line_Rhalf[96]={0}; // radkovy senzor |
38 |
int olsa_lseg[51]={0}; // leva cast radky (pixely 0 - 50) |
| 26 |
unsigned int8 olsa_line_Lhalf[96]={0}; // radkovy senzor |
39 |
int olsa_rseg[51]={0}; // prava cast radky (pixely 51 - 102) |
| - |
|
40 |
int8 *line_lp=&olsa_lseg; // ukazatel na levou cast radky |
| - |
|
41 |
int8 *line_rp=&olsa_rseg; // ukazatel na pravou cast radky |
| 27 |
|
42 |
|
| 28 |
|
43 |
|
| 29 |
//naraznik |
44 |
//naraznik |
| 30 |
#define BUMPL input(PIN_D6) |
45 |
#define BUMPL input(PIN_D6) |
| 31 |
#define BUMPR input(PIN_D7) |
46 |
#define BUMPR input(PIN_D7) |
| 32 |
|
47 |
|
| 33 |
//nouzove senzory |
48 |
//nouzove senzory |
| 34 |
#define LINEL 0 |
49 |
#define LINEL 0 |
| 35 |
#define LINER 1 |
50 |
#define LINER 1 |
| 36 |
#define TRESHOLD 200 // rozhodovaci uroven (zmereno 0 - cerna, 255 cerna) |
51 |
#define TRESHOLD 200 // rozhodovaci uroven (zmereno 0 - cerna, 255 cerna) |
| 37 |
int8 line_l; |
52 |
int8 line_l; |
| 38 |
int8 line_r; |
53 |
int8 line_r; |
| - |
|
54 |
int1 line_position; |
| 39 |
|
55 |
|
| 40 |
// motory |
56 |
// motory |
| 41 |
#define LMF PIN_D0 |
57 |
#define LMF PIN_D0 |
| 42 |
#define LMB PIN_D1 |
58 |
#define LMB PIN_D1 |
| 43 |
#define RMF PIN_D2 |
59 |
#define RMF PIN_D2 |
| Line 66... |
Line 82... |
| 66 |
output_low(SCLK); |
82 |
output_low(SCLK); |
| 67 |
} |
83 |
} |
| 68 |
|
84 |
|
| 69 |
void olsa_send(int info[]) // USART komunikace s modulem OLSA01A - poslani zpravy |
85 |
void olsa_send(int info[]) // USART komunikace s modulem OLSA01A - poslani zpravy |
| 70 |
{ |
86 |
{ |
| 71 |
int8 *ip = &info; // ukazatel na pole s informaci |
87 |
int8 *ip=&info; // ukazatel na pole s informaci |
| 72 |
int i; // pomocna promenna pro nastaveni 0 nebo 1 na SDIN |
88 |
int i; // pomocna promenna pro nastaveni 0 nebo 1 na SDIN |
| 73 |
|
89 |
|
| 74 |
output_low(SDIN); // start bit |
90 |
output_low(SDIN); // start bit |
| 75 |
olsa_pulse(); |
91 |
olsa_pulse(); |
| 76 |
for(ip=0;ip<8;ip++) // predani informace - 8 bit, LSB prvni > MSB posledni |
92 |
for(ip=0;ip<8;ip++) // predani informace - 8 bit, LSB prvni > MSB posledni |
| 77 |
{ |
93 |
{ |
| Line 100... |
Line 116... |
| 100 |
olsa_send(main_reset); |
116 |
olsa_send(main_reset); |
| 101 |
olsa_pulses(5); |
117 |
olsa_pulses(5); |
| 102 |
olsa_send(set_mode_rg); |
118 |
olsa_send(set_mode_rg); |
| 103 |
olsa_send(clear_mode_rg); |
119 |
olsa_send(clear_mode_rg); |
| 104 |
} |
120 |
} |
| - |
|
121 |
|
| - |
|
122 |
void olsa_setup() // kompletni nastaveni, provadi se po resetu |
| - |
|
123 |
{ |
| - |
|
124 |
olsa_send(left_offset); // nastaveni leveho segmentu (offset a zisk) |
| - |
|
125 |
olsa_send(offset); |
| - |
|
126 |
olsa_send(left_gain); |
| - |
|
127 |
olsa_send(gain); |
| - |
|
128 |
olsa_send(mid_offset); // nastaveni prostredniho segmentu (offset a zisk) |
| - |
|
129 |
olsa_send(offset); |
| - |
|
130 |
olsa_send(mid_gain); |
| - |
|
131 |
olsa_send(gain); |
| - |
|
132 |
olsa_send(right_offset); // nastaveni praveho segmentu (offset a zisk) |
| - |
|
133 |
olsa_send(offset); |
| - |
|
134 |
olsa_send(right_gain); |
| - |
|
135 |
olsa_send(gain); |
| - |
|
136 |
} |
| 105 |
|
137 |
|
| 106 |
//ZACHRANNE SENZORY |
138 |
//ZACHRANNE SENZORY |
| 107 |
void read_blue_sensors() // cteni nouzovych senzoru |
139 |
void read_blue_sensors() // cteni nouzovych senzoru |
| 108 |
{ |
140 |
{ |
| 109 |
set_adc_channel(LINEL); // cti levy nouzovy senzor |
141 |
set_adc_channel(LINEL); // cti levy nouzovy senzor |
| Line 235... |
Line 267... |
| 235 |
|
267 |
|
| 236 |
void diagnostika() |
268 |
void diagnostika() |
| 237 |
{ |
269 |
{ |
| 238 |
read_blue_sensors(); |
270 |
read_blue_sensors(); |
| 239 |
printf("LEVA: %u \t",line_l); |
271 |
printf("LEVA: %u \t",line_l); |
| 240 |
delay_ms(20); |
272 |
delay_ms(10); |
| 241 |
printf("PRAVA: %u \t",line_r); |
273 |
printf("PRAVA: %u \t",line_r); |
| 242 |
delay_ms(20); |
274 |
delay_ms(10); |
| 243 |
printf("L_NARAZ: %u \t",BUMPL); |
275 |
printf("L_NARAZ: %u \t",BUMPL); |
| 244 |
delay_ms(20); |
276 |
delay_ms(10); |
| 245 |
printf("P_NARAZ: %u \n",BUMPR); |
277 |
printf("P_NARAZ: %u \n",BUMPR); |
| 246 |
delay_ms(20); |
278 |
delay_ms(10); |
| 247 |
if(BUMPL&&BUMPR) |
279 |
if(BUMPL&&BUMPR) |
| 248 |
{ |
280 |
{ |
| 249 |
motor_test(); |
281 |
motor_test(); |
| 250 |
} |
282 |
} |
| 251 |
} |
283 |
} |
| Line 255... |
Line 287... |
| 255 |
{ |
287 |
{ |
| 256 |
|
288 |
|
| 257 |
printf("POWER ON \n"); |
289 |
printf("POWER ON \n"); |
| 258 |
// NASTAVENI > provede se pouze pri zapnuti |
290 |
// NASTAVENI > provede se pouze pri zapnuti |
| 259 |
setup_adc_ports(sAN0-sAN1-sAN2); |
291 |
setup_adc_ports(sAN0-sAN1-sAN2); |
| 260 |
setup_adc(ADC_CLOCK_INTERNAL); // interni hodniny pro AD prevodnik |
292 |
setup_adc(ADC_CLOCK_INTERNAL); // interni hodniny pro AD prevodnik |
| 261 |
setup_spi(SPI_SS_DISABLED); |
293 |
setup_spi(SPI_SS_DISABLED); |
| 262 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
294 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
| 263 |
setup_timer_1(T1_DISABLED); |
295 |
setup_timer_1(T1_DISABLED); |
| 264 |
setup_timer_2(T2_DIV_BY_16,255,1); // casovac pro PWM |
296 |
setup_timer_2(T2_DIV_BY_16,255,1); // casovac pro PWM |
| 265 |
setup_ccp1(CCP_PWM); // povoli PWM na pinu RC2 |
297 |
setup_ccp1(CCP_PWM); // povoli PWM na pinu RC2 |
| 266 |
setup_ccp2(CCP_PWM); // povolĂ PWM na pinu RC1 |
298 |
setup_ccp2(CCP_PWM); // povolĂ PWM na pinu RC1 |
| 267 |
setup_comparator(NC_NC_NC_NC); |
299 |
setup_comparator(NC_NC_NC_NC); |
| 268 |
setup_vref(FALSE); |
300 |
setup_vref(FALSE); |
| 269 |
output_high(LED1); // zhasne LED1 |
301 |
l_motor_off(); // vypne levy motor |
| 270 |
output_high(LED2); // zhasne LED2 |
302 |
r_motor_off(); // vypne pravy motor |
| 271 |
l_motor_off(); // vypne levy motor |
303 |
olsa_reset(); // reset logiky radkoveho senzoru |
| - |
|
304 |
olsa_setup(); // nastaveni segmentu radkoveho senzoru (offset a zisk) |
| 272 |
r_motor_off(); // vypne pravy motor |
305 |
output_high(LED1); // zhasne LED1 |
| 273 |
olsa_reset(); // reset logiky radkoveho senzoru |
306 |
output_high(LED2); // zhasne LED2 |
| 274 |
beep(500,200); // pipni pri startu |
307 |
beep(500,200); // pipni pri startu |
| 275 |
printf("OK! \n"); |
308 |
printf("OK! \n"); |
| 276 |
delay_ms(500); |
309 |
delay_ms(500); |
| 277 |
printf("VYBRAT MOD... \n"); |
310 |
printf("VYBRAT MOD... \n"); |
| - |
|
311 |
|
| 278 |
while(true) |
312 |
while(true) |
| 279 |
{ |
313 |
{ |
| 280 |
} |
314 |
} |
| 281 |
} |
315 |
} |