0,0 → 1,357 |
#include ".\main.h" |
|
#define KOLMO1 225 // predni kolecko sroubem dopredu |
#define KOLMO2 30 // predni kolecko je hlavou sroubu dozadu |
#define STRED 128 // sredni poloha zataceciho kolecka |
#define BEAR1 12 // 3 stupne zataceni |
#define BEAR2 34 |
#define BEAR3 55 |
#define SPEEDMAX 140 // maximalni rychlost |
|
#define L 1 // cara vlevo |
#define S 2 // casa mezi sensory |
#define R 0 // cara vpravo |
|
// servo |
#define SERVO PIN_A2 |
|
// IR |
#define IRTX PIN_B2 |
#define CIHLA PIN_A3 |
|
//motory |
#define FR output_low(PIN_A7); output_high(PIN_A6) // Vpred |
#define FL output_low(PIN_A1); output_high(PIN_A0) |
#define BR output_low(PIN_A6); output_high(PIN_A7) // Vzad |
#define BL output_low(PIN_A0); output_high(PIN_A1) |
#define STOPR output_low(PIN_A6);output_low(PIN_A7) |
#define STOPL output_low(PIN_A0);output_low(PIN_A1) |
|
//HID |
#define LED1 PIN_B1 //oranzova |
#define LED2 PIN_B2 //zluta |
|
#define STROBE PIN_B0 |
//#define SW1 PIN_A2 // Motory On/off |
|
unsigned int8 sensors; // pomocna promenna pro cteni cidel na caru |
unsigned int8 line; // na ktere strane byla detekovana cara |
//unsigned int8 dira; // pocita dobu po kterou je ztracena cara |
unsigned int8 uhel; // urcuje aktualni uhel zataceni |
//unsigned int8 speed; // maximalni povolena rychlost |
unsigned int8 rovinka; // pocitadlo na zjisteni rovinky |
|
signed int16 Lmotor; // promene, ktere urcuji velikost vykonu na levem |
signed int16 Rmotor; // a pravem motoru |
|
// makro pro PWM pro motory |
#define GO(motor, direction, power) if(get_timer0()<=power) \ |
{direction##motor;} else {stop##motor;} |
|
//////////////////////////////////////////////////////////////////////////////// |
/* |
void diagnostika() |
{ |
if(input(SW1))STOPR;STOPL;While(TRUE); |
// if(LSENSOR==true) output_high(LED2); else output_low(LED2); |
// if(RSENSOR==true) output_high(LED1); else output_low(LED1); |
} |
*/ |
//////////////////////////////////////////////////////////////////////////////// |
#int_TIMER2 |
TIMER2_isr() // ovladani serva |
{ |
unsigned int8 n; |
|
output_high(SERVO); |
delay_us(1000); |
for(n=uhel; n>0; n--) Delay_us(2); |
output_low(SERVO); |
} |
|
//////////////////////////////////////////////////////////////////////////////// |
short int IRcheck() // potvrdi detekci cihly |
{ |
output_high(IRTX); // vypne vysilac IR |
delay_ms(100); |
|
output_low(STROBE); |
sensors = spi_read(0); // cteni senzoru |
sensors=~sensors; |
output_high(STROBE); |
|
if(true==bit_test(sensors,7)) // otestuje, jestli je stale detekovan IR signal |
{ |
output_low(IRTX); // zapne vysilac IR |
delay_ms(100); |
|
output_low(STROBE); |
sensors = spi_read(0); // cteni senzoru |
sensors=~sensors; |
output_high(STROBE); |
|
if(false==bit_test(sensors,7)) // otestuje, jestli je detekovana cihla |
{ |
output_high(IRTX); // vypne vysilac IR |
delay_ms(100); |
|
output_low(STROBE); |
sensors = spi_read(0); // cteni senzoru |
sensors=~sensors; |
output_high(STROBE); |
|
output_low(IRTX); // zapne vysilac IR |
if(bit_test(sensors,7)) return 1; // vrat 1, kdyz je stale cihla |
} |
}; |
output_low(IRTX); // zapne vysilac IR |
return 0; // vrat 0, kdyz je detekovano ruseni |
} |
//////////////////////////////////////////////////////////////////////////////// |
void cikcak() |
{ |
short int movement; |
|
if(uhel>STRED) |
{ |
movement = R; |
uhel=KOLMO1; |
Delay_ms(60); |
} |
|
if(uhel<STRED) |
{ |
movement = L; |
uhel=KOLMO2; |
Delay_ms(60); |
} |
|
sem: |
sensors = spi_read(0); // cteni senzoru |
sensors=~sensors; |
Delay_ms(5); // cekani na SLAVE nez pripravi data od cidel |
if(!bit_test(sensors,3) == sensors) |
{ |
if(R == movement) |
{ |
FR;BL; |
movement=L; |
While(bit_test(sensors,3) == sensors) |
{ |
sensors = spi_read(0); // cteni senzoru |
sensors=~sensors; |
Delay_ms(5); |
} |
STOPL;STOPR; |
GoTo sem; |
} |
|
if(L == movement) |
{ |
FL;BR; |
movement=R; |
While(bit_test(sensors,3) == sensors) |
{ |
sensors = spi_read(0); |
sensors =~ sensors; |
Delay_ms(5); // cekani na SLAVE nez pripravi data od cidel |
} |
|
STOPL;STOPR; |
GoTo sem; |
} |
} |
} |
//////////////////////////////////////////////////////////////////////////////// |
void objizdka() |
{ |
int8 shure=0; |
unsigned int16 n; |
|
BR;BL; |
Delay_ms(400); |
STOPR;STOPL; |
|
uhel=KOLMO1; |
Delay_ms(100); |
BL;FR; |
Delay_ms(200); // minimalni toceni, kdyby se zastavilo sikmo k cihle |
|
While(bit_test(sensors,7)) // toc, dokud neni cihla z primeho senzoru |
{ |
sensors = spi_read(0); // cteni senzoru |
sensors=~sensors; |
Delay_ms(4); // cekani na SLAVE nez pripravi data od cidel |
} |
STOPL; STOPR; |
|
for (n=0;n<1500;n++) // vystred se na hranu cihly |
{ |
if(!input(CIHLA)) {BL; FR;} else {FL; BR;}; |
delay_ms(1); |
} |
STOPR;STOPL; |
|
uhel=STRED; // dopredu |
delay_ms(100); |
FR; FL; |
delay_ms(500); |
BL;BR; |
delay_ms(200); |
STOPL;STOPR; |
|
uhel=STRED+BEAR3; // doprava |
delay_ms(100); |
FL; |
delay_ms(200); |
uhel=STRED+BEAR2; // min doprava |
FL;FR; |
delay_ms(200); |
uhel=STRED+BEAR1; // jeste min doprava |
FL;FR; |
delay_ms(200); |
uhel=STRED; // rovne |
FL;FR; |
delay_ms(100); |
While((sensors & 0b01111111)!=0) //dokud neni cara |
{ |
sensors = spi_read(0); // cteni senzoru |
sensors=~sensors; |
Delay_ms(4); // cekani na SLAVE nez pripravi data od cidel |
} |
BL; BR; |
delay_ms(400); |
|
uhel=STRED-BEAR3; // doleva |
} |
|
//////////////////////////////////////////////////////////////////////////////// |
void main() |
{ |
|
unsigned int16 n; |
unsigned int8 i; |
|
setup_adc_ports(NO_ANALOGS); |
setup_adc(ADC_CLOCK_INTERNAL); |
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4); |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
setup_timer_1(T1_DISABLED); |
setup_timer_2(T2_DIV_BY_16,140,16); |
setup_oscillator(OSC_8MHZ|OSC_INTRC); |
|
STOPR; STOPL; // zastav motory |
Lmotor=0;Rmotor=0; |
|
uhel=STRED; // nastav zadni kolecko na stred |
rovinka=0; |
|
enable_interrupts(INT_TIMER2); |
enable_interrupts(GLOBAL); |
|
output_low(IRTX); // zapni IR vysilac |
|
delay_ms(1000); |
|
while(true) |
{ |
|
GO(L,F,Lmotor);GO(R,F,Rmotor); // zapni motory |
|
delay_us(1500); |
|
output_low(STROBE); |
sensors = spi_read(0); // cteni senzoru |
sensors=~sensors; |
output_high(STROBE); |
|
i=0; // havarijni kod |
for (n=0; n<=6; n++) |
{ |
if(bit_test(sensors,n)) i++; |
} |
if (i>3) While(true){STOPR; STOPL;}; // zastavi, kdyz je cerno pod vice nez tremi cidly |
|
if(bit_test(sensors,7)) // detekce dihly |
{ |
objizdka(); |
} |
|
if(bit_test(sensors,3)) //...|...// |
{ |
uhel=STRED; |
Lmotor=SPEEDMAX; |
Rmotor=SPEEDMAX; |
line=S; |
if (rovinka<255) rovinka++; |
continue; |
} |
|
if(bit_test(sensors,0)) //|......// // z duvodu zkraceni doby reakce se cidla nevyhodnocuji poporade ale od krajnich k prostrednimu |
{ |
uhel=STRED-BEAR3; |
Lmotor=0; |
Rmotor=SPEEDMAX; |
line=L; |
continue; |
} |
|
if(bit_test(sensors,6)) //......|// |
{ |
uhel=STRED+BEAR3; |
Rmotor=0; |
Lmotor=SPEEDMAX; |
line=R; |
continue; |
} |
|
if(bit_test(sensors,1)) //.|.....// |
{ |
uhel=STRED-BEAR2; |
Lmotor=SPEEDMAX-50; |
Rmotor=SPEEDMAX; |
line=S; |
continue; |
} |
|
if(bit_test(sensors,5)) //.....|.// |
{ |
uhel=STRED+BEAR2; |
Rmotor=SPEEDMAX-50; |
Lmotor=SPEEDMAX; |
line=S; |
continue; |
} |
|
if (bit_test(sensors,2)) //..|....// |
{ |
uhel=STRED-BEAR1; |
Lmotor=SPEEDMAX; |
Rmotor=SPEEDMAX; |
line=S; |
if (rovinka<255) rovinka++; |
continue; |
} |
|
if (bit_test(sensors,4)) //....|..// |
{ |
uhel=STRED+BEAR1; |
Rmotor=SPEEDMAX; |
Lmotor=SPEEDMAX; |
line=S; |
if (rovinka<255) rovinka++; |
continue; |
} |
|
if ((L==line) || (R==line)) // Brzdeni pri vyjeti z trate |
{ |
if (rovinka>250) |
{ |
BL; BR; |
Delay_ms(100); |
}; |
rovinka=0; |
}; |
|
} |
} |