feature: program a specific controller speed limit without changing the car's acceleration

Sat, 03 Dec 2011 14:25:06 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Sat, 03 Dec 2011 14:25:06 +0100
changeset 36
aea84f4f5a12
parent 35
00166228a419
child 37
136a79772098

feature: program a specific controller speed limit without changing the car's acceleration

blackbox/main.c file | annotate | diff | comparison | revisions
slotUI/SlotCli.py file | annotate | diff | comparison | revisions
slotUI/freeslot.py file | annotate | diff | comparison | revisions
--- a/blackbox/main.c	Sat Dec 03 13:53:44 2011 +0100
+++ b/blackbox/main.c	Sat Dec 03 14:25:06 2011 +0100
@@ -13,6 +13,9 @@
 #include "main.h"
 #include "lowlevel.h"
 
+#define MAX_SLOTS       5
+uint8_t speedlimit[MAX_SLOTS];
+
 volatile uint8_t program_count = 0;
 volatile uint8_t program_id;
 volatile uint8_t program_command;
@@ -45,8 +48,12 @@
     return 0;
 }
 
+char ok[]       PROGMEM="OK\n";
+char busy[]     PROGMEM="BUSY\n";
+
 
 ISR ( USART_RXC_vect ) {
+    uint8_t tmp;
     char c = UDR;
 
     // check for buffer overflow
@@ -72,8 +79,16 @@
                             program_count = 0x02; // send commands twice (fuel, speed, brake)
                             else
                             program_count = 0x01;
-                        RS232_puts_p(PSTR("OK\n"));
-                    } else RS232_puts_p(PSTR("BUSY\n"));
+                        RS232_puts_p(ok);
+                    } else RS232_puts_p(busy);
+                    break;
+
+                case 'L': // Limit maximum speed for a car
+                    tmp = buffer[2]-'0';
+                    if (tmp > 9)
+                        tmp = buffer[2]-'A'+10;
+                    speedlimit[buffer[1]-'0'] = tmp;
+                    RS232_puts_p(ok);
                     break;
 
             }
@@ -95,28 +110,36 @@
     uint16_t tmp;
     switch (controller) {
         case 0:
-            tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
+            tmp = ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
+            tmp = tmp << 1;
             if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) {
                 tmp |= (1<<5);
                 LED(1,0);
             } else LED(1,1);
             break;
         case 1:
-            tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
+            tmp = ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
+            tmp = tmp << 1;
             if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER2_SW)) != 0) {
                 tmp |= (1<<5);
                 LED(2,0);
             } else LED(2,1);
             break;
         case 2:
-            tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
+            tmp = ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
+            tmp = tmp << 1;
             if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER3_SW)) != 0) {
                 tmp |= (1<<5);
                 LED(3,0);
             } else LED(3,1);
             break;
         case 3:
-            tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F) << 1;
+            tmp = ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) & 0x0F);
+            if (tmp > speedlimit[controller]) tmp = speedlimit[controller];
+            tmp = tmp << 1;
             if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER4_SW)) != 0) {
                 tmp |= (1<<5);
                 LED(4,0);
@@ -260,6 +283,13 @@
     LED(5,2);
 }
 
+void reset_vars(void) {
+    uint8_t i;
+    for (i=0; i<MAX_SLOTS; i++) {
+        speedlimit[i] = 15;
+    }
+}
+
 
 int main(void)
 {
@@ -284,6 +314,7 @@
 
 
     init_hardware();
+    reset_vars();
 
     // switch on rails power
     RAIL_POWER_PORT |= _BV(RAIL_POWER);
--- a/slotUI/SlotCli.py	Sat Dec 03 13:53:44 2011 +0100
+++ b/slotUI/SlotCli.py	Sat Dec 03 14:25:06 2011 +0100
@@ -33,6 +33,8 @@
         help="Set maximum CAR speed", metavar="[0-15]")
     parser.add_option("--blink", dest="blink",
         help="Set car lights blinking state", metavar="[on|off]")
+    parser.add_option("--limit", dest="limit",
+        help="Controlled SPEED LIMIT (15 = no limit)", metavar="[0-15]")
 
     (options, args) = parser.parse_args()
     cli = SlotCli()
@@ -43,14 +45,22 @@
     if options.carid == None:
         print "Option --carid is required for all car programming commands!\nUse --help to get a list of available commands"
         sys.exit(1)
+
     if options.fuel != None:
         print "setFuel: " + cli.box.progcar(int(options.carid), "fuel", int(options.fuel))
+
     if options.speed != None:
         print "setSpeed: " + cli.box.progcar(int(options.carid), "speed", int(options.speed))
+
     if options.brake != None:
         print "setBrake: " + cli.box.progcar(int(options.carid), "brake", int(options.brake))
+
     if options.blink != None:
         state = False
         if options.blink == "on":
             state = True
         print "setBlink: " + cli.box.blinkcar(int(options.carid), state)
+
+    if options.limit != None:
+        print "Change Speed Limit: " + cli.box.speedlimit(int(options.carid), int(options.limit))
+
--- a/slotUI/freeslot.py	Sat Dec 03 13:53:44 2011 +0100
+++ b/slotUI/freeslot.py	Sat Dec 03 14:25:06 2011 +0100
@@ -120,6 +120,20 @@
         else:
             return self.com.query( "P40%i" % carid )
 
+    def speedlimit(self, carid, value):
+        """
+        Set the maximum controller speed for a car
+        Attention: this is software limited, this does not affect car acceleration!
+        """
+        if (carid < 0) or (carid > 5):
+            return "ERR - invalid carid"
+        if (value<0) or (value>15):
+            return "ERR - invalid value"
+        # transform value 10..15 to A..F
+        if (value>9):
+            value = chr(ord("A") + (value-10))
+        return self.com.query( "L%i%s" % (carid, value) )
+
     def setmode(self, mode):
         """
         Switch the Blackbox mode

mercurial