PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
CmsBrachyStepperTest.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 "vtksys/CommandLineArguments.hxx"
9 #include "PlusBrachyStepper.h"
10 #include "PlusCmsBrachyStepper.h"
11 #include <stdlib.h>
12 #include <iostream>
13 
14 int main (int argc, char* argv[])
15 {
16  bool printHelp(false);
17  bool resetStepper(false);
18  std::string stepperType("Burdette Medical Systems Digital Stepper");
19  int inputComPort(2);
20  int inputNumberOfTrials(20);
21  int samplingTimeMs(50);
22 
23  int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
24 
25  vtksys::CommandLineArguments args;
26  args.Initialize(argc, argv);
27 
28  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
29  args.AddArgument("--reset", vtksys::CommandLineArguments::NO_ARGUMENT, &resetStepper, "Reset the stepper (need's to calibrate it).");
30  args.AddArgument("--com-port", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputComPort, "Com port number (Default: 2)." );
31  args.AddArgument("--number-of-trials", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputNumberOfTrials, "Number of trials (Default: 20)." );
32  args.AddArgument("--sampling-time", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &samplingTimeMs, "Sampling time in milliseconds (Default: 20)." );
33  args.AddArgument("--brachy-stepper-type", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &stepperType, "Set brachy stepper type (Default: Burdette Medical Systems Digital Stepper; Burdette Medical Systems Digital Stepper, Burdette Medical Systems Digital Motorized Stepper, CMS Accuseed DS300.)");
34  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
35 
36  if ( !args.Parse() )
37  {
38  std::cerr << "Problem parsing arguments" << std::endl;
39  std::cout << "\n\nCmsBrachyStepperTest help:" << args.GetHelp() << std::endl;
40  exit(EXIT_FAILURE);
41  }
42 
43  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
44 
45  if ( printHelp )
46  {
47  std::cout << "\n\nCmsBrachyStepperTest help:" << args.GetHelp() << std::endl;
48  exit(EXIT_SUCCESS);
49 
50  }
51 
52  for ( int reconnect = 0; reconnect < 5; reconnect++ )
53  {
54  LOG_INFO("Starting test " << reconnect + 1 );
55 
56  PlusCmsBrachyStepper *myStepper = new PlusCmsBrachyStepper(inputComPort, 19200);
57 
58  if ( STRCASECMP("Burdette Medical Systems Digital Stepper", stepperType.c_str()) == 0 )
59  {
61  }
62  else if ( STRCASECMP("Burdette Medical Systems Digital Motorized Stepper", stepperType.c_str()) == 0 )
63  {
65  }
66  else if ( STRCASECMP("CMS Accuseed DS300", stepperType.c_str()) == 0 )
67  {
69  }
70 
71  if (!myStepper->Connect())
72  {
73  LOG_ERROR("Couldn't connect to tracker!");
74  //return EXIT_FAILURE;
75  }
76 
77 
78  if ( resetStepper && reconnect == 0)
79  {
80  LOG_INFO( "Resetting stepper...");
81  if (!myStepper->ResetStepper())
82  {
83  resetStepper = false;
84  LOG_ERROR("Wrong answer received!");
85  return EXIT_FAILURE;
86  }
87  }
88 
89 
90  LOG_INFO( "Checking stepper's calibration state...");
91  if (!myStepper->IsStepperCalibrated())
92  {
93  std::cout << std::endl << "The stepper needs to be initialized!" << std::endl << std::endl;
94  std::string CalibMsg;
95  while(!myStepper->InitializeStepper(CalibMsg))
96  {
97  std::cout << CalibMsg << " ";
98  system("pause");
99  }
100  }
101 
102  LOG_INFO( "Getting stepper's calibration state...");
103  int pState(0), gState(0), rState(0);
104  if (myStepper->GetCalibrationState(pState, gState, rState))
105  {
106  LOG_INFO("Probe state = " << pState << " Grid state = " << gState << " Rotation state = " << rState );
107  }
108  else
109  {
110  LOG_ERROR("Wrong answer received!");
111  return EXIT_FAILURE;
112  }
113 
114  LOG_INFO( "Getting stepper's rotate state...");
115  int State=-1;
116  if (myStepper->GetRotateState(State))
117  {
118  LOG_INFO("Rotate state = " << State);
119  }
120  else
121  {
122  LOG_ERROR("Wrong answer received!");
123  return EXIT_FAILURE;
124  }
125 
126  LOG_INFO( "Getting motorized stepper's tick count...");
127  int MotorizationScaleFactor=-1;
128  if (myStepper->GetMotorizationScaleFactor(MotorizationScaleFactor)) {
129  if (MotorizationScaleFactor > 0)
130  {
131  LOG_INFO("Tick count = " << MotorizationScaleFactor);
132  }
133  else
134  {
135  LOG_INFO("Stepper is not motorized");
136  //LOG_ERROR("Wrong answer received!");
137  //return EXIT_FAILURE;
138  }
139  }
140  else
141  {
142  LOG_ERROR("Wrong answer received!");
143  return EXIT_FAILURE;
144  }
145 
146  LOG_INFO( "Getting stepper's status info...");
147  unsigned int Status=-1;
148  if (myStepper->GetStatusInfo(Status))
149  {
150  LOG_INFO("Status: " << std::hex << Status << std::dec);
151  }
152  else
153  {
154  LOG_ERROR("Wrong answer received!");
155  return EXIT_FAILURE;
156  }
157 
158 
159  LOG_INFO( "Getting stepper's device info...");
160 
161  std::string version;
162  std::string model;
163  std::string serial;
164  if (myStepper->GetDeviceModelInfo(version, model, serial) == PLUS_SUCCESS )
165  {
166  LOG_INFO("Version = " << version << "\tModel = " << model << "\tSerial = " << serial );
167  }
168  else
169  {
170  LOG_ERROR("Wrong answer received!");
171  return EXIT_FAILURE;
172  }
173 
174  LOG_INFO( "Getting stepper's probe reference data...");
175  double count = 0; double dist = 0; double scale = 0;
176  if (myStepper->GetProbeReferenceData(count, dist, scale))
177  {
178  LOG_INFO("Count = " << count << "\tDist = " << dist << "\tScale = " << scale << " mm" );
179  }
180  else
181  {
182  LOG_ERROR("Wrong answer received!");
183  return EXIT_FAILURE;
184  }
185 
186  LOG_INFO( "Getting stepper's grid reference data...");
187  if (myStepper->GetGridReferenceData(count, dist, scale))
188  {
189  LOG_INFO("Count = " << count << "\tDist = " << dist << "\tScale = " << scale << " mm");
190  }
191  else
192  {
193  LOG_ERROR("Wrong answer received!");
194  return EXIT_FAILURE;
195  }
196 
197  LOG_INFO( "Getting stepper's rotation reference data...");
198  if (myStepper->GetRotationReferenceData(count, dist, scale))
199  {
200  LOG_INFO("Count = " << count << "\tDist = " << dist << "\tScale = " << scale << " deg" );
201  }
202  else
203  {
204  LOG_ERROR("Wrong answer received!");
205  return EXIT_FAILURE;
206  }
207 
208  if ( myStepper->IsStepperMotorized() )
209  {
210  LOG_INFO( "Testing motorized stepper..");
211  for ( int i = 1; i <= 10; i++ )
212  {
213  int retCode;
214  myStepper->MoveProbeToPosition( -5 * i , retCode);
215  if ( retCode != 0 )
216  {
217  LOG_ERROR("Unable to move probe to position: " << -5*i << "mm");
218  }
219  else
220  {
221  LOG_INFO( "Probe moved to position: " << -5*i << "mm");
222  }
223  vtkIGSIOAccurateTimer::GetInstance()->Delay(1);
224  //vtkIGSIOAccurateTimer::Delay(1 + 0.001*samplingTimeMs);
225  }
226 
227  for ( int i = 9; i >= 0; i-- )
228  {
229  int retCode;
230  myStepper->MoveProbeToPosition( -5 * i , retCode);
231  if ( retCode != 0 )
232  {
233  LOG_ERROR("Unable to move probe to position: " << -5*i << "mm");
234  }
235  else
236  {
237  LOG_INFO( "Probe moved to position: " << -5*i << "mm");
238  }
239  vtkIGSIOAccurateTimer::GetInstance()->Delay(1);
240  //vtkIGSIOAccurateTimer::Delay(1 + 0.001*samplingTimeMs);
241  }
242  }
243 
244 
245  LOG_INFO( "Getting stepper's probe position...");
246  for (int i = 0; i<inputNumberOfTrials; i++)
247  {
248  double pposition=0, gposition=0, rposition=0;
249  unsigned long positionRequestNumber(0);
250  if (myStepper->GetEncoderValues(pposition, gposition, rposition, positionRequestNumber))
251  {
252  LOG_INFO("Probe = " << pposition << " mm\tGrid = " << gposition << " mm\tRotate = " << rposition << " deg\tRequestNumber = " << positionRequestNumber);
253  }
254  else
255  {
256  LOG_ERROR("Wrong answer received!");
257  //return EXIT_FAILURE;
258  }
259 
260  vtkIGSIOAccurateTimer::Delay(0.001*samplingTimeMs);
261  }
262 
263  LOG_INFO( "Stopping stepper...\n");
264  myStepper->Disconnect();
265  }
266 
267  LOG_INFO( "Exit successfully...");
268 
269  return EXIT_SUCCESS;
270 }
virtual PlusStatus InitializeStepper(std::string &CalibMsg)
PlusStatus GetMotorizationScaleFactor(int &scaleFactor)
virtual PlusStatus Connect()
int main(int argc, char *argv[])
for i
PlusStatus GetCalibrationState(int &PState, int &GState, int &RState)
PlusStatus GetGridReferenceData(double &count, double &dist, double &scale)
virtual PlusStatus GetDeviceModelInfo(std::string &version, std::string &model, std::string &serial)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
PlusStatus GetStatusInfo(unsigned int &Status)
PlusStatus GetProbeReferenceData(double &count, double &dist, double &scale)
virtual PlusStatus ResetStepper()
PlusStatus GetRotationReferenceData(double &count, double &dist, double &scale)
virtual PlusStatus GetEncoderValues(double &PPosition, double &GPosition, double &RPosition, unsigned long &PositionRequestNumber)
PlusStatus MoveProbeToPosition(double PositionInMm, int &ReturnCode)
Phidget_ChannelClass uint32_t * count
Definition: phidget22.h:1321
static vtkIGSIOLogger * Instance()
PlusStatus GetRotateState(int &State)
virtual PlusStatus Disconnect()
void SetBrachyStepperType(BRACHY_STEPPER_TYPE type)