Avionics
Dropship Simulator
Spatial.cpp
Go to the documentation of this file.
1 #include "Spatial.h"
2 
3 namespace Devices
4 {
5  int CCONV Spatial::AttachHandler(CPhidgetHandle IFK, void* usrptr)
6  {
7  Spatial* spatial = static_cast<Spatial*>(usrptr);
8 
9  int serialNo;
10  const char* name;
11 
12  CPhidget_getDeviceName(IFK, &name);
13  CPhidget_getSerialNumber(IFK, &serialNo);
14 
15  if (spatial->config->serialno == -1)
16  spatial->config->serialno = serialNo;
17 
18  char msg[99];
19  sprintf_s(msg, 99, "Spatial::AttachHandler %s (%i) attached!", name, serialNo);
20  spatial->logger->Log(msg);
21  return 0;
22  }
23 
24  int CCONV Spatial::DetachHandler(CPhidgetHandle IFK, void* usrptr)
25  {
26  Spatial* spatial = static_cast<Spatial*>(usrptr);
27 
28  int serialNo;
29  const char* name;
30 
31  CPhidget_getDeviceName(IFK, &name);
32  CPhidget_getSerialNumber(IFK, &serialNo);
33 
34  char msg[99];
35  sprintf_s(msg, 99, "Spatial::DetachHandler %s (%i) detached!", name, serialNo);
36  spatial->logger->Log(msg, Logger::Error);
37 
38  return 0;
39  }
40 
41  int CCONV Spatial::ErrorHandler(CPhidgetHandle IFK, void* usrptr, int ErrorCode, const char* unknown)
42  {
43  Spatial* spatial = static_cast<Spatial*>(usrptr);
44  char msg[99];
45  sprintf_s(msg, 99, "Spatial::ErrorHandler Error handled. %s", unknown);
46  spatial->logger->Log(msg, Logger::Error, ErrorCode);
47  return 0;
48  }
49 
53  int CCONV Spatial::SpatialDataHandler(CPhidgetSpatialHandle IFK, void* usrptr, CPhidgetSpatial_SpatialEventDataHandle* data, int count)
54  {
55  Spatial* spatial = static_cast<Spatial*>(usrptr);
56 
57  for (int i = 0; i < count; i++)
58  {
59  spatial->cumulativeAcceleration += D3DXVECTOR3(
60  static_cast<float>(data[i]->acceleration[spatial->xyzMapping[0]]) + spatial->config->accelerationAxes[spatial->xyzMapping[0]].bias,
61  static_cast<float>(data[i]->acceleration[spatial->xyzMapping[1]]) + spatial->config->accelerationAxes[spatial->xyzMapping[1]].bias,
62  static_cast<float>(data[i]->acceleration[spatial->xyzMapping[2]]) + spatial->config->accelerationAxes[spatial->xyzMapping[2]].bias);
63  spatial->cumulativeAccelSamples++;
64  }
65 
66  return 0;
67  }
68 
69  //Display the properties of the attached phidget to the screen. We will be displaying the name, serial number and version of the attached device.
70  //Will also display the number of inputs, outputs, and analog inputs on the interface kit as well as the state of the ratiometric flag
71  //and the current analog sensor sensitivity.
72  int Spatial::display_properties(CPhidgetHandle phid) const
73  {
74  int serialNo, version;
75  const char* ptr;
76  int numAccelAxes, numGyroAxes, numCompassAxes, dataRateMax, dataRateMin;
77 
78  CPhidget_getDeviceType(phid, &ptr);
79  CPhidget_getSerialNumber(phid, &serialNo);
80  CPhidget_getDeviceVersion(phid, &version);
81  CPhidgetSpatial_getAccelerationAxisCount(reinterpret_cast<CPhidgetSpatialHandle>(phid), &numAccelAxes);
82  CPhidgetSpatial_getGyroAxisCount(reinterpret_cast<CPhidgetSpatialHandle>(phid), &numGyroAxes);
83  CPhidgetSpatial_getCompassAxisCount(reinterpret_cast<CPhidgetSpatialHandle>(phid), &numCompassAxes);
84  CPhidgetSpatial_getDataRateMax(reinterpret_cast<CPhidgetSpatialHandle>(phid), &dataRateMax);
85  CPhidgetSpatial_getDataRateMin(reinterpret_cast<CPhidgetSpatialHandle>(phid), &dataRateMin);
86 
87  char msg[99];
88  sprintf_s(msg, 99, "Spatial::display_properties Type: %s", ptr);
89  logger->Log(msg);
90  sprintf_s(msg, 99, "Spatial::display_properties Serial Number: %i", serialNo);
91  logger->Log(msg);
92  sprintf_s(msg, 99, "Spatial::display_properties Version: %i", version);
93  logger->Log(msg);
94  sprintf_s(msg, 99, "Spatial::display_properties Number of Accel Axes: %i", numAccelAxes);
95  logger->Log(msg);
96  sprintf_s(msg, 99, "Spatial::display_properties Number of Gyro Axes: %i", numGyroAxes);
97  logger->Log(msg);
98  sprintf_s(msg, 99, "Spatial::display_properties Number of Compass Axes: %i", numCompassAxes);
99  logger->Log(msg);
100  sprintf_s(msg, 99, "Spatial::display_properties Maximum Datarate: %d", dataRateMax);
101  logger->Log(msg);
102  sprintf_s(msg, 99, "Spatial::display_properties Minimum Datarate: %d", dataRateMin);
103  logger->Log(msg);
104 
105  return 0;
106  }
107 
108  void Spatial::Initialize(Logger* prmLogger, SpatialConfig* prmConfig, Bus* prmBus)
109  {
110  logger = prmLogger;
111  config = prmConfig;
112  bus = prmBus;
113 
114  int result;
115  const char* err;
116 
117  // Declare an spatial handle
118  spatial = nullptr;
119  cumulativeAcceleration = D3DXVECTOR3(0, 0, 0);
120  cumulativeAccelTime = 0.0f;
122  bus->PlatformAcceleration = D3DXVECTOR3(logf(-1), logf(-1), logf(-1));
123 
124  for (UINT i = 0; i < config->accelerationAxes.size(); i++)
125  {
126  if (config->accelerationAxes[i].axis == 'X') xyzMapping[0] = i;
127  if (config->accelerationAxes[i].axis == 'Y') xyzMapping[1] = i;
128  if (config->accelerationAxes[i].axis == 'Z') xyzMapping[2] = i;
129  }
130 
132  CPhidgetSpatial_create(&spatial);
133 
134  //Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
135  CPhidget_set_OnAttach_Handler(reinterpret_cast<CPhidgetHandle>(spatial), AttachHandler, this);
136  CPhidget_set_OnDetach_Handler(reinterpret_cast<CPhidgetHandle>(spatial), DetachHandler, this);
137  CPhidget_set_OnError_Handler(reinterpret_cast<CPhidgetHandle>(spatial), ErrorHandler, this);
138 
139  //Registers a callback that will run according to the set data rate that will return the spatial data changes
140  //Requires the handle for the Spatial, the callback handler function that will be called,
141  //and an arbitrary pointer that will be supplied to the callback function (may be NULL)
142  CPhidgetSpatial_set_OnSpatialData_Handler(spatial, SpatialDataHandler, this);
143 
144  //open the interfacekit for device connections
145  CPhidget_open(reinterpret_cast<CPhidgetHandle>(spatial), -1);
146 
147  //get the program to wait for an interface kit device to be attached
148  logger->Log("Spatial::ctor Waiting for spatial to be attached....");
149  result = CPhidget_waitForAttachment(reinterpret_cast<CPhidgetHandle>(spatial), 5000);
150  if (result)
151  {
152  CPhidget_getErrorDescription(result, &err);
153  char msg[99];
154  sprintf_s(msg, 99, "Spatial::ctor Problem waiting for attachment: %s", err);
155  logger->Log(msg, Logger::Fatal);
156  config->enable = false;
157  return;
158  }
159 
160  result = CPhidgetSpatial_setDataRate(spatial, config->sampleRateMs);
161  if (result)
162  {
163  CPhidget_getErrorDescription(result, &err);
164  char msg[99];
165  sprintf_s(msg, 99, "Spatial::ctor Problem %s setting sample rate: %i", err, config->sampleRateMs);
166  logger->Log(msg, Logger::Warn);
167  }
168 
169  // Log the properties of the attached spatial device
170  display_properties(reinterpret_cast<CPhidgetHandle>(spatial));
171  }
172 
173  void Spatial::Destroy() const
174  {
175  logger->Log("Spatial::Destroy...");
176  CPhidget_close(reinterpret_cast<CPhidgetHandle>(spatial));
177  CPhidget_delete(reinterpret_cast<CPhidgetHandle>(spatial));
178  }
179 
180  void Spatial::FrameMove(float fElapsed)
181  {
182  cumulativeAccelTime += fElapsed;
183  if (cumulativeAccelTime > 1.0f)
184  {
185  if (cumulativeAccelSamples > 0)
186  {
188  cumulativeAcceleration = D3DXVECTOR3(0, 0, 0);
190  }
191  else
192  {
194  bus->PlatformAcceleration = D3DXVECTOR3(-999, -999, -999);
195  }
196  cumulativeAccelTime = 0.0f;
197  }
198  }
199 }
int display_properties(CPhidgetHandle phid) const
Definition: Spatial.cpp:72
static int CCONV DetachHandler(CPhidgetHandle IFK, void *userptr)
Definition: Spatial.cpp:24
Definition: Logger.h:5
static int CCONV AttachHandler(CPhidgetHandle IFK, void *userptr)
Definition: Spatial.cpp:5
void FrameMove(float fElapsed)
Definition: Spatial.cpp:180
okay, the portable keyboard numbers don&#39;t work like the outside keypad because the outside keypad is ...
Definition: Analog.cpp:3
void Initialize(Logger *logger, SpatialConfig *config, Bus *prmBus)
Definition: Spatial.cpp:108
static int CCONV SpatialDataHandler(CPhidgetSpatialHandle spatial, void *userptr, CPhidgetSpatial_SpatialEventDataHandle *data, int count)
Definition: Spatial.cpp:53
float cumulativeAccelTime
Definition: Spatial.h:34
Definition: Bus.h:12
CPhidgetSpatialHandle spatial
Definition: Spatial.h:30
void Destroy() const
Definition: Spatial.cpp:173
int xyzMapping[3]
Definition: Spatial.h:32
int cumulativeAccelSamples
Definition: Spatial.h:35
SpatialConfig * config
Definition: Spatial.h:27
void Log(const char *msg, Level level=Info, int errorCode=0)
These have to be in this order.
Definition: Logger.cpp:16
D3DXVECTOR3 PlatformAcceleration
Definition: Bus.h:29
std::vector< AccelerationAxis > accelerationAxes
Definition: Spatial.h:20
Logger * logger
Definition: Spatial.h:26
static int CCONV ErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
Definition: Spatial.cpp:41
D3DXVECTOR3 cumulativeAcceleration
Definition: Spatial.h:33