def __init__(self): os.chdir(os.path.dirname(os.path.abspath(__file__))) self.pi = pigpio.pi() config = configparser.ConfigParser() config.read("rover.conf") self.childProcesses = [] videoConfig = config["VIDEO"] driverConfig = config["DRIVER"] leftMotorConfig = config["LEFTMOTOR"] rightMotorConfig = config["RIGHTMOTOR"] servoConfig = config["SERVO"] lightConfig = config["LIGHT"] print("Starting GStreamer pipeline...") self.execAndMonitor(videoConfig["GStreamerStartCommand"]) print("Starting Janus gateway...") self.execAndMonitor(videoConfig["JanusStartCommand"]) print("Creating motor controller...") leftMotor = Motor(self.pi, int(leftMotorConfig["PWMPin"]), int(leftMotorConfig["ForwardPin"]), int(leftMotorConfig["ReversePin"]), float(leftMotorConfig["Trim"])) rightMotor = Motor(self.pi, int(rightMotorConfig["PWMPin"]), int(rightMotorConfig["ForwardPin"]), int(rightMotorConfig["ReversePin"]), float(rightMotorConfig["Trim"])) self.motorController = MotorController( leftMotor, rightMotor, float(driverConfig["HalfTurnSpeed"])) self.servoController = ServoController(self.pi, int(servoConfig["PWMPin"])) self.lightController = LightController(self.pi, int(lightConfig["CONTROLPin"])) heartbeatInterval = float(driverConfig["MaxHeartbeatInvervalMS"]) self.ssidRegex = re.compile(r"ESSID:\"(.+?)\"") self.qualityRegex = re.compile(r"Link Quality=([^ ]+)") self.signalRegex = re.compile(r"Signal level=(.*? dBm)") self.lastHeartbeat = time.time() self.heartbeatThread = Thread(daemon=True, target=self.heartbeatLoop, args=[heartbeatInterval]) self.heartbeatThread.start()
# and the borders that trigger robot rotation side_borders_distance = 200 # face tracking area thresholds are used for forward/backward movement of the robot # max square area threshold for face tracking max_face_tracking_area = 2200 # min square area threshold for face tracking min_face_tracking_area = 1900 tracked_face_color = (0, 255, 0) side_border_color = (0, 0, 255) # give camera time to warm up time.sleep(0.1) motor_controller = MotorController() for still in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # take the frame as an array, convert it to black and white, and look for facial features image = still.array gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv.CASCADE_SCALE_IMAGE ) if len(faces) == 0: motor_controller.stop()
global servo1DutyCycle global servo2DutyCycle servo1DutyCycle = motor1 servo2DutyCycle = motor2 servo1.ChangeDutyCycle(servo1DutyCycle) servo2.ChangeDutyCycle(servo2DutyCycle) def disableServos(): servo1.ChangeDutyCycle(0) servo2.ChangeDutyCycle(0) try: motorcontroller = MotorController() servo1.start(servo1DutyCycle) servo2.start(servo2DutyCycle) disableServos() camera = PiCamera() camera.rotation = 0 camera.start_preview(fullscreen=False, window=(200, -100, 600, 800)) while True: char = screen.getch() if char == ord('q'): GPIO.output(5, GPIO.LOW) GPIO.output(11, GPIO.LOW)
class Driver: def __init__(self): os.chdir(os.path.dirname(os.path.abspath(__file__))) self.pi = pigpio.pi() config = configparser.ConfigParser() config.read("rover.conf") self.childProcesses = [] videoConfig = config["VIDEO"] driverConfig = config["DRIVER"] leftMotorConfig = config["LEFTMOTOR"] rightMotorConfig = config["RIGHTMOTOR"] servoConfig = config["SERVO"] print("Starting GStreamer pipeline...") self.execAndMonitor(videoConfig["GStreamerStartCommand"]) print("Starting Janus gateway...") self.execAndMonitor(videoConfig["JanusStartCommand"]) print("Creating motor controller...") leftMotor = Motor(self.pi, int(leftMotorConfig["PWMPin"]), int(leftMotorConfig["ForwardPin"]), int(leftMotorConfig["ReversePin"]), float(leftMotorConfig["Trim"])) rightMotor = Motor(self.pi, int(rightMotorConfig["PWMPin"]), int(rightMotorConfig["ForwardPin"]), int(rightMotorConfig["ReversePin"]), float(rightMotorConfig["Trim"])) self.motorController = MotorController( leftMotor, rightMotor, float(driverConfig["HalfTurnSpeed"])) self.servoController = ServoController(self.pi, int(servoConfig["PWMPin"])) heartbeatInterval = float(driverConfig["MaxHeartbeatInvervalMS"]) self.ssidRegex = re.compile(r"ESSID:\"(.+?)\"") self.qualityRegex = re.compile(r"Link Quality=([^ ]+)") self.signalRegex = re.compile(r"Signal level=(.*? dBm)") self.lastHeartbeat = time.time() self.heartbeatThread = Thread(daemon=True, target=self.heartbeatLoop, args=[heartbeatInterval]) self.heartbeatThread.start() shuttingDown = False lastHeartbeat = -1 heartbeatStop = False lastHeartbeatData = {"SSID": "-", "Quality": "-", "Signal": "-"} def heartbeatLoop(self, maxInterval): print("Starting heartbeat thread...") while not self.shuttingDown: if (time.time() - self.lastHeartbeat) > maxInterval: if not self.heartbeatStop: self.stop() self.lookStop() self.heartbeatStop = True else: self.heartbeatStop = False self.lastHeartbeatData = self.collectHeartbeatData() time.sleep(0.5) def collectHeartbeatData(self): try: wifiInfo = subprocess.check_output("iwconfig wlan0", shell=True).decode("utf-8") ssidMatch = self.ssidRegex.search(wifiInfo) ssid = ssidMatch.group(1) if ssidMatch else "-" qualityMatch = self.qualityRegex.search(wifiInfo) quality = qualityMatch.group(1) if qualityMatch else "-" signalMatch = self.signalRegex.search(wifiInfo) signal = signalMatch.group(1) if signalMatch else "-" return {"SSID": ssid, "Quality": quality, "Signal": signal} except Exception as ex: print(str(ex), file=sys.stderr) return {"SSID": "-", "Quality": "-", "Signal": "-"} def setBearing(self, bearing): self.motorController.setBearing(bearing) def stop(self): self.motorController.setBearing(-1) def lookUp(self): self.servoController.backward() def lookDown(self): self.servoController.forward() def lookStop(self): self.servoController.lookStop() def onHeartbeat(self): self.lastHeartbeat = time.time() return self.lastHeartbeatData def execAndMonitor(self, command): args = shlex.split(command) print("Starting: " + str(args)) p = subprocess.Popen(args, stdout=sys.stdout, stderr=sys.stderr, shell=True) self.childProcesses.append(p) def cleanup(self): self.servoController.stop() self.pi.stop() self.shuttingDown = True print("Waiting for heartbeat thread to stop...") self.heartbeatThread.join() print("Heartbeat thread stopped") for p in self.childProcesses: p.terminate()
gpio = pigpio.pi() if not gpio.connected: print('GPIO not connected') exit() config = ConfigParser() config.read(os.path.join(homePath, "rover.conf")) audioConfig = config["AUDIO"] videoConfig = config["VIDEO"] loop = asyncio.get_event_loop() loop.set_exception_handler(loopExceptionHandler) audioManager = AudioManager(config) motorController = MotorController(config, gpio, audioManager) alsa = Alsa(gpio, config) servoController = ServoController(gpio, config, audioManager) lightsController = LightsController(gpio, config) tts = TTSSpeaker(config, alsa, audioManager) powerPlant = PowerPlant(config) startupController = StartupSequenceController(config, servoController, lightsController, tts) heartbeat = Heartbeat(config, servoController, motorController, alsa, lightsController, powerPlant)
homePath = os.path.dirname(os.path.abspath(__file__)) sslctx = createSSLContext(os.path.dirname(homePath)) GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) config = ConfigParser() config.read(os.path.join(homePath, "rover.conf")) audioConfig = config["AUDIO"] videoConfig = config["VIDEO"] loop = asyncio.get_event_loop() loop.set_exception_handler(loopExceptionHandler) motorController = MotorController(config) alsa = Alsa(config) servoController = ServoController(config) tts = TTSSpeaker(config, alsa) heartbeat = Heartbeat(config, servoController, motorController, alsa) heartbeat.start() janus = ExternalProcess(videoConfig["JanusStartCommand"], False, False, "janus.log") videoStream = ExternalProcess(videoConfig["GStreamerStartCommand"], True, False, "video.log") audioStream = ExternalProcess(audioConfig["GStreamerStartCommand"], True,