15 #include "mitkIGTHardwareException.h" 18 #include <itksys/SystemTools.hxx> 19 #include <itkMutexLockHolder.h> 27 #include <vtkSphereSource.h> 31 const unsigned char CR = 0xD;
32 const unsigned char LF = 0xA;
38 m_IlluminationActivationRate(
Hz20), m_DataTransferMode(
TX), m_6DTools(), m_ToolsMutex(nullptr),
39 m_SerialCommunication(nullptr), m_SerialCommunicationMutex(nullptr), m_DeviceProtocol(nullptr),
40 m_MultiThreader(nullptr), m_ThreadID(0), m_OperationMode(
ToolTracking6D), m_MarkerPointsMutex(nullptr), m_MarkerPoints()
59 if (ndiTool ==
nullptr)
123 itkDebugMacro(
"setting PortNumber to " << _arg);
135 itkDebugMacro(
"setting eviceName to " << _arg);
147 itkDebugMacro(
"setting BaudRate to " << _arg);
159 itkDebugMacro(
"setting DataBits to " << _arg);
171 itkDebugMacro(
"setting Parity to " << _arg);
183 itkDebugMacro(
"setting StopBits to " << _arg);
195 itkDebugMacro(
"setting HardwareHandshake to " << _arg);
207 itkDebugMacro(
"setting IlluminationActivationRate to " << _arg);
219 itkDebugMacro(
"setting DataTransferMode to " << _arg);
229 if (input ==
nullptr)
235 message = *input +
CalcCRC(input) + std::string(1,
CR);
237 message = *input + std::string(1,
CR);
247 if (returnvalue == 0)
255 if (answer ==
nullptr)
261 if (returnvalue == 0)
269 if (answer ==
nullptr)
278 if ((returnvalue == 0) || (m.size() != 1))
287 if (answer ==
nullptr)
297 if ((returnvalue == 0) || (m.size() != 1))
300 }
while (m.at(0) !=
LF);
318 if (input ==
nullptr)
321 static int oddparity[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
323 unsigned int crcValue = 0;
324 unsigned int* puCRC16 = &crcValue;
325 for (
unsigned int i = 0; i < input->length(); i++)
328 data = (data ^ (*(puCRC16)& 0xff)) & 0xff;
330 if (oddparity[data & 0x0f] ^ oddparity[data >> 4])
332 *(puCRC16) ^= 0xc001;
340 char returnvalue[13];
341 sprintf(returnvalue,
"%04X", crcValue);
342 return std::string(returnvalue);
378 static const std::string
reset(
"RESETBE6F\r");
379 std::string answer =
"";
380 this->
Receive(&answer, reset.length());
382 if (reset.compare(answer) != 0)
404 itksys::SystemTools::Delay(500);
486 std::string portHandle;
488 for (
auto it =
m_6DTools.begin(); it != endIt; ++it)
494 (*it)->SetPortHandle(portHandle.c_str());
498 returnvalue =
m_DeviceProtocol->PVWR(&portHandle, (*it)->GetSROMData(), (*it)->GetSROMDataLength());
510 if ((*it)->IsEnabled() ==
true)
512 returnvalue =
m_DeviceProtocol->PENA(&portHandle, (*it)->GetTrackingPriority());
516 std::string(
"' for tool '") + (*it)->GetToolName() + std::string(
"'")).c_str();
554 std::string portHandle;
565 for (
unsigned int i = 0; i < portHandle.size(); i += 2)
567 ph = portHandle.substr(i, 2);
593 std::string(
"' for tool '") + pt->
GetToolName() + std::string(
"'")).c_str();
635 static const std::string
reset(
"RESETBE6F\r");
636 std::string answer =
"";
637 this->
Receive(&answer, reset.length());
639 if (reset.compare(answer) != 0)
688 struct itk::MultiThreader::ThreadInfoStruct * pInfo = (
struct itk::MultiThreader::ThreadInfoStruct*)pInfoStruct;
689 if (pInfo ==
nullptr)
691 return ITK_THREAD_RETURN_VALUE;
693 if (pInfo->UserData ==
nullptr)
695 return ITK_THREAD_RETURN_VALUE;
698 if (trackingDevice !=
nullptr)
712 return ITK_THREAD_RETURN_VALUE;
743 bool localStopTracking;
793 bool localStopTracking;
804 std::cout <<
"Error in POS3D: could not read data. Possibly no markers present." << std::endl;
811 itksys::SystemTools::Delay(1);
834 bool localStopTracking;
845 std::cout <<
"Error in TX: could not read data. Possibly no markers present." << std::endl;
874 for (
auto iterator =
m_6DTools.begin(); iterator != end; ++iterator)
875 if (name.compare((*iterator)->GetToolName()) == 0)
884 for (
auto iterator =
m_6DTools.begin(); iterator != end; ++iterator)
885 if (portHandle.compare((*iterator)->GetPortHandle()) == 0)
911 if (t->LoadSROMFile(fileName) ==
false)
913 t->SetToolName(toolName);
914 t->SetTrackingPriority(p);
917 return t.GetPointer();
924 NDIPassiveTool::Pointer p = tool;
929 std::string newPortHandle;
934 p->SetPortHandle(newPortHandle.c_str());
936 returnvalue =
m_DeviceProtocol->PVWR(&newPortHandle, p->GetSROMData(), p->GetSROMDataLength());
946 std::string(
"' for tool '") + p->GetToolName() + std::string(
"'")).c_str();
949 if (p->IsEnabled() ==
true)
951 returnvalue =
m_DeviceProtocol->PENA(&newPortHandle, p->GetTrackingPriority());
955 std::string(
"' for tool '") + p->GetToolName() + std::string(
"'")).c_str();
982 if (ndiTool ==
nullptr)
989 if ((portHandle.length() == 2) && (this->
GetState() ==
Ready))
998 for (
auto iterator =
m_6DTools.begin(); iterator != end; ++iterator)
1000 if (iterator->GetPointer() == ndiTool)
1013 for (
auto iterator =
m_6DTools.begin(); iterator != end; ++iterator)
1015 if ((*iterator).GetPointer() == ndiTool)
1031 for (
auto iterator =
m_6DTools.begin(); iterator != end; ++iterator)
1032 (*iterator)->SetDataValid(
false);
1054 return (markerpositions->size() != 0);
1072 std::string portHandle;
1081 for (
unsigned int i = 0; i < portHandle.size(); i += 2)
1083 std::string ph = portHandle.substr(i, 2);
1116 for (
unsigned int i = 0; i < portHandle.size(); i += 2)
1118 std::string ph = portHandle.substr(i, 2);
1127 newTool->SetPortHandle(ph.c_str());
1128 newTool->SetTrackingPriority(trackingPriority);
1131 newTool->SetToolName((std::string(
"Port ") + ph).c_str());
1139 std::string(
"' for tool '") + newTool->GetToolName() + std::string(
"'")).c_str();
1154 for (
unsigned int i = 0; i < portHandle.size(); i += 2)
1156 std::string ph = portHandle.substr(i, 2);
1158 std::string portInfo;
1160 if ((returnvaluePort ==
NDIOKAY) && (portInfo.size() > 31))
1163 itksys::SystemTools::Delay(10);
1173 std::string portHandle;
1181 if (portHandle.empty() ==
true)
1185 for (
unsigned int i = 0; i < portHandle.size(); i += 2)
1187 ph = portHandle.substr(i, 2);
1210 std::string revision;
1213 MITK_ERROR <<
"Could not receive firmware revision number!";
1217 const std::string majrevno = revision.substr(2, 3);
1219 return std::atoi(majrevno.c_str());
1224 static std::string revision;
1227 MITK_ERROR <<
"Could not receive firmware revision number!";
1229 return revision.c_str();
1231 return revision.c_str();
1237 else {
return false; }
1245 else {
return true; }
1260 MITK_WARN <<
"Warning, can not auto-detect tools! (" << e.GetDescription() <<
")";
1261 return autoDetectedStorage;
1264 for (
unsigned int i = 0; i < this->
GetToolCount(); i++)
1267 std::stringstream toolname;
1268 toolname <<
"AutoDetectedTool" << i;
1270 newTool->SetSerialNumber(dynamic_cast<mitk::NDIPassiveTool*>(this->
GetTool(i))->GetSerialNumber());
1271 newTool->SetIdentifier(toolname.str());
1273 newTool->GetDataNode()->SetName(toolname.str());
1274 autoDetectedStorage->AddTool(newTool);
1279 return autoDetectedStorage;
1284 if (numberOfVolumes ==
nullptr || volumes ==
nullptr || volumesDimensions ==
nullptr)
1287 static std::string
info;
1290 MITK_ERROR <<
"Could not receive tracking volume information of tracking system!";
1297 (*numberOfVolumes) = (
unsigned int)std::atoi(info.substr(0, 1).c_str());
1299 for (
unsigned int i = 0; i < (*numberOfVolumes); i++)
1304 std::string::size_type
offset, end;
1305 offset = (i * 73) + 1;
1306 end = 73 + (i * 73);
1307 std::string currentVolume = info.substr(offset, end);
1310 currentVolume = currentVolume.substr(1, currentVolume.size());
1317 currentVolume = currentVolume.substr(1, currentVolume.size());
1328 for (
unsigned int index = 0; index < 10; index++)
1330 std::string::size_type offD, endD;
1331 offD = 1 + (index * 7);
1333 int dimension = std::atoi(currentVolume.substr(offD, endD).c_str());
1335 volumesDimensions->push_back(dimension);
NDIProtocol::Pointer m_DeviceProtocol
create and parse NDI protocol strings
bool AutoDetectToolsAvailable() override
NDIErrorCode ReceiveByte(char *answer)
lightweight receive function, that reads just one byte
An object of this class represents an exception of the MITK-IGT module which are releated to the hard...
itk::MutexLockHolder< itk::FastMutexLock > MutexLockHolder
RotationMode m_RotationMode
defines the rotation mode Standard or Transposed, Standard is default
HardwareHandshake m_HardwareHandshake
which tracking volume is currently used (if device supports multiple volumes) (
virtual bool GetMarkerPositions(MarkerPointContainerType *markerpositions)
Get 3D marker positions (operation mode must be set to MarkerTracking3D or HybridTracking) ...
NDIPassiveTool * GetInternalTool(std::string portHandle)
returns the tool object that has been assigned the port handle or nullptr if no tool can be found ...
NDIErrorCode FreePortHandles()
free all port handles that need to be freed
std::vector< std::string > NDITrackingVolumeContainerType
vector of tracking volumes
virtual bool InternalAddTool(NDIPassiveTool *tool)
Add a passive 6D tool to the list of tracked tools. This method is used by AddTool.
virtual bool UpdateTool(mitk::TrackingTool *tool)
reloads the srom file and reinitializes the tool
bool OpenConnection() override
Set the type of the NDI Tracking Device because it can not jet handle this itself.
OperationMode
Error codes of NDI tracking devices.
virtual int GetMajorFirmwareRevisionNumber()
Get major revision number from tracking device should not be called directly after starting to track...
itk::FastMutexLock::Pointer m_TrackingFinishedMutex
mutex to manage control flow of StopTracking()
virtual void SetBaudRate(const BaudRate _arg)
returns the device name for serial communication
NDIErrorCode Send(const std::string *message, bool addCRC=true)
Send message to tracking device.
int m_ThreadID
ID of tracking thread.
IlluminationActivationRate m_IlluminationActivationRate
update rate of IR illuminator for Polaris
IlluminationActivationRate
activation rate of IR illuminator for NDI Polaris tracking device
NDIErrorCode Receive(std::string *answer, unsigned int numberOfBytes)
receive numberOfBytes bytes from tracking device
An object of this class represents an exception of the MITK-IGT module.
virtual bool RemoveTool(TrackingTool *tool)
Remove a passive 6D tool from the list of tracked tools.
virtual void TrackTools()
TrackTools() continuously polls serial interface for new 6d tool positions until StopTracking is call...
DataCollection - Class to facilitate loading/accessing structured data.
virtual void SetDataBits(const DataBits _arg)
returns the baud rate for serial communication
virtual void SetDataTransferMode(const DataTransferMode _arg)
returns the activation rate of IR illumator for polaris
Tool6DContainerType m_6DTools
list of 6D tools
PortNumber m_PortNumber
COM Port Number.
virtual void TrackToolsAndMarkers()
continuously polls serial interface for new 3D marker positions and 6D tool positions until StopTrack...
virtual bool GetSupportedVolumes(unsigned int *numberOfVolumes, NDITrackingVolumeContainerType *volumes, TrackingVolumeDimensionType *volumesDimensions)
Get number of supported tracking volumes, a vector containing the supported volumes and a vector cont...
static ITK_THREAD_RETURN_TYPE ThreadStartTracking(void *data)
static start method for the tracking thread.
virtual void InvalidateAll()
invalidate all tools
virtual void SetIlluminationActivationRate(const IlluminationActivationRate _arg)
returns the hardware handshake setting
static void info(const char *fmt,...)
TrackingDeviceState GetState() const
return current object state (Setup, Ready or Tracking)
virtual OperationMode GetOperationMode()
get current operation mode
itk::MultiThreader::Pointer m_MultiThreader
creates tracking thread that continuously polls serial interface for new tracking data ...
NDIErrorCode ReceiveLine(std::string *answer)
receive characters until the first LF (The LF is included in the answer string)
virtual bool SetVolume(mitk::TrackingDeviceData volume)
Sets the desired tracking volume. Returns true if the volume type could be set. It is set in the Open...
virtual void SetStopBits(const StopBits _arg)
returns the parity mode
bool DiscoverWiredTools()
retrieves all wired tools from the tracking device
bool AddSingleToolIsAvailable() override
const std::string CalcCRC(const std::string *input)
returns the CRC16 for input as a std::string
DataTransferMode m_DataTransferMode
use TX (text) or BX (binary) (
virtual void SetParity(const Parity _arg)
returns the data bits for serial communication
std::vector< int > TrackingVolumeDimensionType
List of the supported tracking volume dimensions.
mitk::SerialCommunication::Pointer m_SerialCommunication
serial communication interface
std::string m_DeviceName
Device Name.
virtual bool StopTracking()
stop retrieving tracking data from the device. stop retrieving tracking data from the device...
void ClearReceiveBuffer()
empty receive buffer of serial communication interface
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
mitk::NavigationToolStorage::Pointer AutoDetectTools() override
bool m_StopTracking
signal stop to tracking thread
Interface for all Tracking Devices.
virtual mitk::TrackingDeviceType TestConnection()
TestConnection() tries to connect to a NDI tracking device on the current port/device and returns whi...
NDITrackingDevice()
Constructor.
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
std::vector< MarkerPointType > MarkerPointContainerType
virtual void TrackMarkerPositions()
continuously polls serial interface for new 3D marker positions until StopTracking is called...
itk::FastMutexLock::Pointer m_MarkerPointsMutex
mutex for marker point data container
void Start(itk::Object::Pointer device)
starts the time-acquisition
virtual void SetHardwareHandshake(const HardwareHandshake _arg)
returns the number of stop bits
OperationMode m_OperationMode
tracking mode (6D tool tracking, 3D marker tracking,...)
TrackingTool * GetTool(unsigned int toolNumber) const override
return the tool with index toolNumber
virtual const char * GetFirmwareRevisionNumber()
Get revision number from tracking device as string should not be called directly after starting to tr...
serial communication interface
void ClearSendBuffer()
empty send buffer of serial communication interface
Parity m_Parity
Parity mode for communication.
virtual void SetPortNumber(const PortNumber _arg)
set port number for serial communication
TrackingDeviceData m_Data
current device Data
wbAdvisor reset(new berry::WorkbenchAdvisor)
#define mitkThrowException(classname)
std::string TrackingDeviceType
itk::FastMutexLock::Pointer m_StopTrackingMutex
mutex to control access to m_StopTracking
unsigned int GetToolCount() const override
return current number of tools
mitk::TrackingTool * AddTool(const char *toolName, const char *fileName, TrackingPriority p=NDIPassiveTool::Dynamic)
Create a passive 6D tool with toolName and fileName and add it to the list of tools.
void SetRotationMode(RotationMode r) override
BaudRate m_BaudRate
COM Port Baud Rate.
superclass for specific NDI tracking Devices that use serial communication.
DataBits m_DataBits
Number of Data Bits per token.
virtual bool Beep(unsigned char count)
returns the data transfer mode
NDIErrorCode
Error codes of NDI tracking devices.
~NDITrackingDevice() override
Destructor.
MarkerPointContainerType m_MarkerPoints
container for markers (3D point tracking mode)
DataTransferMode
Data transfer mode for NDI tracking devices.
virtual void SetDeviceName(std::string _arg)
returns the port number for serial communication
bool CloseConnection() override
Closes the connection.
virtual bool SetOperationMode(OperationMode mode)
set operation mode to 6D tool tracking, 3D marker tracking or 6D&3D hybrid tracking (see OperationMod...
StopBits m_StopBits
number of stop bits per token
void SetState(TrackingDeviceState state)
change object state
bool InitializeWiredTools()
itk::FastMutexLock::Pointer m_SerialCommunicationMutex
mutex for coordinated access of serial communication interface
TrackingDeviceType GetType() const
Deprecated! Use the more specific getData or GetTrackingDeviceName instead. return device type identi...
bool StartTracking() override
Start the tracking.
itk::FastMutexLock::Pointer m_ToolsMutex
mutex for coordinated access of tool container
mitk::TrackingTool * GetToolByName(std::string name) const override
Returns the tool with the given tool name.
void SetType(TrackingDeviceType type)
Deprecated! Use the more specific setDeviceData instead. set device type.