def measure(trigger, echo): bbio.digitalWrite(trigger, bbio.HIGH) time.sleep(DECPULSETRIGGER) bbio.digitalWrite(trigger, bbio.LOW) # Wait for echo to go high (or timeout) intcountdown = INTTIMEOUT while (bbio.digitalRead(echo) == 0 and intcountdown > 0): intcountdown = intcountdown - 1 # If echo is high if intcountdown > 0: # Start timer and init timeout countdown echostart = time.time() intcountdown = INTTIMEOUT # Wait for echo to go low (or timeout) while (bbio.digitalRead(echo) == 1 and intcountdown > 0): intcountdown = intcountdown - 1 # Stop timer echoend = time.time() # Echo duration echoduration = echoend - echostart intdistance = (echoduration * 1000000) / 58.0 return intdistance
def initial_start(start_stop_button): """ Before launching the main function, shows that the script is ready. """ def blink_leds(): """ Makes the leds blink to show that everything is on. """ while True: time.sleep(0.25) io.toggle(cst.STATUS_LED) io.toggle(cst.START_LED) start_process = False print 'Start blinking' blinking = Process(target=blink_leds) blinking.start() while not start_process: if start_stop_button.is_activated(): start_process = True print 'Stop blinking' blinking.terminate() io.digitalWrite(cst.STATUS_LED, io.LOW) io.digitalWrite(cst.START_LED, io.LOW) time.sleep(1)
def measure(trigger, echo): bbio.digitalWrite(trigger, bbio.HIGH) time.sleep(DECPULSETRIGGER) bbio.digitalWrite(trigger, bbio.LOW) # Wait for echo to go high (or timeout) intcountdown = INTTIMEOUT while (bbio.digitalRead(echo) == 0 and intcountdown > 0): intcountdown = intcountdown - 1 # If echo is high if intcountdown > 0: # Start timer and init timeout countdown echostart = time.time() intcountdown = INTTIMEOUT # Wait for echo to go low (or timeout) while (bbio.digitalRead(echo) == 1 and intcountdown > 0): intcountdown = intcountdown - 1 # Stop timer echoend = time.time() # Echo duration echoduration = echoend - echostart intdistance = (echoduration*1000000) / 58.0 return intdistance
def motor_ex(): DMCC.setPIDConstants(0, 2, 1, -3000, -32768, 0) while 1: try: bbio.digitalWrite(tp, bbio.HIGH) DMCC.setTargetVel(0, 2, 150) bbio.digitalWrite(tp, bbio.LOW) except KeyboardInterrupt: DMCC.setTargetVel(0, 2, 0) exit()
def _writeByte(self, byte): """ Writes out the lowest byte of the given integer value. """ if self.rw: bbio.digitalWrite(self.rw, bbio.LOW) bbio.digitalWrite(self.enable, bbio.LOW) if self.mode_bits == 8: for bit in range(8): bbio.digitalWrite(self.data_pins[bit], (byte>>bit) & 0x1) bbio.digitalWrite(self.enable, bbio.HIGH) bbio.digitalWrite(self.enable, bbio.LOW) else: self._write4bits(byte>>4) self._write4bits(byte)
def _writeByte(self, byte): """ Writes out the lowest byte of the given integer value. """ if self.rw: bbio.digitalWrite(self.rw, bbio.LOW) bbio.digitalWrite(self.enable, bbio.LOW) if self.mode_bits == 8: for bit in range(8): bbio.digitalWrite(self.data_pins[bit], (byte >> bit) & 0x1) bbio.digitalWrite(self.enable, bbio.HIGH) bbio.digitalWrite(self.enable, bbio.LOW) else: self._write4bits(byte >> 4) self._write4bits(byte)
def _write4bits(self, bits): """ Writes out the lowest 4 bits of the given value. """ if self.rw: bbio.digitalWrite(self.rw, bbio.LOW) for bit in range(4): bbio.digitalWrite(self.data_pins[bit], (bits >> bit) & 0x1) bbio.digitalWrite(self.enable, bbio.HIGH) bbio.digitalWrite(self.enable, bbio.LOW)
def move_thread(kill, pin, steps=-1, default_ramp_step = 2000, min_sleep = 100-15, stop_condition = None): """ run this biatch """ STOP_CONDITION_INTERVAL = 50 stop_condition = stop_condition or (lambda: False); #Default: No stop conditions bbio.pinMode(pin, bbio.OUTPUT) step = 0 ramp_step = float(default_ramp_step if steps == -1 else min(default_ramp_step, steps)) ramp_sleep = 100.0 #ramp_sleep_decrement = ramp_sleep / (ramp_step*ramp_step) half_ramp_step = ramp_step/2 dec = 0 while (step < ramp_step) and not kill.isSet() and \ (step%STOP_CONDITION_INTERVAL != 0 or not stop_condition()): bbio.digitalWrite(pin,bbio.LOW) bbio.digitalWrite(pin,bbio.HIGH) step +=1 x = (step/ramp_step) dec = x*x if step > half_ramp_step: dec = -(x-1)*(x-1) + 0.5 bbio.delayMicroseconds( min_sleep + ramp_sleep - 2*dec*ramp_sleep) while (step < steps or steps == -1) and not kill.isSet() and \ (step%STOP_CONDITION_INTERVAL != 0 or not stop_condition()): bbio.digitalWrite(pin,bbio.LOW) bbio.digitalWrite(pin,bbio.HIGH) step +=1 bbio.delayMicroseconds(min_sleep)
def begin(self, cols, rows): """ Initializes the LCD driver and sets it to the given number of rows and columns. """ assert cols == 8 or cols == 16 or cols == 20, \ "LiquidCrystal only supports 8, 16 and 20 row displays" assert rows == 1 or rows == 2 or rows == 4, \ "LiquidCrystal only supports 1, 2 and 4 column displays" self.cols = cols self.rows = rows for pin in (self.rs, self.enable) + self.data_pins: bbio.pinMode(pin, bbio.OUTPUT) if self.rw: bbio.pinMode(self.rw, bbio.OUTPUT) bbio.digitalWrite(self.enable, bbio.LOW) # idle state # This is the initialization procedure as defined in the HD44780 datasheet: if self.mode_bits == 8: self.writeCommand(self.COMMAND_FUNCTIONSET | self.FUNCTIONSET_8BIT) else: self._write4bits((self.COMMAND_FUNCTIONSET | self.FUNCTIONSET_8BIT) >> 4) bbio.delay(5) if self.mode_bits == 8: self.writeCommand(self.COMMAND_FUNCTIONSET | self.FUNCTIONSET_8BIT) else: self._write4bits((self.COMMAND_FUNCTIONSET | self.FUNCTIONSET_8BIT) >> 4) bbio.delay(5) if self.mode_bits == 8: self.writeCommand(self.COMMAND_FUNCTIONSET | self.FUNCTIONSET_8BIT) else: self._write4bits((self.COMMAND_FUNCTIONSET | self.FUNCTIONSET_8BIT) >> 4) function_set = 0 if self.mode_bits == 8: function_set = self.FUNCTIONSET_8BIT else: # Must put in 4 bit mode before we can set the rows and cols self._write4bits(self.COMMAND_FUNCTIONSET >> 4) if self.rows > 1: function_set |= self.FUNCTIONSET_2LINE self.writeCommand(self.COMMAND_FUNCTIONSET | function_set) self.setDisplay(self.OFF) self.clear() self._entry_mode = self.ENTRYMODE_SHIFTRIGHT self.writeCommand(self.COMMAND_ENTRYMODE | self._entry_mode) self.setDisplay(self.ON)
def _write4bits(self, bits): """ Writes out the lowest 4 bits of the given value. """ if self.rw: bbio.digitalWrite(self.rw, bbio.LOW) for bit in range(4): bbio.digitalWrite(self.data_pins[bit], (bits>>bit) & 0x1) bbio.digitalWrite(self.enable, bbio.HIGH) bbio.digitalWrite(self.enable, bbio.LOW)
def motor_club(): dmccs = pyDMCC.autodetect() motor = dmccs[0].motors[1] constants = (-15000, -30000, 0) motor.velocity_pid = constants velocity = 0 while 1: try: bbio.digitalWrite(tp, bbio.HIGH) if velocity == 0: velocity = 450 else: velocity -= 50 motor.velocity = velocity time.sleep(.3) bbio.digitalWrite(tp, bbio.LOW) except KeyboardInterrupt: motor.velocity = 0 exit()
def setup(): print "setup..." for trigger, echo in ULTRAS: bbio.pinMode(echo, bbio.INPUT) bbio.pinMode(trigger, bbio.OUTPUT) bbio.digitalWrite(trigger, bbio.LOW)
def writeData(self, data): """ Writes the given byte to the display in data mode. """ bbio.digitalWrite(self.rs, bbio.HIGH) self._writeByte(data)
def writeCommand(self, command): """ Writes the given byte to the display in command mode. """ bbio.digitalWrite(self.rs, bbio.LOW) self._writeByte(command)