# HG changeset patch # User Malte Bayer # Date 1323461856 -3600 # Node ID 61f88f973eba75715a422edee80eccc4959aa00c # Parent ea1e8dcbec44f58fef9b738ced4d4806e264302f structured vars in pitlane code diff -r ea1e8dcbec44 -r 61f88f973eba blackbox/main.c --- a/blackbox/main.c Fri Dec 09 20:02:17 2011 +0100 +++ b/blackbox/main.c Fri Dec 09 21:17:36 2011 +0100 @@ -25,7 +25,6 @@ uint8_t byte[4]; // all four bytes } u32; -#define MAX_SLOTS 6 #define FUEL_FULL 10000 #define FUEL_DIVISOR 25 //#define FUEL_DIVISOR 10 diff -r ea1e8dcbec44 -r 61f88f973eba blackbox/main.h --- a/blackbox/main.h Fri Dec 09 20:02:17 2011 +0100 +++ b/blackbox/main.h Fri Dec 09 21:17:36 2011 +0100 @@ -90,6 +90,9 @@ #define PIN(x) (*(&x - 2)) // address of input register of port x +// constants +#define MAX_SLOTS 6 + // MAYBE USELESS STUFF: diff -r ea1e8dcbec44 -r 61f88f973eba pitlane/main.c --- a/pitlane/main.c Fri Dec 09 20:02:17 2011 +0100 +++ b/pitlane/main.c Fri Dec 09 21:17:36 2011 +0100 @@ -25,25 +25,38 @@ #define SOLENOID_B_PIN PB2 #define TRACKSWITCH_TYPE 2 // 1=double, 2=single, 3=pitlane - +#define MAX_SENSORS 3 // internal analog comparator doesnt work well //#define ANALOG_COMPARATOR 1 +typedef struct { + unsigned speed:4; + unsigned trackswitch:1; + unsigned inside:1; +} cardata; + +typedef struct { + unsigned car:4; + unsigned state:4; + uint16_t old, new; +} sensordata; + +volatile cardata slot[MAX_SLOTS]; +volatile sensordata sens[MAX_SENSORS]; + volatile uint16_t data = 0; volatile uint8_t data_len = 0; volatile uint8_t bitbuf_len = 0; volatile uint16_t bitbuf = 0; -volatile uint8_t car_speed[8]; -volatile uint8_t car_switch[8]; -volatile uint16_t car0, car1, car2; -volatile uint16_t car0_new, car0_old; -volatile uint16_t car1_new, car1_old; -volatile uint16_t car2_new, car2_old; volatile uint8_t response = 0; uint8_t self_id = 0b1111; // ONLY ONE pitlane +void solenoid_delay(void) { + _delay_ms(10); +} + void send_response(uint16_t data) { /* frame format: 1 startbit @@ -116,8 +129,8 @@ // write data of controllers to array if (data_len == 10) { // controller data packet clock = (bitbuf >> 6) & 0b00000111; - car_speed[clock] = (bitbuf >> 1) & 0x0F; - car_switch[clock] = (bitbuf >> 5) & 1; + slot[clock].speed = (bitbuf >> 1) & 0x0F; + slot[clock].trackswitch = (bitbuf >> 5) & 1; // current response for this car? if (response != 0) { if ( ((response & 0b00001110) >> 1) == clock) { @@ -147,79 +160,52 @@ ISR (TIMER1_OVF_vect) { - // reset both car counters to overflow - car0_old = 0xffff; - car1_old = 0xffff; - // pitlane exit sensor - car2_old = 0xffff; + // reset car counters to overflow + uint8_t i; + for (i=0;i 54) && (tmp < 74) ) tmp = 1; + else if ( (tmp > 118) && (tmp < 138) ) tmp = 2; + else if ( (tmp > 186) && (tmp < 206) ) tmp = 3; + else if ( (tmp > 246) && (tmp < 266) ) tmp = 4; + else if ( (tmp > 310) && (tmp < 330) ) tmp = 5; + else if ( (tmp > 374) && (tmp < 394) ) tmp = 6; + else tmp = 0; + sens[idx].car = tmp; + } + sens[idx].old = sens[idx].new; } ISR (INT1_vect) { // car0 detector - uint16_t tmp = 0; - car0_new = TCNT1; // get current counter - if (car0_old < car0_new) { - // calculate difference - if (car0 == 0) tmp = car0_new-car0_old; - if ( (tmp > 54) && (tmp < 74) ) car0 = 1; - if ( (tmp > 118) && (tmp < 138) ) car0 = 2; - if ( (tmp > 186) && (tmp < 206) ) car0 = 3; - if ( (tmp > 246) && (tmp < 266) ) car0 = 4; - if ( (tmp > 310) && (tmp < 330) ) car0 = 5; - if ( (tmp > 374) && (tmp < 394) ) car0 = 6; - } - car0_old = car0_new; + detect_car(0); } // ISR (TIMER1_CAPT_vect) { #ifdef ANALOG_COMPARATOR ISR (ANA_COMP_vect) { // car1 detector - uint16_t tmp = 0; - car1_new = TCNT1; // get current counter - if (car1_old < car1_new) { - // calculate difference - if (car1 == 0) tmp = car1_new-car1_old; - if ( (tmp > 50) && (tmp < 78) ) car1 = 1; - if ( (tmp > 114) && (tmp < 146) ) car1 = 2; - if ( (tmp > 183) && (tmp < 210) ) car1 = 3; - if ( (tmp > 242) && (tmp < 270) ) car1 = 4; - if ( (tmp > 310) && (tmp < 330) ) car1 = 5; - if ( (tmp > 374) && (tmp < 394) ) car1 = 6; - } - car1_old = car1_new; + detect_car(1); } #else // ALTERNATIV: ISR (TIMER1_CAPT_vect) { // car1 detector - uint16_t tmp = 0; - car1_new = TCNT1; // get current counter - if (car1_old < car1_new) { - // calculate difference - if (car1 == 0) tmp = car1_new-car1_old; - if ( (tmp > 50) && (tmp < 78) ) car1 = 1; - if ( (tmp > 114) && (tmp < 146) ) car1 = 2; - if ( (tmp > 183) && (tmp < 210) ) car1 = 3; - if ( (tmp > 242) && (tmp < 270) ) car1 = 4; - if ( (tmp > 310) && (tmp < 330) ) car1 = 5; - if ( (tmp > 374) && (tmp < 394) ) car1 = 6; - } - car1_old = car1_new; + detect_car(1); } #endif -void solenoid_delay(void) { - _delay_ms(10); -} - - - int main(void) { - uint8_t car0_state, car1_state; - // setup data bit timer2 TCCR2 = (1<0) ) { - response = (1 | ((car0_state-1)<<1) | (1 << 4)); + if (sens[0].car != sens[0].state) { + sens[0].state = sens[0].car; + if ( (sens[0].state != 0) && (slot[sens[0].state-1].trackswitch == 0) && (slot[sens[0].state-1].speed>0) ) { + response = (1 | ((sens[0].state-1)<<1) | (1 << 4)); // trigger solenoid A RS232_putc('A'); RS232_putc('B'); - RS232_putc('0'+car0_state); + RS232_putc('0'+sens[0].state); RS232_putc('\n'); SOLENOID_A_PORT |= _BV(SOLENOID_A_PIN); @@ -285,24 +271,24 @@ SOLENOID_A_PORT &= ~_BV(SOLENOID_A_PIN); solenoid_delay(); } - if (car0_state != 0) { - response = (1 | ((car0_state-1)<<1)); + if (sens[0].state != 0) { + response = (1 | ((sens[0].state-1)<<1)); RS232_putc('A'); RS232_putc('A'); - RS232_putc('0'+car0_state); + RS232_putc('0'+sens[0].state); RS232_putc('\n'); } - } car0 = 0; + } sens[0].car = 0; - if (car1 != car1_state) { - car1_state = car1; - if ( (car1_state != 0) && (car_switch[car1_state-1] == 0) && (car_speed[car1_state-1]>0) ) { - response = (1 | ((car1_state-1)<<1) | (3 << 4)); + if (sens[1].car != sens[1].state) { + sens[1].state = sens[1].car; + if ( (sens[1].state != 0) && (slot[sens[1].state-1].trackswitch == 0) && (slot[sens[1].state-1].speed>0) ) { + response = (1 | ((sens[1].state-1)<<1) | (3 << 4)); // trigger solenoid B RS232_putc('B'); RS232_putc('A'); - RS232_putc('0'+car1_state); + RS232_putc('0'+sens[1].state); RS232_putc('\n'); SOLENOID_B_PORT |= _BV(SOLENOID_B_PIN); @@ -310,14 +296,14 @@ SOLENOID_B_PORT &= ~_BV(SOLENOID_B_PIN); solenoid_delay(); } - if (car1_state != 0) { - response = (1 | ((car1_state-1)<<1) | (2 << 4)); + if (sens[1].state != 0) { + response = (1 | ((sens[1].state)<<1) | (2 << 4)); RS232_putc('B'); RS232_putc('B'); - RS232_putc('0'+car1_state); + RS232_putc('0'+sens[1].state); RS232_putc('\n'); } - } car1 = 0; + } sens[0].car = 0; } // main loop end