pitlane/main.c

Sat, 17 Dec 2011 13:35:40 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Sat, 17 Dec 2011 13:35:40 +0100
changeset 90
7ed95dbadf1f
parent 73
ec888cfa024e
child 104
95b2c23cb973
permissions
-rw-r--r--

added remote start button press

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

mercurial