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 
82  public yarp::dev::IService,
84 {
85 public:
86  explicit OVRHeadset();
87  virtual ~OVRHeadset();
88 
89  // yarp::dev::DeviceDriver methods
90  virtual bool open(yarp::os::Searchable& cfg);
91  virtual bool close();
92 
93  // yarp::os::RateThread methods
94  virtual bool threadInit();
95  virtual void threadRelease();
96  virtual void run();
97 
98  // yarp::dev::IService methods
99  virtual bool startService();
100  virtual bool updateService();
101  virtual bool stopService();
102 
103  // yarp::dev::IJoypadController methods
104  bool getAxisCount(unsigned int& axis_count) override;
105  bool getButtonCount(unsigned int& button_count) override;
106  bool getTrackballCount(unsigned int& Trackball_count) override;
107  bool getHatCount(unsigned int& Hat_count) override;
108  bool getTouchSurfaceCount(unsigned int& touch_count) override;
109  bool getStickCount(unsigned int& stick_count) override;
110  bool getStickDoF(unsigned int stick_id, unsigned int& DoF) override;
111  bool getButton(unsigned int button_id, float& value) override;
112  bool getTrackball(unsigned int trackball_id, yarp::sig::Vector& value) override;
113  bool getHat(unsigned int hat_id, unsigned char& value) override;
114  bool getAxis(unsigned int axis_id, double& value) override;
115  bool getStick(unsigned int stick_id, yarp::sig::Vector& value, JoypadCtrl_coordinateMode coordinate_mode) override;
116  bool getTouch(unsigned int touch_id, yarp::sig::Vector& value) override;
117 
118 private:
119 
121 
122  bool createWindow(int w, int h);
123  void onKey(int key, int scancode, int action, int mods);
124  void reconfigureRendering();
125  void reconfigureFOV();
126 
127  static void glfwKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
128  static void glfwErrorCallback(int error, const char* description);
129  static void ovrDebugCallback(uintptr_t userData, int level, const char* message);
130  static void DebugHmd(ovrHmdDesc hmdDesc);
131  void errorManager(ovrResult error);
132  void fillAxisStorage();
133  void fillErrorStorage();
134  void fillButtonStorage();
135  void fillHatStorage();
136  void resetInput();
137 
138 
141  yarp::os::BufferedPort<yarp::os::Bottle>* angularVelocityPort;
142  yarp::os::BufferedPort<yarp::os::Bottle>* linearVelocityPort;
143  yarp::os::BufferedPort<yarp::os::Bottle>* angularAccelerationPort;
144  yarp::os::BufferedPort<yarp::os::Bottle>* linearAccelerationPort;
145  yarp::os::BufferedPort<yarp::os::Bottle>* predictedOrientationPort;
146  yarp::os::BufferedPort<yarp::os::Bottle>* predictedPositionPort;
147  yarp::os::BufferedPort<yarp::os::Bottle>* predictedAngularVelocityPort;
148  yarp::os::BufferedPort<yarp::os::Bottle>* predictedLinearVelocityPort;
149  yarp::os::BufferedPort<yarp::os::Bottle>* predictedAngularAccelerationPort;
150  yarp::os::BufferedPort<yarp::os::Bottle>* predictedLinearAccelerationPort;
151 
152  FlexImagePort* gui_ports{ nullptr };
153  std::vector<guiParam> huds;
154  InputCallback* displayPorts[2]{ nullptr, nullptr };
155  ovrEyeRenderDesc EyeRenderDesc[2];
156  TextureStatic* textureLogo{ nullptr };
157  ovrLayerQuad logoLayer;
158  TextureStatic* textureCrosshairs{ nullptr };
159  ovrLayerQuad crosshairsLayer;
160  TextureBattery* textureBattery{ nullptr };
161  ovrLayerQuad batteryLayer;
162  ovrMirrorTexture mirrorTexture{ nullptr };
163  GLuint mirrorFBO{ 0 };
164  ovrSession session;
165  ovrHmdDesc hmdDesc;
166  GLFWwindow* window{ nullptr };
167  ovrTrackingState ts;
168  ovrPoseStatef headpose;
169  ovrPoseStatef predicted_headpose;
170  unsigned int guiCount;
171  bool guiEnabled{ true };
172  std::mutex inputStateMutex;
173  ovrInputState inputState;
174  bool inputStateError{ false };
175  bool getStickAsAxis;
176  std::vector<ovrButton> buttonIdToOvrButton;
177  std::vector<float*> axisIdToValue;
178  std::map<int, int> DButtonToHat;
179  std::map<ovrResult, std::string> error_messages;
180 
181  IFrameTransform* tfPublisher;
182  bool relative;
183  std::string left_frame;
184  std::string right_frame;
185  std::string root_frame;
186  PolyDriver driver;
187 
188  bool closed{ false };
189  long long distortionFrameIndex{ 0 };
190 
191  unsigned int texWidth;
192  unsigned int texHeight;
193  double camHFOV[2];
194  size_t camWidth[2];
195  size_t camHeight[2];
196  ovrFovPort fov[2];
197 
198  bool flipInputEnabled{ false };
199  bool imagePoseEnabled{ true };
200  bool userPoseEnabled{ false };
201 
202  // Layers
203  bool logoEnabled{ true };
204  bool crosshairsEnabled{ true };
205  bool batteryEnabled{ true };
206 
207  double prediction;
208 
209 }; // class OVRHeadset
210 
211 } // namespace dev
212 } // namespace yarp
213 
214 
215 #endif // YARP_OVRHEADSET_OVRHEADSET_H
yarp::dev::IJoypadController::JoypadCtrl_coordinateMode
JoypadCtrl_coordinateMode
Definition: IJoypadController.h:37
yarp::dev::OVRHeadset::getTouch
bool getTouch(unsigned int touch_id, yarp::sig::Vector &value) override
Get the value of a touch if present, return false otherwise.
Definition: OVRHeadset.cpp:1656
ServiceInterfaces.h
yarp::dev::OVRHeadset::getAxis
bool getAxis(unsigned int axis_id, double &value) override
Get the value of an axis if present, return false otherwise.
Definition: OVRHeadset.cpp:1616
yarp::dev::OVRHeadset::startService
virtual bool startService()
Initiate the service, whatever it is.
Definition: OVRHeadset.cpp:835
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::dev::OVRHeadset::~OVRHeadset
virtual ~OVRHeadset()
Definition: OVRHeadset.cpp:269
yarp::dev::OVRHeadset::getAxisCount
bool getAxisCount(unsigned int &axis_count) override
Get number of Axes.
Definition: OVRHeadset.cpp:1534
InputCallback
Definition: InputCallback.h:31
yarp::dev::OVRHeadset::getHat
bool getHat(unsigned int hat_id, unsigned char &value) override
Get the value of an Hat.
Definition: OVRHeadset.cpp:1600
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
yarp::dev::OVRHeadset::stopService
virtual bool stopService()
Shut down the service, whatever it is.
Definition: OVRHeadset.cpp:876
ImageType.h
yarp::dev::OVRHeadset::getButton
bool getButton(unsigned int button_id, float &value) override
Get the value of a button.
Definition: OVRHeadset.cpp:1582
yarp::dev::IFrameTransform
Transform Interface.
Definition: IFrameTransform.h:33
yarp::sig::VectorOf< double >
yarp::os::BufferedPort< yarp::sig::FlexImage >
yarp::dev::OVRHeadset::getButtonCount
bool getButtonCount(unsigned int &button_count) override
Get number of Buttons.
Definition: OVRHeadset.cpp:1541
PolyDriver.h
yarp::dev::OVRHeadset::open
virtual bool open(yarp::os::Searchable &cfg)
Open the DeviceDriver.
Definition: OVRHeadset.cpp:375
yarp::dev::OVRHeadset::getTrackballCount
bool getTrackballCount(unsigned int &Trackball_count) override
Get number of trackballs.
Definition: OVRHeadset.cpp:1548
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
yarp::dev::OVRHeadset::updateService
virtual bool updateService()
Give the service the chance to run for a while.
Definition: OVRHeadset.cpp:841
yarp::dev::IService
Common interface for devices that act like services (by which we mean they do something for remote us...
Definition: ServiceInterfaces.h:28
yarp::dev::OVRHeadset::getStickDoF
bool getStickDoF(unsigned int stick_id, unsigned int &DoF) override
Get the Degree Of Freedom count for desired stick.
Definition: OVRHeadset.cpp:1576
yarp::dev::OVRHeadset::getStick
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.
Definition: OVRHeadset.cpp:1629
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
yarp::dev::OVRHeadset::getTouchSurfaceCount
bool getTouchSurfaceCount(unsigned int &touch_count) override
get the number of touch surface.
Definition: OVRHeadset.cpp:1562
PeriodicThread.h
yarp::dev::OVRHeadset::getTrackball
bool getTrackball(unsigned int trackball_id, yarp::sig::Vector &value) override
Get the axes change of a Trackball.
Definition: OVRHeadset.cpp:1595
Image.h
yarp::dev::OVRHeadset::run
virtual void run()
Loop function.
Definition: OVRHeadset.cpp:897
yarp::dev::OVRHeadset::threadRelease
virtual void threadRelease()
Release method.
Definition: OVRHeadset.cpp:737
yarp::dev::OVRHeadset
ovrheadset: Device that manages the Oculus Rift Headset.
Definition: OVRHeadset.h:84
TextureStatic
Definition: TextureStatic.h:27
yarp::dev::OVRHeadset::getHatCount
bool getHatCount(unsigned int &Hat_count) override
Get number of Hats.
Definition: OVRHeadset.cpp:1555
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::dev::OVRHeadset::threadInit
virtual bool threadInit()
Initialization method.
Definition: OVRHeadset.cpp:612
yarp::dev::OVRHeadset::getStickCount
bool getStickCount(unsigned int &stick_count) override
get the number of the sticks
Definition: OVRHeadset.cpp:1569
yarp::dev::OVRHeadset::OVRHeadset
OVRHeadset()
Definition: OVRHeadset.cpp:263
guiParam
Definition: OVRHeadset.cpp:85
IJoypadController.h
yarp::dev::IJoypadController
Definition: IJoypadController.h:35
IFrameTransform.h
yarp::dev::OVRHeadset::close
virtual bool close()
Close the DeviceDriver.
Definition: OVRHeadset.cpp:828
TextureBattery
Definition: TextureBattery.h:34
DeviceDriver.h