iMSTK
Interactive Medical Simulation Toolkit
imstkDeviceClient.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 "imstkDeviceClient.h"
8 #include "imstkLogger.h"
9 #include <limits>
10 
11 namespace imstk
12 {
13 DeviceClient::DeviceClient(const std::string& name, const std::string& ip) :
14  m_deviceName(name),
15  m_ip(ip),
16  m_position(Vec3d::Zero()),
17  m_velocity(Vec3d::Zero()),
18  m_orientation(Quatd::Identity()),
19  m_force(Vec3d::Zero())
20 {
21 }
22 
23 Vec3d
25 {
26  Vec3d pos;
27  m_transformLock.lock();
28  pos = m_position;
29  m_transformLock.unlock();
30  return pos;
31 }
32 
33 Vec3d
35 {
36  Vec3d vel;
37  m_transformLock.lock();
38  vel = m_velocity;
39  m_transformLock.unlock();
40  return vel;
41 }
42 
43 Vec3d
45 {
46  Vec3d angVel;
47  m_transformLock.lock();
48  angVel = m_angularVelocity;
49  m_transformLock.unlock();
50  return angVel;
51 }
52 
53 Quatd
55 {
56  Quatd orientation;
57  m_transformLock.lock();
58  orientation = m_orientation;
59  m_transformLock.unlock();
60  return orientation;
61 }
62 
63 Vec3d
65 {
66  Vec3d force;
67  m_forceLock.lock();
68  force = m_force;
69  m_forceLock.unlock();
70  return force;
71 }
72 
73 void
74 DeviceClient::setForce(Vec3d force)
75 {
76  m_forceLock.lock();
77  m_force = force;
78  m_forceLock.unlock();
79 }
80 
81 const std::unordered_map<int, int>&
83 {
84  return m_buttons;
85 }
86 
87 int
88 DeviceClient::getButton(const int buttonId)
89 {
90  int result = 0;
91  m_dataLock.lock();
92  if (m_buttons.find(buttonId) != m_buttons.end())
93  {
94  result = m_buttons.at(buttonId);
95  }
96  m_dataLock.unlock();
97  return result;
98 }
99 
100 std::vector<double>
102 {
103  std::vector<double> result;
104  m_dataLock.lock();
105  result = m_analogChannels;
106  m_dataLock.unlock();
107  return result;
108 }
109 
110 double
112 {
113  if (i > m_analogChannels.size())
114  {
115  LOG(WARNING) << "Requested unknown channel, returning NAN";
116  return std::numeric_limits<double>::quiet_NaN();
117  }
118  else
119  {
120  double result;
121  m_dataLock.lock();
122  result = m_analogChannels[i];
123  m_dataLock.unlock();
124  return result;
125  }
126 }
127 } // namespace imstk
Vec3d getForce()
Get/Set the device force.
Vec3d getPosition()
Get the device position.
Vec3d getVelocity()
Get the device velocity.
Vec3d getAngularVelocity()
Get the device angular velocity.
Compound Geometry.
Quatd getOrientation()
Get the device orientation.
std::vector< double > getAnalog() const
get values of the analog input
const std::unordered_map< int, int > & getButtons() const
Get button map.
int getButton(const int buttonId)
Get the state of a button returns 0 if can&#39;t find button.