12 #define _USE_MATH_DEFINES 20 #include <vtkDoubleArray.h> 21 #include <QCoreApplication> 49 connect(
settings(), SIGNAL(valueChangedFor(QString)),
this, SLOT(globalConfigurationFileChangedSlot(QString)));
51 this->setConfigurationFile(
profile()->getToolConfigFilePath());
56 this->destroyTrackerThread();
69 void TrackingSystemIGSTKService::configure()
82 CX_LOG_DEBUG() <<
"TrackingSystemIGSTKService::configure(): Not using IGSTK tracking.";
86 std::vector<ToolFileParser::TrackerInternalStructure> trackers = configParser.
getTrackers();
97 std::vector<ToolFileParser::ToolInternalStructurePtr> toolStructures;
100 for (std::vector<QString>::iterator it = toolfiles.begin(); it != toolfiles.end(); ++it)
104 if ((*it) == referenceToolFile)
105 referenceToolStructure = internalTool;
107 toolStructures.push_back(internalTool);
111 mTrackerThread.reset(
new IgstkTrackerThread(trackerStructure, toolStructures, referenceToolStructure));
113 connect(mTrackerThread.get(), SIGNAL(
configured(
bool)),
this, SLOT(trackerConfiguredSlot(
bool)));
114 connect(mTrackerThread.get(), SIGNAL(
initialized(
bool)),
this, SLOT(initializedSlot(
bool)));
115 connect(mTrackerThread.get(), SIGNAL(tracking(
bool)),
this, SLOT(trackerTrackingSlot(
bool)));
116 connect(mTrackerThread.get(), SIGNAL(error()),
this, SLOT(uninitialize()));
120 mTrackerThread->start();
123 void TrackingSystemIGSTKService::trackerConfiguredSlot(
bool on)
133 reportDebug(
"Received a configured signal in ToolManager, but we don't have a mTrackerThread, this should never happen, contact programmer.");
139 std::map<QString, IgstkToolPtr> igstkTools = mTrackerThread->getTools();
140 IgstkToolPtr reference = mTrackerThread->getRefereceTool();
141 std::map<QString, IgstkToolPtr>::iterator it = igstkTools.begin();
142 for (; it != igstkTools.end(); ++it)
148 mTools.push_back(tool);
151 reportWarning(
"Creation of the cxTool " + it->second->getUid() +
" failed.");
166 void TrackingSystemIGSTKService::deconfigure()
173 connect(
this, SIGNAL(
uninitialized()),
this, SLOT(deconfigureAfterUninitializedSlot()));
174 this->uninitialize();
179 this->destroyTrackerThread();
186 report(
"IGSTK Tracking Service is deconfigured.");
189 void TrackingSystemIGSTKService::initialize()
193 connect(
this, SIGNAL(
configured()),
this, SLOT(initializeAfterConfigSlot()));
200 reportWarning(
"Please configure before trying to initialize.");
205 if (!this->createSymlink())
213 mTrackerThread->initialize(
true);
215 reportError(
"Cannot initialize the tracking system because the tracking thread does not exist.");
218 void TrackingSystemIGSTKService::uninitialize()
222 connect(
this, SIGNAL(
trackingStopped()),
this, SLOT(uninitializeAfterTrackingStoppedSlot()));
223 this->stopTracking();
232 mTrackerThread->initialize(
false);
241 bool TrackingSystemIGSTKService::createSymlink()
244 QFileInfo symlink = this->getSymlink();
245 QDir linkDir(symlink.absolutePath());
246 QString linkfile = symlink.absoluteFilePath();
249 if (!linkDir.exists())
252 QString(
"Folder %1 does not exist. It is required to exist and be writable in order to connect to IGSTK. Create it and set the correct permission.").arg(linkDir.path()));
256 QDir devDir(
"/dev/");
260 filters <<
"cu.usbserial*" <<
"cu.KeySerial*" <<
"serial" <<
"ttyUSB*";
262 QStringList files = devDir.entryList(filters, QDir::System);
267 QString(
"No usb connections found in /dev using filters %1").arg(filters.join(
";")));
272 report(QString(
"Device files: %1").arg(files.join(
",")));
273 if (files.size() > 1)
275 QString(
"More than one tracker connected? Will only try to connect to: %1").arg(files[0]));
278 QString device = devDir.filePath(files[0]);
281 QFile(linkfile).remove();
282 QFile devFile(device);
283 QFileInfo devFileInfo(device);
284 if (!devFileInfo.isWritable())
286 reportError(QString(
"Device %1 is not writable. Connection will fail.").arg(device));
290 bool val = devFile.link(linkfile);
294 QString(
"Symlink %1 creation to device %2 failed with code %3").arg(linkfile).arg(device).arg(
300 report(QString(
"Created symlink %1 to device %2").arg(linkfile).arg(device));
303 devFile.setPermissions(
304 QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner | QFile::ReadGroup | QFile::WriteGroup
305 | QFile::ExeGroup | QFile::ReadOther | QFile::WriteOther | QFile::ExeOther);
309 QFileInfo TrackingSystemIGSTKService::getSymlink()
const 311 QString name(
"/Library/CustusX/igstk.links");
313 QDir::root().mkdir(name);
314 QString linkFile = linkDir.path() +
"/cu.CustusX.dev0";
315 return QFileInfo(linkDir, linkFile);
320 void TrackingSystemIGSTKService::cleanupSymlink()
322 report(
"Cleaning up symlinks.");
323 QFile(this->getSymlink().absoluteFilePath()).remove();
327 void TrackingSystemIGSTKService::startTracking()
331 connect(
this, SIGNAL(
initialized()),
this, SLOT(startTrackingAfterInitSlot()));
337 mTrackerThread->track(
true);
340 void TrackingSystemIGSTKService::stopTracking()
347 mTrackerThread->track(
false);
350 void TrackingSystemIGSTKService::setConfigurationFile(QString configurationFile)
357 connect(
this, SIGNAL(
deconfigured()),
this, SLOT(configureAfterDeconfigureSlot()));
371 connect(
this, SIGNAL(
deconfigured()),
this, SLOT(configureAfterDeconfigureSlot()));
378 void TrackingSystemIGSTKService::initializedSlot(
bool value)
390 report(
"IGSTK Tracking Service is uninitialized.");
396 void TrackingSystemIGSTKService::trackerTrackingSlot(
bool value)
414 void TrackingSystemIGSTKService::startTrackingAfterInitSlot()
416 disconnect(
this, SIGNAL(
initialized()),
this, SLOT(startTrackingAfterInitSlot()));
417 this->startTracking();
420 void TrackingSystemIGSTKService::initializeAfterConfigSlot()
422 disconnect(
this, SIGNAL(
configured()),
this, SLOT(initializeAfterConfigSlot()));
426 void TrackingSystemIGSTKService::uninitializeAfterTrackingStoppedSlot()
428 disconnect(
this, SIGNAL(
trackingStopped()),
this, SLOT(uninitializeAfterTrackingStoppedSlot()));
429 this->uninitialize();
432 void TrackingSystemIGSTKService::deconfigureAfterUninitializedSlot()
434 disconnect(
this, SIGNAL(
uninitialized()),
this, SLOT(deconfigureAfterUninitializedSlot()));
438 void TrackingSystemIGSTKService::configureAfterDeconfigureSlot()
440 disconnect(
this, SIGNAL(
deconfigured()),
this, SLOT(configureAfterDeconfigureSlot()));
444 void TrackingSystemIGSTKService::globalConfigurationFileChangedSlot(QString key)
446 if (key ==
"toolConfigFile")
448 this->setConfigurationFile(
profile()->getToolConfigFilePath());
460 void TrackingSystemIGSTKService::destroyTrackerThread()
464 mTrackerThread->quit();
465 mTrackerThread->wait(2000);
466 if (mTrackerThread->isRunning())
468 mTrackerThread->terminate();
469 mTrackerThread->wait();
471 QObject::disconnect(mTrackerThread.get());
472 mTrackerThread.reset();
cxResource_EXPORT ProfilePtr profile()
void reportError(QString msg)
TrackingSystemIGSTKService()
void uninitialized()
system is uninitialized
virtual TrackerConfigurationPtr getConfiguration()
QString getTrackingSystemImplementation()
boost::shared_ptr< class IgstkTool > IgstkToolPtr
QString mConfigurationFilePath
path to the configuration file
virtual void setLoggingFolder(QString loggingFolder)
QString getAbsoluteReferenceFilePath()
const char * TRACKING_SYSTEM_IMPLEMENTATION_IGSTK
boost::shared_ptr< class TrackerConfiguration > TrackerConfigurationPtr
QString mLoggingFolder
path to where logging should be saved
void reportWarning(QString msg)
void trackingStarted()
system starts tracking
Class for reading the files defining a CustusX tool.
Settings * settings()
Shortcut for accessing the settings instance.
virtual bool isInitialized() const
void reportSuccess(QString msg)
void trackingStopped()
system stops tracking
std::vector< QString > getAbsoluteToolFilePaths()
virtual bool isConfigured() const
virtual bool isTracking() const
std::vector< ToolFileParser::TrackerInternalStructure > getTrackers()
void initialized()
system is initialized
Thread containing all of IGSTK.
void configured()
system is configured
boost::shared_ptr< ToolUsingIGSTK > ToolUsingIGSTKPtr
void internalSetState(Tool::State val)
virtual ~TrackingSystemIGSTKService()
virtual std::vector< ToolPtr > getTools()
virtual void setState(const Tool::State val)
asynchronously request a state. Wait for signal stateChanged()
void reportDebug(QString msg)
Namespace for all CustusX production code.