pitlane/main.c

Sat, 10 Dec 2011 14:12:09 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Sat, 10 Dec 2011 14:12:09 +0100
changeset 69
27c0c0095e26
parent 68
c3460121ad8b
child 70
3a57b7a70683
permissions
-rw-r--r--

implemented responsewire in CU firmware

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

mercurial