Sat, 03 Dec 2011 14:25:06 +0100
feature: program a specific controller speed limit without changing the car's acceleration
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
1 | #include <avr/interrupt.h> |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
2 | #include <avr/pgmspace.h> |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
3 | #include <util/delay.h> |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
4 | #include "stdint.h" |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
5 | #include "main.h" |
3 | 6 | #include "driver/adc.h" |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
7 | #include "driver/rs232.h" |
3 | 8 | #include "lowlevel.h" |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
9 | |
34 | 10 | void blinkdelay(void) { |
11 | _delay_ms(50); | |
12 | } | |
13 | ||
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
14 | void LED(uint8_t num, uint8_t state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
15 | switch (num) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
16 | case 1: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
17 | case 0: LED1_PORT &= ~_BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
18 | case 1: LED1_PORT |= _BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
19 | case 2: LED1_PORT ^= _BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
20 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
21 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
22 | case 2: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
23 | case 0: LED2_PORT &= ~_BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
24 | case 1: LED2_PORT |= _BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
25 | case 2: LED2_PORT ^= _BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
26 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
27 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
28 | case 3: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
29 | case 0: LED3_PORT &= ~_BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
30 | case 1: LED3_PORT |= _BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
31 | case 2: LED3_PORT ^= _BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
32 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
33 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
34 | case 4: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
35 | case 0: LED4_PORT &= ~_BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
36 | case 1: LED4_PORT |= _BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
37 | case 2: LED4_PORT ^= _BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
38 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
39 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
40 | case 5: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
41 | case 0: LED5_PORT &= ~_BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
42 | case 1: LED5_PORT |= _BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
43 | case 2: LED5_PORT ^= _BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
44 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
45 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
46 | } |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
47 | |
3 | 48 | void init_hardware(void) { |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
49 | // reset all ports to input, no pullup |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
50 | DDRA = 0; PORTA = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
51 | DDRB = 0; PORTB = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
52 | DDRC = 0; PORTC = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
53 | DDRD = 0; PORTD = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
54 | |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
55 | |
34 | 56 | RS232_init(); // initialize RS232 interface |
3 | 57 | initADC(); |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
58 | SFIOR = 0; |
3 | 59 | |
60 | // set LED output | |
61 | DDR(LED1_PORT) |= _BV(LED1); | |
62 | DDR(LED2_PORT) |= _BV(LED2); | |
63 | DDR(LED3_PORT) |= _BV(LED3); | |
64 | DDR(LED4_PORT) |= _BV(LED4); | |
65 | DDR(LED5_PORT) |= _BV(LED5); | |
66 | ||
67 | // set Controller Input Pull-UPs | |
68 | CONTROLLER_PORT |= (_BV(CONTROLLER1_SW) | _BV(CONTROLLER2_SW) | _BV(CONTROLLER3_SW) | _BV(CONTROLLER4_SW)); | |
69 | ||
70 | // switch pull-ups | |
71 | SW_FUEL_PORT |= _BV(SW_FUEL); | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
72 | SW_START_PORT |= _BV(SW_START); |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
73 | SW_PACECAR_PORT |= _BV(SW_PACECAR); |
3 | 74 | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
75 | // control outputs |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
76 | DDR(LAP_COUNTER_PORT) |= _BV(LAP_COUNTER); |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
77 | |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
78 | // setup rail control |
3 | 79 | //RAIL_DETECT_PORT |= _BV(RAIL_DETECT); // enable internal pull-up |
80 | DDR(RAIL_POWER_PORT) |= _BV(RAIL_POWER); | |
81 | ||
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
82 | // display init sequence on LEDs |
34 | 83 | LED(1, 1); blinkdelay(); |
84 | LED(2, 1); blinkdelay(); | |
85 | LED(3, 1); blinkdelay(); | |
86 | LED(4, 1); blinkdelay(); | |
87 | LED(5, 1); blinkdelay(); | |
88 | LED(1, 0); blinkdelay(); | |
89 | LED(2, 0); blinkdelay(); | |
90 | LED(3, 0); blinkdelay(); | |
91 | LED(4, 0); blinkdelay(); | |
92 | LED(5, 0); | |
3 | 93 | |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
94 | |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
95 | // setup response receiver timer |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
96 | TCCR0 = (1<<CS01); //divide by 8 |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
97 | // interrupt enable + tcnt0 set in timer2 |
3 | 98 | |
99 | ||
100 | // setup data bit timer | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
101 | TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match |
3 | 102 | OCR2 = TIMER2_50US; |
103 | TIMSK |= 1<<OCIE2; //enable timer2 interrupt | |
104 | ||
105 | // setup data packet timer | |
106 | //TCCR1A = (1<<COM1A1); | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
107 | TCCR1B = (1<<CS11) | (1<<WGM12); //divide by 8, set compare match |
3 | 108 | //TCCR1B = (1<<CS11) | (1<<CS10); //divide by 64 |
109 | //TCNT1 = TIMER_7500NS; | |
110 | OCR1A = TIMER1_7500NS; | |
111 | TIMSK |= 1<<OCIE1A; //enable timer1 interrupt | |
112 | ||
33 | 113 | RS232_puts_p(PSTR("FreeSlot Blackbox v1.0\n")); |
3 | 114 | |
115 | } | |
116 | ||
117 | ||
118 | ||
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
119 | void check_rails_shortcut(void) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
120 | // check for short circuit on the rails |
10 | 121 | uint8_t i = 100; |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
122 | if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) { |
10 | 123 | while (i>0) { |
124 | if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) != 0) return 0; | |
125 | _delay_us(20); | |
33 | 126 | i--; |
10 | 127 | } |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
128 | if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
129 | cli(); // disable ALL Interrupts |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
130 | RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // disable rails power |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
131 | RS232_puts_p(PSTR("!!! SHORT CIRCUIT ON RAILS, POWERED OFF !!!\n")); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
132 | LED(1, 1); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
133 | LED(2, 1); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
134 | LED(3, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
135 | LED(4, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
136 | LED(5, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
137 | while (1) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
138 | LED(1, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
139 | LED(2, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
140 | LED(4, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
141 | LED(5, 2); |
33 | 142 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
143 | LED(3, 2); |
33 | 144 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
145 | LED(3, 2); |
33 | 146 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
147 | LED(3, 2); |
33 | 148 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
149 | LED(3, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
150 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
151 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
152 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
153 | } |