iMSTK
Interactive Medical Simulation Toolkit
imstkHaplyDeviceManager.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkHaplyDeviceManager.h"
8 #include "imstkHaplyDeviceClient.h"
9 #include "imstkLogger.h"
10 
11 #include <HardwareAPI.h>
12 #include <DeviceDetection.h>
13 
14 using namespace Haply::HardwareAPI;
15 
16 namespace imstk
17 {
18 std::vector<std::string>
19 HaplyDeviceManager::getInverse3PortNames()
20 {
21  // Get all the inverse3 devices (max 10)
22  std::string portNames[10];
23  const int portCount = Devices::DeviceDetection::AutoDetectInverse3(portNames);
24  // \note: The above detection function prints stuff. Undesirable
25  std::cout << std::endl;
26 
27  std::vector<std::string> results;
28  for (int i = 0; i < portCount; i++)
29  {
30  results.push_back(portNames[i]);
31  }
32  return results;
33 }
34 
35 std::vector<std::string>
36 HaplyDeviceManager::getHandlePortNames()
37 {
38  // Find Handle
39  std::string portNames[10];
40  const int portCount = Devices::DeviceDetection::AutoDetectHandle(portNames);
41 
42  std::vector<std::string> results;
43  for (int i = 0; i < portCount; i++)
44  {
45  results.push_back(portNames[i]);
46  }
47  return results;
48 }
49 
50 std::shared_ptr<DeviceClient>
51 HaplyDeviceManager::makeDeviceClient(std::string portName = "", std::string handlePortName = "")
52 {
53  // Autodetect the first device found
54  if (portName == "")
55  {
56  std::vector<std::string> portNames = getInverse3PortNames();
57  CHECK(portNames.size() > 0) << "No Haply Inverse3 devices found";
58  portName = portNames[0];
59  }
60  // Handle is optional
61  if (handlePortName == "")
62  {
63  std::vector<std::string> portNames = getHandlePortNames();
64  if (portNames.size() > 0)
65  {
66  handlePortName = portNames[0];
67  }
68  }
69 
70  auto deviceClient = std::shared_ptr<HaplyDeviceClient>(new HaplyDeviceClient(portName, handlePortName));
71  m_deviceClients.push_back(deviceClient);
72  return deviceClient;
73 }
74 
75 bool
76 HaplyDeviceManager::isDevicePresent()
77 {
78  std::vector<std::string> portNames = HaplyDeviceManager::getInverse3PortNames();
79  // List the devices found
80  for (const auto& portName : portNames)
81  {
82  if (portName != "")
83  {
84  return true;
85  }
86  }
87  return false;
88 }
89 
90 bool
91 HaplyDeviceManager::initModule()
92 {
93  // \todo: Figure out how to stop module in order to reinitialize
94  if (!getInit())
95  {
96  LOG(INFO) << "Haply HardwareAPI version " << GetLibraryVersion();
97 
98  std::vector<std::string> portNames = HaplyDeviceManager::getInverse3PortNames();
99  // List the devices found
100  for (const auto& portName : portNames)
101  {
102  LOG(INFO) << "Inverse3 device available with name: " << portName;
103  }
104 
105  // List the handles found
106  portNames = HaplyDeviceManager::getHandlePortNames();
107  for (const auto& portName : portNames)
108  {
109  LOG(INFO) << "Haply Handle device available with name: " << portName;
110  }
111  if (portNames.size() == 0)
112  {
113  LOG(INFO) << "No Haply Handle device available.";
114  }
115 
116  for (const auto& client : m_deviceClients)
117  {
118  client->initialize();
119  }
120  //hdStartScheduler();
121  }
122  else
123  {
124  LOG(WARNING) << "HaplyDeviceManager already initialized. Reinitialization not implemented.";
125  }
126  return true;
127 }
128 
129 void
130 HaplyDeviceManager::updateModule()
131 {
132  for (const auto& client : m_deviceClients)
133  {
134  client->update();
135  }
136 }
137 
138 void
139 HaplyDeviceManager::uninitModule()
140 {
141  // \todo: Other threads could be mid update call here
142  //hdStopScheduler();
143  for (const auto& client : m_deviceClients)
144  {
145  client->disable();
146  }
147 }
148 } // namespace imstk
Subclass of DeviceClient for haply, currently implemented only for the Inverse3. Warning: This code i...
Compound Geometry.