Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
fix #13
Browse files Browse the repository at this point in the history
  • Loading branch information
MPIBR-tushevg committed Aug 9, 2018
1 parent 2507c0b commit 27245aa
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 85 deletions.
38 changes: 17 additions & 21 deletions BrainATUMtome/driverepos.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include "driverepos.h"

DriverEpos::DriverEpos(QObject *parent) : QObject(parent),
m_keyHandle_lower(0),
m_keyHandle_upper(0),
m_keyHandle_lower(nullptr),
m_keyHandle_upper(nullptr),
m_nodeId(1),
m_errorCode(0)
{
// open motor interfaces
m_keyHandle_lower = open("USB0");
m_keyHandle_upper = open("USB1");
char portLower[] = "USB0";
char portUpper[] = "USB1";
m_keyHandle_lower = open(portLower);
m_keyHandle_upper = open(portUpper);

// set velocity mode
setVelocityMode(m_keyHandle_lower);
Expand All @@ -18,13 +20,10 @@ DriverEpos::DriverEpos(QObject *parent) : QObject(parent),
DriverEpos::~DriverEpos()
{
close(m_keyHandle_lower);
delete m_keyHandle_lower;

close(m_keyHandle_upper);
delete m_keyHandle_upper;
}

HANDLE DriverEpos::open(const char *portName)
HANDLE DriverEpos::open(char *portName)
{

char deviceName[] = "EPOS2";
Expand All @@ -34,32 +33,32 @@ HANDLE DriverEpos::open(const char *portName)
HANDLE m_keyHandle = VCS_OpenDevice(deviceName,
protocolStackName,
interfaceName,
(char *)portName,
portName,
&m_errorCode);

if (!m_keyHandle)
{
throwError("VCS_OpenDevice");
return 0;
throwError(QString("%1 %2").arg("failed to open device on port", portName));
return nullptr;
}

qDebug() << "DriverMaxonMotors:: device is open on port " << portName;
emit error("DriverEpos", QString("%1 %2").arg("device is open on port", portName));

return m_keyHandle;
}

void DriverEpos::close(HANDLE m_keyHandle)
{
if (m_keyHandle == 0)
if (m_keyHandle == nullptr)
return;

if (!VCS_CloseDevice(m_keyHandle, &m_errorCode))
{
throwError("VCS_CloseDevice");
throwError("failed to close device");
return;
}

m_keyHandle = 0;
m_keyHandle = nullptr;
}

void DriverEpos::clear(HANDLE m_keyHandle)
Expand Down Expand Up @@ -233,20 +232,17 @@ void DriverEpos::setLedRed(BOOL state)
}


void DriverEpos::throwError(QString message)
void DriverEpos::throwError(const QString &errorMessage)
{
const WORD errorInfoSize = 256;
char errorInfo[errorInfoSize];


if (!VCS_GetErrorInfo(m_errorCode, errorInfo, errorInfoSize))
{
strncpy(errorInfo, "failed to read error information", errorInfoSize);
strncpy_s(errorInfo, "failed to read error information", errorInfoSize);
}

qDebug() << "DriverMaxonMotors::Error, " << message;
qDebug() << "\terror code = " << QString::number(m_errorCode, 16);
qDebug() << "\terror info = " << errorInfo;
emit error("DriverEpos", QString("%1 %2 %3").arg(errorMessage, QString::number(m_errorCode, 16), errorInfo));

// led indicator
setLedGreen(FALSE);
Expand Down
9 changes: 6 additions & 3 deletions BrainATUMtome/driverepos.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define DRIVEREPOS_H

#include <QObject>
#include <QDebug>

#include "Definitions.h"

Expand All @@ -20,7 +19,7 @@ class DriverEpos : public QObject
WORD m_nodeId;
DWORD m_errorCode;

HANDLE open(const char *portName);
HANDLE open(char *portName);
void close(HANDLE m_keyHandle);
void clear(HANDLE m_keyHandle);
void enable(HANDLE m_keyHandle);
Expand All @@ -30,7 +29,6 @@ class DriverEpos : public QObject
void stop(HANDLE m_keyHandle);
void setDOPin(HANDLE m_keyHandle, WORD digitalOutputNb, WORD configuration, BOOL state);

void throwError(QString messages);
void setLedRed(BOOL state);
void setLedGreen(BOOL state);
void setSound(BOOL state);
Expand All @@ -43,9 +41,14 @@ class DriverEpos : public QObject
MOTORS_BOTH = 0x11
};

void throwError(const QString &errorMessage);

public slots:
void on_uirequest_run(quint8 motor, qint16 velocity, bool direction, bool tension);

signals:
void error(const QString &errorSource, const QString &errorMessage);

};

#endif // DRIVEREPOS_H
63 changes: 34 additions & 29 deletions BrainATUMtome/drivermicrotome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ DriverMicrotome::DriverMicrotome(QObject *parent) : QObject(parent),
m_sections(0)
{
// configure serial communication
m_dsp = new DriverSerialPort("DriverMicrotome", "COM2", QSerialPort::Baud19200, this);
connect(this, &DriverMicrotome::error, m_dsp, &DriverSerialPort::error);
connect(this, &DriverMicrotome::send, m_dsp, &DriverSerialPort::on_bufferSchedule);
connect(m_dsp, &DriverSerialPort::received, this, &DriverMicrotome::on_respond);
m_driverSerialPort = new DriverSerialPort("DriverMicrotome", "COM2", QSerialPort::Baud19200, this);
connect(this, &DriverMicrotome::send, m_driverSerialPort, &DriverSerialPort::on_bufferSchedule);
connect(m_driverSerialPort, &DriverSerialPort::received, this, &DriverMicrotome::on_respond);
connect(m_driverSerialPort, &DriverSerialPort::error,
[this](const QString &errorSource, const QString &errorMessage)
{
QString errorParent = QString("%1::%2").arg("DriverSyringe", errorSource);
emit error(errorParent, errorMessage);
});

// configure command set
m_parser = new QHash<quint16, ParserCallback>();
Expand Down Expand Up @@ -61,9 +66,9 @@ QString DriverMicrotome::parserCallback_GetPartId(QString respond)

QString DriverMicrotome::parserCallback_GetVersion(QString respond)
{
quint8 revisionHardware = respond.mid(5, 2).toUInt(NULL, 16);
quint8 revisionMajor = respond.mid(7, 2).toUInt(NULL, 16);
quint8 revisionMinor = respond.mid(9, 2).toUInt(NULL, 16);
quint8 revisionHardware = static_cast<quint8>(respond.mid(5, 2).toUInt(nullptr, 16));
quint8 revisionMajor = static_cast<quint8>(respond.mid(7, 2).toUInt(nullptr, 16));
quint8 revisionMinor = static_cast<quint8>(respond.mid(9, 2).toUInt(nullptr, 16));
return QString("%1\n\t%2 %3\n\t%4 %5\n\t%6 %7").arg("Controller Version",
"Hardware Revision", QString::number(revisionHardware),
"Major Revision", QString::number(revisionMajor),
Expand All @@ -74,7 +79,7 @@ QString DriverMicrotome::parserCallback_GetVersion(QString respond)

QString DriverMicrotome::parserCallback_CommandTransmissionError(QString respond)
{
quint8 code = respond.mid(5, 2).toUInt(NULL, 16);
quint8 code = static_cast<quint8>(respond.mid(5, 2).toUInt(nullptr, 16));
QString log = "Command Transmission Error";
switch (code)
{
Expand Down Expand Up @@ -105,9 +110,9 @@ QString DriverMicrotome::parserCallback_CommandTransmissionError(QString respond
QString DriverMicrotome::parserCallback_FeedrateMotorControl(QString respond)
{
static const quint32 positionMax = 200000;
quint32 position = respond.mid(7, respond.size() - 9).toUInt(NULL, 16);
quint32 position = respond.mid(7, respond.size() - 9).toUInt(nullptr, 16);
quint32 positionLeft = (positionMax > position) ? (positionMax - position) : 0;
float ratioLeft = (positionMax > position) ? (100.0 * (positionMax - position)) / positionMax : 0.0;
float ratioLeft = (positionMax > position) ? static_cast<float>((100.0 * (positionMax - position)) / positionMax) : 0.0;
emit advance(positionLeft, ratioLeft);
return QString("%1 %2").arg("Feedrate Position", QString::number(position));
}
Expand All @@ -116,8 +121,8 @@ QString DriverMicrotome::parserCallback_FeedrateMotorControl(QString respond)

QString DriverMicrotome::parserCallback_Feed(QString respond)
{
quint8 code = respond.mid(5, 2).toUInt(NULL, 16);
quint16 value = respond.mid(7, 4).toUInt(NULL, 16);
quint8 code = static_cast<quint8>(respond.mid(5, 2).toUInt(nullptr, 16));
quint16 value = static_cast<quint8>(respond.mid(7, 4).toUInt(nullptr, 16));
QString status = (code == 0x01) ? "Set" : "Get";

emit feedStep(value);
Expand All @@ -129,8 +134,8 @@ QString DriverMicrotome::parserCallback_Feed(QString respond)

QString DriverMicrotome::parserCallback_CuttingMotorControl(QString respond)
{
quint8 code = respond.mid(5, 2).toUInt(NULL, 16);
quint8 value = respond.mid(7, 2).toUInt(NULL, 16);
quint8 code = static_cast<quint8>(respond.mid(5, 2).toUInt(nullptr, 16));
quint8 value = static_cast<quint8>(respond.mid(7, 2).toUInt(nullptr, 16));
QString log = "Cutting Motor Control";
QString status = (code == 0xFF) ? "Get" : "Set";
QString state = "invalid status";
Expand Down Expand Up @@ -159,8 +164,8 @@ QString DriverMicrotome::parserCallback_CuttingMotorControl(QString respond)

QString DriverMicrotome::parserCallback_CuttingSpeed(QString respond)
{
quint8 code = respond.mid(5, 2).toUInt(NULL, 16);
quint16 value = respond.mid(7, 4).toUInt(NULL, 16);
quint8 code = static_cast<quint8>(respond.mid(5, 2).toUInt(nullptr, 16));
quint16 value = static_cast<quint8>(respond.mid(7, 4).toUInt(nullptr, 16));
QString status = (code == 0x01) ? "Set" : "Get";

emit cuttingSpeed(static_cast<quint32>(value * 10));
Expand All @@ -172,8 +177,8 @@ QString DriverMicrotome::parserCallback_CuttingSpeed(QString respond)

QString DriverMicrotome::parserCallback_ReturnSpeed(QString respond)
{
quint8 code = respond.mid(5, 2).toUInt(NULL, 16);
quint16 value = respond.mid(7, 4).toUInt(NULL, 16);
quint8 code = static_cast<quint8>(respond.mid(5, 2).toUInt(nullptr, 16));
quint16 value = static_cast<quint8>(respond.mid(7, 4).toUInt(nullptr, 16));
QString status = (code == 0x01) ? "Set" : "Get";

emit returnSpeed(static_cast<quint32>(value * 10));
Expand All @@ -184,7 +189,7 @@ QString DriverMicrotome::parserCallback_ReturnSpeed(QString respond)

QString DriverMicrotome::parserCallback_HandwheelPosition(QString respond)
{
quint8 code = respond.mid(7, 2).toUInt(NULL, 16);
quint8 code = static_cast<quint8>(respond.mid(7, 2).toUInt(nullptr, 16));
QString log = "Handwheel Position";
switch (code)
{
Expand Down Expand Up @@ -237,14 +242,14 @@ QByteArray DriverMicrotome::packCommand(quint8 EA, quint8 CC, quint8 status, qui
void DriverMicrotome::verifyCheckSum(QString respond)
{
QString checksum_received = respond.mid(respond.size() - 2, 2);
quint8 checksum_value_received = checksum_received.toUInt(NULL, 16);
quint8 checksum_value_received = static_cast<quint8>(checksum_received.toUInt(nullptr, 16));

QString checksum_expected = calculateCheckSum(respond.mid(1, respond.size() - 3));
quint8 checksum_value_expected = checksum_expected.toUInt(NULL, 16);
quint8 checksum_value_expected = static_cast<quint8>(checksum_expected.toUInt(nullptr, 16));

if (checksum_value_received != checksum_value_expected)
{
emit error("VerifyCheckSum::wrong check sum of the respond");
emit error("DriverMicrotome","VerifyCheckSum::wrong check sum of the respond");
}
}

Expand All @@ -254,14 +259,14 @@ void DriverMicrotome::verifyCheckSum(QString respond)
QString DriverMicrotome::calculateCheckSum(QString package)
{
// command components to 16 bit values
quint16 EA = package.mid(0, 2).toUInt(NULL, 16);
quint16 CC = package.mid(2, 2).toUInt(NULL, 16);
quint16 EA = static_cast<quint16>(package.mid(0, 2).toUInt(nullptr, 16));
quint16 CC = static_cast<quint16>(package.mid(2, 2).toUInt(nullptr, 16));
quint16 DD = 0;
if (package.size() > 4)
{
for (int i = 4; i < package.size(); i += 2)
{
DD += package.mid(i, 2).toUInt(NULL, 16);
DD += static_cast<quint16>(package.mid(i, 2).toUInt(nullptr, 16));
}
}

Expand Down Expand Up @@ -290,16 +295,16 @@ void DriverMicrotome::on_respond(const QByteArray &package)
verifyCheckSum(respond);

// parse based on current address and command
quint8 EA = QString("%2%1").arg(respond.mid(1, 1), respond.mid(2, 1)).toUInt(NULL, 16);
quint8 CC = respond.mid(3, 2).toUInt(NULL, 16);
quint8 EA = static_cast<quint8>(QString("%2%1").arg(respond.mid(1, 1), respond.mid(2, 1)).toUInt(nullptr, 16));
quint8 CC = static_cast<quint8>(respond.mid(3, 2).toUInt(nullptr, 16));

quint16 parserKey = calculateParserKey(EA, CC);
if (m_parser->contains(parserKey))
{
QString log = "";
ParserCallback callback = m_parser->value(parserKey);

if (callback != NULL)
if (callback != nullptr)
log = (this->*callback)(respond);

if (!log.isEmpty())
Expand All @@ -310,7 +315,7 @@ void DriverMicrotome::on_respond(const QByteArray &package)
QString errorMessage = QString("%1\n\t%2 %3\n\t%4 %5").arg("unknown parser",
"address", QString::number(EA, 16),
"command", QString::number(CC, 16));
emit error(errorMessage);
emit error("DriverMicrotome", errorMessage);
}
}

Expand Down
4 changes: 2 additions & 2 deletions BrainATUMtome/drivermicrotome.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DriverMicrotome : public QObject
private:
bool m_cuttingMotor_requestStop;
quint32 m_sections;
DriverSerialPort *m_dsp;
DriverSerialPort *m_driverSerialPort;

enum CommandSet : quint8
{
Expand Down Expand Up @@ -66,7 +66,7 @@ public slots:
void on_uirequest_setFeed(quint16 value);

signals:
void error(const QString &errorMessage);
void error(const QString &errorSource, const QString &errorMessage);
void send(const QByteArray &package);

void sections(quint32 sectionsCount);
Expand Down
20 changes: 5 additions & 15 deletions BrainATUMtome/driverserialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ DriverSerialPort::DriverSerialPort(const QString &deviceName, const QString &por
// configure callbacks
connect(m_serialPort, &QSerialPort::readyRead, this, &DriverSerialPort::on_bufferReading);
connect(state_sender, &QState::entered, this, &DriverSerialPort::on_bufferWriting);
connect(this, &DriverSerialPort::error, this, &DriverSerialPort::on_raiseError);
connect(m_respondTimer, &QTimer::timeout, [this](){emit error("respond timeout");});
connect(m_respondTimer, &QTimer::timeout, [this](){emit error("DriverSerialPort", "respond timeout");});
connect(m_handshakeTimer, &QTimer::timeout, [this](){emit command();});

// check if port is open
if (!m_serialPort->isOpen())
{
emit error("open Serial Port failed");
emit error("DriverSerialPort","open Serial Port failed");
}
else
{
Expand Down Expand Up @@ -128,31 +127,22 @@ void DriverSerialPort::on_bufferWriting()

if (bytesWritten == -1)
{
emit error("SerialPort::Write, failed to write data to serial port");
emit error("DriverSerialPort","failed to write data to serial port");
}
else if (bytesWritten != bufferWrite.size())
{
emit error("SerialPort::Write, failed to write full buffer to serial port");
emit error("DriverSerialPort","failed to write full buffer to serial port");
}
else
{
qDebug() << "DriverSerialPort::Command:: " << QString(bufferWrite);
emit error("DriverSerialPort", QString("%1::%2").arg("command", QString(bufferWrite)));
m_respondTimer->start();
emit sent();
}

}


void DriverSerialPort::on_raiseError(const QString &errorMessage)
{
QString logMessage = QString("%1::%2, %3").arg(m_deviceName, "Error", errorMessage);
qDebug("%s", logMessage.toStdString().c_str());
//QCoreApplication::exit(1);
}



void DriverSerialPort::on_bufferSchedule(const QByteArray &package)
{
m_commandQueue->append(package);
Expand Down
Loading

0 comments on commit 27245aa

Please sign in to comment.