OSVR-Core
SetupSensors.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_SetupSensors_h_GUID_7F0F52BD_57B4_4ABB_0834_C61DB7C22132
26 #define INCLUDED_SetupSensors_h_GUID_7F0F52BD_57B4_4ABB_0834_C61DB7C22132
27 
28 // Internal Includes
30 #include "Types.h"
31 #include "VideoBasedTracker.h"
32 
33 #include <CameraParameters.h>
34 #include <HDKData.h>
35 #include <LoadCalibration.h>
36 
37 // Library/third-party includes
38 #include <json/reader.h>
39 #include <json/value.h>
40 
41 // Standard includes
42 #include <fstream>
43 #include <iostream>
44 #include <vector>
45 
46 namespace osvr {
47 namespace vbtracker {
48 
49  static bool frontPanelFixedBeaconShared(int id) {
50  return (id == 16) || (id == 17) || (id == 19) || (id == 20);
51  }
52 
53  static bool backPanelFixedBeaconShared(int) { return true; }
54 
55  namespace messages {
56  inline void loadedCalibFileSuccessfully(ConfigParams const &config) {
57  loadedCalibFileSuccessfully(config.calibrationFile);
58  }
59 
60  inline void calibFileSpecifiedButNotLoaded(ConfigParams const &config) {
61  calibFileSpecifiedButNotLoaded(config.calibrationFile);
62  }
63  } // namespace messages
64 
65  using BeaconPredicate = std::function<bool(int)>;
66 
68  inline float computeDistanceBetweenPanels(ConfigParams const &config) {
69  return static_cast<float>(computeDistanceBetweenPanels(
71  }
72  inline void addRearPanelBeaconLocations(float distanceBetweenPanels,
73  Point3Vector &locations) {
74  // For the back panel beacons: have to rotate 180 degrees
75  // about Y, which is the same as flipping sign on X and Z
76  // then we must translate along Z by head diameter +
77  // distance from head to front beacon origins
78  for (auto &pt : OsvrHdkLedLocations_SENSOR1) {
79  locations.emplace_back(-pt.x, pt.y, -pt.z - distanceBetweenPanels);
80  }
81  }
82 
83  inline void addRearPanelBeaconLocations(ConfigParams const &config,
84  Point3Vector &locations) {
85  addRearPanelBeaconLocations(computeDistanceBetweenPanels(config),
86  locations);
87  }
88 
90  VideoBasedTracker &vbtracker, ConfigParams const &config,
91  bool attemptToLoadCalib = true,
92  BeaconPredicate &&beaconFixedPredicate = &frontPanelFixedBeaconShared) {
93 
94  Point3Vector locations = OsvrHdkLedLocations_SENSOR0;
95  Vec3Vector directions = OsvrHdkLedDirections_SENSOR0;
96  std::vector<double> variances = OsvrHdkLedVariances_SENSOR0;
97 
98  // Have to rotate and translate the rear beacons
99  addRearPanelBeaconLocations(config, locations);
100  // Add variance for each location
101  variances.insert(end(variances), OsvrHdkLedLocations_SENSOR1.size(),
103  // Similarly, rotate the directions.
104  for (auto &vec : OsvrHdkLedDirections_SENSOR1) {
105  directions.emplace_back(-vec[0], vec[1], -vec[2]);
106  }
107  double autocalibScale = 1;
108  if (attemptToLoadCalib) {
109  auto calibLocations =
110  tryLoadingArrayOfPointsFromFile(config.calibrationFile);
111  bool gotCalib = false;
112  if (calibLocations.size() == locations.size()) {
114  gotCalib = true;
115  } else if (calibLocations.size() == getNumHDKFrontPanelBeacons()) {
116  // just need to add the rear beacons to the calibration file.
117  // This is actually ideal, since we can use the
118  // currently-configured head size, instead of calibration-time
119  // head size, to estimate the rear panel location.
120  addRearPanelBeaconLocations(config, calibLocations);
121  gotCalib = true;
122  }
123 
124  if (gotCalib) {
125  messages::loadedCalibFileSuccessfully(config);
126  locations = calibLocations;
127  autocalibScale = BEACON_AUTOCALIB_ERROR_SCALE_IF_CALIBRATED;
128 
129  } else if (!config.calibrationFile.empty()) {
130  messages::calibFileSpecifiedButNotLoaded(config);
131  }
132  }
133 
134  auto camParams = getHDKCameraParameters();
135  vbtracker.addSensor(createHDKUnifiedLedIdentifier(), camParams,
136  locations, directions, variances,
137  beaconFixedPredicate, 4, 0, autocalibScale);
138  }
139 
140  inline void
141  setupSensorsWithoutRearPanel(VideoBasedTracker &vbtracker,
142  ConfigParams const &config,
143  bool attemptToLoadCalib = true,
144  BeaconPredicate &&frontBeaconFixedPredicate =
145  &frontPanelFixedBeaconShared,
146  BeaconPredicate &&backBeaconFixedPredicate =
147  &backPanelFixedBeaconShared) {
148 
149  auto camParams = getHDKCameraParameters();
150 
151  bool needFrontSensor = true;
152  if (attemptToLoadCalib) {
153  auto calibLocations =
154  tryLoadingArrayOfPointsFromFile(config.calibrationFile);
155  if (calibLocations.size() == getNumHDKFrontPanelBeacons() ||
156  calibLocations.size() ==
157  getNumHDKFrontPanelBeacons() +
158  getNumHDKRearPanelBeacons()) {
159  // Remove any extra (rear panel) "calibrated" locations
160  calibLocations.resize(getNumHDKFrontPanelBeacons());
161  messages::loadedCalibFileSuccessfully(config);
162  needFrontSensor = false;
163  vbtracker.addSensor(
164  createHDKLedIdentifier(0), camParams, calibLocations,
165  OsvrHdkLedDirections_SENSOR0, OsvrHdkLedVariances_SENSOR0,
166  frontBeaconFixedPredicate, 6, 0,
167  BEACON_AUTOCALIB_ERROR_SCALE_IF_CALIBRATED);
168  } else if (!config.calibrationFile.empty()) {
169  messages::calibFileSpecifiedButNotLoaded(config);
170  }
171  }
172  if (needFrontSensor) {
173  vbtracker.addSensor(
174  createHDKLedIdentifier(0), camParams,
175  OsvrHdkLedLocations_SENSOR0, OsvrHdkLedDirections_SENSOR0,
176  OsvrHdkLedVariances_SENSOR0, frontBeaconFixedPredicate, 6, 0);
177  }
178  vbtracker.addSensor(
179  createHDKLedIdentifier(1), camParams, OsvrHdkLedLocations_SENSOR1,
180  OsvrHdkLedDirections_SENSOR1, backBeaconFixedPredicate, 4, 0);
181  }
182 } // namespace vbtracker
183 } // namespace osvr
184 
185 #endif // INCLUDED_SetupSensors_h_GUID_7F0F52BD_57B4_4ABB_0834_C61DB7C22132
const std::vector< double > OsvrHdkLedVariances_SENSOR0
generated by python script in this directory.
Definition: HDKData.cpp:182
Definition: CommonComponent.h:44
std::string calibrationFile
If non-empty, the file to load (or save to) for calibration data.
Definition: ConfigParams.h:251
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
double backPanelMeasurementError
This used to be different than the other beacons, but now it&#39;s mostly the same.
Definition: ConfigParams.h:198
CameraParameters getHDKCameraParameters()
Definition: CameraParameters.h:126
void setupSensorsIncludeRearPanel(VideoBasedTracker &vbtracker, ConfigParams const &config, bool attemptToLoadCalib=true, BeaconPredicate &&beaconFixedPredicate=&frontPanelFixedBeaconShared)
Definition: SetupSensors.h:89
Header.
Definition: VideoBasedTracker.h:53
Header.
LedIdentifierPtr createHDKLedIdentifier(uint8_t sensor)
Factory function to create an HDK Led Identifier object.
Definition: HDKLedIdentifierFactory.cpp:157
LedIdentifierPtr createHDKUnifiedLedIdentifier()
Factory function to create an HDK Led Identifier object containing all the beacons in order for the f...
Definition: HDKLedIdentifierFactory.cpp:172
General configuration parameters.
Definition: ConfigParams.h:82
float computeDistanceBetweenPanels(ConfigParams const &config)
distance between front and back panel target origins, in mm.
Definition: SetupSensors.h:68
double headCircumference
Head circumference at the head strap, in cm - 55.75 is our estimate for an average based on some hat ...
Definition: ConfigParams.h:188
double headToFrontBeaconOriginDistance
This is the distance fron the front of the head to the origin of the front sensor coordinate system i...
Definition: ConfigParams.h:194