33 #define _USE_MATH_DEFINES
41 #include <vtkDoubleArray.h>
42 #include <QCoreApplication>
77 mConfigurationFilePath(
""),
81 connect(
settings(), SIGNAL(valueChangedFor(QString)),
this, SLOT(globalConfigurationFileChangedSlot(QString)));
83 this->setConfigurationFile(
profile()->getToolConfigFilePath());
88 this->destroyTrackerThread();
109 this->startTracking();
118 this->stopTracking();
120 this->uninitialize();
128 bool TrackingSystemIGSTKService::isConfigured()
const
133 bool TrackingSystemIGSTKService::isInitialized()
const
138 bool TrackingSystemIGSTKService::isTracking()
const
143 void TrackingSystemIGSTKService::configure()
145 if (mConfigurationFilePath.isEmpty() || !QFile::exists(mConfigurationFilePath))
147 reportWarning(QString(
"Configuration file [%1] is not valid, could not configure the toolmanager.").arg(mConfigurationFilePath));
152 ConfigurationFileParser configParser(mConfigurationFilePath, mLoggingFolder);
154 std::vector<IgstkTracker::InternalStructure> trackers = configParser.getTrackers();
156 if (trackers.empty())
162 IgstkTracker::InternalStructure trackerStructure = trackers[0];
164 IgstkTool::InternalStructure referenceToolStructure;
165 std::vector<IgstkTool::InternalStructure> toolStructures;
166 QString referenceToolFile = configParser.getAbsoluteReferenceFilePath();
167 std::vector<QString> toolfiles = configParser.getAbsoluteToolFilePaths();
168 for (std::vector<QString>::iterator it = toolfiles.begin(); it != toolfiles.end(); ++it)
170 ToolFileParser toolParser(*it, mLoggingFolder);
171 IgstkTool::InternalStructure internalTool = toolParser.getTool();
172 if ((*it) == referenceToolFile)
173 referenceToolStructure = internalTool;
175 toolStructures.push_back(internalTool);
179 mTrackerThread.reset(
new IgstkTrackerThread(trackerStructure, toolStructures, referenceToolStructure));
181 connect(mTrackerThread.get(), SIGNAL(
configured(
bool)),
this, SLOT(trackerConfiguredSlot(
bool)));
182 connect(mTrackerThread.get(), SIGNAL(
initialized(
bool)),
this, SLOT(initializedSlot(
bool)));
183 connect(mTrackerThread.get(), SIGNAL(tracking(
bool)),
this, SLOT(trackerTrackingSlot(
bool)));
184 connect(mTrackerThread.get(), SIGNAL(error()),
this, SLOT(uninitialize()));
188 mTrackerThread->start();
191 void TrackingSystemIGSTKService::trackerConfiguredSlot(
bool on)
201 reportDebug(
"Received a configured signal in ToolManager, but we don't have a mTrackerThread, this should never happen, contact programmer.");
207 std::map<QString, IgstkToolPtr> igstkTools = mTrackerThread->getTools();
208 IgstkToolPtr reference = mTrackerThread->getRefereceTool();
209 std::map<QString, IgstkToolPtr>::iterator it = igstkTools.begin();
210 for (; it != igstkTools.end(); ++it)
213 cxToolPtr tool(
new ToolUsingIGSTK(igstkTool));
216 mTools.push_back(tool);
217 if (tool->getProbe())
221 reportWarning(
"Creation of the cxTool " + it->second->getUid() +
" failed.");
236 void TrackingSystemIGSTKService::deconfigure()
238 if (!this->isConfigured())
241 if (this->isInitialized())
243 connect(
this, SIGNAL(
uninitialized()),
this, SLOT(deconfigureAfterUninitializedSlot()));
244 this->uninitialize();
249 this->destroyTrackerThread();
256 report(
"IGSTK Tracking Service is deconfigured.");
259 void TrackingSystemIGSTKService::initialize()
261 if (!this->isConfigured())
263 connect(
this, SIGNAL(
configured()),
this, SLOT(initializeAfterConfigSlot()));
268 if (!this->isConfigured())
270 reportWarning(
"Please configure before trying to initialize.");
275 if (!this->createSymlink())
283 mTrackerThread->initialize(
true);
285 reportError(
"Cannot initialize the tracking system because the tracking thread does not exist.");
288 void TrackingSystemIGSTKService::uninitialize()
290 if (this->isTracking())
292 connect(
this, SIGNAL(
trackingStopped()),
this, SLOT(uninitializeAfterTrackingStoppedSlot()));
293 this->stopTracking();
297 if (!this->isInitialized())
302 mTrackerThread->initialize(
false);
311 bool TrackingSystemIGSTKService::createSymlink()
314 QFileInfo symlink = this->getSymlink();
315 QDir linkDir(symlink.absolutePath());
316 QString linkfile = symlink.absoluteFilePath();
319 if (!linkDir.exists())
322 QString(
"Folder %1 does not exist. System is not properly installed.").arg(linkDir.path()));
326 QDir devDir(
"/dev/");
330 filters <<
"cu.usbserial*" <<
"cu.KeySerial*" <<
"serial" <<
"ttyUSB*";
332 QStringList files = devDir.entryList(filters, QDir::System);
337 QString(
"No usb connections found in /dev using filters %1").arg(filters.join(
";")));
342 report(QString(
"Device files: %1").arg(files.join(
",")));
343 if (files.size() > 1)
345 QString(
"More than one tracker connected? Will only try to connect to: %1").arg(files[0]));
348 QString device = devDir.filePath(files[0]);
351 QFile(linkfile).remove();
352 QFile devFile(device);
353 QFileInfo devFileInfo(device);
354 if (!devFileInfo.isWritable())
356 reportError(QString(
"Device %1 is not writable. Connection will fail.").arg(device));
360 bool val = devFile.link(linkfile);
364 QString(
"Symlink %1 creation to device %2 failed with code %3").arg(linkfile).arg(device).arg(
370 report(QString(
"Created symlink %1 to device %2").arg(linkfile).arg(device));
373 devFile.setPermissions(
374 QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner | QFile::ReadGroup | QFile::WriteGroup
375 | QFile::ExeGroup | QFile::ReadOther | QFile::WriteOther | QFile::ExeOther);
379 QFileInfo TrackingSystemIGSTKService::getSymlink()
const
381 QString name(
"/Library/CustusX/igstk.links");
383 QDir::root().mkdir(name);
384 QString linkFile = linkDir.path() +
"/cu.CustusX.dev0";
385 return QFileInfo(linkDir, linkFile);
390 void TrackingSystemIGSTKService::cleanupSymlink()
392 report(
"Cleaning up symlinks.");
393 QFile(this->getSymlink().absoluteFilePath()).remove();
397 void TrackingSystemIGSTKService::startTracking()
399 if (!this->isInitialized())
401 connect(
this, SIGNAL(
initialized()),
this, SLOT(startTrackingAfterInitSlot()));
407 mTrackerThread->track(
true);
410 void TrackingSystemIGSTKService::stopTracking()
412 if (!this->isTracking())
417 mTrackerThread->track(
false);
420 void TrackingSystemIGSTKService::setConfigurationFile(QString configurationFile)
422 if (configurationFile == mConfigurationFilePath)
425 if (this->isConfigured())
427 connect(
this, SIGNAL(
deconfigured()),
this, SLOT(configureAfterDeconfigureSlot()));
431 mConfigurationFilePath = configurationFile;
436 if (mLoggingFolder == loggingFolder)
439 if (this->isConfigured())
441 connect(
this, SIGNAL(
deconfigured()),
this, SLOT(configureAfterDeconfigureSlot()));
445 mLoggingFolder = loggingFolder;
448 void TrackingSystemIGSTKService::initializedSlot(
bool value)
460 report(
"IGSTK Tracking Service is uninitialized.");
466 void TrackingSystemIGSTKService::trackerTrackingSlot(
bool value)
484 void TrackingSystemIGSTKService::startTrackingAfterInitSlot()
486 disconnect(
this, SIGNAL(
initialized()),
this, SLOT(startTrackingAfterInitSlot()));
487 this->startTracking();
490 void TrackingSystemIGSTKService::initializeAfterConfigSlot()
492 disconnect(
this, SIGNAL(
configured()),
this, SLOT(initializeAfterConfigSlot()));
496 void TrackingSystemIGSTKService::uninitializeAfterTrackingStoppedSlot()
498 disconnect(
this, SIGNAL(
trackingStopped()),
this, SLOT(uninitializeAfterTrackingStoppedSlot()));
499 this->uninitialize();
502 void TrackingSystemIGSTKService::deconfigureAfterUninitializedSlot()
504 disconnect(
this, SIGNAL(
uninitialized()),
this, SLOT(deconfigureAfterUninitializedSlot()));
508 void TrackingSystemIGSTKService::configureAfterDeconfigureSlot()
510 disconnect(
this, SIGNAL(
deconfigured()),
this, SLOT(configureAfterDeconfigureSlot()));
514 void TrackingSystemIGSTKService::globalConfigurationFileChangedSlot(QString key)
516 if (key ==
"toolConfigFile")
518 this->setConfigurationFile(
profile()->getToolConfigFilePath());
529 void TrackingSystemIGSTKService::destroyTrackerThread()
533 mTrackerThread->quit();
534 mTrackerThread->wait(2000);
535 if (mTrackerThread->isRunning())
537 mTrackerThread->terminate();
538 mTrackerThread->wait();
540 QObject::disconnect(mTrackerThread.get());
541 mTrackerThread.reset();
cxResource_EXPORT ProfilePtr profile()
void reportError(QString msg)
TrackingSystemIGSTKService()
void uninitialized()
system is uninitialized
boost::shared_ptr< IgstkTool > IgstkToolPtr
virtual TrackerConfigurationPtr getConfiguration()
virtual void setLoggingFolder(QString loggingFolder)
boost::shared_ptr< class TrackerConfiguration > TrackerConfigurationPtr
void reportWarning(QString msg)
void trackingStarted()
system starts tracking
virtual Tool::State getState() const
Settings * settings()
Shortcut for accessing the settings instance.
void reportSuccess(QString msg)
void trackingStopped()
system stops tracking
void newProbe(const ToolPtr probe)
void initialized()
system is initialized
void configured()
system is configured
boost::shared_ptr< ToolUsingIGSTK > cxToolPtr
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)