ChimeraTK-ControlSystemAdapter-OPCUAAdapter 04.00.05
Loading...
Searching...
No Matches
runtime_value_generator.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ChimeraTKs ControlSystem-OPC-UA-Adapter.
3 *
4 * ChimeraTKs ControlSystem-OPC-UA-Adapter is free software: you can
5 * redistribute it and/or modify it under the terms of the Lesser GNU
6 * General Public License as published by the Free Software Foundation,
7 * either version 3 of the License, or (at your option) any later version.
8 *
9 * ChimeraTKs ControlSystem-OPC-UA-Adapter is distributed in the hope
10 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
11 * implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the Lesser GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Foobar. If not, see https://www.gnu.org/licenses/lgpl.html
16 *
17 * Copyright (c) 2016 Chris Iatrou <Chris_Paul.Iatrou@tu-dresden.de>
18 * Copyright (c) 2016 Julian Rahm <Julian.Rahm@tu-dresden.de>
19 */
20
22
23#include "csa_opcua_adapter.h"
24
25#include <ChimeraTK/ReadAnyGroup.h>
26
27#include <sys/sysinfo.h>
28
29#include <math.h>
30#include <unistd.h>
31
32#include <iostream>
33
34using std::cout;
35using std::endl;
36using namespace ChimeraTK;
37
39 this->valueGeneratorThread = std::thread(&runtime_value_generator::generateValues, this, devManager);
40}
41
43 this->running = false;
44 if(this->valueGeneratorThread.joinable()) {
45 this->valueGeneratorThread.join();
46 }
47}
48
49void runtime_value_generator::generateValues(boost::shared_ptr<DevicePVManager> devManager) {
50 this->running = true;
51
52 ReadAnyGroup readAnyGroup;
53 for(auto& pv : devManager->getAllProcessVariables()) {
54 if(pv->isReadable()) readAnyGroup.add(pv);
55 }
56 readAnyGroup.finalise();
57
58 // Time meassureing
59 clock_t start, end, im1, im2, writeTime;
60 start = clock();
61 end = clock();
62 int counter = 0;
63 float duration1 = 0.0f;
64 devManager->getProcessArray<int32_t>("t")->accessChannel(0) = vector<int32_t>{(int32_t)start};
65
66 while(this->running) {
67 double double_sine = devManager->getProcessArray<double>("amplitude")->accessChannel(0).at(0) *
68 sin((2 * 3.141) / devManager->getProcessArray<double>("period")->accessChannel(0).at(0) *
69 devManager->getProcessArray<int32_t>("t")->accessChannel(0).at(0));
70 int32_t int_sine = round(double_sine);
71
72 devManager->getProcessArray<double>("double_sine")->accessChannel(0) = vector<double>{double_sine};
73 devManager->getProcessArray<double>("double_sine")->write();
74 devManager->getProcessArray<int32_t>("int_sine")->accessChannel(0) = vector<int32_t>{int_sine};
75 devManager->getProcessArray<int32_t>("int_sine")->write();
76 devManager->getProcessArray<ChimeraTK::Boolean>("bool")->accessChannel(0) =
77 vector<ChimeraTK::Boolean>{!devManager->getProcessArray<ChimeraTK::Boolean>("bool")->accessChannel(0).at(0)};
78 devManager->getProcessArray<ChimeraTK::Boolean>("bool")->write();
79 devManager->getProcessArray<ChimeraTK::Void>("void")->write();
80 devManager->getProcessArray<int32_t>("t")->accessChannel(0) =
81 vector<int32_t>{(int32_t)((end - start) / (CLOCKS_PER_SEC / 1000))};
82 devManager->getProcessArray<int32_t>("t")->write();
83
84 usleep(devManager->getProcessArray<int32_t>("dt")->accessChannel(0).at(0));
85 end = clock();
86
87 counter = 0;
88 writeTime = 0;
89 for(int32_t i = 1000; i < 65535; i = i + 1000) {
90 string nameDouble = "testDoubleArray_" + to_string(i);
91 string nameInt = "testIntArray_" + to_string(i);
92 ProcessArray<double>::SharedPtr testDoubleArray = devManager->getProcessArray<double>(nameDouble);
93 ProcessArray<int32_t>::SharedPtr testIntArray = devManager->getProcessArray<int32_t>(nameInt);
94 for(int32_t k = 0; k < i; k++) {
95 if(k % 2 == 0) {
96 testDoubleArray->accessChannel(0).at(k) = rand() % 6 + 1;
97 testIntArray->accessChannel(0).at(k) = rand() % 6 + 1;
98 }
99 else {
100 testDoubleArray->accessChannel(0).at(k) = rand() % 50 + 10;
101 testIntArray->accessChannel(0).at(k) = rand() % 50 + 10;
102 }
103 }
104 im1 = clock();
105 testDoubleArray->write();
106 testIntArray->write();
107 im2 = clock();
108 writeTime += im2 - im1;
109 counter++;
110 }
111
112 duration1 = ((float)writeTime / CLOCKS_PER_SEC) * 1000.0f;
113 printf("1: %d write passes, duration write: %.3f ms\n", counter, duration1);
114
115 ProcessArray<double>::SharedPtr testDoubleArray = devManager->getProcessArray<double>("testDoubleArray_65535");
116 ProcessArray<int32_t>::SharedPtr testIntArray = devManager->getProcessArray<int32_t>("testIntArray_65535");
117 for(int32_t i = 0; i < 65535; i++) {
118 if(i % 2 == 0) {
119 testDoubleArray->accessChannel(0).at(i) = rand() % 6 + 1;
120 testIntArray->accessChannel(0).at(i) = rand() % 6 + 1;
121 }
122 else {
123 testDoubleArray->accessChannel(0).at(i) = rand() % 50 + 10;
124 testIntArray->accessChannel(0).at(i) = rand() % 50 + 10;
125 }
126 }
127
128 clock_t tmp2 = clock();
129
130 testDoubleArray->write();
131 testIntArray->write();
132
133 clock_t tmp3 = clock();
134 duration1 = ((float)(tmp3 - tmp2) / CLOCKS_PER_SEC) * 1000.0f;
135 printf("write pass 2, duration write: %.3f ms\n", duration1);
136
137 while(readAnyGroup.readAnyNonBlocking().isValid()) continue;
138 }
139}
runtime_value_generator(boost::shared_ptr< DevicePVManager > devManager)
void generateValues(boost::shared_ptr< DevicePVManager > devManager)
boost::shared_ptr< ChimeraTK::DevicePVManager > devManager