#if defined(__PCM__) #include <16f877.h> #fuses HS,NOWDT,NOPROTECT #device ADC=8 #use delay(clock=4000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N) #use fast_io(D) //pins RD2 and RD3 flip between inputs and outputs, //***Define Default Port Constants. #define IO_SETA 0x3F //AN0 - AN3 analog inputs from CDs '00111111', AN4 - unused #define IO_SETB 0x05 //RB0,RB1 - UART, RB7-RB3 LEDs, RB2 - unused #define IO_SETC 0xB6 //RC7,RC6-UART, RC1,RC2 - heartbeat, RC3 - sound, RC4,RC5 - dips, RC0 - unused #define IO_SETD_I 0xFC //RD0,RD1 - enables, RD2,RD3 - tactiles(inputs), RD4-RD7 - compass #define IO_SETD_O 0xF0 //RD2 and RD3 tactiles are both outputs. #define IO_SETD_R 0xF8 //RD3 is input RD2 output #define IO_SETD_L 0xF4 //RD2 is input RD3 output #define IO_SETE 0x07 //RE0-RE2 used as analog inputs(AN5-AN7) #define FL 0 //NEW indices into CDs sensor arrays #define FR 1 #define SR 2 #define SL 3 #define BR 4 #define BL 5 #define BCL 6 #define BCR 7 //portd pin value defines for individual bit manipulation // #define NORTH 71 //RD4 - RD7 input from compass #define EAST 70 #define SOUTH 69 #define WEST 68 #define RSENSOR 67 //RD3 - Right Tactile Sensor (input/output) #define LSENSOR 66 //RD2 - Left Tactile Sensor (input/output) #define ENB1 65 //RD1 - Enable HeartBeat1 ( bc2 output, walk) #define ENB2 64 //RD0 - Enable HeartBeat2 //portc defines #define HEARTBT1 58 //RC2 - output from master bicore #define HEARTBT2 57 //RC1 - output from heartbeat2 bicore #define TONE_PIN PIN_C3 #define DIP1 PIN_C4 #define DIP2 PIN_C5 //state machine defines #define FWD 1 #define REV 2 #define LFT 3 #define RGHT 4 #define STP 5 #define FUB 6 const long BEEP = 1175; // frequency of tone const long DUR = 100; // duration of tone int black[8], white[8], c_sensor[8], bin_sensor[8]; int bot_step, action, black_limit, until_black, revcount; //******************************************************************************************* // routines to make beep, do_delay and generate_tone are taken from the CCS Library. // For embedded code, I like it all in one file. //******************************************************************************************* void do_delay(int ms_delay, int num_ms, int us_delay, int num_us) { int i; for(i=0;i= black_limit) ? 1 : 0); } //prtdec_sensors(); //determine action if( !(bin_sensor[FL]) && !(bin_sensor[FR]) ) //do both sensors see white? { if( bin_sensor[SL] || bin_sensor[SR] ) //check side sensors for black { if(bin_sensor[SL] && !(bin_sensor[SR]) ) { if(bin_sensor[BL]) { bot_step=2; } else { bot_step=3; } action=LFT; } else if(bin_sensor[SR] && !(bin_sensor[SL]) ) { if(bin_sensor[BR]) { bot_step=2; } else { bot_step=3; } action=RGHT; } else // SL and SR both see black { if(bin_sensor[BL] && !(bin_sensor[BR]) ) { bot_step=2; action=LFT; } else if(bin_sensor[BR] && !(bin_sensor[BL]) ) { bot_step=2; action=RGHT; } else { action=STP; } } } // check back sensors for black else if( bin_sensor[BL] || bin_sensor[BCL] || bin_sensor[BCR] || bin_sensor[BR] ) { if( bin_sensor[BL] && !(bin_sensor[BR]) ) { bot_step=3; action=LFT; } else if( bin_sensor[BR] && !(bin_sensor[BL]) ) { bot_step=3; action=RGHT; } else if( c_sensor[BR] > c_sensor[BL] ) { bot_step=2; action=RGHT; } else if( c_sensor[BL] > c_sensor[BR] ) { bot_step=2; action=LFT; } } else //all sensors see white { bot_step=1; action=REV; } } else if( bin_sensor[FL] && !(bin_sensor[FR]) ) { if( bin_sensor[BL]) { bot_step=1; action=FWD; } else if(bin_sensor[BR] || bin_sensor[BCR] ) { bot_step=1; action=LFT; } else if( bin_sensor[SL] ) { bot_step=1; action=FWD; } else //only FL is on the black { bot_step=1; action=FWD; } } else if( bin_sensor[FR] && !(bin_sensor[FL]) ) { if( bin_sensor[BR] ) { bot_step=1; action=FWD; } else if(bin_sensor[BL] || bin_sensor[BCL]) { bot_step=1; action=RGHT; } else if( bin_sensor[SR] ) { bot_step=1; action=FWD; } else // only FR is on the black { bot_step=1; action=FWD; } } else //FR and FL are both 1 { bot_step=1; action=FWD; } } //**************************************************************************************** //State Machine routines for the five possible actions //**************************************************************************************** void forward() { int n, c_act,bstep; bstep = bot_step; //record current bot_step c_act = action; //record current action revcount = 0; //reset reverse count imx_forward(); //set IMX to forward movement for(n=0; n= black_limit) ? 1 : 0); } //prtdec_sensors(); if( bin_sensor[FL] || bin_sensor[FR]) { until_black=0; action=STP; } else if( bin_sensor[SL] || bin_sensor[SR] ) { until_black=0; action=STP; } else if( bin_sensor[BL] || bin_sensor[BCL] || bin_sensor[BCR] || bin_sensor[BR] ) { until_black=0; action=STP; } } } void stop() { motors_off(); delay_ms(2000); sensor_read(); update_action(); motors_on(); } void main() { int n,i; set_tris_b(IO_SETB); //setup default io direction port_b_pullups(TRUE); set_tris_c(IO_SETC); delay_cycles(1); set_tris_d(IO_SETD_O); //set tactile tris bits for outputs to IMX all_enables_off(); //make sure everything is off in portd setup_adc_ports (ALL_ANALOG); // set analog inputs setup_adc (ADC_CLOCK_INTERNAL); // sets analog clock to internal if( (input(DIP1)) && (input(DIP2)) ) //check dip switch settings { set_tris_d(IO_SETD_I); //set tactile tris bits for inputs to IMX motors_on(); //turn motor enables on delay_ms(2); //wait 2 ms for motors to settle while(1){} //total BEAM mode } delay_ms(2000); //wait 2 second //do_balance(); //get black and white balance black_limit = 220; //preset for now motors_on(); //turn motor enables on delay_ms(2); //wait 2 ms for motors to settle action=FWD; //default action and step count bot_step=2; while(1) // track line forever { switch(action) { case FWD: forward(); break; case REV: reverse(); break; case LFT: left(); break; case RGHT: right(); break; case STP: stop(); break; case FUB: f_until_b(); break; } } //end line forever } //end main