bugfix: responsewire bitmap, todo: trackswitch response

Sat, 10 Dec 2011 16:29:31 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Sat, 10 Dec 2011 16:29:31 +0100
changeset 73
ec888cfa024e
parent 72
60c4b28fd773
child 74
173d0863a804

bugfix: responsewire bitmap, todo: trackswitch response

blackbox/interrupts.c file | annotate | diff | comparison | revisions
blackbox/main.c file | annotate | diff | comparison | revisions
blackbox/main.h file | annotate | diff | comparison | revisions
pitlane/main.c file | annotate | diff | comparison | revisions
--- a/blackbox/interrupts.c	Sat Dec 10 14:56:27 2011 +0100
+++ b/blackbox/interrupts.c	Sat Dec 10 16:29:31 2011 +0100
@@ -1,6 +1,4 @@
 ISR ( TIMER1_COMPA_vect ) {
-    PORTC ^= _BV(PC0); // DEBUG OUTPUT SYSTEM CLOCK
-
     // trigger packet transfer:
     if (sysclk_packettimer == 14) { // 15*500 = 7500 NS
         transmit_len = transmit_len_next;
@@ -49,17 +47,14 @@
                 // response incoming!
                 // start feew µs later
                 _delay_us(5);
-                for (i=16; i>0; i--) { // start receiving all 16 bits
-    PORTC ^= _BV(PC1); // DEBUG
-                    responsewire_data = (responsewire_data << 1); // shift bits
+                for (i=16; i>0; i--) { // start receiving all 16 bits -> shift them in same direction as they're shifted out
+                    responsewire_data = (responsewire_data >> 1); // shift bits right, first received = bit0
                     if ((PIN(RESPONSEWIRE_PORT) & _BV(RESPONSEWIRE_PIN)) == 0) // phsyical low == logic 1
-                        responsewire_data |= 1;
+                        responsewire_data |= 0b1000000000000000;
                     _delay_us(48); // get to next bit
                 }
-                itoa(responsewire_data, s, 16);
-                RS232_puts("RW:");
-                RS232_puts(s);
-                RS232_putc('\n');
+                // we have some little time here to decode the response and do some action for refueling state of cars
+                decode_responsewire();
             }
             TIMSK |= _BV(OCIE2); //enable timer2 interrupt
             // end reading response wire
--- a/blackbox/main.c	Sat Dec 10 14:56:27 2011 +0100
+++ b/blackbox/main.c	Sat Dec 10 16:29:31 2011 +0100
@@ -47,6 +47,8 @@
 // 1: waiting for countdown start
 // 2: race countdown initiated
 // 3: Race start condition
+uint8_t  liveinfo = 0;
+uint8_t  fuel_enabled = 1;
 
 volatile u32 sysclk;
 volatile uint8_t sysclk_packettimer = 0;
@@ -80,6 +82,70 @@
 volatile uint8_t timer0_delay;
 
 
+    /* RESPONSEWIRE frame format:
+        1 startbit
+        2 car id bit 1
+        3 car id bit 2
+        4 car id bit 3
+        5 track change status bit 1
+        6 track change status bit 2
+        7 track change status bit 3
+        8 track change status bit 4
+        9 sender id bit 1
+        10 sender id bit 2
+        11 sender id bit 3
+        12 sender id bit 4
+        13 device type bit 1
+        14 device type bit 2
+        15 device type bit 3
+        16 stopbit
+    */
+
+    /* PITLANE STATUS DEFINITION
+        1 = AA
+        2 = AB
+        3 = BB
+        4 = BA
+        5 = BC
+        6 = ZZ -> pitlane exit
+    */
+
+#define STARTSTOP 0b1000000000000001
+void decode_responsewire(void) {
+    uint16_t data = responsewire_data;
+    // first check if start + stopbit are set
+    // todo future: unsure but last bit doesnt get set?!
+/*
+    if ((data & STARTSTOP) != STARTSTOP) {
+        RS232_puts("RW BAD\n");
+        return; // incomplete response
+    }
+*/
+    // now extract the car id, track change status
+    uint8_t car = ((data >> 1) & 0b111);
+    uint8_t status = ((data >> 4) & 0b1111);
+    uint8_t sender = ((data >> 8) & 0b1111);
+    uint8_t type = ((data >> 12) & 0b111);
+    if (type == 4) {
+        // pitlane response
+        if (status == 5) slot[car].canrefuel = 1;
+        if (status == 6) for (data=0; data<MAX_SLOTS; data++) slot[data].canrefuel = 0;
+    }
+    RS232_puts("RW:");
+    itoa(car, s, 16);
+    RS232_puts(s);
+    RS232_putc(':');
+    itoa(type, s, 16);
+    RS232_puts(s);
+    RS232_putc(':');
+    itoa(sender, s, 16);
+    RS232_puts(s);
+    RS232_putc(':');
+    itoa(status, s, 16);
+    RS232_puts(s);
+    RS232_putc('\n');
+}
+
 int insert_queue(uint16_t tmp, uint8_t len) {
     if (transmit_buffer_queue == 0) {
         transmit_buffer_queue = tmp;
@@ -136,6 +202,17 @@
                     RS232_puts_p(ok);
                     break;
 
+                case 'F': // set fuel enabled
+                    fuel_enabled = buffer[1]-'0';
+                    RS232_puts_p(ok);
+                    break;
+
+                case '*': // set live information
+                    liveinfo = buffer[1]-'0';
+                    RS232_puts_p(ok);
+                    break;
+
+
                 case 'I': // get Information data (incl. important global parameter dump)
                     RS232_puts(VERSION);
                     RS232_putc(':');
@@ -254,22 +331,24 @@
     // THIS REQUIRES PHYSICAL CAR FUEL LEVEL SET TO ZERO BEFORE!
     if ( ((PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) | (slot[controller].fuel == 0)) tmp |= 1; // benzinstand aktiv - tankmodusschalter
     if (insert_queue(tmp, 9)) {
-        if (speed != 0) {
-            // do the fuel calculation, regardless if fuel logic active or not
-            tmp = (uint8_t)(((slot[controller].accel * speed) + 1) / FUEL_DIVISOR);
-            if (tmp == 0) tmp = 1;
-            if (slot[controller].fuel > 0) {
-                // enough fuel left to decrement?
-                if (slot[controller].fuel > tmp) {
-                    slot[controller].fuel -= tmp; // decrement fuel level
-                } else slot[controller].fuel = 0;
+        if (fuel_enabled) {
+            if (speed != 0) {
+                // do the fuel calculation, regardless if fuel logic active or not
+                tmp = (uint8_t)(((slot[controller].accel * speed) + 1) / FUEL_DIVISOR);
+                if (tmp == 0) tmp = 1;
+                if (slot[controller].fuel > 0) {
+                    // enough fuel left to decrement?
+                    if (slot[controller].fuel > tmp) {
+                        slot[controller].fuel -= tmp; // decrement fuel level
+                    } else slot[controller].fuel = 0;
+                }
+            } else if (slot[controller].canrefuel) {
+                // increase fuel by 5%/sec, this equals by adding 50 to the counter
+                slot[controller].fuel += 50;
+                if (slot[controller].fuel > FUEL_FULL) slot[controller].fuel = FUEL_FULL;
             }
-        } else if (slot[controller].canrefuel) {
-            // increase fuel by 5%/sec, this equals by adding 50 to the counter
-            slot[controller].fuel += 50;
-            if (slot[controller].fuel > FUEL_FULL) slot[controller].fuel = FUEL_FULL;
+            return 1;
         }
-        return 1;
     } else return 0;
 }
 
@@ -333,7 +412,7 @@
         slot[i].laps = 0;
         slot[i].seccnt = 0;
         slot[i].accel = 15; // full acceleration per default - TODO
-        slot[i].canrefuel = 1; // TODO: only set to 1 when on a pitlane
+        slot[i].canrefuel = 0;
     }
     sysclk.value = 0;
 }
@@ -375,7 +454,7 @@
                     slot[car0-1].laps++;
                     RS232_putc('L');
                     RS232_putc(':');
-                    RS232_putc('A');
+                    RS232_putc('B');
                     RS232_putc(':');
                     itoa(slot[car0-1].laps, s, 16);
                     RS232_puts(s);
@@ -401,7 +480,7 @@
                     slot[car1-1].laps++;
                     RS232_putc('L');
                     RS232_putc(':');
-                    RS232_putc('B');
+                    RS232_putc('A');
                     RS232_putc(':');
                     itoa(slot[car1-1].laps, s, 16);
                     RS232_puts(s);
@@ -419,6 +498,7 @@
 }
 
 void slot_liveinfo(uint8_t idx) {
+    if (liveinfo == 0) return;
     // increment packet counter, if == 10 output some live info
     if (slot[idx].seccnt == 10) {
         // output current fuel status
--- a/blackbox/main.h	Sat Dec 10 14:56:27 2011 +0100
+++ b/blackbox/main.h	Sat Dec 10 16:29:31 2011 +0100
@@ -4,7 +4,7 @@
 #include <avr/wdt.h>
 #include <stdint.h>
 
-#define VERSION "1.4"
+#define VERSION "1.5"
 
 #define COUNTDOWN_DELAY         10 // x/10 seconds
 
--- a/pitlane/main.c	Sat Dec 10 14:56:27 2011 +0100
+++ b/pitlane/main.c	Sat Dec 10 16:29:31 2011 +0100
@@ -67,36 +67,40 @@
 volatile uint16_t bitbuf = 0;
 
 volatile uint8_t response = 0;
+volatile uint8_t response_car = 0;
 uint8_t self_id = 0b1111; // ONLY ONE pitlane
 
 void solenoid_delay(void) {
     _delay_ms(2);
 }
 
-void send_response(uint16_t data) {
+void send_response(uint8_t car, uint8_t status) {
     /* frame format:
         1 startbit
-        2 car id
-        3 car id
-        4 car id
+        2 car id bit 1
+        3 car id bit 2
+        4 car id bit 3
         5 track change status bit 1
         6 track change status bit 2
-        7 sender id
-        8 sender id
-        9 sender id
-        9 sender id
-        10 device type
-        11 device type
-        12 device type
-        13 device type
-        14 reserved
-        15 reserved
+        7 track change status bit 3
+        8 track change status bit 4
+        9 sender id bit 1
+        10 sender id bit 2
+        11 sender id bit 3
+        12 sender id bit 4
+        13 device type bit 1
+        14 device type bit 2
+        15 device type bit 3
         16 stopbit
     */
+    uint16_t data;
+    // produce packet
+    data = ((car & 0b111) << 1) | ((status & 0b1111) << 4) | ((self_id & 0b1111) << 8) | (TRACKSWITCH_TYPE << 12);
+    data |= 0b100000000000001; // make sure start/stop bits are set
+
     uint8_t index = 16; // bit count maximum
     uint8_t enable = DDR(RESPONSE_PORT) | _BV(RESPONSE_PIN);
     uint8_t disable = DDR(RESPONSE_PORT) & ~_BV(RESPONSE_PIN);
-    data |= 0b100000000000001; // make sure start/stop bits are set
     while (index != 0) {
         if ((data & 1) != 0) {
             DDR(RESPONSE_PORT) = enable; // enable response output
@@ -148,9 +152,9 @@
                 slot[clock].trackswitch = (bitbuf >> 5) & 1;
                 // current response for this car?
                 if (response != 0) {
-                    if ( ((response & 0b00001110) >> 1) == clock) {
+                    if ( response_car == clock) {
                         // add our ID to response:
-                        send_response(response | (self_id << 6));
+                        send_response(clock, response);
                         response = 0;
                     }
                 }
@@ -278,18 +282,19 @@
         // main loop
 
     /*
-        0 = AA
-        1 = AB
-        2 = BB
-        3 = BA
-        4 = ZZ -> pitlane exit
+        1 = AA
+        2 = AB
+        3 = BB
+        4 = BA
         5 = BC
+        6 = ZZ -> pitlane exit
     */
         if (sens[0].car != sens[0].state) {
             sens[0].state = sens[0].car;
 #if (TRACKSWITCH_TYPE != TYPE_PITLANE)
             if ( (sens[0].state != 0) && (slot[sens[0].state-1].trackswitch == 0) && (slot[sens[0].state-1].speed>0) ) {
-                response = (1 | ((sens[0].state-1)<<1) | (1 << 4));
+                response = 2;
+                response_car = sens[0].state - 1;
 
                 // set inside status
                 slot[sens[0].state].inside = 1;
@@ -307,7 +312,8 @@
             } else
 #endif
             if (sens[0].state != 0) {
-                response = (1 | ((sens[0].state-1)<<1));
+                response = 1;
+                response_car = sens[0].state - 1;
                 RS232_putc('A');
                 RS232_putc('A');
                 RS232_putc('0'+sens[0].state);
@@ -319,7 +325,8 @@
         if (sens[1].car != sens[1].state) {
             sens[1].state = sens[1].car;
             if ( (sens[1].state != 0) && (slot[sens[1].state-1].trackswitch == 0) && (slot[sens[1].state-1].speed>0) ) {
-                response = (1 | ((sens[1].state-1)<<1) | (5 << 4));
+                response = 5;
+                response_car = sens[1].state - 1;
 
                 // set inside status
                 slot[sens[1].state-1].inside = 1;
@@ -336,7 +343,8 @@
                 solenoid_delay();
             } else
             if (sens[1].state != 0) {
-                response = (1 | ((sens[1].state)<<1) | (2 << 4));
+                response = 3;
+                response_car = sens[1].state - 1;
                 RS232_putc('B');
                 RS232_putc('B');
                 RS232_putc('0'+sens[1].state);
@@ -352,14 +360,16 @@
                     if (slot[tmp].inside) {
                     slot[tmp].inside = 0;
                 }
-                response = (1 | (0b111 <<1) | (4 << 4));
+                response = 6;
+                response_car = 0;
                 RS232_puts_p(PSTR("PIT:EXIT\n"));
         }
 
         if (sens[2].car != sens[2].state) {
             sens[2].state = sens[2].car;
             if (sens[2].state != 0) {
-                response = (1 | ((sens[2].state-1)<<1) | (4 << 4));
+                response = 6;
+                response_car = sens[2].state-1;
 
                 // set inside status
                 slot[sens[2].state-1].inside = 0;

mercurial