YARP
Yet Another Robot Platform
OVRHeadset.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef YARP_OVRHEADSET_OVRHEADSET_H
20 #define YARP_OVRHEADSET_OVRHEADSET_H
21 
22 #include "ImageType.h"
23 
24 #include <yarp/os/PeriodicThread.h>
25 #include <yarp/dev/DeviceDriver.h>
29 #include <yarp/dev/PolyDriver.h>
30 #include <yarp/sig/Image.h>
31 
32 #include <GL/glew.h>
33 #include <OVR_CAPI.h>
34 #include <OVR_CAPI_GL.h>
35 
36 #include <map>
37 #include <mutex>
38 #include <vector>
39 
40 
41 namespace yarp { namespace os { template <typename T> class BufferedPort; }}
42 namespace yarp { namespace os { class Bottle; }}
43 struct GLFWwindow;
44 class InputCallback;
45 class TextureStatic;
46 class TextureBattery;
47 struct guiParam;
48 
49 namespace yarp {
50 namespace dev {
51 
81  public yarp::dev::IService,
83 {
84 public:
85  explicit OVRHeadset();
86  virtual ~OVRHeadset();
87 
88  // yarp::dev::DeviceDriver methods
89  virtual bool open(yarp::os::Searchable& cfg);
90  virtual bool close();
91 
92  // yarp::os::RateThread methods
93  virtual bool threadInit();
94  virtual void threadRelease();
95  virtual void run();
96 
97  // yarp::dev::IService methods
98  virtual bool startService();
99  virtual bool updateService();
100  virtual bool stopService();
101 
102  // yarp::dev::IJoypadController methods
103  bool getAxisCount(unsigned int& axis_count) override;
104  bool getButtonCount(unsigned int& button_count) override;
105  bool getTrackballCount(unsigned int& Trackball_count) override;
106  bool getHatCount(unsigned int& Hat_count) override;
107  bool getTouchSurfaceCount(unsigned int& touch_count) override;
108  bool getStickCount(unsigned int& stick_count) override;
109  bool getStickDoF(unsigned int stick_id, unsigned int& DoF) override;
110  bool getButton(unsigned int button_id, float& value) override;
111  bool getTrackball(unsigned int trackball_id, yarp::sig::Vector& value) override;
112  bool getHat(unsigned int hat_id, unsigned char& value) override;
113  bool getAxis(unsigned int axis_id, double& value) override;
114  bool getStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) override;
115  bool getTouch(unsigned int touch_id, yarp::sig::Vector& value) override;
116 
117 private:
118 
120 
121  bool createWindow(int w, int h);
122  void onKey(int key, int scancode, int action, int mods);
123  void reconfigureRendering();
124  void reconfigureFOV();
125 
126  static void glfwKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
127  static void glfwErrorCallback(int error, const char* description);
128  static void ovrDebugCallback(uintptr_t userData, int level, const char* message);
129  static void DebugHmd(ovrHmdDesc hmdDesc);
130  void errorManager(ovrResult error);
131  void fillAxisStorage();
132  void fillErrorStorage();
133  void fillButtonStorage();
134  void fillHatStorage();
135  void resetInput();
136 
137 
140  yarp::os::BufferedPort<yarp::os::Bottle>* angularVelocityPort;
141  yarp::os::BufferedPort<yarp::os::Bottle>* linearVelocityPort;
142  yarp::os::BufferedPort<yarp::os::Bottle>* angularAccelerationPort;
143  yarp::os::BufferedPort<yarp::os::Bottle>* linearAccelerationPort;
144  yarp::os::BufferedPort<yarp::os::Bottle>* predictedOrientationPort;
145  yarp::os::BufferedPort<yarp::os::Bottle>* predictedPositionPort;
146  yarp::os::BufferedPort<yarp::os::Bottle>* predictedAngularVelocityPort;
147  yarp::os::BufferedPort<yarp::os::Bottle>* predictedLinearVelocityPort;
148  yarp::os::BufferedPort<yarp::os::Bottle>* predictedAngularAccelerationPort;
149  yarp::os::BufferedPort<yarp::os::Bottle>* predictedLinearAccelerationPort;
150 
151  FlexImagePort* gui_ports{ nullptr };
152  std::vector<guiParam> huds;
153  InputCallback* displayPorts[2]{ nullptr, nullptr };
154  ovrEyeRenderDesc EyeRenderDesc[2];
155  TextureStatic* textureLogo{ nullptr };
156  ovrLayerQuad logoLayer;
157  TextureStatic* textureCrosshairs{ nullptr };
158  ovrLayerQuad crosshairsLayer;
159  TextureBattery* textureBattery{ nullptr };
160  ovrLayerQuad batteryLayer;
161  ovrMirrorTexture mirrorTexture{ nullptr };
162  GLuint mirrorFBO{ 0 };
163  ovrSession session;
164  ovrHmdDesc hmdDesc;
165  GLFWwindow* window{ nullptr };
166  ovrTrackingState ts;
167  ovrPoseStatef headpose;
168  ovrPoseStatef predicted_headpose;
169  unsigned int guiCount;
170  bool guiEnabled{ true };
171  std::mutex inputStateMutex;
172  ovrInputState inputState;
173  bool inputStateError{ false };
174  bool getStickAsAxis;
175  std::vector<ovrButton> buttonIdToOvrButton;
176  std::vector<float*> axisIdToValue;
177  std::map<int, int> DButtonToHat;
178  std::map<ovrResult, std::string> error_messages;
179 
180  IFrameTransform* tfPublisher;
181  bool relative;
182  std::string left_frame;
183  std::string right_frame;
184  std::string root_frame;
185  PolyDriver driver;
186 
187  bool closed{ false };
188  long long distortionFrameIndex{ 0 };
189 
190  unsigned int texWidth;
191  unsigned int texHeight;
192  double camHFOV[2];
193  size_t camWidth[2];
194  size_t camHeight[2];
195  ovrFovPort fov[2];
196 
197  bool flipInputEnabled{ false };
198  bool imagePoseEnabled{ true };
199  bool userPoseEnabled{ false };
200 
201  // Layers
202  bool logoEnabled{ true };
203  bool crosshairsEnabled{ true };
204  bool batteryEnabled{ true };
205 
206  double prediction;
207 
208 }; // class OVRHeadset
209 
210 } // namespace dev
211 } // namespace yarp
212 
213 
214 #endif // YARP_OVRHEADSET_OVRHEADSET_H
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
Transform Interface.
Common interface for devices that act like services (by which we mean they do something for remote us...
Device that manages the Oculus Rift Headset.
Definition: OVRHeadset.h:83
bool getStickDoF(unsigned int stick_id, unsigned int &DoF) override
Get the Degree Of Freedom count for desired stick.
virtual void threadRelease()
Release method.
Definition: OVRHeadset.cpp:737
virtual bool startService()
Initiate the service, whatever it is.
Definition: OVRHeadset.cpp:835
virtual bool close()
Close the DeviceDriver.
Definition: OVRHeadset.cpp:828
bool getButtonCount(unsigned int &button_count) override
Get number of Buttons.
bool getHat(unsigned int hat_id, unsigned char &value) override
Get the value of an Hat.
virtual bool stopService()
Shut down the service, whatever it is.
Definition: OVRHeadset.cpp:876
bool getHatCount(unsigned int &Hat_count) override
Get number of Hats.
bool getButton(unsigned int button_id, float &value) override
Get the value of a button.
bool getStick(unsigned int stick_id, yarp::sig::Vector &value, JoypadCtrl_coordinateMode coordinate_mode) override
Get the value of a stick if present, return false otherwise.
bool getTouchSurfaceCount(unsigned int &touch_count) override
get the number of touch surface.
bool getTrackballCount(unsigned int &Trackball_count) override
Get number of trackballs.
virtual bool threadInit()
Initialization method.
Definition: OVRHeadset.cpp:612
virtual bool open(yarp::os::Searchable &cfg)
Open the DeviceDriver.
Definition: OVRHeadset.cpp:375
bool getStickCount(unsigned int &stick_count) override
get the number of the sticks
bool getTouch(unsigned int touch_id, yarp::sig::Vector &value) override
Get the value of a touch if present, return false otherwise.
virtual bool updateService()
Give the service the chance to run for a while.
Definition: OVRHeadset.cpp:841
bool getAxis(unsigned int axis_id, double &value) override
Get the value of an axis if present, return false otherwise.
virtual void run()
Loop function.
Definition: OVRHeadset.cpp:897
bool getTrackball(unsigned int trackball_id, yarp::sig::Vector &value) override
Get the axes change of a Trackball.
bool getAxisCount(unsigned int &axis_count) override
Get number of Axes.
A container for a device driver.
Definition: PolyDriver.h:27
An abstraction for a periodic thread.
A base class for nested structures that can be searched.
Definition: Searchable.h:69
The main, catch-all namespace for YARP.
Definition: environment.h:18