blackbox/main.c

Wed, 07 Dec 2011 17:00:32 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Wed, 07 Dec 2011 17:00:32 +0100
changeset 43
4e5d389f7b83
parent 42
018d4d63ad3b
child 44
4f4326794ba4
permissions
-rw-r--r--

jumpstart time output via interface

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
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
16 char ok[] PROGMEM="OK\n";
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
17 char busy[] PROGMEM="BUSY\n";
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
18 char prepare[] PROGMEM="!RACE PREPARE\n";
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
19 char countdownstart[] PROGMEM="!COUNTDOWN\n";
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
20 char racestart[] PROGMEM="!RACE START\n";
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
21
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
22
43
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
23 static unsigned char s[10];
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
24 static uint8_t countdown, countdown_loops;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
25 uint8_t mode = 0;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
26 // valid race modes:
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
27 // 0: free drive / idle
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
28 // 1: waiting for countdown start
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
29 // 2: race countdown initiated
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
30 // 3: Race start condition
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
31
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
32
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
33 #define MAX_SLOTS 6
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
34 volatile uint16_t sysclk_ms_low = 0;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
35 volatile uint16_t sysclk_ms_high = 0;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
36 volatile uint8_t sysclk_packettimer = 0;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
37
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
38 volatile uint8_t speedlimit[MAX_SLOTS];
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
39 volatile uint8_t fuel[MAX_SLOTS];
43
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
40 volatile uint16_t jumpstart_time[MAX_SLOTS];
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
41
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
42 volatile uint16_t car0, car1;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
43 volatile uint16_t car0_new, car0_old;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
44 volatile uint16_t car1_new, car1_old;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
45 uint8_t car0_state, car1_state;
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
46
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
47 volatile uint8_t program_count = 0;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
48 volatile uint8_t program_id;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
49 volatile uint8_t program_command;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
50 volatile uint8_t program_parameter;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
51
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
52 volatile uint8_t datalen = 0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
53 char data[10]; // 8 bytes data buffer + string termination
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
54
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
55 static char buffer[RS232_BUFSIZE+1];
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
56 static uint8_t buffer_len;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
57
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
58 volatile uint16_t transmit_buffer;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
59 volatile uint16_t transmit_buffer_queue;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
60 volatile uint8_t transmit_len;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
61 volatile uint8_t transmit_len_next;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
62 volatile uint8_t transmit_len_queue;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
63
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
64 volatile uint16_t response;
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
65 volatile uint8_t response_len;
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
66 volatile uint8_t timer0_delay;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
67
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
68
35
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
69 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
70 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
71 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
72 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
73 return 1;
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
74 }
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
75 return 0;
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
76 }
00166228a419 cli: implemented setting of fuel, brake, speed, blinkstate for any car
Malte Bayer <mbayer@neo-soft.org>
parents: 34
diff changeset
77
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
78 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
79 uint8_t tmp;
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
80 char c = UDR;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
81
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
82 // check for buffer overflow
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
83 if (buffer_len==sizeof(buffer)) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
84 buffer_len=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
85 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
86 // 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
87 if ( (c=='\n') ) {
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
88 buffer[buffer_len]=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
89
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
90 // packet end received, parse the received packet
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
91 switch (buffer[0]) {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
92 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
93 if (program_count == 0) {
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
94 program_id = buffer[3]-'0';
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
95 program_command = buffer[1]-'0';
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
96 if (program_command > 9)
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
97 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
98 program_parameter = buffer[2]-'0';
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
99 if (program_parameter > 9)
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
100 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
101 if (program_command < 4)
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
102 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
103 else
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
104 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
105 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
106 } 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
107 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
108
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
109 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
110 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
111 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
112 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
113 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
114 RS232_puts_p(ok);
5
7319de27d44a added queue busy response on command injection
Malte Bayer <mbayer@neo-soft.org>
parents: 4
diff changeset
115 break;
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
116
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
117 case 'I': // get Information data (incl. important global parameter dump)
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
118 RS232_puts(VERSION);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
119 RS232_putc(':');
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
120 for (tmp=0;tmp<MAX_SLOTS;tmp++) RS232_putc(speedlimit[tmp]); // output speed limits
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
121 RS232_putc(':');
43
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
122 for (tmp=0;tmp<MAX_SLOTS;tmp++) {
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
123 itoa(fuel[tmp], s, 16);
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
124 RS232_putc(s); // output fuel levels (0=empty, 100=full, 0xff=no fuel option)
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
125 RS232_putc(',');
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
126 }
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
127 RS232_putc(':');
43
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
128 for (tmp=0;tmp<MAX_SLOTS;tmp++) {
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
129 itoa(jumpstart_time[tmp], s, 16);
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
130 RS232_puts(s); // output jumpstart times
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
131 RS232_putc(',');
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
132 }
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
133 RS232_putc(':');
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
134 RS232_putc('\n');
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
135 break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
136
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
137 }
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
138
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
139 // wait for the next packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
140 buffer_len=0;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
141 } else {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
142 buffer[buffer_len++]=c;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
143 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
144 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
145 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
146
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
147
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
148 void jumpstart(uint8_t controller) {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
149 if (jumpstart_time[controller] == 0) {
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
150 jumpstart_time[controller] = sysclk_ms_low;
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
151 RS232_putc('~');
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
152 RS232_putc('0'-controller);
43
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
153 RS232_putc(',');
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
154 itoa(jumpstart_time[controller], s, 16);
4e5d389f7b83 jumpstart time output via interface
Malte Bayer <mbayer@neo-soft.org>
parents: 42
diff changeset
155 RS232_puts(s);
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
156 RS232_putc('\n');
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
157 }
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
158
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
159 }
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
160
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
161
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
162 int do_controller(uint8_t controller) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
163 // read controller X speed & encode controller data packet
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
164 uint16_t tmp = 0;
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
165 switch (controller) {
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
166 case 0:
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
167 if (mode!=1) tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
168 if ((mode == 2) && (tmp != 0)) { jumpstart(controller); tmp = 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
169 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
170 tmp = tmp << 1;
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
171 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) {
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
172 tmp |= (1<<5);
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
173 if (mode == 0) LED(1,0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
174 } else if (mode == 0) LED(1,1);
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
175 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
176 case 1:
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
177 if (mode!=1) tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
178 if ((mode == 2) && (tmp != 0)) { jumpstart(controller); tmp = 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
179 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
180 tmp = tmp << 1;
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
181 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW)) != 0) {
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
182 tmp |= (1<<5);
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
183 if (mode == 0) LED(2,0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
184 } else if (mode == 0) LED(2,1);
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
185 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
186 case 2:
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
187 if (mode!=1) tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
188 if ((mode == 2) && (tmp != 0)) { jumpstart(controller); tmp = 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
189 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
190 tmp = tmp << 1;
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
191 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW)) != 0) {
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
192 tmp |= (1<<5);
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
193 if (mode == 0) LED(4,0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
194 } else if (mode == 0) LED(4,1);
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
195 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
196 case 3:
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
197 if (mode!=1) tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
198 if ((mode == 2) && (tmp != 0)) { jumpstart(controller); tmp = 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
199 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
200 tmp = tmp << 1;
10
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
201 if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW)) != 0) {
6d6e982bbc41 meeting2 stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 8
diff changeset
202 tmp |= (1<<5);
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
203 if (mode == 0) LED(5,0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
204 } else if (mode == 0) LED(5,1);
3
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
205 break;
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
206 case 4: tmp = (1<<5); break; // todo regler 5
1a0ef40ef458 fixed track change keys
Malte Bayer <mbayer@neo-soft.org>
parents: 2
diff changeset
207 case 5: tmp = (1<<5); break; // todo regler 6
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
208 }
34
d12d095e3282 some code cleanup
Malte Bayer <mbayer@neo-soft.org>
parents: 33
diff changeset
209 tmp |= (0b1000000000 | (controller << 6));
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
210 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
211
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
212 return insert_queue(tmp, 9);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
213 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
214
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
215 uint8_t mirror( uint8_t n ) {
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
216 n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
217 n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
218 n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
219 return n;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
220 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
221
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
222 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
223 // send program data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
224 uint16_t tmp;
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
225 parameter = mirror(parameter);
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
226 controller = mirror(controller);
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
227 command = mirror(command);
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
228 tmp = 0b1000000000000 | (parameter << 4) | command | (controller >> 5);
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
229 return insert_queue(tmp, 12);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
230 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
231
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
232 int do_active(void) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
233 // send controller active data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
234 uint16_t tmp = 0b10000000;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
235 if ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b11000001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
236 if ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10100001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
237 if ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10010001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
238 if ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10001001;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
239 // todo: regler 5 und 6
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
240 // 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
241
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
242 return insert_queue(tmp, 7);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
243 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
244
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
245 int do_pace_ghost(void) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
246 // send ghost and pacecar data packet
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
247 // todo: at the moment, both disabled!
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
248 uint16_t tmp = 0b1111100000;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
249 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
250 // todo: PC, NH, TK, (KFR, FR)
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
251
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
252 return insert_queue(tmp, 9);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
253 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
254
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
255 ISR ( TIMER1_COMPA_vect ) {
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
256 //PORTC ^= _BV(PC0); // DEBUG OUTPUT SYSTEM CLOCK
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
257
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
258 // trigger packet transfer:
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
259 if (sysclk_packettimer == 14) { // 15*500 = 7500 NS
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
260 transmit_len = transmit_len_next;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
261 sysclk_packettimer = 0;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
262 } else sysclk_packettimer++;
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
263 // here is some more time to do something else...
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
264
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
265 // reset both car counters to overflow
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
266 car0_old = TIMER1_500NS;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
267 car1_old = TIMER1_500NS;
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
268
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
269 sysclk_ms_low++; // increment 500ns timer
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
270 if (sysclk_ms_low == 0) sysclk_ms_high++; // increment upper 16bits
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
271 }
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
272
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
273 ISR ( TIMER2_COMP_vect ) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
274 //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
275 // data packet timer 100µs pro bit...
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
276 if (transmit_len >= 0xFE) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
277 if (transmit_len != 0xFF) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
278 RAIL_POWER_PORT |= _BV(RAIL_POWER); // end of transmission
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
279 transmit_len = 0xFF;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
280 transmit_buffer = transmit_buffer_queue;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
281 transmit_buffer_queue = 0;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
282 transmit_len_next = transmit_len_queue;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
283
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
284 // start the response receiver timer
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
285 // TODO: only on 8 timeslots, not on every transmission
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
286 // 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
287 TCNT0 = TIMER0_250US;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
288 timer0_delay = TIMER0_2300NS;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
289 response = 0;
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
290 response_len = 0;
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
291 TIMSK |= _BV(TOIE0);
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
292
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
293 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
294 } else {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
295 uint16_t bit = (1<<(transmit_len & 0b01111111));
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
296 uint16_t clock;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
297 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
298 if ( ((transmit_buffer ^ clock) & bit) != 0 )
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
299 RAIL_POWER_PORT |= _BV(RAIL_POWER); else
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
300 RAIL_POWER_PORT &= ~_BV(RAIL_POWER);
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
301 if ( (transmit_len & 0b10000000) == 0 ) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
302 // block 0
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
303 //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
304 transmit_len |= 0b10000000; // set clock
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
305 } else {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
306 // block 1, output the current bit
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
307 transmit_len &= 0b01111111; // reset clock
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
308 //if (transmit_len != 0) transmit_len--; // next bit
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
309 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
310 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
311 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
312 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
313
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
314
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
315 ISR ( TIMER0_OVF_vect ) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
316 // 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
317 TCNT0 = TIMER0_250US;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
318 if (timer0_delay == 0) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
319 RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // pull rails low
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
320 _delay_us(28); // wait some cycles
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
321 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
322 if (response == 0) {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
323 // 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
324 TIMSK &= ~_BV(TOIE0);
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
325 } else {
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
326 // we received a bit (logic low)
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
327 response = response << 1;
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
328 response_len++;
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
329 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
330 } else {
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
331 // okay, we have logic high
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
332 response = response << 1;
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
333 response |= 1;
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
334 response_len++;
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
335 }
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
336 if (response_len == 15) { // maximum response length reached
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
337 RAIL_POWER_PORT |= _BV(RAIL_POWER); // restore rails power
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
338 TIMSK &= ~_BV(TOIE0);
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
339 } else {
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
340 _delay_us(20); // wait some cycles
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
341 RAIL_POWER_PORT |= _BV(RAIL_POWER); // restore rails power
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
342 }
6
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
343 } 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
344 }
2405aff29a51 added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents: 5
diff changeset
345
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
346 ISR (INT0_vect) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
347 // car0 detector
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
348 uint16_t tmp = 0;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
349 car0_new = TCNT1; // get current counter
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
350 if (car0_old < car0_new) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
351 // calculate difference
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
352 if (car0 == 0) tmp = car0_new-car0_old;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
353 if ( (tmp > 54) && (tmp < 74) ) car0 = 1;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
354 if ( (tmp > 118) && (tmp < 138) ) car0 = 2;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
355 if ( (tmp > 186) && (tmp < 206) ) car0 = 3;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
356 if ( (tmp > 246) && (tmp < 266) ) car0 = 4;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
357 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
358 car0_old = car0_new;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
359 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
360
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
361 ISR (INT1_vect) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
362 // car1 detector
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
363 uint16_t tmp = 0;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
364 car1_new = TCNT1; // get current counter
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
365 if (car1_old < car1_new) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
366 // calculate difference
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
367 if (car1 == 0) tmp = car1_new-car1_old;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
368 if ( (tmp > 54) && (tmp < 74) ) car1 = 1;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
369 if ( (tmp > 118) && (tmp < 138) ) car1 = 2;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
370 if ( (tmp > 186) && (tmp < 206) ) car1 = 3;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
371 if ( (tmp > 246) && (tmp < 266) ) car1 = 4;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
372 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
373 car1_old = car1_new;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
374 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
375
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
376
7
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
377 ISR (INT2_vect) {
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
378 // Lap counter Interrupt
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
379 // do not know if this ever occurs ?! this is normally an output pin to trigger the counter start
7
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
380 }
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
381
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
382 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
383 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
384 for (i=0; i<MAX_SLOTS; i++) {
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
385 if (i<4) speedlimit[i] = 15; else speedlimit[i] = 0;
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
386 fuel[i] = 100;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
387 jumpstart_time[i] = 0;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
388 }
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
389 }
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
390
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
391 void countdown_progress(void) {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
392 // decrement COUNTDOWN_LOOPS
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
393 if (countdown_loops>0) {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
394 countdown_loops--;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
395 } else {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
396 countdown--;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
397 countdown_loops = COUNTDOWN_DELAY;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
398 }
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
399 switch (countdown) {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
400 case 5: LED(1, 1); break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
401 case 4: LED(2, 1); break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
402 case 3: LED(3, 1); break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
403 case 2: LED(4, 1); break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
404 case 1: LED(5, 1); break;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
405 case 0: { // RACE START!
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
406 sysclk_ms_low = 1;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
407 sysclk_ms_high = 0;
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
408 LEDS_OFF();
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
409 LED(3, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
410 mode = 3;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
411 } break;
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
412 }
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
413 }
aea84f4f5a12 feature: program a specific controller speed limit without changing the car's acceleration
Malte Bayer <mbayer@neo-soft.org>
parents: 35
diff changeset
414
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
415 void check_cars(void) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
416 if (car0 != car0_state) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
417 car0_state = car0;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
418 if (car0_state != 0) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
419 RS232_putc('A');
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
420 RS232_putc('A');
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
421 RS232_putc('0'+car0_state);
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
422 RS232_putc('\n');
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
423 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
424 } car0 = 0;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
425
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
426 if (car1 != car1_state) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
427 car1_state = car1;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
428 if (car1_state != 0) {
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
429 RS232_putc('B');
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
430 RS232_putc('B');
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
431 RS232_putc('0'+car1_state);
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
432 RS232_putc('\n');
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
433 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
434 } car1 = 0;
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
435 }
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
436
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
437
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
438 int main(void)
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
439 {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
440
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
441 uint8_t packet_index = 1;
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
442
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
443 uint8_t btn_start = _BV(SW_START);
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
444 uint8_t old_start = btn_start;
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
445
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
446
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
447 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
448 reset_vars();
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
449 LED(3, 1); // enable middle led == idle mode
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
450
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
451 // switch on rails power
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
452 RAIL_POWER_PORT |= _BV(RAIL_POWER);
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
453
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
454 while (1) {
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
455 // check for short circuit on the rails
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
456 check_rails_shortcut();
41
9857c18c5e64 implemented car detection to cu
Malte Bayer <mbayer@neo-soft.org>
parents: 37
diff changeset
457 check_cars();
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
458
33
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
459 if (response_len > 0) {
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
460 itoa(response, s, 2);
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
461 response_len = 0;
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
462 RS232_puts("ANSWER RX: ");
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
463 RS232_puts(s);
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
464 RS232_putc('\n');
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
465 }
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
466
c261350bb394 fixed shortcut detection
Malte Bayer <mbayer@neo-soft.org>
parents: 11
diff changeset
467
7
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
468 // read in button presses
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
469 btn_start = (PIN(SW_START_PORT) & _BV(SW_START));
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
470 if (old_start != btn_start) {
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
471 // start button changed
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
472 if (btn_start == 0) {
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
473 // start button press active
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
474 if (mode == 0) {
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
475 mode = 1; // set wait for race start mode
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
476 reset_vars();
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
477 LED(1, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
478 LED(2, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
479 LED(3, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
480 LED(4, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
481 LED(5, 1);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
482 RS232_puts_p(prepare);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
483 } else if (mode == 1) {
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
484 // Initiate race countdown
42
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
485 sysclk_ms_low = 1;
018d4d63ad3b implemented 500ns timer and set jumpstart time
Malte Bayer <mbayer@neo-soft.org>
parents: 41
diff changeset
486 sysclk_ms_high = 0;
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
487 countdown = 5;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
488 countdown_loops = COUNTDOWN_DELAY;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
489 mode = 2;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
490 LED(1, 0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
491 LED(2, 0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
492 LED(3, 0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
493 LED(4, 0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
494 LED(5, 0);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
495 RS232_puts_p(countdownstart);
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
496 }
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
497 }
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
498 old_start = btn_start;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
499 }
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
500
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
501 if (mode==3) {
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
502 // RACE START!
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
503 // issue reset command to lap counter
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
504 mode = 0;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
505 RS232_puts_p(racestart);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
506 program_command = 6;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
507 program_parameter = 9;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
508 program_id = 0;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
509 program_count = 1;
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
510 LAP_COUNTER_PORT |= _BV(LAP_COUNTER);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
511 // TODO: beep long
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
512 _delay_us(50);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
513 LAP_COUNTER_PORT &= ~_BV(LAP_COUNTER);
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
514 }
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
515
7
f4e250d5402b fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents: 6
diff changeset
516
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
517 switch (packet_index) {
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
518 case 1:
4
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
519 if (program_count > 0) {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
520 // command in queue
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
521 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
522 packet_index++;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
523 program_count--;
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
524 }
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
525 } else {
58529e14c45c implemented rs232 program word injection
Malte Bayer <mbayer@neo-soft.org>
parents: 3
diff changeset
526 // output idle command
11
69c2a1408619 done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents: 10
diff changeset
527 if (do_program(7, 19, 0)) packet_index++; // reset
8
84249ab5d691 debugging stuff
Malte Bayer <mbayer@neo-soft.org>
parents: 7
diff changeset
528 //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
529 //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
530 }
37
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
531
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
532 if ( (packet_index>1) && (mode == 2) ) countdown_progress();
136a79772098 implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents: 36
diff changeset
533
2
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
534 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
535 case 2:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
536 if (do_pace_ghost()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
537 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
538 case 3:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
539 if (do_active()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
540 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
541 case 4:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
542 if (do_controller(0)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
543 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
544 case 5:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
545 if (do_controller(4)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
546 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
547 case 6:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
548 if (do_controller(1)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
549 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
550 case 7:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
551 if (do_controller(5)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
552 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
553 case 8:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
554 if (do_controller(2)) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
555 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
556 case 9:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
557 if (do_active()) packet_index++;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
558 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
559 case 10:
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
560 if (do_controller(3)) packet_index = 1;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
561 // last packet, so reset packet index
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
562 break;
6c59b4293fa9 finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents: 0
diff changeset
563 }
0
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
564
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
565
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
566 } // main loop end
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
567 };
9b7de464f0ea initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
568

mercurial