def run(self): adcTolerance = 5 adcPins = 3 adcLast = [0, 0, 0, 0, 0, 0, 0, 0] adcOut = [0, 0, 0, 0, 0, 0, 0, 0] adcChanged = [False, False, False, False, False, False, False, False] while not self.stopped(): sleep(SENDER_DELAY) for i in range(adcPins): adcChanged[i] = False updates = [] for i in range(adcPins): adcOut[i] = adc.readADC(i) if (abs(adcOut[i] - adcLast[i]) > adcTolerance): adcChanged[i] = True updates.append((SCRATCH_NAME_INPUT[i], adcOut[i])) if (DEBUG): print "ADC values: ", adcOut if updates != []: try: self.sendUpdates(updates) for i in range(adcPins): if (changed[i]): adcLast = adcOut except Exception as e: print e break
def run (self): adcTolerance = 5 adcPins = 3 adcLast = [0,0,0,0,0,0,0,0] adcOut = [0,0,0,0,0,0,0,0] adcChanged = [False,False,False,False,False,False,False,False] while not self.stopped (): sleep (SENDER_DELAY) for i in range (adcPins): adcChanged[i] = False updates = [] for i in range (adcPins): adcOut[i] = adc.readADC (i) if (abs (adcOut[i] - adcLast[i]) > adcTolerance): adcChanged[i] = True updates.append ((SCRATCH_NAME_INPUT[i], adcOut[i])) if (DEBUG): print "ADC values: " , adcOut if updates != []: try: self.sendUpdates (updates) for i in range (adcPins): if (changed[i]): adcLast = adcOut except Exception as e: print e break
def run ((initR, initL)): mode = MODE_INIT reset = False resetR = initR resetL = initL ticks = 0 sense = SENSE_NONE direc = DIR_FWD while True: led.tick (ticks, mode) ticks = ticks + 1 sleep (READ_DELAY) # find the current values of the line sensors lineR = adc.readADC (ADC_LINE_RIGHT) lineL = adc.readADC (ADC_LINE_LEFT) button = adc.readADC (ADC_BUTTON) switchR = adc.readADC (ADC_SWITCH_RIGHT) switchL = adc.readADC (ADC_SWITCH_LEFT) if (adc.high (button) == True): reset = True if (adc.high (switchR) == True and mode != MODE_LINE): mode = MODE_LINE reset = True elif (adc.high (switchL) == True and mode != MODE_MAZE): mode = MODE_MAZE reset = True elif (adc.low (switchR) == True and adc.low (switchL) == True and mode != MODE_WAIT): mode = MODE_WAIT motor.stopMotors () if (reset == True): ticks = 0 led.tick (ticks, mode) if (mode == MODE_LINE or mode == MODE_MAZE): motor.stopMotors () sleep (5) resetR = adc.readADC (ADC_LINE_RIGHT) resetL = adc.readADC (ADC_LINE_LEFT) elif (mode == MODE_WAIT): os.system ("sudo shutdown -h now") if (DEBUG): print "resetR: %d | resetL: %d" % (resetR, resetL) print "Right: %d | Left: %d" % (lineR, lineL) print "Mode: %s | Reset: %s" % (showMode (mode), reset) print "Button, switch R, switch L: %d, %d, %d" % (adc.high (button), adc.high (switchR), adc.high (switchL)) if (mode == MODE_WAIT and reset == True): motor.stopMotors () if (reset == False): sense = SENSE_NONE if (lineR - resetR > thresholdR): sense |= SENSE_RIGHT if (lineL - resetL > thresholdL): sense |= SENSE_LEFT if (mode == MODE_LINE): direc = lineFollowerTransition((direc, sense)) if (direc == DIR_RIGHT): steerRight () elif (direc == DIR_LEFT): steerLeft () else: goForward () elif (mode == MODE_MAZE): direc = mazeSolverTransition((direc, sense)) if (direc == DIR_RIGHT): giveInRight () elif (direc == DIR_LEFT): swayLeft () else: goForward () reset = False
def init (): motor.initMotors () initR = adc.readADC (ADC_LINE_RIGHT) initL = adc.readADC (ADC_LINE_LEFT) return (initR, initL)
def init(): motor.initMotors() initR = adc.readADC(ADC_LINE_RIGHT) initL = adc.readADC(ADC_LINE_LEFT) return (initR, initL)
def run((initR, initL)): mode = MODE_INIT reset = False resetR = initR resetL = initL ticks = 0 sense = SENSE_NONE direc = DIR_FWD while True: led.tick(ticks, mode) ticks = ticks + 1 sleep(READ_DELAY) # find the current values of the line sensors lineR = adc.readADC(ADC_LINE_RIGHT) lineL = adc.readADC(ADC_LINE_LEFT) button = adc.readADC(ADC_BUTTON) switchR = adc.readADC(ADC_SWITCH_RIGHT) switchL = adc.readADC(ADC_SWITCH_LEFT) if (adc.high(button) == True): reset = True if (adc.high(switchR) == True and mode != MODE_LINE): mode = MODE_LINE reset = True elif (adc.high(switchL) == True and mode != MODE_MAZE): mode = MODE_MAZE reset = True elif (adc.low(switchR) == True and adc.low(switchL) == True and mode != MODE_WAIT): mode = MODE_WAIT motor.stopMotors() if (reset == True): ticks = 0 led.tick(ticks, mode) if (mode == MODE_LINE or mode == MODE_MAZE): motor.stopMotors() sleep(5) resetR = adc.readADC(ADC_LINE_RIGHT) resetL = adc.readADC(ADC_LINE_LEFT) elif (mode == MODE_WAIT): os.system("sudo shutdown -h now") if (DEBUG): print "resetR: %d | resetL: %d" % (resetR, resetL) print "Right: %d | Left: %d" % (lineR, lineL) print "Mode: %s | Reset: %s" % (showMode(mode), reset) print "Button, switch R, switch L: %d, %d, %d" % ( adc.high(button), adc.high(switchR), adc.high(switchL)) if (mode == MODE_WAIT and reset == True): motor.stopMotors() if (reset == False): sense = SENSE_NONE if (lineR - resetR > thresholdR): sense |= SENSE_RIGHT if (lineL - resetL > thresholdL): sense |= SENSE_LEFT if (mode == MODE_LINE): direc = lineFollowerTransition((direc, sense)) if (direc == DIR_RIGHT): steerRight() elif (direc == DIR_LEFT): steerLeft() else: goForward() elif (mode == MODE_MAZE): direc = mazeSolverTransition((direc, sense)) if (direc == DIR_RIGHT): giveInRight() elif (direc == DIR_LEFT): swayLeft() else: goForward() reset = False