OSVR-Core
TrackerThread.h
Go to the documentation of this file.
1 
11 // Copyright 2016 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_TrackerThread_h_GUID_6544B03C_4EB4_4B82_77F1_16EF83578C64
26 #define INCLUDED_TrackerThread_h_GUID_6544B03C_4EB4_4B82_77F1_16EF83578C64
27 
28 // Internal Includes
29 #include "CameraParameters.h"
30 #include "IMUMessage.h"
32 #include "TrackingSystem.h"
33 
35 
36 // Library/third-party includes
38 
39 #include <opencv2/core/core.hpp> // for basic OpenCV types
40 
41 #include <boost/noncopyable.hpp>
42 #include <folly/ProducerConsumerQueue.h>
43 #include <folly/sorted_vector_types.h>
44 #include <osvr/TypePack/List.h>
45 
46 // Standard includes
47 #include <array>
48 #include <chrono>
49 #include <condition_variable>
50 #include <cstdint>
51 #include <future>
52 #include <iosfwd>
53 #include <mutex>
54 #include <queue>
55 #include <thread>
56 
57 namespace osvr {
58 namespace vbtracker {
59  static const std::chrono::milliseconds IMU_OVERRIDE_SPACING{2};
60 
61  static const auto MAX_DEBUG_BEACONS = 34;
62  static const auto DEBUG_ANALOGS_PER_BEACON = 6;
63  static const auto DEBUG_ANALOGS_REQUIRED =
64  MAX_DEBUG_BEACONS * DEBUG_ANALOGS_PER_BEACON;
65 
66  using DebugArray = std::array<double, DEBUG_ANALOGS_REQUIRED>;
67  struct BodyIdOrdering {
68  bool operator()(BodyId const &lhs, BodyId const &rhs) const {
69  return lhs.value() < rhs.value();
70  }
71  };
72 
73  using UpdatedBodyIndices = folly::sorted_vector_set<BodyId, BodyIdOrdering>;
74 
76 
77  class TrackerThread : boost::noncopyable {
78  public:
79  TrackerThread(TrackingSystem &trackingSystem, ImageSource &imageSource,
80  BodyReportingVector &reportingVec,
81  CameraParameters const &camParams,
82  std::int32_t cameraUsecOffset = 0, bool bufferImu = false,
83  bool debugData = false);
84  ~TrackerThread();
85 
88  void threadAction();
89 
94  void permitStart();
95 
98  void triggerStop();
99 
102  bool submitIMUReport(TrackedBodyIMU &imu,
103  util::time::TimeValue const &tv,
104  OSVR_OrientationReport const &report);
107  bool submitIMUReport(TrackedBodyIMU &imu,
108  util::time::TimeValue const &tv,
109  OSVR_AngularVelocityReport const &report);
110 
111  bool checkForDebugData(DebugArray &data);
113 
116  void signalImageProcessingComplete(ImageOutputDataPtr &&imageData,
117  cv::Mat const &frame,
118  cv::Mat const &frameGray);
119 
120  private:
122  std::ostream &msg() const;
124  std::ostream &warn() const;
125 
128  void doFrame();
129 
132  void setupReportingVectorProcessModels();
133 
135  bool setupReportingVectorRoomTransforms();
136 
138  void updateReportingVector(UpdatedBodyIndices const &bodyIds);
139 
143  void updateReportingVector(BodyId const bodyId);
144 
147  void launchTimeConsumingImageStep();
148 
149  std::pair<BodyId, ImuMessageCategory>
150  processIMUMessage(IMUMessage const &m);
151 
153  BodyReporting *getCamPoseReporting() const;
155  BodyReporting *getIMUReporting() const;
157  BodyReporting *getIMUCamReporting() const;
159  BodyReporting *getHMDCamReporting() const;
160 
161  void updateExtraCameraReport();
162  void updateExtraIMUReports();
163 
164  TrackingSystem &m_trackingSystem;
165  ImageSource &m_cam;
166  BodyReportingVector &m_reportingVec;
167  CameraParameters m_camParams;
168  std::size_t m_numBodies = 0; //< initialized when loop started.
169  const std::int32_t m_cameraUsecOffset = 0;
170 
174  const bool m_bufferImu = false;
175 
176  const bool m_debugData = false;
177 
178  using our_clock = std::chrono::steady_clock;
179 
180  bool shouldSendImuReport() {
181  auto now = our_clock::now();
182  if (now > m_nextImuOverrideReport) {
183  m_nextImuOverrideReport = now + IMU_OVERRIDE_SPACING;
184  return true;
185  }
186  return false;
187  }
188 
189  void setImuOverrideClock() {
190  m_nextImuOverrideReport = our_clock::now() + IMU_OVERRIDE_SPACING;
191  }
192 
193  our_clock::time_point m_nextImuOverrideReport;
194  boost::optional<our_clock::time_point> m_nextCameraPoseReport;
195 
198  std::promise<void> m_startupSignal;
199 
200  bool m_setCameraPose = false;
201 
204  cv::Mat m_frame;
205  cv::Mat m_frameGray;
206  ImageOutputDataPtr m_imageData;
208 
211  std::mutex m_runMutex;
212  bool m_run = true;
214 
218  std::condition_variable m_messageCondVar;
219  std::mutex m_messageMutex;
220  bool m_timeConsumingImageStepComplete = false;
221  folly::ProducerConsumerQueue<IMUMessage> m_imuMessages;
223 
224  folly::ProducerConsumerQueue<DebugArray> m_debugDataMessages;
225 
226  ImageProcessingThread *imageProcThreadObj_ = nullptr;
227 
229  std::thread m_imageThread;
230  };
231 } // namespace vbtracker
232 } // namespace osvr
233 #endif // INCLUDED_TrackerThread_h_GUID_6544B03C_4EB4_4B82_77F1_16EF83578C64
Definition: ImageProcessingThread.h:47
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: TrackerThread.h:77
Definition: CameraParameters.h:41
Definition: TrackingSystem.h:54
Header.
Report type for an orientation callback on a tracker interface.
Definition: ClientReportTypesC.h:145
Definition: TrackedBodyIMU.h:44
A per-body class intended to marshall data coming from the tracking/processing thread back to the mai...
Definition: ThreadsafeBodyReporting.h:65
Uniform interface for the various normal to strange image sources for the tracking algorithm...
Definition: ImageSource.h:42
boost::variant< boost::none_t, TimestampedOrientation, TimestampedAngVel > IMUMessage
A "typesafe tagged-union" variant that can hold an IMU report data along with the timestamp and the p...
Definition: IMUMessage.h:93
Report type for an angular velocity callback on a tracker interface.
Definition: ClientReportTypesC.h:187
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
Header.