ChimeraTK-ControlSystemAdapter-OPCUAAdapter  04.00.01
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 #include <sys/sysinfo.h>
25 
26 #include <ChimeraTK/ReadAnyGroup.h>
27 
28 #include <iostream>
29 #include <math.h>
30 #include <unistd.h>
31 
32 using std::cout;
33 using std::endl;
34 using namespace ChimeraTK;
35 
36 runtime_value_generator::runtime_value_generator(boost::shared_ptr<DevicePVManager> devManager) {
37  this->valueGeneratorThread = std::thread(&runtime_value_generator::generateValues, this, devManager);
38 }
39 
41  this->running = false;
42  if(this->valueGeneratorThread.joinable()) {
43  this->valueGeneratorThread.join();
44  }
45 }
46 
47 void runtime_value_generator::generateValues(boost::shared_ptr<DevicePVManager> devManager) {
48  this->running = true;
49 
50  ReadAnyGroup readAnyGroup;
51  for(auto& pv : devManager->getAllProcessVariables()) {
52  if(pv->isReadable()) readAnyGroup.add(pv);
53  }
54  readAnyGroup.finalise();
55 
56  // Time meassureing
57  clock_t start, end, im1, im2, writeTime;
58  start = clock();
59  end = clock();
60  int counter = 0;
61  float duration1 = 0.0f;
62  devManager->getProcessArray<int32_t>("t")->accessChannel(0) = vector<int32_t>{(int32_t)start};
63 
64  while(this->running) {
65  double double_sine = devManager->getProcessArray<double>("amplitude")->accessChannel(0).at(0) *
66  sin((2 * 3.141) / devManager->getProcessArray<double>("period")->accessChannel(0).at(0) *
67  devManager->getProcessArray<int32_t>("t")->accessChannel(0).at(0));
68  int32_t int_sine = round(double_sine);
69 
70  devManager->getProcessArray<double>("double_sine")->accessChannel(0) = vector<double>{double_sine};
71  devManager->getProcessArray<double>("double_sine")->write();
72  devManager->getProcessArray<int32_t>("int_sine")->accessChannel(0) = vector<int32_t>{int_sine};
73  devManager->getProcessArray<int32_t>("int_sine")->write();
74  devManager->getProcessArray<ChimeraTK::Boolean>("bool")->accessChannel(0) =
75  vector<ChimeraTK::Boolean>{!devManager->getProcessArray<ChimeraTK::Boolean>("bool")->accessChannel(0).at(0)};
76  devManager->getProcessArray<ChimeraTK::Boolean>("bool")->write();
77  devManager->getProcessArray<ChimeraTK::Void>("void")->write();
78  devManager->getProcessArray<int32_t>("t")->accessChannel(0) =
79  vector<int32_t>{(int32_t)((end - start) / (CLOCKS_PER_SEC / 1000))};
80  devManager->getProcessArray<int32_t>("t")->write();
81 
82  usleep(devManager->getProcessArray<int32_t>("dt")->accessChannel(0).at(0));
83  end = clock();
84 
85  counter = 0;
86  writeTime = 0;
87  for(int32_t i = 1000; i < 65535; i = i + 1000) {
88  string nameDouble = "testDoubleArray_" + to_string(i);
89  string nameInt = "testIntArray_" + to_string(i);
90  ProcessArray<double>::SharedPtr testDoubleArray = devManager->getProcessArray<double>(nameDouble);
91  ProcessArray<int32_t>::SharedPtr testIntArray = devManager->getProcessArray<int32_t>(nameInt);
92  for(int32_t k = 0; k < i; k++) {
93  if(k % 2 == 0) {
94  testDoubleArray->accessChannel(0).at(k) = rand() % 6 + 1;
95  testIntArray->accessChannel(0).at(k) = rand() % 6 + 1;
96  }
97  else {
98  testDoubleArray->accessChannel(0).at(k) = rand() % 50 + 10;
99  testIntArray->accessChannel(0).at(k) = rand() % 50 + 10;
100  }
101  }
102  im1 = clock();
103  testDoubleArray->write();
104  testIntArray->write();
105  im2 = clock();
106  writeTime += im2 - im1;
107  counter++;
108  }
109 
110  duration1 = ((float)writeTime / CLOCKS_PER_SEC) * 1000.0f;
111  printf("1: %d write passes, duration write: %.3f ms\n", counter, duration1);
112 
113  ProcessArray<double>::SharedPtr testDoubleArray = devManager->getProcessArray<double>("testDoubleArray_65535");
114  ProcessArray<int32_t>::SharedPtr testIntArray = devManager->getProcessArray<int32_t>("testIntArray_65535");
115  for(int32_t i = 0; i < 65535; i++) {
116  if(i % 2 == 0) {
117  testDoubleArray->accessChannel(0).at(i) = rand() % 6 + 1;
118  testIntArray->accessChannel(0).at(i) = rand() % 6 + 1;
119  }
120  else {
121  testDoubleArray->accessChannel(0).at(i) = rand() % 50 + 10;
122  testIntArray->accessChannel(0).at(i) = rand() % 50 + 10;
123  }
124  }
125 
126  clock_t tmp2 = clock();
127 
128  testDoubleArray->write();
129  testIntArray->write();
130 
131  clock_t tmp3 = clock();
132  duration1 = ((float)(tmp3 - tmp2) / CLOCKS_PER_SEC) * 1000.0f;
133  printf("write pass 2, duration write: %.3f ms\n", duration1);
134 
135  while(readAnyGroup.readAnyNonBlocking().isValid()) continue;
136  }
137 }
csa_opcua_adapter.h
devManager
boost::shared_ptr< ChimeraTK::DevicePVManager > devManager
Definition: csa_opcua_application.cpp:60
runtime_value_generator::~runtime_value_generator
~runtime_value_generator()
Definition: runtime_value_generator.cpp:40
runtime_value_generator::runtime_value_generator
runtime_value_generator(boost::shared_ptr< DevicePVManager > devManager)
Definition: runtime_value_generator.cpp:36
runtime_value_generator::generateValues
void generateValues(boost::shared_ptr< DevicePVManager > devManager)
Definition: runtime_value_generator.cpp:47
ChimeraTK
Definition: csa_additionalvariable.h:28
runtime_value_generator.h