1
/*=========================================================================
3
Program: Image Guided Surgery Software Toolkit
4
Module: $RCSfile: igstkTrackerController.h,v $
6
Date: $Date: 2009-04-20 18:23:54 $
7
Version: $Revision: 1.4 $
9
Copyright (c) ISC Insight Software Consortium. All rights reserved.
10
See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.
12
This software is distributed WITHOUT ANY WARRANTY; without even
13
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14
PURPOSE. See the above copyright notices for more information.
16
=========================================================================*/
18
#ifndef __igstkTrackerController_h
19
#define __igstkTrackerController_h
21
#include "igstkConfigure.h"
23
#include "igstkTrackerConfiguration.h"
24
#include "igstkPolarisTrackerConfiguration.h"
25
#include "igstkAuroraTrackerConfiguration.h"
26
#include "igstkAscensionTrackerConfiguration.h"
28
#include "igstkMacros.h"
29
#include "igstkEvents.h"
30
#include "igstkObject.h"
31
#include "igstkSerialCommunication.h"
32
#include "igstkTransform.h"
34
#include "igstkTracker.h"
35
#include "igstkTrackerTool.h"
36
#include "igstkPolarisTrackerTool.h"
37
#include "igstkAuroraTrackerTool.h"
38
#include "igstkAscensionTrackerTool.h"
40
#include "igstkSpatialObject.h"
42
#ifdef IGSTK_USE_MicronTracker
43
#include "igstkMicronTrackerConfiguration.h"
44
#include "igstkMicronTrackerTool.h"
51
/** \class TrackerController
53
* \brief This class encapsulates the startup and shutdown procedures required
54
* for the tracker classes supported by IGSTK.
56
* This class enables the user to intialize a tracker with minimal effort.
57
* Initialization is performed by passing the controller a TrackerConfiguration
58
* object. The controller identifies the specific tracker internally. The
59
* user can then request to intialize the tracker or to tell the tracker
60
* controller to shut down a tracker that is running.
62
* NOTE: This class is usefull only when all tools are connected to the tracker
63
* in advance (no swapping tools in and out after initialization).
65
class TrackerController : public Object
68
/**This is the container type in which the tools are found.*/
69
typedef std::map < std::string, igstk::TrackerTool::Pointer >
72
typedef std::pair < std::string, igstk::TrackerTool::Pointer >
75
typedef Transform TransformType;
77
typedef SpatialObject SpatialObjectType;
79
/** Macro with standard traits declarations (Self, SuperClass, State
81
igstkStandardClassTraitsMacro( TrackerController, Object )
84
* Initialize a tracker using the given configuration. Communication with the
85
* haredware is established and the tracker is initialized.
86
* NOTE: If a tracker was already initialized using this object it will be
87
* shut down (stop tracking, close communication, and if using serial
88
* communication that will be closed too).
90
* @param configuration A specific tracker configuration (Aurora/Micron...)
91
* which is identified at runtime.
92
* This method generates two events, InitializeEvent if everything
93
* went well and, InitializeErrorEvent if the tracker could not be
94
* initialized (the specific error is described in the string contained in the
97
void RequestInitialize(const TrackerConfiguration *configuration);
100
* Start the tracking. The method generates igstk::TrackerStartTrackingEvent
101
* and igstk::TrackerStartTrackingErrorEvent (see igstkTracker.h).
103
void RequestStartTracking();
106
* Stop the tracking. The method generates igstk::TrackerStopTrackingEvent and
107
* igstk::TrackerStopTrackingErrorEvent (see igstkTracker.h).
109
void RequestStopTracking();
112
* This method stops the tracking and closes communication.
113
* The method generates igstk::TrackerStopTrackingEvent or
114
* igstk::TrackerStopTrackingErrorEvent followed by
115
* CloseCommunicationEvent or CloseCommunicationErrorEvent.
117
void RequestShutdown();
120
* Get the container (ToolContainerType) with all the tools.
121
* The method generates a RequestToolsEvent if the tracker was previously
122
* initialized successfuly, otherwise it generates an
123
* InvalidRequestErrorEvent.*/
124
void RequestGetNonReferenceToolList();
127
* Get a specific tracker tool according to its unique name.
128
* The method generates a RequestToolEvent if the tracker was
129
* previously initialized successfuly and the tool exists, otherwise it
130
* generates a RequestToolErrorEvent if the tool doesn't exist, or
131
* InvalidRequestErrorEvent if the tracker wasn't initialized.*/
132
void RequestGetTool( const std::string &toolName );
135
* Get the reference tool it it exists.
136
* The method generates a RequestToolEvent if the tracker was
137
* previously initialized successfuly, otherwise it generates an
138
* InvalidRequestErrorEvent.
139
* Note that when no reference tool is used the payload of the event
140
* will be a NULL pointer.*/
141
void RequestGetReferenceTool();
145
* Sets the tracker's parent spatial object
147
void RequestSetParentSpatialObject( TransformType transform,
148
SpatialObjectType * spatialObject);
151
* Adds a Spatial Object as a child of the tracker. For example, if you want
152
* to display the tracker's working volume (e.g. frustum or cube).*/
153
void RequestAddChildSpatialObject( TransformType transform,
154
SpatialObjectType * spatialObject);
156
/** This event is generated if the initialization succeeds. */
157
igstkEventMacro( InitializeEvent, IGSTKEvent );
159
/** This event is generated if the initialization fails.*/
160
igstkEventMacro( InitializeErrorEvent, IGSTKErrorWithStringEvent );
162
/** This event is generated if the communication was closed successfully. */
163
igstkEventMacro( CloseCommunicationEvent, IGSTKEvent );
165
/** This event is generated if closing communication failed.*/
166
igstkEventMacro( CloseCommunicationErrorEvent, IGSTKErrorWithStringEvent );
168
/** This event is generated if the user requests the tracker tools and the
169
* tracker is initialized. */
170
igstkLoadedEventMacro( RequestToolsEvent,
174
/** This event is generated if the user requests a specific tracker tool and
175
* the tracker is initialized. */
176
igstkLoadedEventMacro( RequestToolEvent,
180
/** This event is generated if the user requested a specific tracker tool and
181
* the tool does not exist.*/
182
igstkEventMacro( RequestToolErrorEvent,
187
TrackerController( void );
188
virtual ~TrackerController( void );
193
/** List of state machine states */
194
igstkDeclareStateMacro( Idle );
195
igstkDeclareStateMacro( AttemptingToInitialize );
196
igstkDeclareStateMacro( AttemptingToInitializePolarisVicra );
197
igstkDeclareStateMacro( AttemptingToInitializePolarisHybrid );
198
igstkDeclareStateMacro( AttemptingToInitializeAurora );
199
igstkDeclareStateMacro( AttemptingToInitializeAscension );
200
igstkDeclareStateMacro( AttemptingToInitializeMicron );
201
igstkDeclareStateMacro( Initialized );
202
igstkDeclareStateMacro( AttemptingToStartTracking );
203
igstkDeclareStateMacro( Tracking );
204
igstkDeclareStateMacro( AttemptingToCloseCommunication );
205
igstkDeclareStateMacro( AttemptingToStopTracking );
207
/** List of state machine inputs */
208
igstkDeclareInputMacro( TrackerInitialize );
209
igstkDeclareInputMacro( PolarisVicraInitialize );
210
igstkDeclareInputMacro( PolarisHybridInitialize );
211
igstkDeclareInputMacro( AuroraInitialize );
212
igstkDeclareInputMacro( AscensionInitialize );
213
igstkDeclareInputMacro( MicronInitialize );
214
igstkDeclareInputMacro( StartTracking );
215
igstkDeclareInputMacro( StopTracking );
216
igstkDeclareInputMacro( Failed );
217
igstkDeclareInputMacro( Succeeded );
218
igstkDeclareInputMacro( CloseCommunication );
219
igstkDeclareInputMacro( GetTools );
220
igstkDeclareInputMacro( GetTool );
221
igstkDeclareInputMacro( GetReferenceTool );
222
igstkDeclareInputMacro( SetParentSpatialObject );
223
igstkDeclareInputMacro( SetChildSpatialObject );
226
/**List of state machine actions*/
227
void TrackerInitializeProcessing();
228
void PolarisVicraInitializeProcessing();
229
void PolarisHybridInitializeProcessing();
230
void AuroraInitializeProcessing();
231
void AscensionInitializeProcessing();
232
void MicronInitializeProcessing();
233
void StartTrackingProcessing();
234
void StopTrackingProcessing();
235
void CloseCommunicationProcessing();
236
void GetToolsProcessing();
237
void GetToolProcessing();
238
void GetReferenceToolProcessing();
239
void SetParentSpatialObjectProcessing();
240
void SetChildSpatialObjectProcessing();
242
void ReportInvalidRequestProcessing();
244
void ReportInitializationFailureProcessing();
245
void ReportInitializationSuccessProcessing();
247
void ReportStartTrackingFailureProcessing();
248
void ReportStartTrackingSuccessProcessing();
250
void ReportStopTrackingFailureProcessing();
251
void ReportStopTrackingSuccessProcessing();
253
void ReportCloseCommunicationFailureProcessing();
254
void ReportCloseCommunicationSuccessProcessing();
256
bool InitializeSerialCommunication();
257
PolarisTrackerTool::Pointer InitializePolarisWirelessTool(
258
const PolarisWirelessToolConfiguration *toolConfiguration );
259
PolarisTrackerTool::Pointer InitializePolarisWiredTool(
260
const PolarisWiredToolConfiguration *toolConfiguration );
261
AuroraTrackerTool::Pointer InitializeAuroraTool(
262
const AuroraToolConfiguration *toolConfiguration );
264
#ifdef IGSTK_USE_MicronTracker
266
MicronTrackerTool::Pointer InitializeMicronTool(
267
const MicronToolConfiguration *toolConfiguration );
270
AscensionTrackerTool::Pointer InitializeAscensionTool(
271
const AscensionToolConfiguration *toolConfiguration );
275
* \class Observer for the TrackerToolAttachmentToTrackerErrorEvent.
277
class ToolAttachErrorObserver : public ::itk::Command
280
typedef ToolAttachErrorObserver Self;
281
typedef ::itk::Command Superclass;
282
typedef ::itk::SmartPointer<Self> Pointer;
285
void Execute(itk::Object *caller, const itk::EventObject & event)
287
const itk::Object * constCaller = caller;
288
this->Execute( constCaller, event );
292
void Execute(const itk::Object *caller, const itk::EventObject & event)
294
if( dynamic_cast<const TrackerToolAttachmentToTrackerErrorEvent *> (
301
bool GotToolAttachError() const
311
ToolAttachErrorObserver()
315
~ToolAttachErrorObserver() { }
320
class ErrorObserver : public itk::Command
323
typedef ErrorObserver Self;
324
typedef ::itk::Command Superclass;
325
typedef ::itk::SmartPointer<Self> Pointer;
326
typedef ::itk::SmartPointer<const Self> ConstPointer;
329
igstkTypeMacro(ErrorObserver, itk::Command)
331
/** When an error occurs in an IGSTK component it will invoke this method
332
* with the appropriate error event object as a parameter.*/
333
virtual void Execute(itk::Object *caller,
334
const itk::EventObject & event) throw (std::exception);
336
/** When an error occurs in an IGSTK component it will invoke this method
337
* with the appropriate error event object as a parameter.*/
338
virtual void Execute(const itk::Object *caller,
339
const itk::EventObject & event) throw (std::exception);
341
/**Clear the current error.*/
342
void ClearError() { this->m_ErrorOccured = false;
343
this->m_ErrorMessage.clear(); }
344
/**If an error occurs in one of the observed IGSTK components this method
345
* will return true.*/
346
bool ErrorOccured() { return this->m_ErrorOccured; }
347
/**Get the error message associated with the last IGSTK error.*/
348
void GetErrorMessage(std::string &errorMessage)
350
errorMessage = this->m_ErrorMessage;
355
/**Construct an error observer for all the possible errors that occur in
356
* the observed IGSTK components.*/
358
virtual ~ErrorObserver(){}
361
std::string m_ErrorMessage;
362
std::map<std::string,std::string> m_ErrorEvent2ErrorMessage;
364
//purposely not implemented
365
ErrorObserver(const Self&);
366
void operator=(const Self&);
369
class TrackerUpdateObserver : public itk::Command
373
typedef TrackerUpdateObserver Self;
374
typedef ::itk::Command Superclass;
375
typedef ::itk::SmartPointer<Self> Pointer;
376
typedef ::itk::SmartPointer<const Self> ConstPointer;
378
void SetParent( TrackerController *parent );
381
igstkTypeMacro(TrackerUpdateObserver, itk::Command)
383
/** When an error occurs in an IGSTK component it will invoke this method
384
* with the appropriate error event object as a parameter.*/
385
virtual void Execute(itk::Object *caller,
386
const itk::EventObject & event) throw (std::exception);
388
/** When an error occurs in an IGSTK component it will invoke this method
389
* with the appropriate error event object as a parameter.*/
390
virtual void Execute(const itk::Object *caller,
391
const itk::EventObject & event) throw (std::exception);
394
TrackerController *m_parent;
397
//this object observes the tracker's igstk::TrackerUpdateStatusEvent and
398
//igstk::TrackerUpdateStatusErrorEvent it then causes its parent to invoke
399
//these events too. In this manner the user is inderictly observing the
401
TrackerUpdateObserver::Pointer m_TrackerUpdateStatusObserver;
403
ErrorObserver::Pointer m_ErrorObserver;
404
std::string m_ErrorMessage;
406
SpatialObjectType *m_TmpSpatialObject;
407
TransformType m_TmpTransform;
409
TrackerConfiguration *m_TmpTrackerConfiguration;
410
TrackerConfiguration *m_TrackerConfiguration;
411
std::string m_RequestedToolName;
413
Tracker::Pointer m_Tracker;
414
std::map<std::string, TrackerTool::Pointer> m_Tools;
415
TrackerTool::Pointer m_ReferenceTool;
416
SerialCommunication::Pointer m_SerialCommunication;
419
} // end of namespace