Skip to content

Commit

Permalink
Merge pull request micro-manager#359 from martinzak-zaber/zaber-zml
Browse files Browse the repository at this point in the history
Adding ZML and Objective Changer
  • Loading branch information
marktsuchida authored Aug 25, 2023
2 parents 97e1f73 + 056a6ac commit 556d847
Show file tree
Hide file tree
Showing 19 changed files with 950 additions and 689 deletions.
39 changes: 39 additions & 0 deletions DeviceAdapters/Zaber/ConnectionManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "ConnectionManager.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));
auto id = connection->getInterfaceId();
connection->getDisconnected().subscribe([=](std::shared_ptr<zmlbase::MotionLibException>) {
removeConnection(port, id);
});
connections_[port] = connection;

return connection;
}

bool ConnectionManager::removeConnection(std::string port, int interfaceId)
{
std::lock_guard<std::mutex> lockGuard(lock_);
if (connections_.count(port) == 0) {
return false;
}

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

connections_.erase(port);
return true;
}
22 changes: 22 additions & 0 deletions DeviceAdapters/Zaber/ConnectionManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <MMDevice.h>
#include <zaber/motion/ascii/connection.h>
#include <map>
#include <string>
#include <mutex>
#include <memory>

namespace zmlbase = zaber::motion;
namespace zml = zaber::motion::ascii;

class ConnectionManager
{
public:
std::shared_ptr<zml::Connection> getConnection(std::string port);
bool removeConnection(std::string port, int interfaceId = -1);
private:
std::mutex lock_;
std::map<std::string, std::weak_ptr<zml::Connection>> connections_;
};

42 changes: 11 additions & 31 deletions DeviceAdapters/Zaber/FilterCubeTurret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ FilterCubeTurret::FilterCubeTurret()
this->LogMessage("FilterCubeTurret::FilterCubeTurret\n", true);

InitializeDefaultErrorMessages();
SetErrorText(ERR_PORT_CHANGE_FORBIDDEN, g_Msg_PORT_CHANGE_FORBIDDEN);
SetErrorText(ERR_DRIVER_DISABLED, g_Msg_DRIVER_DISABLED);
SetErrorText(ERR_BUSY_TIMEOUT, g_Msg_BUSY_TIMEOUT);
SetErrorText(ERR_COMMAND_REJECTED, g_Msg_COMMAND_REJECTED);
SetErrorText(ERR_SETTING_FAILED, g_Msg_SETTING_FAILED);
ZaberBase::setErrorMessages([&](auto code, auto message) { this->SetErrorText(code, message); });

EnableDelay(); // signals that the delay setting will be used

Expand All @@ -56,7 +52,7 @@ FilterCubeTurret::FilterCubeTurret()
CreateProperty(MM::g_Keyword_Description, "Zaber filter cube turret device adapter", MM::String, true);

CPropertyAction* pAct = new CPropertyAction(this, &FilterCubeTurret::PortGetSet);
CreateProperty(MM::g_Keyword_Port, "COM1", MM::String, false, pAct, true);
CreateProperty("Zaber Serial Port", port_.c_str(), MM::String, false, pAct, true);

pAct = new CPropertyAction (this, &FilterCubeTurret::DeviceAddressGetSet);
CreateIntegerProperty("Controller Device Number", deviceAddress_, false, pAct, true);
Expand All @@ -83,41 +79,26 @@ void FilterCubeTurret::GetName(char* name) const

int FilterCubeTurret::Initialize()
{
if (initialized_)
if (initialized_)
{
return DEVICE_OK;
}

core_ = GetCoreCallback();

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

int ret = ClearPort();
if (ret != DEVICE_OK)
{
this->LogMessage("Clearing the serial port receive buffer failed. Are the port settings correct?\n", true);
return ret;
}

// Disable alert messages.
ret = SetSetting(deviceAddress_, 0, "comm.alert", 0);
if (ret != DEVICE_OK)
{
this->LogMessage("Initial attempt to communicate with device failed.\n", true);
return ret;
}
this->LogMessage("FilterCubeTurret::Initialize\n", true);

// Home the device to make sure it has its index.
ret = SendAndPollUntilIdle(deviceAddress_, 0, "home", 10000);
if (ret != DEVICE_OK)
auto ret = SendAndPollUntilIdle(deviceAddress_, 0, "home");
if (ret != DEVICE_OK)
{
this->LogMessage("Attempt to detect filter holder type failed; is this device an X-FWR?\n", true);
return ret;
}

long index = -1;
ret = GetRotaryIndexedDeviceInfo(deviceAddress_, 0, numPositions_, index);
if (ret != DEVICE_OK)
if (ret != DEVICE_OK)
{
this->LogMessage("Attempt to detect filter cube turret state and number of positions failed.\n", true);
return ret;
Expand All @@ -143,7 +124,7 @@ int FilterCubeTurret::Initialize()
}

ret = UpdateStatus();
if (ret != DEVICE_OK)
if (ret != DEVICE_OK)
{
return ret;
}
Expand Down Expand Up @@ -197,7 +178,7 @@ int FilterCubeTurret::GetPositionLabel(long pos, char* label) const
{
std::string str("Filter Cube ");
char numBuf[15];
snprintf(numBuf, 15, "%ld", pos);
snprintf(numBuf, 15, "%ld", pos + 1);
str.append(numBuf);
CDeviceUtils::CopyLimitedString(label, str.c_str());
}
Expand Down Expand Up @@ -281,7 +262,7 @@ int FilterCubeTurret::PositionGetSet(MM::PropertyBase* pProp, MM::ActionType eAc
if (initialized_)
{
int ret = GetSetting(deviceAddress_, 0, "motion.index.num", index);
if (ret != DEVICE_OK)
if (ret != DEVICE_OK)
{
return ret;
}
Expand All @@ -301,7 +282,7 @@ int FilterCubeTurret::PositionGetSet(MM::PropertyBase* pProp, MM::ActionType eAc
if ((index >= 0) && (index < numPositions_))
{
int ret = SendMoveCommand(deviceAddress_, 0, "index", index + 1);
if (ret != DEVICE_OK)
if (ret != DEVICE_OK)
{
return ret;
}
Expand All @@ -317,4 +298,3 @@ int FilterCubeTurret::PositionGetSet(MM::PropertyBase* pProp, MM::ActionType eAc

return DEVICE_OK;
}

42 changes: 11 additions & 31 deletions DeviceAdapters/Zaber/FilterWheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ FilterWheel::FilterWheel()
this->LogMessage("FilterWheel::FilterWheel\n", true);

InitializeDefaultErrorMessages();
SetErrorText(ERR_PORT_CHANGE_FORBIDDEN, g_Msg_PORT_CHANGE_FORBIDDEN);
SetErrorText(ERR_DRIVER_DISABLED, g_Msg_DRIVER_DISABLED);
SetErrorText(ERR_BUSY_TIMEOUT, g_Msg_BUSY_TIMEOUT);
SetErrorText(ERR_COMMAND_REJECTED, g_Msg_COMMAND_REJECTED);
SetErrorText(ERR_SETTING_FAILED, g_Msg_SETTING_FAILED);
ZaberBase::setErrorMessages([&](auto code, auto message) { this->SetErrorText(code, message); });

EnableDelay(); // signals that the delay setting will be used

Expand All @@ -55,7 +51,7 @@ FilterWheel::FilterWheel()
CreateProperty(MM::g_Keyword_Description, "Zaber filter wheel device adapter", MM::String, true);

CPropertyAction* pAct = new CPropertyAction(this, &FilterWheel::PortGetSet);
CreateProperty(MM::g_Keyword_Port, "COM1", MM::String, false, pAct, true);
CreateProperty("Zaber Serial Port", port_.c_str(), MM::String, false, pAct, true);

pAct = new CPropertyAction (this, &FilterWheel::DeviceAddressGetSet);
CreateIntegerProperty("Controller Device Number", deviceAddress_, false, pAct, true);
Expand All @@ -82,33 +78,18 @@ void FilterWheel::GetName(char* name) const

int FilterWheel::Initialize()
{
if (initialized_)
if (initialized_)
{
return DEVICE_OK;
}

core_ = GetCoreCallback();

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

int ret = ClearPort();
if (ret != DEVICE_OK)
{
this->LogMessage("Clearing the serial port receive buffer failed. Are the port settings correct?\n", true);
return ret;
}

// Disable alert messages.
ret = SetSetting(deviceAddress_, 0, "comm.alert", 0);
if (ret != DEVICE_OK)
{
this->LogMessage("Initial attempt to communicate with device failed.\n", true);
return ret;
}
this->LogMessage("FilterWheel::Initialize\n", true);

// Make the Filter Wheel detect the current holder.
ret = SendAndPollUntilIdle(deviceAddress_, 0, "tools detectholder", 60000);
if (ret != DEVICE_OK)
auto ret = SendAndPollUntilIdle(deviceAddress_, 0, "tools detectholder");
if (ret != DEVICE_OK)
{
this->LogMessage("Attempt to detect filter holder type failed; is this device an X-FWR?\n", true);
return ret;
Expand All @@ -117,7 +98,7 @@ int FilterWheel::Initialize()
// Get the number of positions and the current position.
long index = -1;
ret = GetRotaryIndexedDeviceInfo(deviceAddress_, 0, numPositions_, index);
if (ret != DEVICE_OK)
if (ret != DEVICE_OK)
{
this->LogMessage("Attempt to detect filter wheel state and number of positions failed.\n", true);
return ret;
Expand All @@ -143,7 +124,7 @@ int FilterWheel::Initialize()
}

ret = UpdateStatus();
if (ret != DEVICE_OK)
if (ret != DEVICE_OK)
{
return ret;
}
Expand Down Expand Up @@ -193,7 +174,7 @@ int FilterWheel::GetPositionLabel(long pos, char* label) const
{
std::string str("Filter ");
char numBuf[15];
snprintf(numBuf, 15, "%ld", pos);
snprintf(numBuf, 15, "%ld", pos + 1);
str.append(numBuf);
CDeviceUtils::CopyLimitedString(label, str.c_str());
}
Expand Down Expand Up @@ -276,7 +257,7 @@ int FilterWheel::PositionGetSet(MM::PropertyBase* pProp, MM::ActionType eAct)
if (initialized_)
{
int ret = GetSetting(deviceAddress_, 0, "motion.index.num", index);
if (ret != DEVICE_OK)
if (ret != DEVICE_OK)
{
return ret;
}
Expand All @@ -296,7 +277,7 @@ int FilterWheel::PositionGetSet(MM::PropertyBase* pProp, MM::ActionType eAct)
if ((index >= 0) && (index < numPositions_))
{
int ret = SendMoveCommand(deviceAddress_, 0, "index", index + 1);
if (ret != DEVICE_OK)
if (ret != DEVICE_OK)
{
return ret;
}
Expand All @@ -312,4 +293,3 @@ int FilterWheel::PositionGetSet(MM::PropertyBase* pProp, MM::ActionType eAct)

return DEVICE_OK;
}

Loading

0 comments on commit 556d847

Please sign in to comment.