PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkOpenHapticsDeviceTest.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"
10 #include "vtkSmartPointer.h"
11 #include "vtkCommand.h"
12 #include "vtkCallbackCommand.h"
13 
14 void PrintLogsCallback(vtkObject* obj, unsigned long eid, void* clientdata, void* calldata);
15 
16 
17 int main(int argc, char **argv)
18 {
19  std::string deviceName = "Plus";
20  double frameRate=20;
21  bool printHelp(false);
22 
23  vtksys::CommandLineArguments args;
24  args.Initialize(argc, argv);
25 
26  int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
27 
28  args.AddArgument("--device-name", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &deviceName, "Name of the OpenHaptics device to connect");
29  args.AddArgument("--frame-rate", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &frameRate, "Requested acquisition frame rate (in FPS, default = 30)");
30  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
31  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
32 
33  if ( !args.Parse() )
34  {
35  std::cerr << "Problem parsing arguments" << std::endl;
36  std::cout << "\n\nvtkPlusOpenHapticsDeviceTest help:" << args.GetHelp() << std::endl;
37  exit(EXIT_FAILURE);
38  }
39 
40  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
41 
42  if ( printHelp )
43  {
44  std::cout << "\n\nvtkPlusOpenHapticsDeviceTest help:" << args.GetHelp() << std::endl;
45  exit(EXIT_SUCCESS);
46  }
47 
48  vtkSmartPointer<vtkPlusOpenHapticsDevice> openHapticsDevice = vtkSmartPointer<vtkPlusOpenHapticsDevice>::New();
49 
50  openHapticsDevice->SetAcquisitionRate(frameRate);
51  openHapticsDevice->SetDeviceName(deviceName);
52  // Add an observer to warning and error events for redirecting it to the stdout
53  vtkSmartPointer<vtkCallbackCommand> callbackCommand = vtkSmartPointer<vtkCallbackCommand>::New();
54  callbackCommand->SetCallback(PrintLogsCallback);
55  openHapticsDevice->AddObserver("WarningEvent", callbackCommand);
56  openHapticsDevice->AddObserver("ErrorEvent", callbackCommand);
57 
58  LOG_INFO("Initialize...");
59 
60  if ( openHapticsDevice->Connect()!=PLUS_SUCCESS )
61  {
62  LOG_ERROR( "Unable to connect to device");
63  exit(EXIT_FAILURE);
64  }
65 
66  LOG_INFO("Start recording...");
67  if ( openHapticsDevice->StartRecording()!=PLUS_SUCCESS )
68  {
69  LOG_ERROR( "Unable to connect to device");
70  exit(EXIT_FAILURE);
71  }
72 
73 
74  openHapticsDevice->StopRecording();
75  openHapticsDevice->Disconnect();
76  LOG_INFO("Exit successfully");
77  exit(EXIT_SUCCESS);
78 }
79 
80 // Callback function for error and warning redirects
81 void PrintLogsCallback(vtkObject* obj, unsigned long eid, void* clientdata, void* calldata)
82 {
83  if (eid == vtkCommand::GetEventIdFromString("WarningEvent"))
84  {
85  LOG_WARNING((const char*)calldata);
86  }
87  else if (eid == vtkCommand::GetEventIdFromString("ErrorEvent"))
88  {
89  LOG_ERROR((const char*)calldata);
90  }
91 }
int main(int argc, char **argv)
const char ** deviceName
Definition: phidget22.h:1316
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
static vtkIGSIOLogger * Instance()
void PrintLogsCallback(vtkObject *obj, unsigned long eid, void *clientdata, void *calldata)