Sat, 03 Dec 2011 14:25:06 +0100
feature: program a specific controller speed limit without changing the car's acceleration
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
1 | #include <avr/interrupt.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
2 | #include <avr/io.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
3 | #include <avr/wdt.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
4 | #include <avr/eeprom.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
5 | #include <stdlib.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
6 | #include <stdint.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
7 | #include <avr/pgmspace.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
8 | #include <util/delay.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
9 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
10 | #include "driver/rs232.h" |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
11 | #include "driver/adc.h" |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
12 | |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
13 | #include "main.h" |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
14 | #include "lowlevel.h" |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
15 | |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
16 | #define MAX_SLOTS 5 |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
17 | uint8_t speedlimit[MAX_SLOTS]; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
18 | |
4
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
19 | volatile uint8_t program_count = 0; |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
20 | volatile uint8_t program_id; |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
21 | volatile uint8_t program_command; |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
22 | volatile uint8_t program_parameter; |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
23 | |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
24 | volatile uint8_t datalen = 0; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
25 | char data[10]; // 8 bytes data buffer + string termination |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
26 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
27 | static char buffer[RS232_BUFSIZE+1]; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
28 | static uint8_t buffer_len; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
29 | |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
30 | |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
31 | volatile uint16_t transmit_buffer; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
32 | volatile uint16_t transmit_buffer_queue; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
33 | volatile uint8_t transmit_len; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
34 | volatile uint8_t transmit_len_next; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
35 | volatile uint8_t transmit_len_queue; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
36 | |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
37 | volatile uint16_t response; |
33 | 38 | volatile uint8_t response_len; |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
39 | volatile uint8_t timer0_delay; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
40 | |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
41 | |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
42 | int insert_queue(uint16_t tmp, uint8_t len) { |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
43 | if (transmit_buffer_queue == 0) { |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
44 | transmit_buffer_queue = tmp; |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
45 | transmit_len_queue = len; |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
46 | return 1; |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
47 | } |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
48 | return 0; |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
49 | } |
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
50 | |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
51 | char ok[] PROGMEM="OK\n"; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
52 | char busy[] PROGMEM="BUSY\n"; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
53 | |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
54 | |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
55 | ISR ( USART_RXC_vect ) { |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
56 | uint8_t tmp; |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
57 | char c = UDR; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
58 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
59 | // check for buffer overflow |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
60 | if (buffer_len==sizeof(buffer)) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
61 | buffer_len=0; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
62 | } else { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
63 | // collect characters until end of line |
35
00166228a419
cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
64 | if ( (c=='\n') ) { |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
65 | buffer[buffer_len]=0; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
66 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
67 | // packet end received, parse the received packet |
4
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
68 | switch (buffer[0]) { |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
69 | case 'P': // inject a program data word to the rails |
5
7319de27d44a
added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
70 | if (program_count == 0) { |
7319de27d44a
added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
71 | program_id = buffer[3]-'0'; |
7319de27d44a
added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
72 | program_command = buffer[1]-'0'; |
10 | 73 | if (program_command > 9) |
74 | program_command = buffer[1]-'A'+10; | |
5
7319de27d44a
added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
75 | program_parameter = buffer[2]-'0'; |
10 | 76 | if (program_parameter > 9) |
77 | program_parameter = buffer[2]-'A'+10; | |
5
7319de27d44a
added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
78 | if (program_command < 4) |
7319de27d44a
added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
79 | program_count = 0x02; // send commands twice (fuel, speed, brake) |
7319de27d44a
added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
80 | else |
7319de27d44a
added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
81 | program_count = 0x01; |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
82 | RS232_puts_p(ok); |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
83 | } else RS232_puts_p(busy); |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
84 | break; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
85 | |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
86 | case 'L': // Limit maximum speed for a car |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
87 | tmp = buffer[2]-'0'; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
88 | if (tmp > 9) |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
89 | tmp = buffer[2]-'A'+10; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
90 | speedlimit[buffer[1]-'0'] = tmp; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
91 | RS232_puts_p(ok); |
5
7319de27d44a
added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
92 | break; |
4
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
93 | |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
94 | } |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
95 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
96 | // wait for the next packet |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
97 | buffer_len=0; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
98 | } else { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
99 | buffer[buffer_len++]=c; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
100 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
101 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
102 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
103 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
104 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
105 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
106 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
107 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
108 | int do_controller(uint8_t controller) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
109 | // read controller X speed & encode controller data packet |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
110 | uint16_t tmp; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
111 | switch (controller) { |
3 | 112 | case 0: |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
113 | tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F); |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
114 | if (tmp > speedlimit[controller]) tmp = speedlimit[controller]; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
115 | tmp = tmp << 1; |
10 | 116 | if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) { |
117 | tmp |= (1<<5); | |
118 | LED(1,0); | |
119 | } else LED(1,1); | |
3 | 120 | break; |
121 | case 1: | |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
122 | tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F); |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
123 | if (tmp > speedlimit[controller]) tmp = speedlimit[controller]; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
124 | tmp = tmp << 1; |
10 | 125 | if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW)) != 0) { |
126 | tmp |= (1<<5); | |
127 | LED(2,0); | |
128 | } else LED(2,1); | |
3 | 129 | break; |
130 | case 2: | |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
131 | tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F); |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
132 | if (tmp > speedlimit[controller]) tmp = speedlimit[controller]; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
133 | tmp = tmp << 1; |
10 | 134 | if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW)) != 0) { |
135 | tmp |= (1<<5); | |
136 | LED(3,0); | |
137 | } else LED(3,1); | |
3 | 138 | break; |
139 | case 3: | |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
140 | tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F); |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
141 | if (tmp > speedlimit[controller]) tmp = speedlimit[controller]; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
142 | tmp = tmp << 1; |
10 | 143 | if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW)) != 0) { |
144 | tmp |= (1<<5); | |
145 | LED(4,0); | |
146 | } else LED(4,1); | |
3 | 147 | break; |
148 | case 4: tmp = (1<<5); break; // todo regler 5 | |
149 | case 5: tmp = (1<<5); break; // todo regler 6 | |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
150 | } |
34 | 151 | tmp |= (0b1000000000 | (controller << 6)); |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
152 | if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
153 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
154 | return insert_queue(tmp, 9); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
155 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
156 | |
4
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
157 | uint8_t mirror( uint8_t n ) { |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
158 | n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
159 | n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
160 | n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
161 | return n; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
162 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
163 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
164 | int do_program(uint8_t controller, uint8_t command, uint8_t parameter) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
165 | // send program data packet |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
166 | uint16_t tmp; |
4
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
167 | parameter = mirror(parameter); |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
168 | controller = mirror(controller); |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
169 | command = mirror(command); |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
170 | tmp = 0b1000000000000 | (parameter << 4) | command | (controller >> 5); |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
171 | return insert_queue(tmp, 12); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
172 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
173 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
174 | int do_active(void) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
175 | // send controller active data packet |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
176 | uint16_t tmp = 0b10000000; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
177 | if ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b11000001; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
178 | if ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10100001; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
179 | if ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10010001; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
180 | if ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10001001; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
181 | // todo: regler 5 und 6 |
4
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
182 | // todo: wenn Daten enpfangen wurden hier eine Quittierung senden anstatt dem Active Word |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
183 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
184 | return insert_queue(tmp, 7); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
185 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
186 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
187 | int do_pace_ghost(void) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
188 | // send ghost and pacecar data packet |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
189 | // todo: at the moment, both disabled! |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
190 | uint16_t tmp = 0b1111100000; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
191 | if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
192 | // todo: PC, NH, TK, (KFR, FR) |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
193 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
194 | return insert_queue(tmp, 9); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
195 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
196 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
197 | ISR ( TIMER1_COMPA_vect ) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
198 | // trigger packet transfer: |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
199 | transmit_len = transmit_len_next; |
10 | 200 | //LED(2,2); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
201 | |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
202 | // here is some more time to do something else... |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
203 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
204 | |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
205 | ISR ( TIMER2_COMP_vect ) { |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
206 | //OCR2 = TIMER2_50US; // make sure that timer2 is 50µs !!! |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
207 | // data packet timer 100µs pro bit... |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
208 | if (transmit_len >= 0xFE) { |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
209 | if (transmit_len != 0xFF) { |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
210 | RAIL_POWER_PORT |= _BV(RAIL_POWER); // end of transmission |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
211 | transmit_len = 0xFF; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
212 | transmit_buffer = transmit_buffer_queue; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
213 | transmit_buffer_queue = 0; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
214 | transmit_len_next = transmit_len_queue; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
215 | |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
216 | // start the response receiver timer |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
217 | // TODO: only on 8 timeslots, not on every transmission |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
218 | // TODO: give slot number to timer - then store the transmission to 8 slots array |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
219 | TCNT0 = TIMER0_250US; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
220 | timer0_delay = TIMER0_2300NS; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
221 | response = 0; |
33 | 222 | response_len = 0; |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
223 | TIMSK |= _BV(TOIE0); |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
224 | |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
225 | } |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
226 | } else { |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
227 | uint16_t bit = (1<<(transmit_len & 0b01111111)); |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
228 | uint16_t clock; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
229 | if ((transmit_len & 0b10000000) == 0) clock = 0; else clock = 0xffff; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
230 | if ( ((transmit_buffer ^ clock) & bit) != 0 ) |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
231 | RAIL_POWER_PORT |= _BV(RAIL_POWER); else |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
232 | RAIL_POWER_PORT &= ~_BV(RAIL_POWER); |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
233 | if ( (transmit_len & 0b10000000) == 0 ) { |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
234 | // block 0 |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
235 | //if (transmit_len == 0) transmit_len = 0xFF; else transmit_len |= 0b10000000; // set clock |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
236 | transmit_len |= 0b10000000; // set clock |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
237 | } else { |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
238 | // block 1, output the current bit |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
239 | transmit_len &= 0b01111111; // reset clock |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
240 | //if (transmit_len != 0) transmit_len--; // next bit |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
241 | if (transmit_len == 0) transmit_len = 0xFE; else transmit_len--; // next bit |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
242 | } |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
243 | } |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
244 | |
10 | 245 | //LED(3,2); |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
246 | } |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
247 | |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
248 | |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
249 | ISR ( TIMER0_OVF_vect ) { |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
250 | // TODO: last bit should be set by the sender, not from us! |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
251 | TCNT0 = TIMER0_250US; |
10 | 252 | //LED(1,2); |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
253 | if (timer0_delay == 0) { |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
254 | RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // pull rails low |
8 | 255 | _delay_us(28); // wait some cycles |
256 | if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) != 0) { // check for logic zero | |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
257 | if (response == 0) { |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
258 | // there is no start bit, so stop the timer and cancel response receiving |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
259 | TIMSK &= ~_BV(TOIE0); |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
260 | } else { |
8 | 261 | // we received a bit (logic low) |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
262 | response = response << 1; |
33 | 263 | response_len++; |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
264 | } |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
265 | } else { |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
266 | // okay, we have logic high |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
267 | response = response << 1; |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
268 | response |= 1; |
33 | 269 | response_len++; |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
270 | } |
33 | 271 | if (response_len == 15) { // maximum response length reached |
272 | RAIL_POWER_PORT |= _BV(RAIL_POWER); // restore rails power | |
273 | TIMSK &= ~_BV(TOIE0); | |
274 | } else { | |
275 | _delay_us(20); // wait some cycles | |
276 | RAIL_POWER_PORT |= _BV(RAIL_POWER); // restore rails power | |
277 | } | |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
278 | } else timer0_delay--; // 2.3 ms delay not reached yet |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
279 | } |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
5
diff
changeset
|
280 | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
281 | ISR (INT2_vect) { |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
282 | // Lap counter Interrupt |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
283 | LED(5,2); |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
284 | } |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
285 | |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
286 | void reset_vars(void) { |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
287 | uint8_t i; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
288 | for (i=0; i<MAX_SLOTS; i++) { |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
289 | speedlimit[i] = 15; |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
290 | } |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
291 | } |
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
292 | |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
293 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
294 | int main(void) |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
295 | { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
296 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
297 | unsigned char s[30]; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
298 | uint16_t tmp; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
299 | uint8_t packet_index = 1; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
300 | |
8 | 301 | uint8_t btn_start = _BV(SW_START); |
302 | uint8_t old_start = btn_start; | |
303 | uint8_t mode = 0; | |
304 | // valid race modes: | |
305 | // 0: free drive | |
306 | // 1: race countdown initiated | |
307 | // 2: countdown 5 | |
308 | // 3: countdown 4 | |
309 | // 4: countdown 3 | |
310 | // 5: countdown 2 | |
311 | // 6: countdown 1 | |
312 | // 7: race start initiated (next mode will be zero = free drive) | |
313 | ||
314 | ||
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
315 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
316 | init_hardware(); |
36
aea84f4f5a12
feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents:
35
diff
changeset
|
317 | reset_vars(); |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
318 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
319 | // switch on rails power |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
320 | RAIL_POWER_PORT |= _BV(RAIL_POWER); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
321 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
322 | while (1) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
323 | // check for short circuit on the rails |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
324 | check_rails_shortcut(); |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
325 | |
33 | 326 | if (response_len > 0) { |
327 | itoa(response, s, 2); | |
328 | response_len = 0; | |
329 | RS232_puts("ANSWER RX: "); | |
330 | RS232_puts(s); | |
331 | RS232_putc('\n'); | |
332 | } | |
333 | ||
334 | ||
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
335 | // read in button presses |
8 | 336 | btn_start = (PIN(SW_START_PORT) & _BV(SW_START)); |
337 | if (old_start != btn_start) { | |
338 | // start button changed | |
339 | if (btn_start == 0) { | |
340 | // start button press active | |
341 | if (mode == 0) { | |
342 | // Initiate race countdown | |
343 | mode = 0; // todo: set to 1 | |
344 | // issue reset command to lap counter | |
345 | program_command = 6; | |
346 | program_parameter = 9; | |
347 | program_id = 0; | |
348 | program_count = 1; | |
10 | 349 | LAP_COUNTER_PORT |= _BV(LAP_COUNTER); |
8 | 350 | } else { |
351 | // do a reset, switch back to free drive mode 0 | |
352 | mode = 0; | |
353 | // issue reset command | |
354 | program_command = 19; | |
355 | program_parameter = 0; | |
356 | program_id = 7; | |
357 | program_count = 1; | |
10 | 358 | LAP_COUNTER_PORT &= ~_BV(LAP_COUNTER); |
8 | 359 | } |
360 | } | |
361 | //LAP_COUNTER_PORT |= _BV(LAP_COUNTER); | |
362 | } else old_start = btn_start; | |
363 | // LAP_COUNTER_PORT &= ~_BV(LAP_COUNTER); | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
364 | |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
365 | switch (packet_index) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
366 | case 1: |
4
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
367 | if (program_count > 0) { |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
368 | // command in queue |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
369 | if (do_program(program_id, program_command, program_parameter)) { |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
370 | packet_index++; |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
371 | program_count--; |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
372 | } |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
373 | } else { |
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
374 | // output idle command |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
375 | if (do_program(7, 19, 0)) packet_index++; // reset |
8 | 376 | //if (do_program(7, 20, 15)) packet_index++; // reset / pitstop detect |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
377 | //if (insert_queue(0, 0)) packet_index++; // null command |
4
58529e14c45c
implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents:
3
diff
changeset
|
378 | } |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
379 | break; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
380 | case 2: |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
381 | if (do_pace_ghost()) packet_index++; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
382 | break; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
383 | case 3: |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
384 | if (do_active()) packet_index++; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
385 | break; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
386 | case 4: |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
387 | if (do_controller(0)) packet_index++; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
388 | break; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
389 | case 5: |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
390 | if (do_controller(4)) packet_index++; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
391 | break; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
392 | case 6: |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
393 | if (do_controller(1)) packet_index++; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
394 | break; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
395 | case 7: |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
396 | if (do_controller(5)) packet_index++; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
397 | break; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
398 | case 8: |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
399 | if (do_controller(2)) packet_index++; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
400 | break; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
401 | case 9: |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
402 | if (do_active()) packet_index++; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
403 | break; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
404 | case 10: |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
405 | if (do_controller(3)) packet_index = 1; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
406 | // last packet, so reset packet index |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
407 | break; |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
408 | } |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
409 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
410 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
411 | } // main loop end |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
412 | }; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
413 |