iMSTK
Interactive Medical Simulation Toolkit
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
imstkHaplyDeviceClient.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 "imstkHaplyDeviceClient.h"
8 #include "imstkLogger.h"
9 
10 #include <SerialStream.h>
11 
12 using namespace Haply::HardwareAPI;
13 
14 namespace imstk
15 {
16 std::ostream&
17 operator<<(std::ostream& os, const HaplyDeviceClient::DeviceInfo& info)
18 {
19  os << "Device id: " << info.deviceId << std::endl;
20  os << "Device Model #: " << static_cast<int>(info.modelNumber) << std::endl;
21  os << "Hardware Version: " << static_cast<int>(info.hardwareVersion) << std::endl;
22  os << "Firmware Version: " << static_cast<int>(info.firmwareVersion) << std::endl;
23  return os;
24 }
25 
26 void
27 HaplyDeviceClient::initialize()
28 {
29  m_deviceStream = std::make_shared<IO::SerialStream>(m_deviceName.c_str(), true);
30  m_device = std::make_shared<Devices::Inverse3>(m_deviceStream.get());
31 
32  if (m_handleName != "")
33  {
34  m_handleEnabled = true;
35  m_handleDeviceStream = std::make_shared<IO::SerialStream>(m_handleName.c_str(), true);
36  m_handleDevice = std::make_shared<HaplyHandle>(m_handleDeviceStream.get());
37  }
38 
39  m_device->SendDeviceWakeup();
40 
41  m_device->ReceiveDeviceInfo(&m_deviceInfo.deviceId,
42  &m_deviceInfo.modelNumber,
43  &m_deviceInfo.hardwareVersion, &m_deviceInfo.firmwareVersion,
44  &m_deviceInfo.quat);
45 
46  // Wake up the handle. If the handle is awake, the first response may not be correct response type, so we wait until
47  if (m_handleEnabled)
48  {
49  m_handleDevice->SendDeviceWakeup();
50  while (m_lastReturnType != 0xD0)
51  {
52  m_handleDevice->Receive(&m_lastReturnType);
53  }
54  //Haply::HardwareAPI::Devices::Handle::HandleInfoResponse handleInfo = m_handleDevice->GetInfoResponse();
55  // Calibrate the handle. Make sure to point the handle toward the screen
56  //handle->Calibrate();
57  }
58 
59  LOG(INFO) << m_deviceInfo;
60 }
61 
62 void
63 HaplyDeviceClient::update()
64 {
65  m_forceLock.lock();
66  m_deviceForce = m_force.cast<float>();
67  m_deviceForce = Vec3f(
68  static_cast<float>(m_force[2]),
69  static_cast<float>(m_force[0]),
70  static_cast<float>(m_force[1]));
71  m_forceLock.unlock();
72 
73  m_device->SendEndEffectorForce(m_deviceForce.data());
74  m_device->ReceiveEndEffectorState(m_devicePos.data(), m_deviceVelocity.data());
75 
76  //m_deviceResponse = m_device->EndEffectorForce({ m_deviceForce[0], m_deviceForce[1], m_deviceForce[2] });
77 
78  // Get handle update
79  if (m_handleEnabled)
80  {
81  m_handleDevice->Receive(&m_lastReturnType);
82  }
83 
84  // Swap the axes a bit (Haply uses a RHS z-up)
85  m_transformLock.lock();
86  m_position = Vec3d(
87  static_cast<double>(m_devicePos[1]),
88  static_cast<double>(m_devicePos[2]),
89  static_cast<double>(m_devicePos[0]));
90  if (m_handleEnabled)
91  {
92  m_orientation = Quatd(
93  static_cast<double>(m_handleDevice->m_statusResponse.quaternion[0]),
94  static_cast<double>(m_handleDevice->m_statusResponse.quaternion[1]),
95  static_cast<double>(m_handleDevice->m_statusResponse.quaternion[2]),
96  static_cast<double>(m_handleDevice->m_statusResponse.quaternion[3]));
97  }
98  m_transformLock.unlock();
99 }
100 
101 void
102 HaplyDeviceClient::disable()
103 {
104  LOG(INFO) << "Closing Haply device streams";
105  m_deviceStream->CloseDevice();
106  if (m_handleEnabled)
107  {
108  m_handleDeviceStream->CloseDevice();
109  }
110 }
111 } // namespace imstk
Compound Geometry.