iMSTK
Interactive Medical Simulation Toolkit
imstkDeviceClient.h
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 #pragma once
8 
9 #include "imstkMath.h"
10 #include "imstkEventObject.h"
11 #include "imstkSpinLock.h"
12 
13 #include <unordered_map>
14 
15 namespace imstk
16 {
17 using DeviceType = int;
18 #define UNKNOWN_DEVICE 0
19 #define OPENHAPTICS_DEVICE 1
20 #define OPENVR_LEFT_CONTROLLER 3
21 #define OPENVR_RIGHT_CONTROLLER 4
22 #define OPENVR_HMD 5
23 
24 using ButtonStateType = int;
25 #define BUTTON_RELEASED 0
26 #define BUTTON_TOUCHED 1
27 #define BUTTON_UNTOUCHED 2
28 #define BUTTON_PRESSED 3
29 
30 class ButtonEvent : public Event
31 {
32 public:
33  ButtonEvent(std::string type, const int button, const ButtonStateType keyPressType) : Event(type),
34  m_buttonState(keyPressType),
35  m_button(button)
36  {
37  }
38 
39  virtual ~ButtonEvent() override = default;
40 
41  ButtonStateType m_buttonState;
42  const int m_button = -1;
43 };
44 
53 class DeviceClient : public EventObject
54 {
55 public:
56  virtual ~DeviceClient() = default;
57 
58  // *INDENT-OFF*
59  SIGNAL(DeviceClient,buttonStateChanged);
60  // *INDENT-ON*
61 
65  const std::string& getIp() { return m_ip; }
66  void setIp(const std::string& ip) { m_ip = ip; }
68 
72  const std::string& getDeviceName() { return m_deviceName; }
73  void setDeviceName(const std::string& deviceName) { m_deviceName = deviceName; }
75 
79  bool getTrackingEnabled() const { return m_trackingEnabled; }
80  void setTrackingEnabled(const bool status) { m_trackingEnabled = status; }
81  bool getAnalogicEnabled() const { return m_analogicEnabled; }
82  void setAnalogicEnabled(const bool status) { m_analogicEnabled = status; }
83  bool getButtonsEnabled() const { return m_buttonsEnabled; }
84  void setButtonsEnabled(const bool status) { m_buttonsEnabled = status; }
85  bool getForceEnabled() const { return m_forceEnabled; }
86  void setForceEnabled(const bool status) { m_forceEnabled = status; }
87 
91  Vec3d getPosition();
92 
96  Vec3d getVelocity();
97 
101  Vec3d getAngularVelocity();
102 
106  Quatd getOrientation();
107 
111  const Vec3d& getOffset() const { return m_endEffectorOffset; }
112 
116  Vec3d getForce();
117  void setForce(Vec3d force);
119 
123  const std::unordered_map<int, int>& getButtons() const;
124 
129  int getButton(const int buttonId);
130 
134  std::vector<double> getAnalog() const;
135 
140  double getAnalog(int i) const;
141 
145  virtual void update() {}
146 
147 protected:
148  DeviceClient(const std::string& name, const std::string& ip);
149 
150  std::string m_deviceName;
151  std::string m_ip;
152 
153  bool m_trackingEnabled = true;
154  bool m_analogicEnabled = true;
155  bool m_buttonsEnabled = true;
156  bool m_forceEnabled = false;
157 
158  Vec3d m_position;
159  Vec3d m_velocity;
162  Vec3d m_force;
163  Vec3d m_endEffectorOffset = Vec3d(0.0, 0.0, 0.0);
164 
165  std::unordered_map<int, int> m_buttons;
166  std::vector<double> m_analogChannels;
167 
171 };
172 } // namespace imstk
The SpinLock class.
Definition: imstkSpinLock.h:20
The device client&#39;s represents the device and provides an interface to acquire data from a device...
Vec3d m_angularVelocity
Angular velocity of the end effector.
const std::string & getDeviceName()
Get/Set the device name.
const std::string & getIp()
Get/Set the device IP.
std::string m_ip
Connection device IP.
Base class for events which contain a type, priority, and data priority defaults to 0 and uses a grea...
const Vec3d & getOffset() const
Get offset from position for device end effector.
bool getTrackingEnabled() const
Get/Set what listeners to enable on the device: tracking, analogic, force, buttons.
ParallelUtils::SpinLock m_dataLock
Used for button and analog data.
Compound Geometry.
ParallelUtils::SpinLock m_transformLock
Used for devices filling data from other threads.
virtual void update()
Do runtime logic.
Quatd m_orientation
Orientation of the end effector.
ParallelUtils::SpinLock m_forceLock
Used for devices filling data from other threads.
Vec3d m_velocity
Linear velocity of end effector.
Vec3d m_position
Position of end effector.
EventObject is the base class for all objects in iMSTK that can receive and emit events. It supports direct and queued observer functions. Direct observers receive events immediately on the same thread This can either be posted on an object or be a function pointer Queued observers receive events within their queue which they can process whenever they like. These can be connected with the connect/queuedConnect/disconnect functions Lambda recievers cannot be disconnected unless all receivers to a signal are removed.
Vec3d m_force
Force vector.
std::string m_deviceName
Device Name.