diff --git a/BrainATUMtome/BrainATUMtome.pro b/BrainATUMtome/BrainATUMtome.pro index 922c1a7..34e4717 100644 --- a/BrainATUMtome/BrainATUMtome.pro +++ b/BrainATUMtome/BrainATUMtome.pro @@ -46,3 +46,11 @@ FORMS += \ qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target + +win32: LIBS += -L$$PWD/../libs/epos/lib/ -lEposCmd64 + +INCLUDEPATH += $$PWD/../libs/epos/include +DEPENDPATH += $$PWD/../libs/epos/include + +macx: INCLUDEPATH += $$PWD/../libs/epos/macx +macx: LIBS += -L$$PWD/../libs/epos/macx -lepos diff --git a/BrainATUMtome/epos.cpp b/BrainATUMtome/epos.cpp index 74676e6..0606cfb 100644 --- a/BrainATUMtome/epos.cpp +++ b/BrainATUMtome/epos.cpp @@ -1,8 +1,220 @@ #include "epos.h" -Epos::Epos(QObject *parent) : QObject(parent) +Epos::Epos(QObject *parent) : QObject(parent), + m_keyHandle_lower(nullptr), + m_keyHandle_upper(nullptr), + m_nodeId(0), + m_errorCode(0) { + // open motor interfaces + char portLower[] = "USB0"; + char portUpper[] = "USB1"; + m_keyHandle_lower = open(portLower); + m_keyHandle_upper = open(portUpper); + // set velocity mode + setVelocityMode(m_keyHandle_lower); + setVelocityMode(m_keyHandle_upper); +} + + +Epos::~Epos() +{ + close(m_keyHandle_lower); + close(m_keyHandle_upper); +} + + +HANDLE Epos::open(char *portName) +{ + + char deviceName[] = "EPOS2"; + char protocolStackName[] = "MAXON SERIAL V2"; + char interfaceName[] = "USB"; + + HANDLE m_keyHandle = VCS_OpenDevice(deviceName, + protocolStackName, + interfaceName, + portName, + &m_errorCode); + + if (!m_keyHandle) + { + throwError("failed to open device on port " + QString(portName)); + return nullptr; + } + + emit log(EventLogType::Notify, "Epos :: device is open on port " + QString(portName)); + + return m_keyHandle; +} + +void Epos::close(HANDLE m_keyHandle) +{ + if (m_keyHandle == nullptr) + return; + + if (!VCS_CloseDevice(m_keyHandle, &m_errorCode)) + { + throwError("failed to close device"); + return; + } + + m_keyHandle = nullptr; +} + +void Epos::clear(HANDLE m_keyHandle) +{ + BOOL isFaultState = FALSE; + + if (!m_keyHandle) + return; + + if (!VCS_GetFaultState(m_keyHandle, m_nodeId, &isFaultState, &m_errorCode)) + { + throwError("VCS_GetFaultState"); + return; + } + + if (isFaultState) + { + if (!VCS_ClearFault(m_keyHandle, m_nodeId, &m_errorCode)) + { + throwError("VCS_ClearFault"); + } + + // led indicator + setLedRed(FALSE); + setLedGreen(FALSE); + } +} + +void Epos::enable(HANDLE m_keyHandle) +{ + if (!m_keyHandle) + return; + + clear(m_keyHandle); + + if (!VCS_SetEnableState(m_keyHandle, m_nodeId, &m_errorCode)) + { + throwError("VCS_SetEnableState"); + } +} + +void Epos::disable(HANDLE m_keyHandle) +{ + if (!m_keyHandle) + return; + + if (!VCS_SetDisableState(m_keyHandle, m_nodeId, &m_errorCode)) + { + throwError("VCS_SetDisableState"); + } +} + + +void Epos::setVelocityMode(HANDLE m_keyHandle) +{ + if (!m_keyHandle) + return; + + // set operation mode + if (!VCS_ActivateVelocityMode(m_keyHandle, m_nodeId, &m_errorCode)) + { + throwError("VCS_ActivateVelocityMode"); + } +} + +void Epos::move(HANDLE m_keyHandle, qint16 velocityMust) +{ + enable(m_keyHandle); + + if (!VCS_SetVelocityMust(m_keyHandle, m_nodeId, velocityMust, &m_errorCode)) + { + throwError("VCS_SetVelocityMust"); + } +} + +void Epos::stop(HANDLE m_keyHandle) +{ + if (!VCS_SetQuickStopState(m_keyHandle, m_nodeId, &m_errorCode)) + { + throwError("VCS_SetQuickStopState"); + } + + disable(m_keyHandle); +} + +void Epos::setDOPin(HANDLE m_keyHandle, WORD digitalOutputNb, WORD configuration, BOOL state) +{ + BOOL mask = state; + BOOL polarity = FALSE; + + if (!m_keyHandle) + return; + + if (!VCS_DigitalOutputConfiguration(m_keyHandle, + m_nodeId, + digitalOutputNb, + configuration, + state, + mask, + polarity, + &m_errorCode)) + { + throwError("VCS_DigitalOutputConfiguration"); + } +} + + +void Epos::setLedGreen(BOOL state) +{ + WORD digitalOutputNb = 0x0003; + WORD configuration = DIC_GENERAL_PURPOSE_C; + setDOPin(m_keyHandle_lower, digitalOutputNb, configuration, state); +} + + +void Epos::setSound(BOOL state) +{ + WORD digitalOutputNb = 0x0004; + WORD configuration = DIC_GENERAL_PURPOSE_D; + setDOPin(m_keyHandle_lower, digitalOutputNb, configuration, state); +} + + +void Epos::setTension(BOOL state) +{ + WORD digitalOutputNb = 0x0003; + WORD configuration = DIC_GENERAL_PURPOSE_C; + setDOPin(m_keyHandle_upper, digitalOutputNb, configuration, state); +} + + +void Epos::setLedRed(BOOL state) +{ + WORD digitalOutputNb = 0x0004; + WORD configuration = DIC_GENERAL_PURPOSE_D; + setDOPin(m_keyHandle_upper, digitalOutputNb, configuration, state); +} + + +void Epos::throwError(const QString &errorMessage) +{ + const WORD errorInfoSize = 256; + char errorInfo[errorInfoSize]; + + if (!VCS_GetErrorInfo(m_errorCode, errorInfo, errorInfoSize)) + { + //std::copy(errorInfo, "failed to read error information", errorInfoSize); + } + + emit log(EventLogType::Warn, "EPOS :: Error, " + errorMessage + "\n" + QString(errorInfo) + ", errorCode: " + QString::number(m_errorCode, 16)); + + // led indicator + setLedGreen(FALSE); + setLedRed(TRUE); } diff --git a/BrainATUMtome/epos.h b/BrainATUMtome/epos.h index d0b3822..f41a8fd 100644 --- a/BrainATUMtome/epos.h +++ b/BrainATUMtome/epos.h @@ -3,6 +3,7 @@ #include +#include "Definitions.h" #include "eventlog.h" class Epos : public QObject @@ -10,11 +11,34 @@ class Epos : public QObject Q_OBJECT public: explicit Epos(QObject *parent = nullptr); + ~Epos(); private: + HANDLE m_keyHandle_lower; + HANDLE m_keyHandle_upper; + WORD m_nodeId; + DWORD m_errorCode; + const int MOTORID_LOWER = 0; const int MOTORID_UPPER = 1; + HANDLE open(char *portName); + void close(HANDLE m_keyHandle); + void clear(HANDLE m_keyHandle); + void enable(HANDLE m_keyHandle); + void disable(HANDLE m_keyHandle); + void setVelocityMode(HANDLE m_keyHandle); + void move(HANDLE m_keyHandle, qint16 velocityMust); + void stop(HANDLE m_keyHandle); + void setDOPin(HANDLE m_keyHandle, WORD digitalOutputNb, WORD configuration, BOOL state); + + void setLedRed(BOOL state); + void setLedGreen(BOOL state); + void setSound(BOOL state); + void setTension(BOOL state); + + void throwError(const QString &message); + public slots: void on_uirequest_velocity(int motorid, int velocity); void on_uirequest_tension(bool state); diff --git a/BrainATUMtome/mainwindow.h b/BrainATUMtome/mainwindow.h index da8f846..56e1b67 100644 --- a/BrainATUMtome/mainwindow.h +++ b/BrainATUMtome/mainwindow.h @@ -38,8 +38,8 @@ class MainWindow : public QMainWindow void configure_microtome(); void configure_syringe(); - static const int EPOS_LOWER_MOTTOR = 0; - static const int EPOS_UPPER_MOTTOR = 1; + const int EPOS_LOWER_MOTTOR = 0; + const int EPOS_UPPER_MOTTOR = 1; private slots: void on_radioButtonsEposSyncToggled(bool checked); diff --git a/BrainATUMtome/mainwindow.ui b/BrainATUMtome/mainwindow.ui index f900a97..a2297b2 100644 --- a/BrainATUMtome/mainwindow.ui +++ b/BrainATUMtome/mainwindow.ui @@ -7,7 +7,7 @@ 0 0 1000 - 753 + 769 @@ -64,7 +64,7 @@ 15000 - 1 + 10 1000 @@ -120,7 +120,7 @@ 15000 - 1 + 10 1000 @@ -176,7 +176,7 @@ 15000 - 1 + 10 1000 @@ -270,6 +270,9 @@ 100000 + + 10 + 100 @@ -319,6 +322,9 @@ 15000 + + 10 + 30