def process(self): try: msg = self.connection.read() if not msg: return msg = msg.decode("utf-8") items = msg.split(" ") cmd = items[0] if cmd != "": # run the robot command print(cmd) if cmd == "f": motorcontrol.forward() if cmd == "b": motorcontrol.backward() if cmd == "l": motorcontrol.left() if cmd == "r": motorcontrol.right() if cmd == "<<": motorcontrol.hardleft() if cmd == ">>": motorcontrol.hardright() if cmd == "x": motorcontrol.stop_all() # respond self.counter += 1 self.led.value(1) self.connection.write(cmd + str(self.counter)) self.led.value(0) except ClientClosedError: self.connection.close()
import motorcontrol import soundsensor motorcontrol.forward(2) print(soundsensor.distance('cm'))
mycall_R = partial(pulse_mainR, eR) GPIO.add_event_detect(eL.pinA, GPIO.RISING, callback=mycall_L) GPIO.add_event_detect(eR.pinA, GPIO.RISING, callback=mycall_R) """ ==========Begin Routine========== """ #camera.start_preview() camera.start_recording('/home/pi/Videos/latest.mp4') # moving forwards for i in range(10): distL = ultra_check(uL) distR = ultra_check(uR) if min(distL, distR) > 5: eL.dir, eR.dir = MC.forward(mL, mR, 25) sleep(3) else: lcd.clear() lcd.cursor_pos = (0, 0) lcd.write("L %02d" % distL) lcd.cursor_pos = (1, 0) lcd.write("R %02d" % distL) #moving backwards for i in range(10): distB = ultra_check(uB) if min(distL, distR) > 5: eL.dir, eR.dir = MC.backward(mL, mR, 25) sleep(3)
# instant (no waiting) key response, and use special values for cursor keys screen = curses.initscr() curses.noecho() curses.cbreak() screen.keypad(True) dc = 50 # set dc variable to 0 for 0% motorcontrol.start(GPIO, dc) try: while True: char = screen.getch() if char == ord('q'): break elif char == curses.KEY_UP: motorcontrol.forward(GPIO) print "up" elif char == curses.KEY_DOWN: motorcontrol.reverse(GPIO) print "down" elif char == curses.KEY_RIGHT: motorcontrol.right(GPIO) print "right" elif char == curses.KEY_LEFT: motorcontrol.left(GPIO) print "left" elif char == 10:
import motorcontrol import soundsensor import random while True: #Always move forward motorcontrol.forward(0.2) #If close to object, backup dist = soundsensor.distance('cm') print(dist) if soundsensor.distance('cm') <= 20: motorcontrol.reverse(0.5) #Randomly turn then loop again turn = random.randint(0, 9) % 2 if random.randint == 0: motorcontrol.left_turn(random.randint(0, 9) % 2) else: motorcontrol.right_turn(random.randint(0, 9) % 2)