PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusCapistranoCommand.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 "vtkPlusDataCollector.h"
12 
13 #include "vtkImageData.h"
14 #include "vtkDICOMImageReader.h"
15 #include "vtkObjectFactory.h"
16 #include "vtkPlusChannel.h"
18 #include "vtkIGSIOTrackedFrameList.h"
21 #include <vtkImageFlip.h>
22 #include <vtkPointData.h>
23 #include <vtkDirectory.h>
24 
25 
27 
28 namespace
29 {
30  static const std::string CAPISTRANO_COMMAND = "CapistranoCommand";
31 }
32 
33 //----------------------------------------------------------------------------
35  : ResponseExpected(true)
36 {
37  // It handles only one command, set its name by default
38  this->SetName(CAPISTRANO_COMMAND);
39 }
40 
41 //----------------------------------------------------------------------------
43 {
44 }
45 
46 //----------------------------------------------------------------------------
47 void vtkPlusCapistranoCommand::GetCommandNames(std::list<std::string>& cmdNames)
48 {
49  cmdNames.clear();
50  cmdNames.push_back(CAPISTRANO_COMMAND);
51 }
52 
53 //----------------------------------------------------------------------------
54 std::string vtkPlusCapistranoCommand::GetDescription(const std::string& commandName)
55 {
56  std::string desc;
57  if (commandName.empty() || igsioCommon::IsEqualInsensitive(commandName, CAPISTRANO_COMMAND))
58  {
59  desc += CAPISTRANO_COMMAND;
60  desc += ": Send commands to Capistrano device.";
61  }
62  return desc;
63 }
64 
65 //----------------------------------------------------------------------------
67 {
68  return this->DeviceId;
69 }
70 
71 //----------------------------------------------------------------------------
72 void vtkPlusCapistranoCommand::SetDeviceId(const std::string& deviceId)
73 {
74  this->DeviceId = deviceId;
75 }
76 
77 //----------------------------------------------------------------------------
79 {
80  return this->CommandName;
81 }
82 
83 //----------------------------------------------------------------------------
85 {
86  this->CommandName = text;
87 }
88 
89 //----------------------------------------------------------------------------
91 {
92  return this->CommandValue;
93 }
94 
95 //----------------------------------------------------------------------------
97 {
98  this->CommandValue = text;
99 }
100 
101 //----------------------------------------------------------------------------
102 void vtkPlusCapistranoCommand::PrintSelf(ostream& os, vtkIndent indent)
103 {
104  this->Superclass::PrintSelf(os, indent);
105 }
106 
107 //----------------------------------------------------------------------------
109 {
110  this->RequestedParameterChanges.clear();
112  {
113  return PLUS_FAIL;
114  }
115 
116  this->SetDeviceId(aConfig->GetAttribute("UsDeviceId"));
117 
118  // Parse nested elements and store requested parameter changes
119  for (int elemIndex = 0; elemIndex < aConfig->GetNumberOfNestedElements(); ++elemIndex)
120  {
121  vtkXMLDataElement* currentElem = aConfig->GetNestedElement(elemIndex);
122  if (igsioCommon::IsEqualInsensitive(currentElem->GetName(), "Parameter"))
123  {
124  const char* parameterName = currentElem->GetAttribute("Name");
125  const char* parameterValue = currentElem->GetAttribute("Value");
126  if (!parameterValue)
127  {
128  parameterValue = "None";
129  }
130  if (!parameterName)
131  {
132  LOG_ERROR("Unable to find required Name" << (currentElem->GetName() ? currentElem->GetName() : "(undefined)") << " element in CapistranoCommand command");
133  continue;
134  }
135  this->RequestedParameterChanges.push_back(std::make_pair(parameterName, parameterValue));
136  }
137  }
138 
139  return PLUS_SUCCESS;
140 }
141 
142 
143 //----------------------------------------------------------------------------
145 {
147  {
148  return PLUS_FAIL;
149  }
150  XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(DeviceId, aConfig);
151  XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(CommandName, aConfig);
152  XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(CommandValue, aConfig);
153  return PLUS_SUCCESS;
154 }
155 
156 
157 //----------------------------------------------------------------------------
159 {
160  LOG_DEBUG("vtkPlusSetUsParameterCommand::Execute: " << (!this->Name.empty() ? this->Name : "(undefined)")
161  << ", device: " << (this->DeviceId.empty() ? "(undefined)" : this->DeviceId));
162 
163  if (this->Name.empty())
164  {
165  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", "No command name specified.");
166  return PLUS_FAIL;
167  }
168  else if (!igsioCommon::IsEqualInsensitive(this->Name, CAPISTRANO_COMMAND))
169  {
170  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", "Unknown command name: " + this->Name + ".");
171  return PLUS_FAIL;
172  }
173  vtkPlusDataCollector* dataCollector = GetDataCollector();
174  vtkPlusDevice* aDevice = NULL;
175  if (dataCollector->GetDevice(aDevice, this->DeviceId) != PLUS_SUCCESS)
176  {
177  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", std::string("Device ")
178  + (this->DeviceId.empty() ? "(undefined)" : this->DeviceId) + std::string(" is not found."));
179  return PLUS_FAIL;
180  }
181  vtkPlusCapistranoVideoSource* device = dynamic_cast<vtkPlusCapistranoVideoSource*>(aDevice);
182 
183  std::string DeviceId = (aDevice->GetDeviceId().empty() ? "(unknown)" : aDevice->GetDeviceId());
184  std::string resultString = "<CommandReply>";
185  std::string error = "";
186  std::map < std::string, std::pair<IANA_ENCODING_TYPE, std::string> > metaData;
187  PlusStatus status = PLUS_SUCCESS;
188  bool hasFailure = false;
189 
190  std::list<std::pair<std::string, std::string>>::iterator paramIt;
191  for (paramIt = this->RequestedParameterChanges.begin(); paramIt != this->RequestedParameterChanges.end(); ++paramIt)
192  {
193  std::string parameterName = paramIt->first;
194  std::string value = paramIt->second;
195  resultString += "<Parameter Name=\"" + parameterName + "\"";
207  {
208  bool valid = false;
209  double parameterValue = vtkVariant(value).ToDouble(&valid);
210  if (!valid)
211  {
212  error += "Failed to parse " + parameterName + ". ";
213  resultString += " Success=\"false\"";
214  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "FAIL");
215  status = PLUS_FAIL;
216  continue;
217  }
218  }
230  {
231  if (value != "None")
232  {
233  LOG_WARNING("This command does not require a value");
234  }
235  }
236  else
237  {
238  error += "Invalid parameter " + parameterName + ". ";
239  resultString += " Success=\"false\"";
240  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "FAIL");
241  status = PLUS_FAIL;
242  continue;
243  }
245  {
246  int wobbleRate_int = std::stoi(value);
247  status = device->SetWobbleRate((unsigned char)wobbleRate_int);
248  resultString += " Success=\"true\"";
249  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "SUCCESS");
250  }
252  {
253  value = std::to_string(device->GetWobbleRate());
254  resultString += " Success=\"true\"";
255  resultString += " Value=\"" + value + "\"";
256  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, value);
257  status = PLUS_SUCCESS;
258  }
260  {
261  bool bidi = std::stoi(value);
262  device->SetBidirectionalMode(bidi);
263  resultString += " Success=\"true\"";
264  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "SUCCESS");
265  }
267  {
269  resultString += " Success=\"true\"";
270  resultString += " Value=\"" + value + "\"";
271  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, value);
272  }
274  {
275  bool freezeProbe_bool = std::stoi(value);
276  if (device->FreezeDevice(freezeProbe_bool) == PLUS_SUCCESS)
277  {
278  resultString += " Success=\"true\"";
279  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "SUCCESS");
280  }
281  else
282  {
283  LOG_ERROR("Failed to freeze probe");
284  resultString += " Success=\"false\"";
285  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "FAIL");
286  }
287  }
289  {
290  value = device->IsFrozen() ? "1" : "0";
291  resultString += " Success=\"true\"";
292  resultString += " Value=\"" + value + "\"";
293  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, value);
294  status = PLUS_SUCCESS;
295  }
297  {
298  int jitterCompensation_int = std::stoi(value);
299  status = device->SetJitterCompensation((unsigned char)jitterCompensation_int);
300  resultString += " Success=\"true\"";
301  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "SUCCESS");
302  }
304  {
306  resultString += " Success=\"true\"";
307  resultString += " Value=\"" + value + "\"";
308  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, value);
309  status = PLUS_SUCCESS;
310  }
312  {
313  bool MISMode_bool = std::stoi(value);
314  if (device->SetMISMode(MISMode_bool) == PLUS_SUCCESS)
315  {
316  resultString += " Success=\"true\"";
317  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "SUCCESS");
318  }
319  else
320  {
321  LOG_ERROR("Failed to set MIS mode");
322  resultString += " Success=\"false\"";
323  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "FAIL");
324  }
325  }
327  {
328  bool MISMode;
329  if (device->GetMISMode(MISMode) == PLUS_SUCCESS)
330  {
331  value = std::to_string(MISMode);
332  resultString += " Success=\"true\"";
333  resultString += " Value=\"" + value + "\"";
334  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, value);
335  }
336  else
337  {
338  LOG_ERROR("Failed to get MIS mode");
339  resultString += " Success=\"false\"";
340  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "FAIL");
341  }
342  }
344  {
345  int misPulse_int = std::stoi(value);
346  if (device->SetMISPulsePeriod((unsigned char)misPulse_int) == PLUS_SUCCESS)
347  {
348  resultString += " Success=\"true\"";
349  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "SUCCESS");
350  }
351  else
352  {
353  LOG_ERROR("Failed to set MIS pulse period");
354  resultString += " Success=\"false\"";
355  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "FAIL");
356  }
357  }
359  {
360  unsigned int PulsePeriod;
361  if (device->GetMISPulsePeriod(PulsePeriod) == PLUS_SUCCESS)
362  {
363  value = std::to_string(PulsePeriod);
364  resultString += " Success=\"true\"";
365  resultString += " Value=\"" + value + "\"";
366  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, value);
367  }
368  else
369  {
370  LOG_ERROR("Failed to get MIS pulse period");
371  resultString += " Success=\"false\"";
372  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "FAIL");
373  }
374  }
376  {
377  float sweepAngle = std::stof(value);
378  status = device->SetSweepAngle(sweepAngle);
379  resultString += " Success=\"true\"";
380  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "SUCCESS");
381  }
383  {
384  value = std::to_string(device->GetSweepAngle());
385  resultString += " Success=\"true\"";
386  resultString += " Value=\"" + value + "\"";
387  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, value);
388  status = PLUS_SUCCESS;
389  }
391  {
392  int derComp = std::stoi(value);
393  status = device->SetDerivativeCompensation((unsigned char)derComp);
394  resultString += " Success=\"true\"";
395  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "SUCCESS");
396  }
398  {
400  resultString += " Success=\"true\"";
401  resultString += " Value=\"" + value + "\"";
402  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, value);
403  status = PLUS_SUCCESS;
404  }
406  {
407  int servoGain = std::stoi(value);
408  status = device->SetServoGain((unsigned char)servoGain);
409  resultString += " Success=\"true\"";
410  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "SUCCESS");
411  }
413  {
414  value = std::to_string(device->GetServoGain());
415  resultString += " Success=\"true\"";
416  resultString += " Value=\"" + value + "\"";
417  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, value);
418  status = PLUS_SUCCESS;
419  }
421  {
422  int positionScale = std::stoi(value);
423  status = device->SetPositionScale((unsigned char)positionScale);
424  resultString += " Success=\"true\"";
425  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "SUCCESS");
426  }
428  {
430  resultString += " Success=\"true\"";
431  resultString += " Value=\"" + value + "\"";
432  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, value);
433  status = PLUS_SUCCESS;
434  }
435  else
436  {
437  error += "Failed to set " + parameterName + ". ";
438  resultString += " Success=\"false\"";
439  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, "FAIL");
440  status = PLUS_FAIL;
441  }
442  if (status != PLUS_SUCCESS)
443  {
444  hasFailure = true;
445  }
446  resultString += "/>";
447  } // For each parameter
448  resultString += "</CommandReply>";
449  if (hasFailure)
450  {
451  status = PLUS_FAIL;
452  LOG_WARNING("Failed to set some requested parameter(s), result string was: " << resultString);
453  }
454 
455  vtkSmartPointer<vtkPlusCommandRTSCommandResponse> commandResponse = vtkSmartPointer<vtkPlusCommandRTSCommandResponse>::New();
456  commandResponse->UseDefaultFormatOff();
457  commandResponse->SetClientId(this->ClientId);
458  commandResponse->SetOriginalId(this->Id);
459  commandResponse->SetDeviceName(this->DeviceName);
460  commandResponse->SetCommandName(this->GetName());
461  commandResponse->SetStatus(status);
462  commandResponse->SetRespondWithCommandMessage(this->RespondWithCommandMessage);
463  commandResponse->SetErrorString(error);
464  commandResponse->SetResultString(resultString);
465  commandResponse->SetParameters(metaData);
466  this->CommandResponseQueue.push_back(commandResponse);
467 
468  return status;
469 }
virtual void PrintSelf(ostream &os, vtkIndent indent)
virtual void PrintSelf(ostream &os, vtkIndent indent)
Abstract interface for tracker and video devices.
Definition: vtkPlusDevice.h:60
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *aConfig)
static const char * CAPISTRANO_SET_SAMPLE_FREQUENCY
PlusStatus GetDevice(vtkPlusDevice *&aDevice, const std::string &aDeviceId) const
std::string Name
static const char * CAPISTRANO_GET_JITTER_COMPENSATION
PlusStatus SetSweepAngle(float sweepAngle)
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *aConfig)
PlusStatus GetMISPulsePeriod(unsigned int &PulsePeriod)
igsioStatus PlusStatus
Definition: PlusCommon.h:40
PlusStatus SetWobbleRate(unsigned char wobbleRate)
virtual std::string GetDescription(const std::string &commandName)
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *aConfig)
virtual std::string GetDeviceId() const
PlusStatus SetServoGain(unsigned char servoGain)
This command is for communicating with vtkPlusCapistranoDevice.This command is used for communicating...
std::string to_string(ClariusAvailability avail)
#define PLUS_FAIL
Definition: PlusCommon.h:43
vtkStandardNewMacro(vtkPlusCapistranoCommand)
static const char * CAPISTRANO_SET_BIDIRECTIONAL_MODE
static const char * CAPISTRANO_SET_MIS_PULSE_PERIOD
void SetCommandName(const std::string &text)
static const char * CAPISTRANO_SET_DERIVATIVE_COMPENSATION
virtual void SetDeviceId(const std::string &deviceId)
PhidgetLCD_Font int int const char * text
Definition: phidget22.h:4287
virtual void GetCommandNames(std::list< std::string > &cmdNames)
PlusStatus SetJitterCompensation(unsigned char jitterComp)
Manages devices that record image or positional data.
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
bool RespondWithCommandMessage
void SetCommandValue(const std::string &text)
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *aConfig)
virtual std::string GetDeviceId() const
void QueueCommandResponse(PlusStatus status, const std::string &message, const std::string &error="", const igtl::MessageBase::MetaDataMap *metaData=nullptr)
virtual vtkPlusDataCollector * GetDataCollector()
std::string DeviceId
std::string DeviceName
const char const char * value
Definition: phidget22.h:5111
static const char * CAPISTRANO_GET_MIS_PULSE_PERIOD
static const char * CAPISTRANO_GET_BIDIRECTIONAL_MODE
PlusStatus SetDerivativeCompensation(unsigned char derivativeCompensation)
PlusStatus SetPositionScale(unsigned char scale)
PlusCommandResponseList CommandResponseQueue
Class for acquiring ultrasound images from Capistrano Labs USB ultrasound systems.
std::list< std::pair< std::string, std::string > > RequestedParameterChanges
static const char * CAPISTRANO_GET_SAMPLE_FREQUENCY
static const char * CAPISTRANO_SET_JITTER_COMPENSATION
PlusStatus SetMISPulsePeriod(unsigned int val)
static const char * CAPISTRANO_GET_DERIVATIVE_COMPENSATION