34 #include "org_osvr_filter_videoimufusion_json.h"
47 static const auto INTERVAL_BETWEEN_CAMERA_REPORTS = std::chrono::seconds(1);
49 std::string
const &name,
50 std::string
const &imuPath,
51 std::string
const &videoPath,
63 throw std::runtime_error(
"Could not initialize analysis plugin!");
76 m_imu, &VideoIMUFusionDevice::s_handleIMUData,
this);
78 m_imu, &VideoIMUFusionDevice::s_handleIMUVelocity,
this);
83 m_videoTracker, &VideoIMUFusionDevice::s_handleVideoTrackerData,
this);
99 OSVR_ReturnCode VideoIMUFusionDevice::update() {
100 if (m_shouldReportCamera()) {
101 m_nextCameraReport = our_clock::now() + INTERVAL_BETWEEN_CAMERA_REPORTS;
110 bool VideoIMUFusionDevice::m_shouldReportCamera()
const {
111 if (m_fusion.running()) {
112 if (!m_reportedCamera || m_nextCameraReport < our_clock::now()) {
119 void VideoIMUFusionDevice::s_handleIMUData(
123 ->handleIMUData(*timestamp, *report);
125 void VideoIMUFusionDevice::s_handleIMUVelocity(
129 ->handleIMUVelocity(*timestamp, *report);
131 void VideoIMUFusionDevice::s_handleVideoTrackerData(
135 ->handleVideoTrackerData(*timestamp, *report);
138 void VideoIMUFusionDevice::handleIMUData(
const OSVR_TimeValue ×tamp,
141 sendMainPoseReport();
143 void VideoIMUFusionDevice::handleIMUVelocity(
147 Eigen::Quaterniond q = map(report.
state.incrementalRotation);
149 if (q.w() >= 1. || q.vec().isZero(1e-10)) {
150 rot = Eigen::Vector3d::Zero();
153 auto magnitude = q.vec().blueNorm();
154 rot = (q.vec() / magnitude * (2. * std::atan2(magnitude, q.w()))) /
157 auto angle = std::acos(q.w());
158 rot = q.vec().normalized() * angle * 2 / report.
state.dt;
162 m_fusion.handleIMUVelocity(timestamp, rot);
163 if (m_fusion.running()) {
164 sendMainPoseReport();
166 sendVelocityReport();
169 void VideoIMUFusionDevice::handleVideoTrackerData(
171 if (!m_fusion.running()) {
178 std::cout <<
"\n\nWarning: Have received several video tracker "
179 "reports without receiving one from the IMU, "
180 "which shouldn't happen. Please try "
181 "disconnecting/reconnecting and restarting the "
182 "server, and if this re-occurs, double-check your "
183 "configuration files.\n"
194 sendMainPoseReport();
197 TRANSFORMED_VIDEO_SENSOR_ID, ×tamp);
200 void VideoIMUFusionDevice::sendMainPoseReport() {
202 m_dev, m_trackerOut, &m_fusion.
getLatestPose(), FUSED_SENSOR_ID,
206 void VideoIMUFusionDevice::sendVelocityReport() {
208 m_dev, m_trackerOut, &m_fusion.getLatestVelocity(), FUSED_SENSOR_ID,
209 &m_fusion.getLatestVelocityTime());
OSVR_PoseState const & getLatestCameraPose() const
OSVR_ReturnCode osvrRegisterAngularVelocityCallback(OSVR_ClientInterface iface, OSVR_AngularVelocityCallback cb, void *userdata)
Register a callback for AngularVelocity reports on an interface.
uint32_t OSVR_ChannelCount
The integer type specifying a number of channels/sensors or a channel/sensor index.
Wrapper class for OSVR_DeviceToken.
OSVR_ReturnCode osvrClientGetInterface(OSVR_ClientContext ctx, const char path[], OSVR_ClientInterface *iface)
Get the interface associated with the given path.
OSVR_ReturnCode osvrClientFreeInterface(OSVR_ClientContext ctx, OSVR_ClientInterface iface)
Free an interface object before context closure.
void handleVideoTrackerDataWhileRunning(const OSVR_TimeValue ×tamp, const OSVR_PoseReport &report)
OSVR_ReturnCode osvrRegisterOrientationCallback(OSVR_ClientInterface iface, OSVR_OrientationCallback cb, void *userdata)
Register a callback for Orientation reports on an interface.
A DeviceToken connects the generic device interaction code in PluginKit's C API with the workings of ...
OSVR_AngularVelocityState state
The state itself.
OSVR_ReturnCode osvrRegisterPoseCallback(OSVR_ClientInterface iface, OSVR_PoseCallback cb, void *userdata)
Register a callback for Pose reports on an interface.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW void handleIMUData(const OSVR_TimeValue ×tamp, const OSVR_OrientationReport &report)
A structure defining a quaternion, often a unit quaternion representing 3D rotation.
OSVR_ReturnCode osvrDeviceTrackerSendPose(OSVR_DeviceToken dev, OSVR_TrackerDeviceInterface iface, OSVR_PoseState const *val, OSVR_ChannelCount sensor)
Report the full rigid body pose of a sensor, automatically generating a timestamp.
OSVR_PoseState const & getLatestReorientedVideoPose() const
Structure used internally to construct the desired type of device.
VideoIMUFusionDevice(OSVR_PluginRegContext ctx, std::string const &name, std::string const &imuPath, std::string const &videoPath, VideoIMUFusionParams const ¶ms=VideoIMUFusionParams())
#define OSVR_RETURN_FAILURE
The "failure" value for an OSVR_ReturnCode.
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
OSVR_DeviceInitOptions osvrDeviceCreateInitOptions(OSVR_PluginRegContext ctx)
Create a OSVR_DeviceInitOptions object.
void registerUpdateCallback(DeviceObjectType *object)
Given a pointer to your object that has a public OSVR_ReturnCode update() method, registers that inst...
void handleVideoTrackerDataDuringStartup(const OSVR_TimeValue ×tamp, const OSVR_PoseReport &report, const OSVR_OrientationState &orientation)
Report type for an orientation callback on a tracker interface.
OSVR_PoseState const & getLatestPose() const
Returns the latest (fusion result, if available) pose.
Internal, configured header file for verbosity macros.
Header for interoperation between the Eigen math library, the internal mini math library, and VRPN's quatlib.
OSVR_ReturnCode osvrDeviceTrackerSendPoseTimestamped(OSVR_DeviceToken dev, OSVR_TrackerDeviceInterface iface, OSVR_PoseState const *val, OSVR_ChannelCount sensor, OSVR_TimeValue const *timestamp)
Report the full rigid body pose of a sensor, using the supplied timestamp.
OSVR_ReturnCode osvrDeviceTrackerConfigure(OSVR_DeviceInitOptions opts, OSVR_TrackerDeviceInterface *iface)
Specify that your device will implement the Tracker interface.
OSVR_ReturnCode osvrGetOrientationState(OSVR_ClientInterface iface, struct OSVR_TimeValue *timestamp, OSVR_OrientationState *state)
Get Orientation state from an interface, returning failure if none \ * exists.
OSVR_EXTERN_C_BEGIN typedef void * OSVR_PluginRegContext
A context pointer passed in to your plugin's entry point and other locations of control flow transfer...
Report type for an angular velocity callback on a tracker interface.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
void sendJsonDescriptor(const char *json, size_t len)
Submit a JSON self-descriptor string for the device.
OSVR_ANALYSISPLUGINKIT_EXPORT OSVR_ReturnCode osvrAnalysisSyncInit(OSVR_PluginRegContext ctx, const char *name, OSVR_DeviceInitOptions options, OSVR_DeviceToken *device, OSVR_ClientContext *clientCtx)
Initialize a synchronous analysis device token with the options specified, also returning the associa...
Report type for a pose (position and orientation) callback on a tracker interface.
osvr::util::time::TimeValue const & getLatestTime() const
OSVR_ReturnCode osvrDeviceTrackerSendVelocityTimestamped(OSVR_DeviceToken dev, OSVR_TrackerDeviceInterface iface, OSVR_VelocityState const *val, OSVR_ChannelCount sensor, OSVR_TimeValue const *timestamp)
Report the linear and angular velocity of a sensor, using the supplied timestamp. ...