Exemplo n.º 1
0
    def connect(self):
        max_init_retries = 4
        retries = 0
        print("===========================")
        print("Initializing OpenVR...")
        while retries < max_init_retries:
            try:
                openvr.init(openvr.VRApplication_Scene)
                break
            except openvr.OpenVRError as e:
                print("Error when initializing OpenVR (try {} / {})".format(
                    retries + 1, max_init_retries))
                print(e)
                retries += 1
                time.sleep(2.0)
        else:
            print("Could not initialize OpenVR, aborting.")
            print(
                "Make sure the system is correctly plugged, you can also try")
            print("to do:")
            print("killall -9 vrcompositor vrmonitor vrdashboard")
            print("Before running this program again.")
            exit(0)

        print("Success!")
        print("===========================")
        self.vrsystem = openvr.VRSystem()
        poses_t = openvr.TrackedDevicePose_t * openvr.k_unMaxTrackedDeviceCount
        self.poses = poses_t()

        #left_id, right_id = None, None
        self.right_id = None
        print("===========================")
        print("Waiting for controllers...")

        try:
            while self.right_id is None:
                #while left_id is None or right_id is None:
                #left_id, right_id = self.get_controller_ids(vrsystem)
                self.right_id = self.get_controller_ids(self.vrsystem)
                #if (left_id or self.left_controller) and (right_id or self.right_controller):
                if self.right_id:
                    break
                #print("Waiting for controllers...")
                print("Waiting for the right controller...")
                time.sleep(1.0)
        except KeyboardInterrupt:
            print("Control+C pressed, shutting down...")
            openvr.shutdown()

        #print("Left controller ID: " + str(left_id))
        print("Right controller ID: " + str(self.right_id))
        print("===========================")

        #pp = pprint.PrettyPrinter(indent=4)

        reading_rate_hz = 100
        #show_only_new_events = True
        #last_unPacketNum_left = 0
        last_unPacketNum_right = 0
Exemplo n.º 2
0
 def __del__(self):
     glBindFramebuffer( GL_FRAMEBUFFER, 0)
     glDeleteFramebuffers(1, self._fb)
     try:
         openvr.shutdown()
         print('VR Interface Closed Successfully.')
     except: Exception("Could not properly close VR")
Exemplo n.º 3
0
 def poll(self):
     openvr.init(openvr.VRApplication_Scene)
     poses_t = openvr.TrackedDevicePose_t * openvr.k_unMaxTrackedDeviceCount
     poses = poses_t()
     while self._keep_polling:
         openvr.VRCompositor().waitGetPoses(poses, len(poses), None, 0)
         self.handle_controller_buttons(poses)
     openvr.shutdown()
Exemplo n.º 4
0
 def dispose_gl(self):
     for actor in self:
         actor.dispose_gl()
     if self.vr_system is not None:
         openvr.shutdown()
         self.vr_system = None
     if self.left_fb is not None:
         self.left_fb.dispose_gl()
         self.right_fb.dispose_gl()
Exemplo n.º 5
0
def main():
    setup_logging(debug=False)
    try:
        logging.info("Starting main loop")
        start_app()
    except Exception:
        logging.exception("")
        input("Press enter to close the program")
        raise SystemExit(-1)
    finally:
        openvr.shutdown()
Exemplo n.º 6
0
def main():
    setup_logging(debug=False)
    try:
        logging.info("Starting main loop")
        start_app()
    except Exception:
        logging.exception("")
        input("Press enter to close the program")
        raise SystemExit(-1)
    finally:
        openvr.shutdown()
Exemplo n.º 7
0
 def startContext(self):
     if (self.m_App): return
     try:
         # Try to init openvr
         self.m_App = vr.init(vr.VRApplication_Utility)
         return
     except:
         print("Could not initialize openvr subsystem")
     # We failed. Try to invalidate any possible open instance via shutdown (should never be necessary, but we do it anyway)
     try:
         self.m_App = None
         vr.shutdown()
     except:
         pass
Exemplo n.º 8
0
    def start(self):
        self.m_Running = True

        # Init audio subsystem -> Small buffer (512) reduces latency. Not sure, if this proposed fix of "pre_init, init, quit and init" will get rid of the remaining (annoying) lag
        mixer.pre_init(44100, -16, 2, 512)
        mixer.init()
        mixer.quit()
        mixer.init(44100, -16, 2, 512)

        sndCur = None  # None: silent, otherwise the Sound() object, which is currently playing
        sndOver = mixer.Sound(g_Over)
        sndNear = mixer.Sound(g_Near)

        while self.m_Running:
            if (self.isVRRunning()):
                # Executed, when VR is running
                dist = self.tick()
                sndNext = None

                if dist >= 0:
                    if (dist == 0):
                        sndNext = sndOver
                    elif (dist < self.m_minDist):
                        # Make amplitude grow exponentially from 1/e^2 to 1
                        amp = 1 - math.exp(
                            -3 * (self.m_minDist - dist) / self.m_minDist)
                        sndNear.set_volume(amp)
                        sndNext = sndNear
                    elif (sndCur):
                        sndNext = None
                elif (sndCur):
                    sndNext = None

                # Did the audiocontext change?
                if (sndNext != sndCur):
                    if (sndCur): sndCur.stop()
                    sndCur = sndNext
                    if (sndCur): sndCur.play(-1)
            else:
                # VR either not running or initializing an OpenVR context failed -> Get new VR context and wait 10 seconds
                self.startContext()
                time.sleep(10)
            time.sleep(0.01)

        mixer.music.stop()
        vr.shutdown()
Exemplo n.º 9
0
def main(buttons=False, args=None):
    if not openvr.isRuntimeInstalled:
        raise RuntimeError("OpenVR / SteamVr is not Installed Exit")
    if not openvr.isHmdPresent():
        raise RuntimeError(
            "SteamVr is not running or Headmount is not plugged in")

    rclpy.init(args=args)
    system = openvr.init(openvr.VRApplication_Scene)

    minimal_publisher = VrPublisher(system, buttons)

    rclpy.spin(minimal_publisher)

    minimal_publisher.destroy_node()
    rclpy.shutdown()
    openvr.shutdown()
Exemplo n.º 10
0
 def shut_down(self):
     if self.hmd:
         openvr.shutdown()
         self.hmd = None
     self.render_models = dict()
     if self.scene_vert_buffer:
         GL.glDeleteBuffers(1, [self.scene_vert_buffer])
         self.scene_vert_buffer = None
         self.left_eye_desc.delete()
         self.right_eye_desc.delete()
         if self.companion_window_vao:
             GL.glDeleteVertexArrays(1, [self.companion_window_vao])
             self.companion_window_vao = None
         if self.scene_vao:
             GL.glDeleteVertexArrays(1, [self.scene_vao])
             self.scene_vao = None
         if self.controller_vao:
             GL.glDeleteVertexArrays(1, [self.controller_vao])
             self.controller_vao = None
     glfw.terminate()
Exemplo n.º 11
0
    def __init__(self, path='config.json'):
        """Start and Scan VR devices."""
        # Initialize OpenVR
        self.vr = openvr.init(openvr.VRApplication_Other)

        # Loading config file
        self.config = None
        try:
            with open(path) as json_data:
                self.config = json.load(json_data)
        except EnvironmentError:  # parent of IOError, OSError
            print('required config.json not found, closing...')
            openvr.shutdown()
            sys.exit(1)

        self.poses_count = 0
        self.devices = {}
        poses = self.vr.getDeviceToAbsoluteTrackingPose(
            openvr.TrackingUniverseStanding, 0,
            openvr.k_unMaxTrackedDeviceCount)
        self.update_devices_dict(poses)
    def controller_inititalization(self):
        """
        Waiting for BOTH controllers to get visible so we can read their data
        """

        print("===========================")
        print("Waiting for BOTH controllers to get visible...")
        try:
            while self.left_controller_id_in_VRsystem is None or self.right_controller_id_in_VRsystem is None:
                self.get_controllers_id()
                if self.left_controller_id_in_VRsystem and self.right_controller_id_in_VRsystem:
                    print("Controllers initialized")
                    break
                print("Waiting for BOTH controllers to get visible...")
                time.sleep(1.0)
        except KeyboardInterrupt:
            print("Control+C pressed, shutting down...")
            openvr.shutdown()
        return [
            self.left_controller_id_in_VRsystem,
            self.right_controller_id_in_VRsystem
        ]
                                                      grab_controller_start))

        cf0.commander.send_position_setpoint(setpoints[0][0],
                                             setpoints[0][1],
                                             setpoints[0][2],
                                             0)
        cf1.commander.send_position_setpoint(setpoints[1][0],
                                             setpoints[1][1],
                                             setpoints[1][2],
                                             0)

        time.sleep(0.02)

    cf0.commander.send_setpoint(0, 0, 0, 0)
    cf1.commander.send_setpoint(0, 0, 0, 0)
    # Make sure that the last packet leaves before the link is closed
    # since the message queue is not flushed before closing
    time.sleep(0.1)


if __name__ == '__main__':
    cflib.crtp.init_drivers(enable_debug_driver=False)

    with SyncCrazyflie(uri0, cf=Crazyflie(rw_cache='./cache')) as scf0:
        reset_estimator(scf0)
        with SyncCrazyflie(uri1, cf=Crazyflie(rw_cache='./cache')) as scf1:
            reset_estimator(scf1)
            run_sequence(scf0, scf1)

    openvr.shutdown()
Exemplo n.º 14
0
 def closeEvent(self, event):
     openvr.shutdown()
Exemplo n.º 15
0
 def shutdown(self):
     self.controllers.dispose_gl()
     openvr.shutdown()
Exemplo n.º 16
0
 def shutdown(self):
     openvr.shutdown()
Exemplo n.º 17
0
 def closeEvent(self, event):
     self.timer.stop()
     openvr.shutdown()
     super().closeEvent(event)