Skip to content

Commit

Permalink
Update open62541 to v0.2
Browse files Browse the repository at this point in the history
Made sampling/publishing intervals tighter in order to draw a nicer
plot.
  • Loading branch information
sjoelund committed Nov 6, 2017
1 parent 1b8b8e6 commit 03be5e2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
41 changes: 24 additions & 17 deletions OMEdit/OMEditGUI/Simulation/OpcUaClient.cpp
Expand Up @@ -20,7 +20,7 @@ static bool writeReal(UA_Client *client, UA_NodeId id, UA_Double value)
wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
wReq.nodesToWrite[0].value.hasValue = UA_TRUE;
wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_DOUBLE];
wReq.nodesToWrite[0].value.value.storageType = UA_Variant::UA_VARIANT_DATA_NODELETE;
wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE;
wReq.nodesToWrite[0].value.value.data = &value;

UA_WriteResponse wResp = UA_Client_Service_write(client, wReq);
Expand Down Expand Up @@ -48,7 +48,7 @@ static bool writeBool(UA_Client *client, UA_NodeId id, UA_Boolean value)
wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
wReq.nodesToWrite[0].value.hasValue = UA_TRUE;
wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_BOOLEAN];
wReq.nodesToWrite[0].value.value.storageType = UA_Variant::UA_VARIANT_DATA_NODELETE;
wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE;
wReq.nodesToWrite[0].value.value.data = &value;

UA_WriteResponse wResp = UA_Client_Service_write(client, wReq);
Expand Down Expand Up @@ -86,11 +86,12 @@ OpcUaClient::~OpcUaClient()
bool OpcUaClient::connectToServer()
{
std::string endPoint = "opc.tcp://localhost:" + std::to_string(mSimulationOptions.getInteractiveSimulationPortNumber());
mpClient = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout);
mpClient = UA_Client_new(UA_ClientConfig_standard);
UA_StatusCode returnValue;
do {
Sleep::msleep(100);
returnValue = UA_Client_connect(mpClient, UA_ClientConnectionTCP, endPoint.c_str());
// QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
returnValue = UA_Client_connect(mpClient, endPoint.c_str());
} while (returnValue != UA_STATUSCODE_GOOD);
// qDebug() << "Connected to OPC-UA server " << endPoint;
return true;
Expand Down Expand Up @@ -316,7 +317,7 @@ double OpcUaClient::getCurrentSimulationTime()
It can be done by using subscriptions or contacting the remote directly each time step (simulate with steps).
*/
OpcUaWorker::OpcUaWorker(OpcUaClient *pClient, bool simulateWithSteps)
: mpParentClient(pClient), mSimulateWithSteps(simulateWithSteps), mSampleInterval(100), mSpeedValue(1.0)
: mpParentClient(pClient), mSimulateWithSteps(simulateWithSteps), mSampleInterval(10), mSpeedValue(1.0), mServerSampleInterval(5)
{
setInterval(mSampleInterval);
if (!mSimulateWithSteps) {
Expand Down Expand Up @@ -346,14 +347,20 @@ void OpcUaWorker::startInteractiveSimulation()
while(mIsRunning) {
const double elapsed = mClock.elapsed();
sample();
if (mInterval > 0.0) {
const double ms = mInterval - (mClock.elapsed() - elapsed);
if (ms > 0.0) {
QTime waitTime= QTime::currentTime().addMSecs(ms);
while (QTime::currentTime() < waitTime) {
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);

if (mSimulateWithSteps) {
if (mInterval > 0.0) {
const double ms = mInterval - (mClock.elapsed() - elapsed);
if (ms > 0.0) {
QTime waitTime= QTime::currentTime().addMSecs(ms);
while (QTime::currentTime() < waitTime) {
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
}
}
} else {
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
QThread::msleep(mServerSampleInterval);
}
}
}
Expand Down Expand Up @@ -437,10 +444,7 @@ void OpcUaWorker::appendVariableValues()
void OpcUaWorker::stepSimulation()
{
// set the step variable to true
UA_Variant *newValue = UA_Variant_new();
UA_Boolean run = true;
UA_Variant_setScalarCopy(newValue, &run, &UA_TYPES[UA_TYPES_BOOLEAN]);
UA_Client_writeValueAttribute(mpParentClient->getClient(), UA_NODEID_NUMERIC(0, 10000), newValue);
writeBool(mpParentClient->getClient(), UA_NODEID_NUMERIC(0, 10000), UA_TRUE);
}

/*!
Expand All @@ -463,7 +467,8 @@ void OpcUaWorker::checkMinMaxValues(const double& value)
void OpcUaWorker::createSubscription()
{
UA_SubscriptionSettings subscriptionSettings = UA_SubscriptionSettings_standard;
subscriptionSettings.requestedPublishingInterval = 1;
subscriptionSettings.requestedPublishingInterval = mServerSampleInterval;
subscriptionSettings.maxNotificationsPerPublish = 4096;
UA_Client_Subscriptions_new(mpParentClient->getClient(), subscriptionSettings, &mSubscriptionId);
monitorTime();
}
Expand Down Expand Up @@ -500,7 +505,9 @@ void OpcUaWorker::timeChanged(UA_UInt32 handle, UA_DataValue *pValue, void *pCli
// catch the time value
mCurrentTime.setLocalData(*(UA_Double*)pValue->value.data);
}
writeBool((UA_Client*) pClient, UA_NODEID_NUMERIC(0, 10000), UA_TRUE);
/* if (mSimulateWithSteps) */ {
writeBool((UA_Client*) pClient, UA_NODEID_NUMERIC(0, 10000), UA_TRUE);
}
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/Simulation/OpcUaClient.h
Expand Up @@ -78,7 +78,7 @@ private slots:
OpcUaClient *mpParentClient;
VariablesTreeItem *mpVariablesTreeItemRoot;
QTime mClock;
double mInterval, mSpeedValue;
double mInterval, mSpeedValue, mServerSampleInterval;
bool mIsRunning;

void createSubscription();
Expand Down
14 changes: 10 additions & 4 deletions OMEdit/OMEditGUI/Simulation/SimulationDialog.cpp
Expand Up @@ -1526,12 +1526,18 @@ void SimulationDialog::simulationPaused()

void SimulationDialog::updateInteractiveSimulationCurves()
{
MainWindow::instance()->getPlotWindowContainer()->getCurrentWindow()->updateCurves();
OMPlot::PlotWindow* window = MainWindow::instance()->getPlotWindowContainer()->getCurrentWindow();
if (window) {
window->updateCurves();
}
}

void SimulationDialog::updateYAxis(double min, double max)
{
MainWindow::instance()->getPlotWindowContainer()->getCurrentWindow()->updateYAxis(qMakePair(min, max));
OMPlot::PlotWindow* window = MainWindow::instance()->getPlotWindowContainer()->getCurrentWindow();
if (window) {
window->updateYAxis(qMakePair(min, max));
}
}

void SimulationDialog::removeVariablesFromTree(QString className)
Expand Down Expand Up @@ -1583,8 +1589,8 @@ void SimulationDialog::terminateSimulationProcess(SimulationOutputWidget *pSimul
void SimulationDialog::killSimulationProcess(int port)
{
std::string endPoint = "opc.tcp://localhost:" + std::to_string(port);
UA_Client *pClient = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout);
UA_StatusCode returnValue = UA_Client_connect(pClient, UA_ClientConnectionTCP, endPoint.c_str());
UA_Client *pClient = UA_Client_new(UA_ClientConfig_standard);
UA_StatusCode returnValue = UA_Client_connect(pClient, endPoint.c_str());

if (returnValue == UA_STATUSCODE_GOOD) {
removeVariablesFromTree(mOpcUaClientsMap.value(port)->getSimulationOptions().getClassName());
Expand Down

0 comments on commit 03be5e2

Please sign in to comment.