Fri, 09 Dec 2011 22:30:55 +0100
pitlane lane response fix
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
1 | #include <avr/interrupt.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
2 | #include <avr/io.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
3 | #include <avr/wdt.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
4 | #include <avr/eeprom.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
5 | #include <stdlib.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
6 | #include <stdint.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
7 | #include <avr/pgmspace.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
8 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
9 | #include "main.h" |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
10 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
11 | #include "driver/rs232.h" |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
12 | #include "util/delay.h" |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
13 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
14 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
15 | ISR ( USART_RXC_vect ) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
16 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
17 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
18 | #define PULSE_PORT PORTD |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
19 | #define PULSE_BIT PD2 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
20 | #define RESPONSE_PORT PORTC |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
21 | #define RESPONSE_PIN PC1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
22 | #define SOLENOID_A_PORT PORTB |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
23 | #define SOLENOID_B_PORT PORTB |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
24 | #define SOLENOID_A_PIN PB1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
25 | #define SOLENOID_B_PIN PB2 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
26 | |
67 | 27 | #define TRACKSWITCH_TYPE 4 // 1=double, 2=single, 4=pitlane |
28 | #define TYPE_PITLANE 4 | |
29 | ||
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
30 | #define MAX_SENSORS 3 |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
31 | |
65
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
32 | #define PIT_PORT PORTC |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
33 | #define PIT_CONNECT_PIN PC3 |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
34 | #define PIT_GROUND_PIN PC2 // low active |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
35 | |
66
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
36 | #define SENS2_PORT PORTD |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
37 | #define SENS2_PIN PD5 |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
38 | |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
39 | #define LED_PORT PORTB |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
40 | #define LED_PIN PB5 |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
41 | |
67 | 42 | |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
43 | // internal analog comparator doesnt work well |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
44 | //#define ANALOG_COMPARATOR 1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
45 | |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
46 | typedef struct { |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
47 | unsigned speed:4; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
48 | unsigned trackswitch:1; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
49 | unsigned inside:1; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
50 | } cardata; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
51 | |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
52 | typedef struct { |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
53 | unsigned car:4; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
54 | unsigned state:4; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
55 | uint16_t old, new; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
56 | } sensordata; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
57 | |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
58 | volatile cardata slot[MAX_SLOTS]; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
59 | volatile sensordata sens[MAX_SENSORS]; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
60 | |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
61 | volatile uint16_t data = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
62 | volatile uint8_t data_len = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
63 | volatile uint8_t bitbuf_len = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
64 | volatile uint16_t bitbuf = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
65 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
66 | volatile uint8_t response = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
67 | uint8_t self_id = 0b1111; // ONLY ONE pitlane |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
68 | |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
69 | void solenoid_delay(void) { |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
70 | _delay_ms(10); |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
71 | } |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
72 | |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
73 | void send_response(uint16_t data) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
74 | /* frame format: |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
75 | 1 startbit |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
76 | 2 car id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
77 | 3 car id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
78 | 4 car id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
79 | 5 track change status bit 1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
80 | 6 track change status bit 2 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
81 | 7 sender id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
82 | 8 sender id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
83 | 9 sender id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
84 | 9 sender id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
85 | 10 device type |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
86 | 11 device type |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
87 | 12 device type |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
88 | 13 device type |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
89 | 14 reserved |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
90 | 15 reserved |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
91 | 16 stopbit |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
92 | */ |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
93 | uint8_t index = 16; // bit count maximum |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
94 | uint8_t enable = DDR(RESPONSE_PORT) | _BV(RESPONSE_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
95 | uint8_t disable = DDR(RESPONSE_PORT) & ~_BV(RESPONSE_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
96 | data |= 0b100000000000001; // make sure start/stop bits are set |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
97 | data = 0b1010101010101011; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
98 | while (index != 0) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
99 | if ((data & 1) != 0) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
100 | DDR(RESPONSE_PORT) = enable; // enable response output |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
101 | } else { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
102 | DDR(RESPONSE_PORT) = disable; // disable response output |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
103 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
104 | data = data >> 1; // next bit prepare |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
105 | index--; // decrement index |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
106 | _delay_us(49); // bit valid phase |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
107 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
108 | // finally be sure to release the bus! |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
109 | DDR(RESPONSE_PORT) = disable; // disable response output |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
110 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
111 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
112 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
113 | ISR ( INT0_vect ) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
114 | GICR &= ~_BV(INT0) ; // Disable INT0 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
115 | // Startsignal erkannt, ab hier den Timer2 starten, |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
116 | // der liest dann alle 50µs den Zustand ein und schreibt das |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
117 | // empfangene Bit in den Puffer |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
118 | bitbuf = 0; // init |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
119 | bitbuf_len = 0b10000000; // init 1 pulse received |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
120 | TCNT2 = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
121 | TIMSK |= _BV(OCIE2); //enable timer2 interrupt |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
122 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
123 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
124 | ISR ( TIMER2_COMP_vect ) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
125 | uint8_t clock; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
126 | uint8_t state; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
127 | uint8_t state2; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
128 | if ((bitbuf_len & 0b10000000) == 0) clock = 0; else clock = 0xff; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
129 | if ((bitbuf_len & 0b01000000) == 0) state = 0; else state = 0xff; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
130 | if ((PIN(PULSE_PORT) & _BV(PULSE_BIT)) == 0) state2 = 0xff; else state2 = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
131 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
132 | if (clock) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
133 | bitbuf_len &= ~_BV(7); // switch clock to low |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
134 | // second pulse of bit |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
135 | if ((state==state2) & state2) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
136 | // two cycles high: packet end received |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
137 | data_len = (bitbuf_len & 0b00111111); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
138 | TIMSK &= ~_BV(OCIE2); //disable timer2 interrupt |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
139 | GICR |= _BV(INT0) ; // Enable INT0 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
140 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
141 | //data = bitbuf; // output data |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
142 | // write data of controllers to array |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
143 | if (data_len == 10) { // controller data packet |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
144 | clock = (bitbuf >> 6) & 0b00000111; |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
145 | slot[clock].speed = (bitbuf >> 1) & 0x0F; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
146 | slot[clock].trackswitch = (bitbuf >> 5) & 1; |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
147 | // current response for this car? |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
148 | if (response != 0) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
149 | if ( ((response & 0b00001110) >> 1) == clock) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
150 | // add our ID to response: |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
151 | send_response(response | (self_id << 6)); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
152 | response = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
153 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
154 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
155 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
156 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
157 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
158 | } else { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
159 | bitbuf_len++; // increment bit counter |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
160 | bitbuf = bitbuf << 1; // shift bits |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
161 | if (state2 == 0) bitbuf |= 1; // receive logic one |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
162 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
163 | } else { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
164 | bitbuf_len |= _BV(7); // switch clock to high |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
165 | // first pulse of bit |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
166 | if (state2) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
167 | bitbuf_len |= _BV(6); // store new state |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
168 | } else { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
169 | bitbuf_len &= ~_BV(6); // store new state |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
170 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
171 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
172 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
173 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
174 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
175 | ISR (TIMER1_OVF_vect) { |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
176 | // reset car counters to overflow |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
177 | uint8_t i; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
178 | for (i=0;i<MAX_SENSORS;i++) |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
179 | sens[i].old = 0xffff; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
180 | } |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
181 | |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
182 | void detect_car(uint8_t idx) { |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
183 | uint16_t tmp = 0; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
184 | sens[idx].new = TCNT1; // get current counter |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
185 | if (sens[idx].old < sens[idx].new) { |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
186 | // calculate difference |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
187 | if (sens[idx].car == 0) tmp = sens[idx].new-sens[idx].old; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
188 | if ( (tmp > 54) && (tmp < 74) ) tmp = 1; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
189 | else if ( (tmp > 118) && (tmp < 138) ) tmp = 2; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
190 | else if ( (tmp > 186) && (tmp < 206) ) tmp = 3; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
191 | else if ( (tmp > 246) && (tmp < 266) ) tmp = 4; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
192 | else if ( (tmp > 310) && (tmp < 330) ) tmp = 5; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
193 | else if ( (tmp > 374) && (tmp < 394) ) tmp = 6; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
194 | else tmp = 0; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
195 | sens[idx].car = tmp; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
196 | } |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
197 | sens[idx].old = sens[idx].new; |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
198 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
199 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
200 | ISR (INT1_vect) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
201 | // car0 detector |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
202 | detect_car(0); |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
203 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
204 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
205 | #ifdef ANALOG_COMPARATOR |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
206 | ISR (ANA_COMP_vect) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
207 | // car1 detector |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
208 | detect_car(1); |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
209 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
210 | #else |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
211 | // ALTERNATIV: |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
212 | ISR (TIMER1_CAPT_vect) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
213 | // car1 detector |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
214 | detect_car(1); |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
215 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
216 | #endif |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
217 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
218 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
219 | int main(void) |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
220 | { |
66
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
221 | uint8_t tmp; |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
222 | |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
223 | // setup data bit timer2 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
224 | TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
225 | OCR2 = TIMER2_50US; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
226 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
227 | // initialize timer1 for IR signal detection |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
228 | #ifdef ANALOG_COMPARATOR |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
229 | TCCR1B = _BV(CS01) ; // 1mhz clock |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
230 | TIMSK = _BV(OCIE2) | _BV(TOIE1) ; //enable timer1+2 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
231 | #else |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
232 | TCCR1B = _BV(CS01) | _BV(ICNC1) | _BV(ICES1); // 1mhz clock, enable ICP on rising edge |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
233 | TIMSK = _BV(OCIE2) | _BV(TOIE1) | _BV(TICIE1); //enable timer1+2 / ICP1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
234 | #endif |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
235 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
236 | // enable both external interrupts |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
237 | // int 0 = data RX |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
238 | // int 1 = car0 input |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
239 | MCUCR = _BV(ISC00) | _BV(ISC01) | _BV(ISC10) | _BV(ISC11); // INT0/1 rising edge |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
240 | GICR = _BV(INT0) | _BV(INT1) ; // Enable INT0 + INT1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
241 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
242 | #ifdef ANALOG_COMPARATOR |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
243 | ACSR = _BV(ACIE) | _BV(ACIS1) | _BV(ACIS0); // setup analog comparator |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
244 | #endif |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
245 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
246 | // oscillator calibration |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
247 | // atmega8@1mhz = 0xac |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
248 | // @4mhz = ca 0xa0 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
249 | //OSCCAL = 0xa0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
250 | //OSCCAL = 0x9A; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
251 | //OSCCAL = 0xa0; // internal oscillator @ 4 mhz.... doesnt work accurate! |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
252 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
253 | RS232_init(); // initialize RS232 interface |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
254 | RS232_puts_p(PSTR("Freeslot TrackSwitch / Pitlane v1.3\n")); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
255 | sei(); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
256 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
257 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
258 | DDR(SOLENOID_A_PORT) |= _BV(SOLENOID_A_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
259 | DDR(SOLENOID_B_PORT) |= _BV(SOLENOID_B_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
260 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
261 | DDR(RESPONSE_PORT) &= ~_BV(RESPONSE_PIN); // switch response off |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
262 | RESPONSE_PORT &= ~_BV(RESPONSE_PIN); // switch response off |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
263 | |
65
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
264 | // setup pitlane output |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
265 | DDR(PIT_PORT) |= _BV(PIT_CONNECT_PIN) | _BV(PIT_GROUND_PIN); |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
266 | |
66
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
267 | // setup LED |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
268 | DDR(LED_PORT) |= _BV(LED_PIN); |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
269 | LED_PORT |= _BV(LED_PIN); // switch LED off |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
270 | |
65
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
271 | // CONNECT PITLANE TO MAIN TRACK |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
272 | PIT_PORT &= ~_BV(PIT_CONNECT_PIN); |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
273 | PIT_PORT |= _BV(PIT_GROUND_PIN); |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
274 | |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
275 | while (1) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
276 | // main loop |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
277 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
278 | /* |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
279 | 0 = AA |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
280 | 1 = AB |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
281 | 2 = BB |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
282 | 3 = BA |
65
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
283 | 4 = ZZ -> pitlane exit |
67 | 284 | 5 = BC |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
285 | */ |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
286 | if (sens[0].car != sens[0].state) { |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
287 | sens[0].state = sens[0].car; |
67 | 288 | #if TRACKSWITCH_TYPE != TYPE_PITLANE |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
289 | if ( (sens[0].state != 0) && (slot[sens[0].state-1].trackswitch == 0) && (slot[sens[0].state-1].speed>0) ) { |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
290 | response = (1 | ((sens[0].state-1)<<1) | (1 << 4)); |
65
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
291 | |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
292 | // set inside status |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
293 | slot[sens[0].state].inside = 1; |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
294 | |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
295 | // trigger solenoid A |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
296 | RS232_putc('A'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
297 | RS232_putc('B'); |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
298 | RS232_putc('0'+sens[0].state); |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
299 | RS232_putc('\n'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
300 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
301 | SOLENOID_A_PORT |= _BV(SOLENOID_A_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
302 | solenoid_delay(); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
303 | SOLENOID_A_PORT &= ~_BV(SOLENOID_A_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
304 | solenoid_delay(); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
305 | } |
67 | 306 | #endif |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
307 | if (sens[0].state != 0) { |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
308 | response = (1 | ((sens[0].state-1)<<1)); |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
309 | RS232_putc('A'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
310 | RS232_putc('A'); |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
311 | RS232_putc('0'+sens[0].state); |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
312 | RS232_putc('\n'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
313 | } |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
314 | } sens[0].car = 0; |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
315 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
316 | |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
317 | if (sens[1].car != sens[1].state) { |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
318 | sens[1].state = sens[1].car; |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
319 | if ( (sens[1].state != 0) && (slot[sens[1].state-1].trackswitch == 0) && (slot[sens[1].state-1].speed>0) ) { |
67 | 320 | response = (1 | ((sens[1].state-1)<<1) | (5 << 4)); |
65
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
321 | |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
322 | // set inside status |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
323 | slot[sens[1].state-1].inside = 1; |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
324 | |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
325 | // trigger solenoid B |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
326 | RS232_putc('B'); |
67 | 327 | RS232_putc('C'); |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
328 | RS232_putc('0'+sens[1].state); |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
329 | RS232_putc('\n'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
330 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
331 | SOLENOID_B_PORT |= _BV(SOLENOID_B_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
332 | solenoid_delay(); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
333 | SOLENOID_B_PORT &= ~_BV(SOLENOID_B_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
334 | solenoid_delay(); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
335 | } |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
336 | if (sens[1].state != 0) { |
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
337 | response = (1 | ((sens[1].state)<<1) | (2 << 4)); |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
338 | RS232_putc('B'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
339 | RS232_putc('B'); |
64
61f88f973eba
structured vars in pitlane code
Malte Bayer <mbayer@neo-soft.org>
parents:
63
diff
changeset
|
340 | RS232_putc('0'+sens[1].state); |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
341 | RS232_putc('\n'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
342 | } |
65
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
343 | } sens[1].car = 0; |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
344 | |
66
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
345 | if ( (PIN(SENS2_PORT) & _BV(SENS2_PIN)) != 0 ) { |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
346 | // set inside status |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
347 | for (tmp=0; tmp<MAX_SLOTS; tmp++) |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
348 | slot[tmp].inside = 0; |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
349 | RS232_puts_p(PSTR("PIT:EXIT\n")); |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
350 | } |
65
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
351 | |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
352 | if (sens[2].car != sens[2].state) { |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
353 | sens[2].state = sens[2].car; |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
354 | if (sens[2].state != 0) { |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
355 | response = (1 | ((sens[2].state-1)<<1) | (4 << 4)); |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
356 | |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
357 | // set inside status |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
358 | slot[sens[2].state-1].inside = 0; |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
359 | |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
360 | RS232_putc('Z'); |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
361 | RS232_putc('Z'); |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
362 | RS232_putc('0'+sens[2].state); |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
363 | RS232_putc('\n'); |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
364 | } |
8960bfa02f63
connected pitlane to main track (correct bit setting)
Malte Bayer <mbayer@neo-soft.org>
parents:
64
diff
changeset
|
365 | } sens[2].car = 0; |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
366 | |
66
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
367 | // enable LED when car is in pitlane |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
368 | LED_PORT |= _BV(LED_PIN); |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
369 | for (tmp=0; tmp<MAX_SLOTS; tmp++) |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
370 | if (slot[tmp].inside) LED_PORT &= ~_BV(LED_PIN); |
1e220602b7a7
pitlane response enter/exit pitlane, led lit when car is in pitlane
Malte Bayer <mbayer@neo-soft.org>
parents:
65
diff
changeset
|
371 | |
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
372 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
373 | } // main loop end |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
374 | }; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
375 |