Avionics
Dropship Simulator
Encoder.cpp
Go to the documentation of this file.
1 /*
2 2015-03-22 10:57:14.151 Warning: Encoder::PositionChangeHandler Received encoder data for undefined index: 0 time: 216 rpos: 1 pos: 229
3 2015-03-22 10:57:14.166 Warning: Encoder::PositionChangeHandler Received encoder data for undefined index: 0 time: 19 rpos: 1 pos: 230
4 2015-03-22 10:57:14.215 Warning: Encoder::PositionChangeHandler Received encoder data for undefined index: 0 time: 38 rpos: 1 pos: 231
5 2015-03-22 10:57:14.454 Warning: Encoder::PositionChangeHandler Received encoder data for undefined index: 0 time: 236 rpos: 1 pos: 232
6 2015-03-22 10:57:14.519 Warning: Encoder::PositionChangeHandler Received encoder data for undefined index: 0 time: 69 rpos: 1 pos: 233
7 2015-03-22 10:57:14.550 Warning: Encoder::PositionChangeHandler Received encoder data for undefined index: 0 time: 25 rpos: 1 pos: 234
8 2015-03-22 10:57:14.603 Warning: Encoder::PositionChangeHandler Received encoder data for undefined index: 0 time: 51 rpos: 1 pos: 235
9 2015-03-22 10:57:14.615 Warning: Encoder::PositionChangeHandler Received encoder data for undefined index: 0 time: 23 rpos: 1 pos: 236
10 2015-03-22 10:57:15.080 Warning: Encoder::PositionChangeHandler Received encoder data for undefined index: 0 time: 441 rpos: 1 pos: 237
11 2015-03-22 10:57:15.094 Warning: Encoder::PositionChangeHandler Received encoder data for undefined index: 0 time: 29 rpos: 1 pos: 238
12 2015-03-22 10:57:15.190 Warning: InterfaceKit::InputChangeHandler Received digital data for undefined index: 0 (1)
13 2015-03-22 10:57:15.511 Warning: InterfaceKit::InputChangeHandler Received digital data for undefined index: 0 (0)
14 */
15 
16 #include "Encoder.h"
17 
18 namespace Devices
19 {
20  int CCONV Encoder::AttachHandler(CPhidgetHandle IFK, void* usrptr)
21  {
22  Encoder* interfaceKit = static_cast<Encoder*>(usrptr);
23 
24  int serialNo;
25  const char* name;
26 
27  CPhidget_getDeviceName(IFK, &name);
28  CPhidget_getSerialNumber(IFK, &serialNo);
29 
30  if (interfaceKit->config->serialno == -1)
31  interfaceKit->config->serialno = serialNo;
32 
33  char msg[99];
34  if (interfaceKit->wasDetached)
35  {
36  interfaceKit->wasDetached = false;
37  sprintf_s(msg, 99, "Encoder::AttachHandler %s (%s) reattached!", name, interfaceKit->config->friendlyName.c_str());
38  interfaceKit->logger->Log(msg, Logger::Error);
39  }
40  else
41  {
42  sprintf_s(msg, 99, "Encoder::AttachHandler %s (%s) attached!", name, interfaceKit->config->friendlyName.c_str());
43  interfaceKit->logger->Log(msg);
44  }
45 
46  interfaceKit->attached = true;
47 
48  return 0;
49  }
50 
51  int CCONV Encoder::DetachHandler(CPhidgetHandle IFK, void* usrptr)
52  {
53  Encoder* interfaceKit = static_cast<Encoder*>(usrptr);
54 
55  int serialNo;
56  const char* name;
57 
58  CPhidget_getDeviceName(IFK, &name);
59  CPhidget_getSerialNumber(IFK, &serialNo);
60 
61  char msg[99];
62  sprintf_s(msg, 99, "Encoder::DetachHandler %s (%s) detached!", name, interfaceKit->config->friendlyName.c_str());
63  interfaceKit->logger->Log(msg, Logger::Error);
64 
66  //PositionChangeHandler((CPhidgetEncoderHandle)IFK, usrptr, 0, 0, 0);
67  //InputChangeHandler((CPhidgetEncoderHandle)IFK, usrptr, 0, -1);
68 
69  interfaceKit->attached = false;
70  interfaceKit->wasDetached = true;
71 
72  return 0;
73  }
74 
75  int CCONV Encoder::ErrorHandler(CPhidgetHandle IFK, void* usrptr, int ErrorCode, const char* unknown)
76  {
77  Encoder* interfaceKit = static_cast<Encoder*>(usrptr);
78  char msg[99];
79  sprintf_s(msg, 99, "Encoder::ErrorHandler Error handled. %s", unknown);
80  interfaceKit->logger->Log(msg, Logger::Error, ErrorCode);
81  return 0;
82  }
83 
84  //callback that will run if an input changes.
85  //Index - Index of the input that generated the event, State - boolean (0 or 1) representing the input state (on or off), -1 means no signal!
86  int CCONV Encoder::InputChangeHandler(CPhidgetEncoderHandle IFK, void* usrptr, int Index, int State)
87  {
88  Encoder* interfaceKit = static_cast<Encoder*>(usrptr);
89 
90  char msg[99];
91  sprintf_s(msg, 99, "Encoder::InputChangeHandler Received digital data for undefined index: %d (%d)", Index, State);
92  interfaceKit->logger->Log(msg, Logger::Warn);
93 
94  /*
95  EncoderConfig::Input input = interfaceKit->config->inputs.at(Index);
96 
97  if (!input.defined)
98  {
99 // char msg[99];
100  // sprintf_s(msg, 99, "InterfaceKit::InputChangeHandler Received digital data for undefined index: %d (%d)", Index, State);
101  // interfaceKit->logger->Log(msg, Logger::Warn);
102  }
103  else
104  {
105  if (input.name == "Component")
106  {
107  if (!input.ptrFloat)
108  input.ptrFloat = interfaceKit->bus->GetComponentSetStatePtr(input.guid);
109  if (!input.ptrFault)
110  input.ptrFault = interfaceKit->bus->GetComponentFaultStatePtr(input.guid);
111 
113  if (State != 0 && State != 1)
114  {
115  *input.ptrFloat = -999.0f;
116  *input.ptrFault = Systems::Fault::NoSignal;
117  }
118  else
119  {
120  *input.ptrFloat = (float)State;
121  *input.ptrFault = Systems::Fault::None;
122  }
123  }
124  else
125  {
126  char msg[99];
127  sprintf_s(msg, 99, "InterfaceKit::InputChangeHandler %s (%i): %i", input.name.c_str(), Index, State);
128  interfaceKit->logger->Log(msg);
129 
132  if (State == 1)
133  {
134  Command command;
135  command.name = input.name;
136  //command.ivalue = State;
137  interfaceKit->bus->commandStream.push_back(command);
138 
139  sprintf_s(msg, 99, "InterfaceKit::InputChangeHandler: Added command: %s (%i)", command.name.c_str(), State);
140  interfaceKit->logger->Log(msg);
141  }
142  }
143  }
144  */
145 
146  return 0;
147  }
148 
149  //callback that will run if the sensor value changes by more than the OnSensorChange trigger.
150  //Index - Index of the sensor that generated the event, Value - the sensor read value
151  int CCONV Encoder::PositionChangeHandler(CPhidgetEncoderHandle IFK, void* usrptr, int Index, int Time, int RelativePosition)
152  {
153  int Position;
154  CPhidgetEncoder_getPosition(IFK, Index, &Position);
155 
156  Encoder* interfaceKit = static_cast<Encoder*>(usrptr);
157 
158  char msg[199];
159  sprintf_s(msg, 199, "Encoder::PositionChangeHandler Received encoder data for undefined index: %d time: %d rpos: %d pos: %d", Index, Time, RelativePosition, Position);
160  interfaceKit->logger->Log(msg, Logger::Warn);
161 
162  /*
163  InterfaceKitConfig::Sensor sensor = interfaceKit->config->sensors.at(Index);
164 
165  if (!sensor.defined)
166  {
167 // char msg[99];
168  // sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler Received sensor data for undefined index: %d (%d)", Index, Value);
169  // interfaceKit->logger->Log(msg, Logger::Warn);
170  }
171  else
172  {
173  if (!sensor.ptrFloat)
174  sensor.ptrFloat = interfaceKit->bus->GetComponentSetStatePtr(sensor.guid);
175  if (!sensor.ptrFault)
176  sensor.ptrFault = interfaceKit->bus->GetComponentFaultStatePtr(sensor.guid);
177 
178 #pragma region // phidgets precision temperature sensor
179  if (sensor.type == "1124_0")
180  {
181  if (Value == -1)
182  {
183  *sensor.ptrFault = Systems::Fault::NoSignal;
184  *sensor.ptrFloat = -1.0f;
185  char msg[99];
186  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): NO SIGNAL", sensor.guid.c_str(), Index);
187  interfaceKit->logger->Log(msg);
188  }
189  else
190  {
191  float celciusTemp = ((float)Value * 0.2222f) - 61.111f;
192  if (sensor.inverse) celciusTemp = -celciusTemp;
193  celciusTemp += sensor.bias;
194  *sensor.ptrFault = Systems::Fault::None;
195  *sensor.ptrFloat = celciusTemp;
196  float farenheitTemp = celciusTemp * 9.0f / 5.0f + 32.0f;
197  char msg[99];
198  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): %.1f°C %.1f°F", sensor.guid.c_str(), Index, celciusTemp, farenheitTemp);
199  interfaceKit->logger->Log(msg);
200  }
201  }
202 #pragma endregion
203 #pragma region // 20 Amp Current Sensor DC Current (DC Amps)
204  else if (sensor.type == "1119_0" && sensor.subtype == "DC")
205  {
206  if (Value == -1)
207  {
208  *sensor.ptrFault = Systems::Fault::NoSignal;
209  *sensor.ptrFloat = -1.0f;
210  char msg[99];
211  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): NO SIGNAL", sensor.guid.c_str(), Index);
212  interfaceKit->logger->Log(msg);
213  }
214  else
215  {
216  float dcAmps = (float)Value / 20.0f - 25.0f;
217  if (sensor.inverse) dcAmps = -dcAmps;
218  dcAmps += sensor.bias;
219  *sensor.ptrFault = Systems::Fault::None;
220  *sensor.ptrFloat = dcAmps;
221  char msg[99];
222  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): %.1f A (%.1f A)", sensor.guid.c_str(), Index, dcAmps, (float)Value / 20.0f - 25.0f);
223  interfaceKit->logger->Log(msg);
224  }
225  }
226 #pragma endregion
227 #pragma region // 200 PSI pressure sensor Custom0
228  else if (sensor.type == "Custom0")
229  {
230  if (Value == -1)
231  {
232  *sensor.ptrFault = Systems::Fault::NoSignal;
233  *sensor.ptrFloat = -1.0f;
234  char msg[99];
235  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): NO SIGNAL", sensor.guid.c_str(), Index);
236  interfaceKit->logger->Log(msg);
237  }
238  else
239  {
240  // sensor value is always 0-1000 for 0V to 5V
241  // for the sensor I bought on ebay at 0PSI at 0.5V, 200PSI at 4.5V
242  // so 0PSI is 0.5V is 100.0f = 0.0 PSI
243  // so 100 PSI is 2.5V is 500.0f = 100 PSI
244  float dcAmps = ((float)Value - 100.0f) / 800.0f * 200.0f;
245  //if (sensor.inverse) dcAmps = -dcAmps;
246  //dcAmps += sensor.bias;
247  *sensor.ptrFault = Systems::Fault::None;
248  *sensor.ptrFloat = dcAmps;
249  char msg[99];
250  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler %s (%d): %.1f PSI (%.1f)", sensor.guid.c_str(), Index, dcAmps, (float)Value);
251  interfaceKit->logger->Log(msg);
252  }
253  }
254 #pragma endregion
255  else
256  {
257  *sensor.ptrFault = Systems::Fault::NoSignal;
258  *sensor.ptrFloat = -1.0f;
259  char msg[99];
260  sprintf_s(msg, 99, "InterfaceKit::SensorChangeHandler Unknown sensor type \"%s\" at index: %d (%d)", sensor.type, Index, Value);
261  interfaceKit->logger->Log(msg, Logger::Warn);
262  }
263  }
264  */
265  return 0;
266  }
267 
268 
271  int Encoder::display_properties(CPhidgetEncoderHandle phid) const
272  {
273  int serialNo, version, numInputs, numEncoders;
274  const char* ptr;
275 
276  CPhidget_getDeviceType(reinterpret_cast<CPhidgetHandle>(phid), &ptr);
277  CPhidget_getSerialNumber(reinterpret_cast<CPhidgetHandle>(phid), &serialNo);
278  CPhidget_getDeviceVersion(reinterpret_cast<CPhidgetHandle>(phid), &version);
279 
280  CPhidgetEncoder_getInputCount(phid, &numInputs);
281  CPhidgetEncoder_getEncoderCount(phid, &numEncoders);
282 
283  char msg[99];
284  sprintf_s(msg, 99, "Encoder::display_properties Type: %s", ptr);
285  logger->Log(msg);
286  sprintf_s(msg, 99, "Encoder::display_properties Serial Number: %i", serialNo);
287  logger->Log(msg);
288  sprintf_s(msg, 99, "Encoder::display_properties Version: %i", version);
289  logger->Log(msg);
290  sprintf_s(msg, 99, "Encoder::display_properties Digital Inputs: %d", numInputs);
291  logger->Log(msg);
292  sprintf_s(msg, 99, "Encoder::display_properties Encoders: %d", numEncoders);
293  logger->Log(msg);
294 
295  return 0;
296  }
297 
298  void Encoder::Initialize(Logger* prmLogger, EncoderConfig* prmConfig, Bus* prmBus)
299  {
300  logger = prmLogger;
301  config = prmConfig;
302  bus = prmBus;
303 
304  int result;
305  const char* err;
306 
307  //Declare an InterfaceKit handle
308  ifKit = nullptr;
309 
310  //create the InterfaceKit object
311  CPhidgetEncoder_create(&ifKit);
312 
313  //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.
314  CPhidget_set_OnAttach_Handler(reinterpret_cast<CPhidgetHandle>(ifKit), AttachHandler, this);
315  CPhidget_set_OnDetach_Handler(reinterpret_cast<CPhidgetHandle>(ifKit), DetachHandler, this);
316  CPhidget_set_OnError_Handler(reinterpret_cast<CPhidgetHandle>(ifKit), ErrorHandler, this);
317 
318  //Registers a callback that will run if an input changes.
319  //Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
320  CPhidgetEncoder_set_OnInputChange_Handler(ifKit, InputChangeHandler, this);
321 
322  //Registers a callback that will run if the encoder changes.
323  //Requires the handle for the Encoder, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
324  CPhidgetEncoder_set_OnPositionChange_Handler(ifKit, PositionChangeHandler, this);
325 
326  //open the interfacekit for device connections
327  CPhidget_open(reinterpret_cast<CPhidgetHandle>(ifKit), config->serialno);
328 
329  //get the program to wait for an encoder device to be attached
330  char msg[99];
331  sprintf_s(msg, 99, "Encoder::ctor Waiting for encoder %i to be attached...", config->serialno);
332  logger->Log(msg);
333  result = CPhidget_waitForAttachment(reinterpret_cast<CPhidgetHandle>(ifKit), 5000);
334  if (result)
335  {
336  CPhidget_getErrorDescription(result, &err);
337  sprintf_s(msg, 99, "Encoder::ctor Problem waiting for attachment of %i: %s", config->serialno, err);
338  logger->Log(msg, Logger::Fatal);
339  config->enable = false;
340  return;
341  }
342 
343  // Log the properties of the attached encoder device
345  }
346 
347  void Encoder::Destroy() const
348  {
349  logger->Log("Encoder::Destroy...");
350  CPhidget_close(reinterpret_cast<CPhidgetHandle>(ifKit));
351  CPhidget_delete(reinterpret_cast<CPhidgetHandle>(ifKit));
352  }
353 }
Definition: Logger.h:5
void Initialize(Logger *logger, EncoderConfig *config, Bus *prmBus)
Definition: Encoder.cpp:298
okay, the portable keyboard numbers don&#39;t work like the outside keypad because the outside keypad is ...
Definition: Analog.cpp:3
static int CCONV ErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
Definition: Encoder.cpp:75
Definition: Bus.h:12
void Destroy() const
Definition: Encoder.cpp:347
bool wasDetached
Definition: Encoder.h:32
CPhidgetEncoderHandle ifKit
Definition: Encoder.h:22
static int CCONV PositionChangeHandler(CPhidgetEncoderHandle IFK, void *usrptr, int Index, int Time, int RelativePosition)
Definition: Encoder.cpp:151
void Log(const char *msg, Level level=Info, int errorCode=0)
These have to be in this order.
Definition: Logger.cpp:16
static int CCONV DetachHandler(CPhidgetHandle IFK, void *userptr)
Definition: Encoder.cpp:51
Logger * logger
Definition: Encoder.h:18
std::string friendlyName
Definition: Encoder.h:12
EncoderConfig * config
Definition: Encoder.h:19
int display_properties(CPhidgetEncoderHandle phid) const
Definition: Encoder.cpp:271
static int CCONV InputChangeHandler(CPhidgetEncoderHandle IFK, void *usrptr, int Index, int State)
Definition: Encoder.cpp:86
static int CCONV AttachHandler(CPhidgetHandle IFK, void *userptr)
Definition: Encoder.cpp:20