Skip to content

Commit

Permalink
Merge pull request #488 from martinzak-zaber/zaber/autofocus-simple
Browse files Browse the repository at this point in the history
Adding autofocus, general improvements
  • Loading branch information
marktsuchida authored Sep 4, 2024
2 parents c8f3108 + 680e00b commit 138684c
Show file tree
Hide file tree
Showing 21 changed files with 544 additions and 107 deletions.
43 changes: 35 additions & 8 deletions DeviceAdapters/Zaber/ConnectionManager.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
#include "ConnectionManager.h"

#include <regex>
#include <zaber/motion/exceptions/invalid_argument_exception.h>

std::shared_ptr<zml::Connection> ConnectionManager::getConnection(std::string port)
{
{
std::lock_guard<std::mutex> lockGuard(lock_);
if (connections_.count(port) > 0) {
if (auto connectionPtr = connections_.at(port).lock()) {
return connectionPtr;
}
}

auto connection = std::make_shared<zml::Connection>(zml::Connection::openSerialPort(port));
std::shared_ptr<zml::Connection> connection;
if (port.find("share://") == 0) {
// share://<host>:<port>/<connection>
std::regex parser("^share:\\/\\/([^:\\/]+)(:\\d+)?(\\/.*)?$", std::regex_constants::ECMAScript);
std::smatch partMatch;
if (!std::regex_match(port, partMatch, parser)) {
throw zmlbase::InvalidArgumentException("Invalid network share connection string: " + port);
}

std::string host = partMatch[1].str();
int sharePort = 11421;
std::string connectionName;
if (partMatch[2].matched) {
sharePort = std::stoi(partMatch[2].str().substr(1));
}
if (partMatch[3].matched) {
connectionName = partMatch[3].str().substr(1);
}

connection = std::make_shared<zml::Connection>(zml::Connection::openNetworkShare(host, sharePort, connectionName));
} else {
connection = std::make_shared<zml::Connection>(zml::Connection::openSerialPort(port));
}

auto id = connection->getInterfaceId();
connection->getDisconnected().subscribe([=](std::shared_ptr<zmlbase::MotionLibException>) {
connection->getDisconnected().subscribe([=, this](std::shared_ptr<zmlbase::MotionLibException>) {
removeConnection(port, id);
});
connections_[port] = connection;
Expand All @@ -22,18 +48,19 @@ std::shared_ptr<zml::Connection> ConnectionManager::getConnection(std::string po
bool ConnectionManager::removeConnection(std::string port, int interfaceId)
{
std::lock_guard<std::mutex> lockGuard(lock_);
if (connections_.count(port) == 0) {
auto it = connections_.find(port);
if (it == connections_.end()) {
return false;
}
}

if (interfaceId != -1) {
if (auto connection = connections_.at(port).lock()) {
if (auto connection = it->second.lock()) {
if (connection->getInterfaceId() != interfaceId) {
return false;
}
}
}

connections_.erase(port);
connections_.erase(it);
return true;
}
}
4 changes: 1 addition & 3 deletions DeviceAdapters/Zaber/FilterCubeTurret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ int FilterCubeTurret::PortGetSet(MM::PropertyBase* pProp, MM::ActionType eAct)
{
if (initialized_)
{
// revert
pProp->Set(port_.c_str());
return ERR_PORT_CHANGE_FORBIDDEN;
resetConnection();
}

pProp->Get(port_);
Expand Down
12 changes: 6 additions & 6 deletions DeviceAdapters/Zaber/FilterCubeTurret.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ class FilterCubeTurret : public CStateDeviceBase<FilterCubeTurret>, public Zaber

// Device API
// ----------
int Initialize();
int Shutdown();
void GetName(char* name) const;
bool Busy();
int Initialize() override;
int Shutdown() override;
void GetName(char* name) const override;
bool Busy() override;

// Stage API
// ---------
unsigned long GetNumberOfPositions() const
unsigned long GetNumberOfPositions() const override
{
return numPositions_;
}

// Base class overrides
// ----------------
virtual int GetPositionLabel(long pos, char* label) const;
int GetPositionLabel(long pos, char* label) const override;

// Properties
// ----------------
Expand Down
4 changes: 1 addition & 3 deletions DeviceAdapters/Zaber/FilterWheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,7 @@ int FilterWheel::PortGetSet(MM::PropertyBase* pProp, MM::ActionType eAct)
{
if (initialized_)
{
// revert
pProp->Set(port_.c_str());
return ERR_PORT_CHANGE_FORBIDDEN;
resetConnection();
}

pProp->Get(port_);
Expand Down
12 changes: 6 additions & 6 deletions DeviceAdapters/Zaber/FilterWheel.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ class FilterWheel : public CStateDeviceBase<FilterWheel>, public ZaberBase

// Device API
// ----------
int Initialize();
int Shutdown();
void GetName(char* name) const;
bool Busy();
int Initialize() override;
int Shutdown() override;
void GetName(char* name) const override;
bool Busy() override;

// Stage API
// ---------
unsigned long GetNumberOfPositions() const
unsigned long GetNumberOfPositions() const override
{
return numPositions_;
}

// Base class overrides
// ----------------
virtual int GetPositionLabel(long pos, char* label) const;
int GetPositionLabel(long pos, char* label) const override;

// Properties
// ----------------
Expand Down
4 changes: 1 addition & 3 deletions DeviceAdapters/Zaber/Illuminator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,7 @@ int Illuminator::PortGetSet(MM::PropertyBase* pProp, MM::ActionType eAct)
{
if (initialized_)
{
// revert
pProp->Set(port_.c_str());
return ERR_PORT_CHANGE_FORBIDDEN;
resetConnection();
}

pProp->Get(port_);
Expand Down
20 changes: 10 additions & 10 deletions DeviceAdapters/Zaber/Illuminator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class Illuminator : public CShutterBase<Illuminator>, public ZaberBase

// Device API
// ----------
int Initialize();
int Shutdown();
int Initialize() override;
int Shutdown() override;

void GetName(char* pszName) const;
bool Busy();
void GetName(char* pszName) const override;
bool Busy() override;

int SetOpen(bool open = true);
int GetOpen(bool& open);
int Fire(double deltaT);
int SetOpen(bool open = true) override;
int GetOpen(bool& open) override;
int Fire(double deltaT) override;

// Properties
// ----------------
Expand All @@ -66,7 +66,7 @@ class Illuminator : public CShutterBase<Illuminator>, public ZaberBase
bool *lampExists_;

// Enables the optimization of using the device-scope lamp on command.
bool canUseDeviceLampOnCommand_;
bool canUseDeviceLampOnCommand_;

// Enables the optimization of only turning on individual axes that
// have nonzero flux when the shutter opens.
Expand All @@ -76,9 +76,9 @@ class Illuminator : public CShutterBase<Illuminator>, public ZaberBase
double* maxFlux_;

// These variables exist only to enable good UX in the absence of
// a device-scope lamp on command. The specific behavior these are
// a device-scope lamp on command. The specific behavior these are
// for is when you are tuning presets, you leave the shutter open
// and adjust intensities. If you change a zero intensity to
// and adjust intensities. If you change a zero intensity to
// nonzero, it must turn that axis on, only if the shutter is open.
bool *lampIsOn_;
bool isOpen_;
Expand Down
2 changes: 2 additions & 0 deletions DeviceAdapters/Zaber/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ AM_CXXFLAGS = $(MMDEVAPI_CXXFLAGS) $(ZML_CPPFLAGS)

deviceadapter_LTLIBRARIES = libmmgr_dal_Zaber.la
libmmgr_dal_Zaber_la_SOURCES = \
WdiAutofocus.cpp \
WdiAutofocus.h \
ObjectiveChanger.cpp \
ObjectiveChanger.h \
Illuminator.cpp \
Expand Down
8 changes: 3 additions & 5 deletions DeviceAdapters/Zaber/ObjectiveChanger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int ObjectiveChanger::Initialize()

this->LogMessage("ObjectiveChanger::Initialize\n", true);

auto ret = handleException([=]() {
auto ret = handleException([&]() {
ensureConnected();
if (!this->changer_.getFocusAxis().isHomed()) {
this->changer_.change(1);
Expand Down Expand Up @@ -232,9 +232,7 @@ int ObjectiveChanger::PortGetSet(MM::PropertyBase* pProp, MM::ActionType eAct)
{
if (initialized_)
{
// revert
pProp->Set(port_.c_str());
return ERR_PORT_CHANGE_FORBIDDEN;
resetConnection();
}

pProp->Get(port_);
Expand Down Expand Up @@ -350,7 +348,7 @@ int ObjectiveChanger::FocusOffsetGetSet(MM::PropertyBase* pProp, MM::ActionType
}

int ObjectiveChanger::setObjective(long objective, bool applyOffset) {
return handleException([=]() {
return handleException([&]() {
ensureConnected();
zmlbase::Measurement offset;
if (applyOffset) {
Expand Down
18 changes: 9 additions & 9 deletions DeviceAdapters/Zaber/ObjectiveChanger.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ class ObjectiveChanger : public CStateDeviceBase<ObjectiveChanger>, public Zaber

// Device API
// ----------
int Initialize();
int Shutdown();
void GetName(char* name) const;
bool Busy();
int Initialize() override;
int Shutdown() override;
void GetName(char* name) const override;
bool Busy() override;

// Stage API
// ---------
unsigned long GetNumberOfPositions() const
unsigned long GetNumberOfPositions() const override
{
return numPositions_;
}

// Base class overrides
// ----------------
virtual int GetPositionLabel(long pos, char* label) const;
int GetPositionLabel(long pos, char* label) const override;

// ZaverBase class overrides
// ----------------
virtual void onNewConnection();
void onNewConnection() override;

// Properties
// ----------------
Expand All @@ -66,9 +66,9 @@ class ObjectiveChanger : public CStateDeviceBase<ObjectiveChanger>, public Zaber
int XLdaAddressGetSet(MM::PropertyBase* pProp, MM::ActionType eAct);
int PositionGetSet(MM::PropertyBase* pProp, MM::ActionType eAct);
int FocusOffsetGetSet(MM::PropertyBase* pProp, MM::ActionType eAct);

int setObjective(long objective, bool applyOffset);
private:
int setObjective(long objective, bool applyOffset);

long xMorAddress_;
long xLdaAddress_;
long numPositions_;
Expand Down
4 changes: 1 addition & 3 deletions DeviceAdapters/Zaber/Stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ int Stage::OnPort (MM::PropertyBase* pProp, MM::ActionType eAct)
{
if (initialized_)
{
// revert
pProp->Set(port_.c_str());
return ERR_PORT_CHANGE_FORBIDDEN;
resetConnection();
}

pProp->Get(port_);
Expand Down
36 changes: 18 additions & 18 deletions DeviceAdapters/Zaber/Stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ class Stage : public CStageBase<Stage>, public ZaberBase

// Device API
// ----------
int Initialize();
int Shutdown();
void GetName(char* name) const;
bool Busy();
int Initialize() override;
int Shutdown() override;
void GetName(char* name) const override;
bool Busy() override;

// Stage API
// ---------
int GetPositionUm(double& pos);
int GetPositionSteps(long& steps);
int SetPositionUm(double pos);
int SetRelativePositionUm(double d);
int SetPositionSteps(long steps);
int SetRelativePositionSteps(long steps);
int Move(double velocity);
int Stop();
int Home();
int SetAdapterOriginUm(double d);
int SetOrigin();
int GetLimits(double& lower, double& upper);
int GetPositionUm(double& pos) override;
int GetPositionSteps(long& steps) override;
int SetPositionUm(double pos) override;
int SetRelativePositionUm(double d) override;
int SetPositionSteps(long steps) override;
int SetRelativePositionSteps(long steps); // not in the base class
int Move(double velocity) override;
int Stop() override;
int Home() override;
int SetAdapterOriginUm(double d) override;
int SetOrigin() override;
int GetLimits(double& lower, double& upper) override;

int IsStageSequenceable(bool& isSequenceable) const {isSequenceable = false; return DEVICE_OK;}
bool IsContinuousFocusDrive() const {return false;}
int IsStageSequenceable(bool& isSequenceable) const override { isSequenceable = false; return DEVICE_OK; }
bool IsContinuousFocusDrive() const override { return false; }

// action interface
// ----------------
Expand Down
Loading

0 comments on commit 138684c

Please sign in to comment.