Sat, 03 Dec 2011 13:53:44 +0100
cli: implemented setting of fuel, brake, speed, blinkstate for any car
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 12:10:10 2011 +0100 +++ b/blackbox/main.c Sat Dec 03 13:53:44 2011 +0100 @@ -36,6 +36,16 @@ volatile uint8_t timer0_delay; +int insert_queue(uint16_t tmp, uint8_t len) { + if (transmit_buffer_queue == 0) { + transmit_buffer_queue = tmp; + transmit_len_queue = len; + return 1; + } + return 0; +} + + ISR ( USART_RXC_vect ) { char c = UDR; @@ -44,14 +54,12 @@ buffer_len=0; } else { // collect characters until end of line - if ( (c==0x0A) ) { + if ( (c=='\n') ) { buffer[buffer_len]=0; // packet end received, parse the received packet switch (buffer[0]) { case 'P': // inject a program data word to the rails - // TODO: at the moment only parameters 0..9 supported - // needs to build a "human" parser if (program_count == 0) { program_id = buffer[3]-'0'; program_command = buffer[1]-'0'; @@ -80,14 +88,6 @@ -int insert_queue(uint16_t tmp, uint8_t len) { - if (transmit_buffer_queue == 0) { - transmit_buffer_queue = tmp; - transmit_len_queue = len; - return 1; - } - return 0; -} int do_controller(uint8_t controller) {
--- a/slotUI/SlotCli.py Sat Dec 03 12:10:10 2011 +0100 +++ b/slotUI/SlotCli.py Sat Dec 03 13:53:44 2011 +0100 @@ -5,6 +5,10 @@ """ from freeslot import Blackbox +from optparse import OptionParser +import time, sys + +VERSION = "1.0" class SlotCli(): def __init__(self): @@ -18,5 +22,35 @@ return None if __name__ == "__main__": - app = SlotCli() - app.run() \ No newline at end of file + parser = OptionParser(version="%prog " + VERSION) + parser.add_option("--carid", dest="carid", + help="Required for programming a car directly", metavar="[0-5]") + parser.add_option("--fuel", dest="fuel", + help="Set maximum CAR fuel level", metavar="[0-15]") + parser.add_option("--brake", dest="brake", + help="Set CAR brake strength", metavar="[0-15]") + parser.add_option("--speed", dest="speed", + help="Set maximum CAR speed", metavar="[0-15]") + parser.add_option("--blink", dest="blink", + help="Set car lights blinking state", metavar="[on|off]") + + (options, args) = parser.parse_args() + cli = SlotCli() + # should a CLI function be started? + + + # check commandline if we have to program something + 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)
--- a/slotUI/freeslot.py Sat Dec 03 12:10:10 2011 +0100 +++ b/slotUI/freeslot.py Sat Dec 03 13:53:44 2011 +0100 @@ -3,8 +3,18 @@ Blackbox communication library """ -import serial -import sys +import serial, sys, string, time + +# how often should a command retried when busy? +RETRIES = 10 + +# define loglevels +DEBUG = 20 +def log(level, msg): + """ + Logging output function + """ + print msg class SerialCommunicator(): def __init__(self, device, speed): @@ -38,12 +48,23 @@ answer = self.com.readline() return string.strip(answer, "\n") + def query(self, msg): + retry = 0 + response = self.write(msg, True) + while (retry < RETRIES) and (response == "BUSY"): + time.sleep(0.1) + response = self.write(msg, True) + retry += 1 + log( DEBUG, "%i> %s\n< %s" % (retry, msg, response) ) + return response + + class Blackbox(): def __init__(self): self.com = None self.info = None - def connect(self, device="/dev/ttyUSB0", speed=115200): + def connect(self, device="/dev/ttyUSB0", speed=57600): if self.com == None: self.com = SerialCommunicator(device, speed) if self.com.connected: @@ -51,10 +72,10 @@ self.com.connect() self.info = self.readinfo() - def disconnect(): + def disconnect(self): self.com.disconnect() - def readinfo(): + def readinfo(self): """ Read complete Information from connected box This does not include race+car status! @@ -64,10 +85,40 @@ def progcar(self, carid, command, value): """ Send program packets to specified car id - valid command: speed, brake + valid command: speed, brake, fuel valid value: 4 bit integer (0..15) + valid carid: 0..5 """ - return True + if (carid < 0) or (carid > 5): + return "ERR - invalid carid" + cmd = -1 + if command == "speed": + cmd = 0 + if command == "brake": + cmd = 1 + if command == "fuel": + cmd = 2 + if (cmd == -1): + return "ERR - invalid command" + 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)) + command = "P%i%s%i" % (cmd, value, carid) + response = self.com.query( command ) + return response + + def blinkcar(self, carid, blink): + """ + Set car blinking state + """ + if (carid < 0) or (carid > 5): + return "ERR - invalid carid" + if blink: + return self.com.query( "P48%i" % carid ) + else: + return self.com.query( "P40%i" % carid ) def setmode(self, mode): """