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