def check_speech_recognition(self): # Read file on disk with suppress(IOError): # we want to always remove the speech recognition file, even if enabled if self.enabled and Sphero().current_room.speech_recognition: with open(self.speech_recognition_file_path) as f: action_name = f.read().strip() dest_room = None # for now, redo a mapping between actions and room name (FIXME, get all events names as config) if action_name == "turn_on_the_light" or action_name == "turn_the_light_off": dest_room = "bedroom" elif (action_name == "turn_on_the_air_conditioning" or action_name == "turn_the_air_conditioning_off"): dest_room = "livingroom" elif action_name == "open_garage_door" or action_name == "close_garage": dest_room = "garage" elif action_name == "kitchen_turn_on_light" or action_name == "kitchen_turn_the_light_off": dest_room = "kitchen" else: logger.info("Unrecognized speech event") if dest_room: Sphero().move_to(dest_room) self.clean_speech_recognition_state() return True
def __init__(self, deviceAddress): self.shift = 0 print 'Start connecting...' self.droid = Sphero(deviceAddress) # Request some sensor stream. self.droid.cmd(0x02, 0x11, [0, 80, 0, 1, 0x80, 0, 0, 0, 0]) print 'initialized successfully!'
def _sendSpheroInfo(self): """Send paired sphero info""" from sphero import Sphero msg = { "spheros": Sphero().sphero_list.keys(), "current": Sphero().sphero_name } self.__sendMessage("spheroinfo", msg)
def test_set_heading2(self): orb = Sphero(MyTestCase.SPHERO) orb.connect() for angle in range(0,360,90): orb.roll(30,90) time.sleep(3) orb.set_heading(angle) time.sleep(1) orb.roll(30, 90) time.sleep(3)
def switchlight(sphero): """simulate light switch on""" logger.info("Light switch on (on sphero)") for i in range(255): sphero.set_rgb(i, i, i) sleep(0.005) sleep(3) from sphero import Sphero Sphero().reset_default_color()
def detect_face(self): # Read the image video_capture = cv2.VideoCapture(0) ret, image = video_capture.read() video_capture.release() # Detect faces in the image faces = self.faceCascade.detectMultiScale( image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE ) # Draw a rectangle around the faces face_detected = False if len(faces) > 0: self._last_detected_face = time() face_detected = True sphero = Sphero() if sphero.current_room == Home().start_room and face_detected: logger.info("Face recognized and sphero in ketten, moving to welcome guest") sphero.move_to(Home().facedetectdest_room.name) # if no activity for a long time and no face either, put the sphero back to the ketten elif (not face_detected and sphero.current_room == Home().facedetectdest_room and (time() - self._last_detected_face) > self.INACTIVITY_PERIOD and (time() - sphero.last_move) > self.INACTIVITY_PERIOD): logger.info("No activity or face showing up for a long time, going back to ketten") sphero.move_to(Home().start_room.name) return True
def welcome(sphero): """Let sphero welcome you""" logger.info("Sphero welcome!") for _ in range(4): sphero.roll(0, 20) sphero.set_rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) sleep(0.2) sphero.roll(0, -20 % 360) sphero.set_rgb(0, 0, 0) sleep(0.2) sphero.roll(0, 0) from sphero import Sphero Sphero().reset_default_color()
def handleMessage(self): """Message received from a client""" logger.debug("Received from {}: {}".format(self.address[0], self.data)) data = json.loads(self.data) topic = data["topic"] message = data["content"] if topic == "facedetectionchange": from facedetection import FaceDetection FaceDetection().enabled = message elif topic == "speechrecognitionchange": from speechrecognition import SpeechRecognition SpeechRecognition().enabled = message elif topic == "move": from sphero import Sphero Sphero().move_to(message) elif topic == "manualmove": from sphero import Sphero Sphero().current_room = Home().rooms[message] elif topic == "moveposition": from sphero import Sphero Sphero().move_to_distance(int(message["distance"]), int(message["angle"])) elif topic == "calibrationstate": from sphero import Sphero if message: Sphero().start_calibration() else: Sphero().end_calibration() elif topic == "recenter": from sphero import Sphero # use -angle to rotate in the counter-clock sense Sphero().recenter(-message) elif topic == "changesphero": from sphero import Sphero Sphero().change_default_sphero(message) elif topic == "quit": from sphero import Sphero Sphero().quit()
def main(): mainloop = MainLoop() parser = argparse.ArgumentParser( description="Home demo gateway with robot") parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase output verbosity (2 levels)") parser.add_argument("--without-sphero", action="store_true", help="Use a mock sphero instead of a real one") parser.add_argument("--no-facedetection", action="store_true", help="Disable face detection feature") parser.add_argument("--no-speechrecognition", action="store_true", help="Disable speech recognition feature") # set logging level set_logging_from_args(sys.argv, parser) args = parser.parse_args() # connect to sphero and set it at starting position sphero = Sphero(without_sphero=args.without_sphero) # start servers StaticServer().start() CommandSocketServer().start() # detect faces every 3 seconds f = FaceDetection() f.enabled = not args.no_facedetection # speech recognition s = SpeechRecognition() s.enabled = not args.no_speechrecognition mainloop.run()
def test_set_heading(self): orb = Sphero(MyTestCase.SPHERO) orb.connect() orb.set_tail_light(255) orb.set_stabilization(False) print("Move Sphero so tail is heading to -y") input("Press Enter to continue...") orb.set_stabilization(True) orb.set_tail_light(0) time.sleep(3)
def test_set_heading(self): orb = Sphero(MyTestCase.SPHERO) orb.connect() orb.set_tail_light(255) orb.set_heading(0) time.sleep(2) orb.set_heading(90) time.sleep(2) orb.set_heading(180) time.sleep(2) orb.set_heading(270) time.sleep(2) orb.set_tail_light(0) time.sleep(3)
def test_roll(self): orb = Sphero(MyTestCase.SPHERO) orb.connect() orb.roll(10,0)
def test_locator(self): orb = Sphero(MyTestCase.SPHERO) orb.connect() orb.set_stream_callback('odometer', callback=self.print_xy, mask_id=2) orb.update_streaming(rate=10) time.sleep(2) before = MyTestCase.current_position orb.config_locator(10,50,0) orb.ping() time.sleep(2) orb.config_locator(50, -10, 0) orb.ping() middle = MyTestCase.current_position orb.ping() time.sleep(2) orb.ping() time.sleep(2) after = MyTestCase.current_position self.assertNotEquals(before, middle) self.assertNotEquals(middle, after)
def test_connect(self): orb = Sphero(MyTestCase.SPHERO) result = orb.connect() self.assertTrue(result)
def test_set_color(self): orb = Sphero(MyTestCase.SPHERO) orb.connect() orb.set_rgb_led(255,0,0)
class Droid: DEFAULT_SPEED = 20 def __init__(self, deviceAddress): self.shift = 0 print 'Start connecting...' self.droid = Sphero(deviceAddress) # Request some sensor stream. self.droid.cmd(0x02, 0x11, [0, 80, 0, 1, 0x80, 0, 0, 0, 0]) print 'initialized successfully!' def move(self, speed, direction): ''' speed - integer in range [0 .. 255] direction - integer in range [0 .. 359]''' v = speed h = ((int)(direction - self.shift) + 360 + 360) % 360 print 'moving! v: ' + repr(v) + ' h: ' + repr(h) self.droid.cmd(0x02, 0x30, [v, (h & 0xff00) >> 8, h & 0xff, 1]) def moveUp(self, time, speed=DEFAULT_SPEED): self.move(speed, 0) sleep(time) def moveDown(self, time, speed=DEFAULT_SPEED): self.move(speed, 180) sleep(time) def moveRight(self, time, speed=DEFAULT_SPEED): self.move(speed, 90) sleep(time) def moveLeft(self, time, speed=DEFAULT_SPEED): self.move(speed, 270) sleep(time) def setColor(self, r, g, b): self.droid.cmd(0x02, 0x20, [r, g, b, 0]) def doCommand(self, command): directionText = command[0] direction = -1 if directionText == 'U' or directionText == 'N': direction = 0 elif directionText == 'R' or directionText == 'E': direction = 90 elif directionText == 'D' or directionText == 'S': direction = 180 elif directionText == 'L' or directionText == 'W': direction = 270 else: return "wrong direction!" # print repr(command) time = float(int(command[2:6])) / 1000 speed = int(command[7:-1]) print repr(command) + '\r\ndirection: ' + repr(direction) + ' time: ' + repr(time) + ' speed: ' + repr( speed) if directionText == 'U' or directionText == 'N': self.moveUp(time, speed) elif directionText == 'R' or directionText == 'E': self.moveRight(time, speed) elif directionText == 'D' or directionText == 'S': self.moveDown(time, speed) elif directionText == 'L' or directionText == 'W': self.moveLeft(time, speed) return "good!" def disconnect(self): # Must manually disconnect or you won't be able to reconnect. self.droid.disconnect() print 'Disconnected successfully!' # raw_input("Press Enter to exit...") def setShift(self, newShift): self.shift = newShift def moveUpDown(self): self.moveUp(0.5, 1) self.moveUp(0.5, 2) self.moveUp(0.5, 1) self.moveUp(1.5, 50) self.moveUp(0.5, 1) self.moveUp(0.5, 2) self.moveUp(0.5, 1) self.moveDown(0.5, 1) self.moveDown(0.5, 2) self.moveDown(0.5, 1) self.moveDown(1.5, 50) self.moveDown(0.5, 1) self.moveDown(0.5, 2) self.moveDown(0.5, 1) def determineShift(self): raw_input("Press Enter to start setting") while True: print "current shift: " + repr(self.shift) self.moveUpDown() newShift = input("enter new shift (or current value to finish settings): ") if newShift == self.shift: break self.setShift(newShift)
def connect_sphero(): s = Sphero("/dev/tty.Sphero-GPR-AMP-SPP") s.connect() return s
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Internationalization # https://docs.djangoproject.com/en/1.8/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.8/howto/static-files/ STATIC_URL = '/static/' from sphero import Sphero SP = Sphero("/dev/tty.Sphero-GPR-AMP-SPP") SP.connect()
def _sendCurrentRoom(self): """Send current rooms""" from sphero import Sphero self.__sendMessage("currentroom", Sphero().current_room.name)
def _sendCalibrationState(self): """Send calibration message state""" from sphero import Sphero self.__sendMessage("calibrationstate", Sphero().in_calibration)