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