PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
IntuitiveDaVinci.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #include "PlusConfigure.h"
8 #include "IntuitiveDaVinci.h"
9 
10 //----------------------------------------------------------------------------
12  : mPrintStream(ISI_FALSE)
13  , mQuit(ISI_FALSE)
14  , mManipIndex(ISI_PSM2)
15  , mStatus(ISI_SUCCESS)
16  , mIpAddr("10.0.0.5")
17  , mPort(5002)
18  , mPassword("")
19  , mRateHz(60)
20 {
21 
22 }
23 
24 //----------------------------------------------------------------------------
26 {
27  stop();
28  disconnect();
29 }
30 
31 //----------------------------------------------------------------------------
33 {
34  if (isConnected())
35  {
36  ISI_STATUS status;
37  status = isi_start_stream(mRateHz);
38  if (status != ISI_SUCCESS)
39  {
40  LOG_ERROR("Unable to start streaming.");
41  return false;
42  }
43 
44  return true;
45  }
46 
47  LOG_ERROR("Not connected. Cannot start streaming.");
48  return false;
49 }
50 
51 //----------------------------------------------------------------------------
53 {
54  if (isConnected())
55  {
56  isi_stop_stream();
57  isi_disconnect();
58  mConnected = false;
59  }
60 }
61 
62 //----------------------------------------------------------------------------
64 {
65  ISI_UINT password = strtol(mPassword.c_str(), 0, 16);
66  mStatus = isi_connect_ex(mIpAddr.c_str(), mPort, password);
67 
68  if (mStatus != ISI_SUCCESS)
69  {
70  LOG_WARNING("IntuitiveDaVinci::connect failed to connect with arguments");
71  isi_stop_stream();
72  isi_disconnect();
73  }
74  mConnected = true;
75  return mStatus;
76 }
77 
78 //----------------------------------------------------------------------------
80 {
81  // check if system is connected
82  if (!this->mConnected)
83  {
84  LOG_ERROR("disconnect: not connected.");
85  return ISI_FAIL;
86  }
87 
88  stop();
89  isi_disconnect();
90  this->mConnected = false;
91  return ISI_SUCCESS;
92 }
93 
94 //----------------------------------------------------------------------------
95 ISI_STATUS IntuitiveDaVinci::subscribe(ISI_EVENT_CALLBACK eCB, ISI_STREAM_CALLBACK sCB, void* eventUserData, void* streamUserData)
96 {
97  setStreamCallback(sCB, streamUserData);
98  setEventCallback(eCB, eventUserData);
99 
100  // Start streaming information at 60 Hz
101  mStatus = isi_start_stream(mRateHz);
102 
103  if (mStatus != ISI_SUCCESS)
104  {
105  LOG_WARNING("IntuitiveDaVinci::subscribe failed to start stream. Disconnecting from da Vinci");
106  stop();
107  }
108  return mStatus;
109 }
110 
111 //----------------------------------------------------------------------------
112 ISI_STATUS IntuitiveDaVinci::setStreamCallback(ISI_STREAM_CALLBACK sCB, void* userData)
113 {
114  // Subscribe to all fields
115  mStatus = isi_subscribe_all_stream_fields();
116  if (mStatus != ISI_SUCCESS)
117  {
118  // Propagate error message to caller
119  return mStatus;
120  }
121 
122  if (sCB != NULL)
123  {
124  mStatus = isi_set_stream_callback(sCB, userData);
125  }
126  Sleep(500);
127 
128  return mStatus;
129 }
130 
131 //----------------------------------------------------------------------------
132 ISI_STATUS IntuitiveDaVinci::setEventCallback(ISI_EVENT_CALLBACK eCB, void* userData)
133 {
134  // Subscribe to all events
135  mStatus = isi_subscribe_all_events();
136 
137  if (mStatus != ISI_SUCCESS)
138  {
139  // Propagate error message to caller
140  return mStatus;
141  }
142 
143  if (eCB != NULL)
144  {
145  mStatus = isi_set_event_callback(eCB, userData);
146  }
147  Sleep(500);
148 
149  return mStatus;
150 }
151 
152 //----------------------------------------------------------------------------
153 std::vector<std::string> IntuitiveDaVinci::getManipulatorNames()
154 {
155  std::vector<std::string> names;
156 
157  for (int i = 0; i != ISI_NUM_MANIPS; i++)
158  {
159  ISI_MANIP_INDEX index = (ISI_MANIP_INDEX) i;
160  std::string name = isi_get_manip_name(index);
161 
162  if (name.size() == 0) { continue; }
163 
164  names.push_back(name);
165  }
166 
167  return names;
168 }
169 
170 //----------------------------------------------------------------------------
171 void IntuitiveDaVinci::getPosition(ISI_TRANSFORM* T)
172 {
173  ISI_STREAM_FIELD stream_data; // Datastreams
174 
175  isi_get_stream_field(mManipIndex, ISI_TIP_TRANSFORM, &stream_data);
176 
177  copyTransform((ISI_TRANSFORM*) stream_data.data, T);
178 }
179 
180 //----------------------------------------------------------------------------
181 void IntuitiveDaVinci::copyTransform(ISI_TRANSFORM* in, ISI_TRANSFORM* out)
182 {
183  if (in == NULL || out == NULL) { return; }
184 
185  out->pos.x = in->pos.x;
186  out->pos.y = in->pos.y;
187  out->pos.z = in->pos.z;
188 
189  out->rot.row0 = in->rot.row0;
190  out->rot.row1 = in->rot.row1;
191  out->rot.row2 = in->rot.row2;
192 }
193 
194 //----------------------------------------------------------------------------
195 void IntuitiveDaVinci::printTransform(const ISI_TRANSFORM* T)
196 {
197  LOG_INFO("Position: " << T->pos.x << " " << T->pos.y << " " << T->pos.z);
198  LOG_INFO("X Axis Rotation: " << T->rot.row0.x << " " << T->rot.row1.x << " " << T->rot.row2.x);
199  LOG_INFO("Y Axis Rotation: " << T->rot.row0.y << " " << T->rot.row1.y << " " << T->rot.row2.y);
200  LOG_INFO("Z Axis Rotation: " << T->rot.row0.z << " " << T->rot.row1.z << " " << T->rot.row2.z);
201 }
202 
203 //----------------------------------------------------------------------------
205 {
206  static const char help[] = {"\
207  API Stream Menu \n\
208  ---------------- \n\
209  Q: Quit \n\
210  M: Set Manip ID \n\
211  V: Print Version \n\
212  P: Print Sample \n\
213  S: Print Stream \n\
214  R: Record file \n\
215  "
216  };
217 
218  LOG_INFO(help);
219 }
220 
221 //----------------------------------------------------------------------------
223 {
224  ISI_STATUS status;
225  ISI_SYSTEM_CONFIG config;
226 
227  status = isi_get_system_config(&config);
228 
229  if (status != ISI_SUCCESS)
230  {
231  return "";
232  }
233 
234  return config.library_version;
235 }
236 
237 //----------------------------------------------------------------------------
239 {
240  return mConnected;
241 }
242 
243 //----------------------------------------------------------------------------
244 void IntuitiveDaVinci::printStreamState(ISI_MANIP_INDEX manipIndex)
245 {
246  ISI_STREAM_FIELD stream_data;
247  int i;
248 
249  LOG_INFO("Retrieving stream state for manipulator: " << isi_get_manip_name(manipIndex));
250 
251  LOG_INFO("\nISI_TIP_TRANSFORM: \n");
252  isi_get_stream_field(manipIndex, ISI_TIP_TRANSFORM, &stream_data);
253  printTransform((ISI_TRANSFORM*) stream_data.data);
254 
255  LOG_INFO("\nISI_JOINT_VALUES: \n");
256 
257  isi_get_stream_field(manipIndex, ISI_JOINT_VALUES, &stream_data);
258  for (i = 0; i != stream_data.count; i++)
259  {
260  LOG_INFO(" " << stream_data.data[i]);
261  }
262 
263  LOG_INFO("\n ISI Timestamp (seconds): " << stream_data.timestamp);
264 }
265 
266 //----------------------------------------------------------------------------
268 {
269  ISI_STATUS status;
270  ISI_SYSTEM_CONFIG config;
271 
272  status = isi_get_system_config(&config);
273 
274  if (status == ISI_SUCCESS)
275  {
276  LOG_INFO("System Name: " << config.system_name);
277  LOG_INFO("System Version: " << config.system_version);
278  LOG_INFO("Library Version: " << config.library_version);
279  }
280  else
281  {
282  LOG_WARNING("Failed to get IntuitiveDaVinci system configuration");
283  }
284 }
285 
286 //----------------------------------------------------------------------------
287 void IntuitiveDaVinci::setHostInfo(const std::string ip, const unsigned int port, const std::string pass)
288 {
289  mIpAddr = ip;
290  mPort = port;
291  mPassword = pass;
292 }
293 
294 //----------------------------------------------------------------------------
295 void IntuitiveDaVinci::setIpAddr(const std::string ip)
296 {
297  mIpAddr = ip;
298 }
299 
300 //----------------------------------------------------------------------------
301 void IntuitiveDaVinci::setPort(const unsigned int port)
302 {
303  mPort = port;
304 }
305 
306 //----------------------------------------------------------------------------
308 {
310 }
ISI_MANIP_INDEX mManipIndex
void setHostInfo(const std::string ip, const unsigned int port, const std::string pass)
void printTransform(const ISI_TRANSFORM *T)
ISI_STATUS setEventCallback(ISI_EVENT_CALLBACK eCB, void *userData)
void getPosition(ISI_TRANSFORM *T)
for i
int port
Definition: phidget22.h:2454
unsigned int mRateHz
std::string mPassword
void setPassword(const std::string password)
ISI_STATUS disconnect()
std::vector< std::string > getManipulatorNames()
void copyTransform(ISI_TRANSFORM *in, ISI_TRANSFORM *out)
ISI_STATUS subscribe(ISI_EVENT_CALLBACK eCB, ISI_STREAM_CALLBACK sCB, void *eventUserData, void *streamUserData)
unsigned int mPort
ISI_STATUS connect()
const char int const char * password
Definition: phidget22.h:2552
void setPort(const unsigned int port)
ISI_STATUS setStreamCallback(ISI_STREAM_CALLBACK sCB, void *userData)
void setIpAddr(const std::string ip)
void printStreamState(ISI_MANIP_INDEX manipIndex)
std::string getLibraryVersion()